LCOV - code coverage report
Current view: top level - gdk - gdk.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 209 244 85.7 %
Date: 2021-10-13 02:24:04 Functions: 19 19 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * @t The Goblin Database Kernel
      11             :  * @v Version 3.05
      12             :  * @a Martin L. Kersten, Peter Boncz, Niels Nes, Sjoerd Mullender
      13             :  *
      14             :  * @+ The Inner Core
      15             :  * The innermost library of the MonetDB database system is formed by
      16             :  * the library called GDK, an abbreviation of Goblin Database Kernel.
      17             :  * Its development was originally rooted in the design of a pure
      18             :  * active-object-oriented programming language, before development
      19             :  * was shifted towards a re-usable database kernel engine.
      20             :  *
      21             :  * GDK is a C library that provides ACID properties on a DSM model
      22             :  * @tex
      23             :  * [@cite{Copeland85}]
      24             :  * @end tex
      25             :  * , using main-memory
      26             :  * database algorithms
      27             :  * @tex
      28             :  * [@cite{Garcia-Molina92}]
      29             :  * @end tex
      30             :  *  built on virtual-memory
      31             :  * OS primitives and multi-threaded parallelism.
      32             :  * Its implementation has undergone various changes over its decade
      33             :  * of development, many of which were driven by external needs to
      34             :  * obtain a robust and fast database system.
      35             :  *
      36             :  * The coding scheme explored in GDK has also laid a foundation to
      37             :  * communicate over time experiences and to provide (hopefully)
      38             :  * helpful advice near to the place where the code-reader needs it.
      39             :  * Of course, over such a long time the documentation diverges from
      40             :  * reality. Especially in areas where the environment of this package
      41             :  * is being described.
      42             :  * Consider such deviations as historic landmarks, e.g. crystallization
      43             :  * of brave ideas and mistakes rectified at a later stage.
      44             :  *
      45             :  * @+ Short Outline
      46             :  * The facilities provided in this implementation are:
      47             :  * @itemize
      48             :  * @item
      49             :  * GDK or Goblin Database Kernel routines for session management
      50             :  * @item
      51             :  *  BAT routines that define the primitive operations on the
      52             :  * database tables (BATs).
      53             :  * @item
      54             :  *  BBP routines to manage the BAT Buffer Pool (BBP).
      55             :  * @item
      56             :  *  ATOM routines to manipulate primitive types, define new types
      57             :  * using an ADT interface.
      58             :  * @item
      59             :  *  HEAP routines for manipulating heaps: linear spaces of memory
      60             :  * that are GDK's vehicle of mass storage (on which BATs are built).
      61             :  * @item
      62             :  *  DELTA routines to access inserted/deleted elements within a
      63             :  * transaction.
      64             :  * @item
      65             :  *  HASH routines for manipulating GDK's built-in linear-chained
      66             :  * hash tables, for accelerating lookup searches on BATs.
      67             :  * @item
      68             :  *  TM routines that provide basic transaction management primitives.
      69             :  * @item
      70             :  *  TRG routines that provided active database support. [DEPRECATED]
      71             :  * @item
      72             :  *  ALIGN routines that implement BAT alignment management.
      73             :  * @end itemize
      74             :  *
      75             :  * The Binary Association Table (BAT) is the lowest level of storage
      76             :  * considered in the Goblin runtime system
      77             :  * @tex
      78             :  * [@cite{Goblin}]
      79             :  * @end tex
      80             :  * .  A BAT is a
      81             :  * self-descriptive main-memory structure that represents the
      82             :  * @strong{binary relationship} between two atomic types.  The
      83             :  * association can be defined over:
      84             :  * @table @code
      85             :  * @item void:
      86             :  *  virtual-OIDs: a densely ascending column of OIDs (takes zero-storage).
      87             :  * @item bit:
      88             :  *  Booleans, implemented as one byte values.
      89             :  * @item bte:
      90             :  *  Tiny (1-byte) integers (8-bit @strong{integer}s).
      91             :  * @item sht:
      92             :  *  Short integers (16-bit @strong{integer}s).
      93             :  * @item int:
      94             :  *  This is the C @strong{int} type (32-bit).
      95             :  * @item oid:
      96             :  *  Unique @strong{long int} values uses as object identifier. Highest
      97             :  *          bit cleared always.  Thus, oids-s are 31-bit numbers on
      98             :  *          32-bit systems, and 63-bit numbers on 64-bit systems.
      99             :  * @item ptr:
     100             :  * Memory pointer values. DEPRECATED.  Can only be stored in transient
     101             :  * BATs.
     102             :  * @item flt:
     103             :  *  The IEEE @strong{float} type.
     104             :  * @item dbl:
     105             :  *  The IEEE @strong{double} type.
     106             :  * @item lng:
     107             :  *  Longs: the C @strong{long long} type (64-bit integers).
     108             :  * @item hge:
     109             :  *  "huge" integers: the GCC @strong{__int128} type (128-bit integers).
     110             :  * @item str:
     111             :  *  UTF-8 strings (Unicode). A zero-terminated byte sequence.
     112             :  * @item bat:
     113             :  *  Bat descriptor. This allows for recursive administered tables, but
     114             :  *  severely complicates transaction management. Therefore, they CAN
     115             :  *  ONLY BE STORED IN TRANSIENT BATs.
     116             :  * @end table
     117             :  *
     118             :  * This model can be used as a back-end model underlying other -higher
     119             :  * level- models, in order to achieve @strong{better performance} and
     120             :  * @strong{data independence} in one go. The relational model and the
     121             :  * object-oriented model can be mapped on BATs by vertically splitting
     122             :  * every table (or class) for each attribute. Each such a column is
     123             :  * then stored in a BAT with type @strong{bat[oid,attribute]}, where
     124             :  * the unique object identifiers link tuples in the different BATs.
     125             :  * Relationship attributes in the object-oriented model hence are
     126             :  * mapped to @strong{bat[oid,oid]} tables, being equivalent to the
     127             :  * concept of @emph{join indexes} @tex [@cite{Valduriez87}] @end tex .
     128             :  *
     129             :  * The set of built-in types can be extended with user-defined types
     130             :  * through an ADT interface.  They are linked with the kernel to
     131             :  * obtain an enhanced library, or they are dynamically loaded upon
     132             :  * request.
     133             :  *
     134             :  * Types can be derived from other types. They represent something
     135             :  * different than that from which they are derived, but their internal
     136             :  * storage management is equal. This feature facilitates the work of
     137             :  * extension programmers, by enabling reuse of implementation code,
     138             :  * but is also used to keep the GDK code portable from 32-bits to
     139             :  * 64-bits machines: the @strong{oid} and @strong{ptr} types are
     140             :  * derived from @strong{int} on 32-bits machines, but is derived from
     141             :  * @strong{lng} on 64 bits machines. This requires changes in only two
     142             :  * lines of code each.
     143             :  *
     144             :  * To accelerate lookup and search in BATs, GDK supports one built-in
     145             :  * search accelerator: hash tables. We choose an implementation
     146             :  * efficient for main-memory: bucket chained hash
     147             :  * @tex
     148             :  * [@cite{LehCar86,Analyti92}]
     149             :  * @end tex
     150             :  * . Alternatively, when the table is sorted, it will resort to
     151             :  * merge-scan operations or binary lookups.
     152             :  *
     153             :  * BATs are built on the concept of heaps, which are large pieces of
     154             :  * main memory. They can also consist of virtual memory, in case the
     155             :  * working set exceeds main-memory. In this case, GDK supports
     156             :  * operations that cluster the heaps of a BAT, in order to improve
     157             :  * performance of its main-memory.
     158             :  *
     159             :  *
     160             :  * @- Rationale
     161             :  * The rationale for choosing a BAT as the building block for both
     162             :  * relational and object-oriented system is based on the following
     163             :  * observations:
     164             :  *
     165             :  * @itemize
     166             :  * @item -
     167             :  * Given the fact that CPU speed and main-memory increase in current
     168             :  * workstation hardware for the last years has been exceeding IO
     169             :  * access speed increase, traditional disk-page oriented algorithms do
     170             :  * no longer take best advantage of hardware, in most database
     171             :  * operations.
     172             :  *
     173             :  * Instead of having a disk-block oriented kernel with a large memory
     174             :  * cache, we choose to build a main-memory kernel, that only under
     175             :  * large data volumes slowly degrades to IO-bound performance,
     176             :  * comparable to traditional systems
     177             :  * @tex
     178             :  * [@cite{boncz95,boncz96}]
     179             :  * @end tex
     180             :  * .
     181             :  *
     182             :  * @item -
     183             :  * Traditional (disk-based) relational systems move too much data
     184             :  * around to save on (main-memory) join operations.
     185             :  *
     186             :  * The fully decomposed store (DSM
     187             :  * @tex
     188             :  * [@cite{Copeland85})]
     189             :  * @end tex
     190             :  * assures that only those attributes of a relation that are needed,
     191             :  * will have to be accessed.
     192             :  *
     193             :  * @item -
     194             :  * The data management issues for a binary association is much
     195             :  * easier to deal with than traditional @emph{struct}-based approaches
     196             :  * encountered in relational systems.
     197             :  *
     198             :  * @item -
     199             :  * Object-oriented systems often maintain a double cache, one with the
     200             :  * disk-based representation and a C pointer-based main-memory
     201             :  * structure.  This causes expensive conversions and replicated
     202             :  * storage management.  GDK does not do such `pointer swizzling'. It
     203             :  * used virtual-memory (@strong{mmap()}) and buffer management advice
     204             :  * (@strong{madvise()}) OS primitives to cache only once. Tables take
     205             :  * the same form in memory as on disk, making the use of this
     206             :  * technique transparent
     207             :  * @tex
     208             :  * [@cite{oo7}]
     209             :  * @end tex
     210             :  * .
     211             :  * @end itemize
     212             :  *
     213             :  * A RDBMS or OODBMS based on BATs strongly depends on our ability to
     214             :  * efficiently support tuples and to handle small joins, respectively.
     215             :  *
     216             :  * The remainder of this document describes the Goblin Database kernel
     217             :  * implementation at greater detail. It is organized as follows:
     218             :  * @table @code
     219             :  * @item @strong{GDK Interface}:
     220             :  *
     221             :  * It describes the global interface with which GDK sessions can be
     222             :  * started and ended, and environment variables used.
     223             :  *
     224             :  * @item @strong{Binary Association Tables}:
     225             :  *
     226             :  * As already mentioned, these are the primary data structure of GDK.
     227             :  * This chapter describes the kernel operations for creation,
     228             :  * destruction and basic manipulation of BATs and BUNs (i.e. tuples:
     229             :  * Binary UNits).
     230             :  *
     231             :  * @item @strong{BAT Buffer Pool:}
     232             :  *
     233             :  * All BATs are registered in the BAT Buffer Pool. This directory is
     234             :  * used to guide swapping in and out of BATs. Here we find routines
     235             :  * that guide this swapping process.
     236             :  *
     237             :  * @item @strong{GDK Extensibility:}
     238             :  *
     239             :  * Atoms can be defined using a unified ADT interface.  There is also
     240             :  * an interface to extend the GDK library with dynamically linked
     241             :  * object code.
     242             :  *
     243             :  * @item @strong{GDK Utilities:}
     244             :  *
     245             :  * Memory allocation and error handling primitives are
     246             :  * provided. Layers built on top of GDK should use them, for proper
     247             :  * system monitoring.  Thread management is also included here.
     248             :  *
     249             :  * @item @strong{Transaction Management:}
     250             :  *
     251             :  * For the time being, we just provide BAT-grained concurrency and
     252             :  * global transactions. Work is needed here.
     253             :  *
     254             :  * @item @strong{BAT Alignment:}
     255             :  * Due to the mapping of multi-ary datamodels onto the BAT model, we
     256             :  * expect many correspondences among BATs, e.g.
     257             :  * @emph{bat(oid,attr1),..  bat(oid,attrN)} vertical
     258             :  * decompositions. Frequent activities will be to jump from one
     259             :  * attribute to the other (`bunhopping'). If the head columns are
     260             :  * equal lists in two BATs, merge or even array lookups can be used
     261             :  * instead of hash lookups. The alignment interface makes these
     262             :  * relations explicitly manageable.
     263             :  *
     264             :  * In GDK, complex data models are mapped with DSM on binary tables.
     265             :  * Usually, one decomposes @emph{N}-ary relations into @emph{N} BATs
     266             :  * with an @strong{oid} in the head column, and the attribute in the
     267             :  * tail column.  There may well be groups of tables that have the same
     268             :  * sets of @strong{oid}s, equally ordered. The alignment interface is
     269             :  * intended to make this explicit.  Implementations can use this
     270             :  * interface to detect this situation, and use cheaper algorithms
     271             :  * (like merge-join, or even array lookup) instead.
     272             :  *
     273             :  * @item @strong{BAT Iterators:}
     274             :  *
     275             :  * Iterators are C macros that generally encapsulate a complex
     276             :  * for-loop.  They would be the equivalent of cursors in the SQL
     277             :  * model. The macro interface (instead of a function call interface)
     278             :  * is chosen to achieve speed when iterating main-memory tables.
     279             :  *
     280             :  * @item @strong{Common BAT Operations:}
     281             :  *
     282             :  * These are much used operations on BATs, such as aggregate functions
     283             :  * and relational operators. They are implemented in terms of BAT- and
     284             :  * BUN-manipulation GDK primitives.
     285             :  * @end table
     286             :  *
     287             :  * @+ Interface Files
     288             :  * In this section we summarize the user interface to the GDK library.
     289             :  * It consist of a header file (gdk.h) and an object library
     290             :  * (gdklib.a), which implements the required functionality. The header
     291             :  * file must be included in any program that uses the library. The
     292             :  * library must be linked with such a program.
     293             :  *
     294             :  * @- Database Context
     295             :  *
     296             :  * The MonetDB environment settings are collected in a configuration
     297             :  * file. Amongst others it contains the location of the database
     298             :  * directory.  First, the database directory is closed for other
     299             :  * servers running at the same time.  Second, performance enhancements
     300             :  * may take effect, such as locking the code into memory (if the OS
     301             :  * permits) and preloading the data dictionary.  An error at this
     302             :  * stage normally lead to an abort.
     303             :  */
     304             : 
     305             : #ifndef _GDK_H_
     306             : #define _GDK_H_
     307             : 
     308             : /* standard includes upon which all configure tests depend */
     309             : #ifdef HAVE_SYS_TYPES_H
     310             : # include <sys/types.h>
     311             : #endif
     312             : #ifdef HAVE_SYS_STAT_H
     313             : # include <sys/stat.h>
     314             : #endif
     315             : #include <stddef.h>
     316             : #include <string.h>
     317             : #ifdef HAVE_UNISTD_H
     318             : # include <unistd.h>
     319             : #endif
     320             : 
     321             : #include <ctype.h>                /* isspace etc. */
     322             : 
     323             : #ifdef HAVE_SYS_FILE_H
     324             : # include <sys/file.h>
     325             : #endif
     326             : 
     327             : #ifdef HAVE_DIRENT_H
     328             : # include <dirent.h>
     329             : #endif
     330             : 
     331             : #include <limits.h>               /* for *_MIN and *_MAX */
     332             : #include <float.h>                /* for FLT_MAX and DBL_MAX */
     333             : 
     334             : typedef enum { GDK_FAIL, GDK_SUCCEED } gdk_return;
     335             : 
     336             : #include "gdk_system.h"
     337             : #include "gdk_posix.h"
     338             : #include "stream.h"
     339             : #include "mstring.h"
     340             : 
     341             : #undef MIN
     342             : #undef MAX
     343             : #define MAX(A,B)        ((A)<(B)?(B):(A))
     344             : #define MIN(A,B)        ((A)>(B)?(B):(A))
     345             : 
     346             : /* defines from ctype with casts that allow passing char values */
     347             : #define GDKisspace(c)   isspace((unsigned char) (c))
     348             : #define GDKisalnum(c)   isalnum((unsigned char) (c))
     349             : #define GDKisdigit(c)   isdigit((unsigned char) (c))
     350             : 
     351             : #define BATDIR          "bat"
     352             : #define TEMPDIR_NAME    "TEMP_DATA"
     353             : 
     354             : #define DELDIR          BATDIR DIR_SEP_STR "DELETE_ME"
     355             : #define BAKDIR          BATDIR DIR_SEP_STR "BACKUP"
     356             : #define SUBDIR          BAKDIR DIR_SEP_STR "SUBCOMMIT" /* note K, not T */
     357             : #define LEFTDIR         BATDIR DIR_SEP_STR "LEFTOVERS"
     358             : #define TEMPDIR         BATDIR DIR_SEP_STR TEMPDIR_NAME
     359             : 
     360             : /*
     361             :    See `man mserver5` or tools/mserver/mserver5.1
     362             :    for a documentation of the following debug options.
     363             : */
     364             : 
     365             : #define THRDMASK        (1)
     366             : #define CHECKMASK       (1<<1)
     367             : #define CHECKDEBUG      if (GDKdebug & CHECKMASK)
     368             : #define PROPMASK        (1<<3)
     369             : #define PROPDEBUG       if (GDKdebug & PROPMASK)
     370             : #define IOMASK          (1<<4)
     371             : #define BATMASK         (1<<5)
     372             : #define PARMASK         (1<<7)
     373             : #define TMMASK          (1<<9)
     374             : #define TEMMASK         (1<<10)
     375             : #define PERFMASK        (1<<12)
     376             : #define DELTAMASK       (1<<13)
     377             : #define LOADMASK        (1<<14)
     378             : #define ACCELMASK       (1<<20)
     379             : #define ALGOMASK        (1<<21)
     380             : 
     381             : #define NOSYNCMASK      (1<<24)
     382             : 
     383             : #define DEADBEEFMASK    (1<<25)
     384             : #define DEADBEEFCHK     if (!(GDKdebug & DEADBEEFMASK))
     385             : 
     386             : #define ALLOCMASK       (1<<26)
     387             : 
     388             : /* M5, only; cf.,
     389             :  * monetdb5/mal/mal.h
     390             :  */
     391             : #define OPTMASK         (1<<27)
     392             : 
     393             : #define HEAPMASK        (1<<28)
     394             : 
     395             : #define FORCEMITOMASK   (1<<29)
     396             : #define FORCEMITODEBUG  if (GDKdebug & FORCEMITOMASK)
     397             : 
     398             : /*
     399             :  * @- GDK session handling
     400             :  * @multitable @columnfractions 0.08 0.7
     401             :  * @item int
     402             :  * @tab GDKinit (char *db, char *dbpath, int allocmap)
     403             :  * @item int
     404             :  * @tab GDKexit (int status)
     405             :  * @end multitable
     406             :  *
     407             :  * The session is bracketed by GDKinit and GDKexit. Initialization
     408             :  * involves setting up the administration for database access, such as
     409             :  * memory allocation for the database buffer pool.  During the exit
     410             :  * phase any pending transaction is aborted and the database is freed
     411             :  * for access by other users.  A zero is returned upon encountering an
     412             :  * erroneous situation.
     413             :  *
     414             :  * @- Definitions
     415             :  * The interface definitions for the application programs are shown
     416             :  * below.  The global variables should not be modified directly.
     417             :  */
     418             : #ifndef TRUE
     419             : #define TRUE            true
     420             : #define FALSE           false
     421             : #endif
     422             : 
     423             : #define BATMARGIN       1.2     /* extra free margin for new heaps */
     424             : #define BATTINY_BITS    8
     425             : #define BATTINY         ((BUN)1<<BATTINY_BITS)    /* minimum allocation buncnt for a BAT */
     426             : 
     427             : enum {
     428             :         TYPE_void = 0,
     429             :         TYPE_msk,               /* bit mask */
     430             :         TYPE_bit,               /* TRUE, FALSE, or nil */
     431             :         TYPE_bte,
     432             :         TYPE_sht,
     433             :         TYPE_bat,               /* BAT id: index in BBPcache */
     434             :         TYPE_int,
     435             :         TYPE_oid,
     436             :         TYPE_ptr,               /* C pointer! */
     437             :         TYPE_flt,
     438             :         TYPE_dbl,
     439             :         TYPE_lng,
     440             : #ifdef HAVE_HGE
     441             :         TYPE_hge,
     442             : #endif
     443             :         TYPE_date,
     444             :         TYPE_daytime,
     445             :         TYPE_timestamp,
     446             :         TYPE_uuid,
     447             :         TYPE_str,
     448             :         TYPE_any = 255,         /* limit types to <255! */
     449             : };
     450             : 
     451             : typedef bool msk;
     452             : typedef int8_t bit;
     453             : typedef int8_t bte;
     454             : typedef int16_t sht;
     455             : /* typedef int64_t lng; -- defined in gdk_system.h */
     456             : typedef uint64_t ulng;
     457             : 
     458             : #define SIZEOF_OID      SIZEOF_SIZE_T
     459             : typedef size_t oid;
     460             : #define OIDFMT          "%zu"
     461             : 
     462             : typedef int bat;                /* Index into BBP */
     463             : typedef void *ptr;              /* Internal coding of types */
     464             : 
     465             : #define SIZEOF_PTR      SIZEOF_VOID_P
     466             : typedef float flt;
     467             : typedef double dbl;
     468             : typedef char *str;
     469             : 
     470             : #ifdef HAVE_UUID_UUID_H
     471             : #include <uuid/uuid.h>
     472             : #endif
     473             : 
     474             : #ifdef HAVE_UUID
     475             : #define UUID_SIZE       ((int) sizeof(uuid_t)) /* size of a UUID */
     476             : #else
     477             : #define UUID_SIZE       16      /* size of a UUID */
     478             : #endif
     479             : #define UUID_STRLEN     36      /* length of string representation */
     480             : 
     481             : typedef union {
     482             : #ifdef HAVE_HGE
     483             :         hge h;                  /* force alignment, not otherwise used */
     484             : #else
     485             :         lng l[2];               /* force alignment, not otherwise used */
     486             : #endif
     487             : #ifdef HAVE_UUID
     488             :         uuid_t u;
     489             : #else
     490             :         uint8_t u[UUID_SIZE];
     491             : #endif
     492             : } uuid;
     493             : 
     494             : #define SIZEOF_LNG              8
     495             : #define LL_CONSTANT(val)        INT64_C(val)
     496             : #define LLFMT                   "%" PRId64
     497             : #define ULLFMT                  "%" PRIu64
     498             : #define LLSCN                   "%" SCNd64
     499             : #define ULLSCN                  "%" SCNu64
     500             : 
     501             : typedef oid var_t;              /* type used for heap index of var-sized BAT */
     502             : #define SIZEOF_VAR_T    SIZEOF_OID
     503             : #define VARFMT          OIDFMT
     504             : 
     505             : #if SIZEOF_VAR_T == SIZEOF_INT
     506             : #define VAR_MAX         ((var_t) INT_MAX)
     507             : #else
     508             : #define VAR_MAX         ((var_t) INT64_MAX)
     509             : #endif
     510             : 
     511             : typedef oid BUN;                /* BUN position */
     512             : #define SIZEOF_BUN      SIZEOF_OID
     513             : #define BUNFMT          OIDFMT
     514             : /* alternatively:
     515             : typedef size_t BUN;
     516             : #define SIZEOF_BUN      SIZEOF_SIZE_T
     517             : #define BUNFMT          "%zu"
     518             : */
     519             : #if SIZEOF_BUN == SIZEOF_INT
     520             : #define BUN_NONE ((BUN) INT_MAX)
     521             : #else
     522             : #define BUN_NONE ((BUN) INT64_MAX)
     523             : #endif
     524             : #define BUN_MAX (BUN_NONE - 1)  /* maximum allowed size of a BAT */
     525             : 
     526             : /*
     527             :  * @- Checking and Error definitions:
     528             :  */
     529             : #define ATOMextern(t)   (ATOMstorage(t) >= TYPE_str)
     530             : 
     531             : typedef enum {
     532             :         PERSISTENT = 0,
     533             :         TRANSIENT,
     534             : } role_t;
     535             : 
     536             : /* Heap storage modes */
     537             : typedef enum {
     538             :         STORE_INVALID = 0,      /* invalid value, used to indicate error */
     539             :         STORE_MEM,              /* load into GDKmalloced memory */
     540             :         STORE_MMAP,             /* mmap() into virtual memory */
     541             :         STORE_PRIV,             /* BAT copy of copy-on-write mmap */
     542             :         STORE_CMEM,             /* load into malloc (not GDKmalloc) memory*/
     543             :         STORE_NOWN,             /* memory not owned by the BAT */
     544             :         STORE_MMAPABS,          /* mmap() into virtual memory from an
     545             :                                  * absolute path (not part of dbfarm) */
     546             : } storage_t;
     547             : 
     548             : typedef struct {
     549             :         size_t free;            /* index where free area starts. */
     550             :         size_t size;            /* size of the heap (bytes) */
     551             :         char *base;             /* base pointer in memory. */
     552             : #if SIZEOF_VOID_P == 4
     553             :         char filename[32];      /* file containing image of the heap */
     554             : #else
     555             :         char filename[40];      /* file containing image of the heap */
     556             : #endif
     557             : 
     558             :         ATOMIC_TYPE refs;       /* reference count for this heap */
     559             :         bte farmid;             /* id of farm where heap is located */
     560             :         bool cleanhash:1,       /* string heaps must clean hash */
     561             :                 dirty:1,        /* specific heap dirty marker */
     562             :                 remove:1,       /* remove storage file when freeing */
     563             :                 wasempty:1;     /* heap was empty when last saved/created */
     564             :         storage_t storage;      /* storage mode (mmap/malloc). */
     565             :         storage_t newstorage;   /* new desired storage mode at re-allocation. */
     566             :         bat parentid;           /* cache id of VIEW parent bat */
     567             : } Heap;
     568             : 
     569             : typedef struct Hash Hash;
     570             : typedef struct Imprints Imprints;
     571             : 
     572             : /*
     573             :  * @+ Binary Association Tables
     574             :  * Having gone to the previous preliminary definitions, we will now
     575             :  * introduce the structure of Binary Association Tables (BATs) in
     576             :  * detail. They are the basic storage unit on which GDK is modeled.
     577             :  *
     578             :  * The BAT holds an unlimited number of binary associations, called
     579             :  * BUNs (@strong{Binary UNits}).  The two attributes of a BUN are
     580             :  * called @strong{head} (left) and @strong{tail} (right) in the
     581             :  * remainder of this document.
     582             :  *
     583             :  *  @c image{http://monetdb.cwi.nl/projects/monetdb-mk/imgs/bat1,,,,feps}
     584             :  *
     585             :  * The above figure shows what a BAT looks like. It consists of two
     586             :  * columns, called head and tail, such that we have always binary
     587             :  * tuples (BUNs). The overlooking structure is the @strong{BAT
     588             :  * record}.  It points to a heap structure called the @strong{BUN
     589             :  * heap}.  This heap contains the atomic values inside the two
     590             :  * columns. If they are fixed-sized atoms, these atoms reside directly
     591             :  * in the BUN heap. If they are variable-sized atoms (such as string
     592             :  * or polygon), however, the columns has an extra heap for storing
     593             :  * those (such @strong{variable-sized atom heaps} are then referred to
     594             :  * as @strong{Head Heap}s and @strong{Tail Heap}s). The BUN heap then
     595             :  * contains integer byte-offsets (fixed-sized, of course) into a head-
     596             :  * or tail-heap.
     597             :  *
     598             :  * The BUN heap contains a contiguous range of BUNs. It starts after
     599             :  * the @strong{first} pointer, and finishes at the end in the
     600             :  * @strong{free} area of the BUN. All BUNs after the @strong{inserted}
     601             :  * pointer have been added in the last transaction (and will be
     602             :  * deleted on a transaction abort). All BUNs between the
     603             :  * @strong{deleted} pointer and the @strong{first} have been deleted
     604             :  * in this transaction (and will be reinserted at a transaction
     605             :  * abort).
     606             :  *
     607             :  * The location of a certain BUN in a BAT may change between
     608             :  * successive library routine invocations.  Therefore, one should
     609             :  * avoid keeping references into the BAT storage area for long
     610             :  * periods.
     611             :  *
     612             :  * Passing values between the library routines and the enclosing C
     613             :  * program is primarily through value pointers of type ptr. Pointers
     614             :  * into the BAT storage area should only be used for retrieval. Direct
     615             :  * updates of data stored in a BAT is forbidden. The user should
     616             :  * adhere to the interface conventions to guarantee the integrity
     617             :  * rules and to maintain the (hidden) auxiliary search structures.
     618             :  *
     619             :  * @- GDK variant record type
     620             :  * When manipulating values, MonetDB puts them into value records.
     621             :  * The built-in types have a direct entry in the union. Others should
     622             :  * be represented as a pointer of memory in pval or as a string, which
     623             :  * is basically the same. In such cases the len field indicates the
     624             :  * size of this piece of memory.
     625             :  */
     626             : typedef struct {
     627             :         union {                 /* storage is first in the record */
     628             :                 int ival;
     629             :                 oid oval;
     630             :                 sht shval;
     631             :                 bte btval;
     632             :                 msk mval;
     633             :                 flt fval;
     634             :                 ptr pval;
     635             :                 bat bval;
     636             :                 str sval;
     637             :                 dbl dval;
     638             :                 lng lval;
     639             : #ifdef HAVE_HGE
     640             :                 hge hval;
     641             : #endif
     642             :                 uuid uval;
     643             :         } val;
     644             :         size_t len;
     645             :         int vtype;
     646             : } *ValPtr, ValRecord;
     647             : 
     648             : /* interface definitions */
     649             : gdk_export void *VALconvert(int typ, ValPtr t);
     650             : gdk_export char *VALformat(const ValRecord *res);
     651             : gdk_export ValPtr VALcopy(ValPtr dst, const ValRecord *src);
     652             : gdk_export ValPtr VALinit(ValPtr d, int tpe, const void *s);
     653             : gdk_export void VALempty(ValPtr v);
     654             : gdk_export void VALclear(ValPtr v);
     655             : gdk_export ValPtr VALset(ValPtr v, int t, void *p);
     656             : gdk_export void *VALget(ValPtr v);
     657             : gdk_export int VALcmp(const ValRecord *p, const ValRecord *q);
     658             : gdk_export bool VALisnil(const ValRecord *v);
     659             : 
     660             : /*
     661             :  * @- The BAT record
     662             :  * The elements of the BAT structure are introduced in the remainder.
     663             :  * Instead of using the underlying types hidden beneath it, one should
     664             :  * use a @emph{BAT} type that is supposed to look like this:
     665             :  * @verbatim
     666             :  * typedef struct {
     667             :  *           // static BAT properties
     668             :  *           bat    batCacheid;       // bat id: index in BBPcache
     669             :  *           bool   batTransient;     // persistence mode
     670             :  *           bool   batCopiedtodisk;  // BAT is saved on disk?
     671             :  *           // dynamic BAT properties
     672             :  *           int    batHeat;          // heat of BAT in the BBP
     673             :  *           bool   batDirtydesc;     // BAT descriptor specific dirty flag
     674             :  *           Heap*  batBuns;          // Heap where the buns are stored
     675             :  *           // DELTA status
     676             :  *           BUN    batInserted;      // first inserted BUN
     677             :  *           BUN    batCount;         // Tuple count
     678             :  *           // Tail properties
     679             :  *           int    ttype;            // Tail type number
     680             :  *           str    tident;           // name for tail column
     681             :  *           bool   tkey;             // tail values are unique
     682             :  *           bool   tnonil;           // tail has no nils
     683             :  *           bool   tsorted;          // are tail values currently ordered?
     684             :  *           bool   tvarsized;        // for speed: tail type is varsized?
     685             :  *           // Tail storage
     686             :  *           int    tloc;             // byte-offset in BUN for tail elements
     687             :  *           Heap   *theap;           // heap for varsized tail values
     688             :  *           Hash   *thash;           // linear chained hash table on tail
     689             :  *           Imprints *timprints;     // column imprints index on tail
     690             :  *           orderidx torderidx;      // order oid index on tail
     691             :  *  } BAT;
     692             :  * @end verbatim
     693             :  *
     694             :  * The internal structure of the @strong{BAT} record is in fact much
     695             :  * more complex, but GDK programmers should refrain of making use of
     696             :  * that.
     697             :  *
     698             :  * Since we don't want to pay cost to keep both views in line with
     699             :  * each other under BAT updates, we work with shared pieces of memory
     700             :  * between the two views. An update to one will thus automatically
     701             :  * update the other.  In the same line, we allow @strong{synchronized
     702             :  * BATs} (BATs with identical head columns, and marked as such in the
     703             :  * @strong{BAT Alignment} interface) now to be clustered horizontally.
     704             :  *
     705             :  *  @c image{http://monetdb.cwi.nl/projects/monetdb-mk/imgs/bat2,,,,feps}
     706             :  */
     707             : 
     708             : typedef struct PROPrec PROPrec;
     709             : 
     710             : /* see also comment near BATassertProps() for more information about
     711             :  * the properties */
     712             : typedef struct {
     713             :         str id;                 /* label for column */
     714             : 
     715             :         uint16_t width;         /* byte-width of the atom array */
     716             :         int8_t type;            /* type id. */
     717             :         uint8_t shift;          /* log2 of bun width */
     718             :         bool varsized:1,        /* varsized/void (true) or fixedsized (false) */
     719             :                 key:1,          /* no duplicate values present */
     720             :                 nonil:1,        /* there are no nils in the column */
     721             :                 nil:1,          /* there is a nil in the column */
     722             :                 sorted:1,       /* column is sorted in ascending order */
     723             :                 revsorted:1;    /* column is sorted in descending order */
     724             :         BUN nokey[2];           /* positions that prove key==FALSE */
     725             :         BUN nosorted;           /* position that proves sorted==FALSE */
     726             :         BUN norevsorted;        /* position that proves revsorted==FALSE */
     727             :         BUN minpos, maxpos;     /* location of min/max value */
     728             :         double unique_est;      /* estimated number of unique values */
     729             :         oid seq;                /* start of dense sequence */
     730             : 
     731             :         Heap *heap;             /* space for the column. */
     732             :         BUN baseoff;            /* offset in heap->base (in whole items) */
     733             :         Heap *vheap;            /* space for the varsized data. */
     734             :         Hash *hash;             /* hash table */
     735             :         Imprints *imprints;     /* column imprints index */
     736             :         Heap *orderidx;         /* order oid index */
     737             : 
     738             :         PROPrec *props;         /* list of dynamic properties stored in the bat descriptor */
     739             : } COLrec;
     740             : 
     741             : #define ORDERIDXOFF             3
     742             : 
     743             : /* assert that atom width is power of 2, i.e., width == 1<<shift */
     744             : #define assert_shift_width(shift,width) assert(((shift) == 0 && (width) == 0) || ((unsigned)1<<(shift)) == (unsigned)(width))
     745             : 
     746             : #define GDKLIBRARY_MINMAX_POS   061042U /* first in Nov2019: no min/max position; no BBPinfo value */
     747             : #define GDKLIBRARY_TAILN        061043U /* first in Jul2021: str offset heaps names don't take width into account */
     748             : #define GDKLIBRARY_HASHASH      061044U /* first in Jul2021: hashash bit in string heaps */
     749             : /* if the version number is updated, also fix snapshot_bats() in bat_logger.c */
     750             : #define GDKLIBRARY              061045U /* first after Jul2021 */
     751             : 
     752             : typedef struct BAT {
     753             :         /* static bat properties */
     754             :         oid hseqbase;           /* head seq base */
     755             :         MT_Id creator_tid;      /* which thread created it */
     756             :         bat batCacheid;         /* index into BBP */
     757             : 
     758             :         /* dynamic bat properties */
     759             :         bool
     760             :          batCopiedtodisk:1,     /* once written */
     761             :          batDirtyflushed:1,     /* was dirty before commit started? */
     762             :          batDirtydesc:1,        /* bat descriptor dirty marker */
     763             :          batTransient:1;        /* should the BAT persist on disk? */
     764             :         uint8_t /* adjacent bit fields are packed together (if they fit) */
     765             :          batRestricted:2;       /* access privileges */
     766             :         role_t batRole;         /* role of the bat */
     767             :         uint16_t unused;        /* value=0 for now (sneakily used by mat.c) */
     768             :         int batSharecnt;        /* incoming view count */
     769             : 
     770             :         /* delta status administration */
     771             :         BUN batInserted;        /* start of inserted elements */
     772             :         BUN batCount;           /* tuple count */
     773             :         BUN batCapacity;        /* tuple capacity */
     774             : 
     775             :         /* dynamic column properties */
     776             :         COLrec T;               /* column info */
     777             :         MT_Lock theaplock;      /* lock protecting heap reference changes */
     778             :         MT_RWLock thashlock;    /* lock specifically for hash management */
     779             :         MT_Lock batIdxLock;     /* lock to manipulate other indexes/properties */
     780             : } BAT;
     781             : 
     782             : /* macros to hide complexity of the BAT structure */
     783             : #define ttype           T.type
     784             : #define tkey            T.key
     785             : #define tvarsized       T.varsized
     786             : #define tseqbase        T.seq
     787             : #define tsorted         T.sorted
     788             : #define trevsorted      T.revsorted
     789             : #define tident          T.id
     790             : #define torderidx       T.orderidx
     791             : #define twidth          T.width
     792             : #define tshift          T.shift
     793             : #define tnonil          T.nonil
     794             : #define tnil            T.nil
     795             : #define tnokey          T.nokey
     796             : #define tnosorted       T.nosorted
     797             : #define tnorevsorted    T.norevsorted
     798             : #define tminpos         T.minpos
     799             : #define tmaxpos         T.maxpos
     800             : #define tunique_est     T.unique_est
     801             : #define theap           T.heap
     802             : #define tbaseoff        T.baseoff
     803             : #define tvheap          T.vheap
     804             : #define thash           T.hash
     805             : #define timprints       T.imprints
     806             : #define tprops          T.props
     807             : 
     808             : 
     809             : /* some access functions for the bitmask type */
     810             : static inline void
     811       68497 : mskSet(BAT *b, BUN p)
     812             : {
     813       68497 :         ((uint32_t *) b->theap->base)[p / 32] |= 1U << (p % 32);
     814       68497 : }
     815             : 
     816             : static inline void
     817       13908 : mskClr(BAT *b, BUN p)
     818             : {
     819       13908 :         ((uint32_t *) b->theap->base)[p / 32] &= ~(1U << (p % 32));
     820       13908 : }
     821             : 
     822             : static inline void
     823       22594 : mskSetVal(BAT *b, BUN p, msk v)
     824             : {
     825       22594 :         if (v)
     826       68497 :                 mskSet(b, p);
     827             :         else
     828       13908 :                 mskClr(b, p);
     829       22594 : }
     830             : 
     831             : static inline msk
     832             : mskGetVal(BAT *b, BUN p)
     833             : {
     834           0 :         return ((uint32_t *) b->theap->base)[p / 32] & (1U << (p % 32));
     835             : }
     836             : 
     837             : /*
     838             :  * @- Heap Management
     839             :  * Heaps are the low-level entities of mass storage in
     840             :  * BATs. Currently, they can either be stored on disk, loaded into
     841             :  * memory, or memory mapped.
     842             :  * @multitable @columnfractions 0.08 0.7
     843             :  * @item int
     844             :  * @tab
     845             :  *  HEAPalloc (Heap *h, size_t nitems, size_t itemsize);
     846             :  * @item int
     847             :  * @tab
     848             :  *  HEAPfree (Heap *h, bool remove);
     849             :  * @item int
     850             :  * @tab
     851             :  *  HEAPextend (Heap *h, size_t size, bool mayshare);
     852             :  * @item int
     853             :  * @tab
     854             :  *  HEAPload (Heap *h, str nme,ext, bool trunc);
     855             :  * @item int
     856             :  * @tab
     857             :  *  HEAPsave (Heap *h, str nme,ext, bool dosync);
     858             :  * @item int
     859             :  * @tab
     860             :  *  HEAPcopy (Heap *dst,*src);
     861             :  * @item int
     862             :  * @tab
     863             :  *  HEAPwarm (Heap *h);
     864             :  * @end multitable
     865             :  *
     866             :  *
     867             :  * These routines should be used to alloc free or extend heaps; they
     868             :  * isolate you from the different ways heaps can be accessed.
     869             :  */
     870             : gdk_export gdk_return HEAPextend(Heap *h, size_t size, bool mayshare)
     871             :         __attribute__((__warn_unused_result__));
     872             : gdk_export size_t HEAPvmsize(Heap *h);
     873             : gdk_export size_t HEAPmemsize(Heap *h);
     874             : gdk_export void HEAPdecref(Heap *h, bool remove);
     875             : gdk_export void HEAPincref(Heap *h);
     876             : 
     877             : /* BAT iterator, also protects use of BAT heaps with reference counts.
     878             :  *
     879             :  * A BAT iterator has to be used with caution, but it does have to be
     880             :  * used in many place.
     881             :  *
     882             :  * An iterator is initialized by assigning it the result of a call to
     883             :  * either bat_iterator or bat_iterator_nolock.  The former must be
     884             :  * accompanied by a call to bat_iterator_end to release resources.
     885             :  *
     886             :  * bat_iterator should be used for BATs that could possibly be modified
     887             :  * in another thread while we're reading the contents of the BAT.
     888             :  * Alternatively, but only for very quick access, the theaplock can be
     889             :  * taken, the data read, and the lock released.  For longer duration
     890             :  * accesses, it is better to use the iterator, even without the BUNt*
     891             :  * macros, since the theaplock is only held very briefly.
     892             :  *
     893             :  * Note, bat_iterator must only be used for read-only access.
     894             :  *
     895             :  * If BATs are to be modified, higher level code must assure that no
     896             :  * other thread is going to modify the same BAT at the same time.  A
     897             :  * to-be-modified BAT should not use bat_iterator.  It can use
     898             :  * bat_iterator_nolock, but be aware that this creates a copy of the
     899             :  * heap pointer(s) (i.e. theap and tvheap) and if the heaps get
     900             :  * extended, the pointers in the BAT structure may be modified, but that
     901             :  * does not modify the pointers in the iterator.  This means that after
     902             :  * operations that may grow a heap, the iterator should be
     903             :  * reinitialized.
     904             :  *
     905             :  * The BAT iterator provides a number of fields that can (and often
     906             :  * should) be used to access information about the BAT.  For string
     907             :  * BATs, if a parallel threads adds values, the offset heap (theap) may
     908             :  * get replaced by a one that is wider.  This involves changing the
     909             :  * twidth and tshift values in the BAT structure.  These changed values
     910             :  * should not be used to access the data in the iterator.  Instead, use
     911             :  * the width and shift values in the iterator itself.
     912             :  */
     913             : typedef struct BATiter {
     914             :         BAT *b;
     915             :         Heap *h;
     916             :         void *base;
     917             :         Heap *vh;
     918             :         BUN count;
     919             :         uint16_t width;
     920             :         uint8_t shift;
     921             :         int8_t type;
     922             :         oid tseq;
     923             :         BUN hfree, vhfree;
     924             :         BUN minpos, maxpos;
     925             :         double unique_est;
     926             :         union {
     927             :                 oid tvid;
     928             :                 bool tmsk;
     929             :         };
     930             : #ifndef NDEBUG
     931             :         bool locked;
     932             : #endif
     933             : } BATiter;
     934             : 
     935             : static inline BATiter
     936   106830781 : bat_iterator_nolock(BAT *b)
     937             : {
     938             :         /* does not get matched by bat_iterator_end */
     939   106830781 :         if (b) {
     940   106830781 :                 return (BATiter) {
     941             :                         .b = b,
     942   106830781 :                         .h = b->theap,
     943   106830781 :                         .base = b->theap->base ? b->theap->base + (b->tbaseoff << b->tshift) : NULL,
     944   106830781 :                         .vh = b->tvheap,
     945   106830781 :                         .count = b->batCount,
     946   106830781 :                         .width = b->twidth,
     947   106830781 :                         .shift = b->tshift,
     948   106830781 :                         .type = b->ttype,
     949   106830781 :                         .tseq = b->tseqbase,
     950   106830781 :                         .hfree = b->theap->free,
     951   106830781 :                         .vhfree = b->tvheap ? b->tvheap->free : 0,
     952   106830781 :                         .minpos = b->tminpos,
     953   106830781 :                         .maxpos = b->tmaxpos,
     954   106830781 :                         .unique_est = b->tunique_est,
     955             : #ifndef NDEBUG
     956             :                         .locked = false,
     957             : #endif
     958             :                 };
     959             :         }
     960           0 :         return (BATiter) {0};
     961             : }
     962             : 
     963             : static inline BATiter
     964    15655021 : bat_iterator(BAT *b)
     965             : {
     966             :         /* needs matching bat_iterator_end */
     967             :         BATiter bi;
     968    15655021 :         if (b) {
     969    13375625 :                 MT_lock_set(&b->theaplock);
     970    13375230 :                 bi = bat_iterator_nolock(b);
     971             : #ifndef NDEBUG
     972             :                 bi.locked = true;
     973             : #endif
     974    13374220 :                 HEAPincref(bi.h);
     975    13375867 :                 if (bi.vh)
     976     3800047 :                         HEAPincref(bi.vh);
     977    13375799 :                 MT_lock_unset(&b->theaplock);
     978             :         } else {
     979     2279396 :                 bi = (BATiter) {
     980             :                         .b = NULL,
     981             : #ifndef NDEBUG
     982             :                         .locked = true,
     983             : #endif
     984             :                 };
     985             :         }
     986    15654636 :         return bi;
     987             : }
     988             : 
     989             : static inline void
     990    15654765 : bat_iterator_end(BATiter *bip)
     991             : {
     992             :         /* matches bat_iterator */
     993    15654765 :         assert(bip);
     994    15654765 :         assert(bip->locked);
     995    15654765 :         if (bip->h)
     996    13375212 :                 HEAPdecref(bip->h, false);
     997    15654145 :         if (bip->vh)
     998     3800035 :                 HEAPdecref(bip->vh, false);
     999    15654157 :         *bip = (BATiter) {0};
    1000    15654157 : }
    1001             : 
    1002             : /*
    1003             :  * @- Internal HEAP Chunk Management
    1004             :  * Heaps are used in BATs to store data for variable-size atoms.  The
    1005             :  * implementor must manage malloc()/free() functionality for atoms in
    1006             :  * this heap. A standard implementation is provided here.
    1007             :  *
    1008             :  * @table @code
    1009             :  * @item void
    1010             :  * HEAP_initialize  (Heap* h, size_t nbytes, size_t nprivate, int align )
    1011             :  * @item void
    1012             :  * HEAP_destroy     (Heap* h)
    1013             :  * @item var_t
    1014             :  * HEAP_malloc      (Heap* heap, size_t nbytes)
    1015             :  * @item void
    1016             :  * HEAP_free        (Heap *heap, var_t block)
    1017             :  * @item int
    1018             :  * HEAP_private     (Heap* h)
    1019             :  * @item void
    1020             :  * HEAP_printstatus (Heap* h)
    1021             :  * @end table
    1022             :  *
    1023             :  * The heap space starts with a private space that is left untouched
    1024             :  * by the normal chunk allocation.  You can use this private space
    1025             :  * e.g. to store the root of an rtree HEAP_malloc allocates a chunk of
    1026             :  * memory on the heap, and returns an index to it.  HEAP_free frees a
    1027             :  * previously allocated chunk HEAP_private returns an integer index to
    1028             :  * private space.
    1029             :  */
    1030             : 
    1031             : gdk_export gdk_return HEAP_initialize(
    1032             :         Heap *heap,             /* nbytes -- Initial size of the heap. */
    1033             :         size_t nbytes,          /* alignment -- for objects on the heap. */
    1034             :         size_t nprivate,        /* nprivate -- Size of private space */
    1035             :         int alignment           /* alignment restriction for allocated chunks */
    1036             :         );
    1037             : 
    1038             : gdk_export var_t HEAP_malloc(BAT *b, size_t nbytes);
    1039             : gdk_export void HEAP_free(Heap *heap, var_t block);
    1040             : 
    1041             : /*
    1042             :  * @- BAT construction
    1043             :  * @multitable @columnfractions 0.08 0.7
    1044             :  * @item @code{BAT* }
    1045             :  * @tab COLnew (oid headseq, int tailtype, BUN cap, role_t role)
    1046             :  * @item @code{BAT* }
    1047             :  * @tab BATextend (BAT *b, BUN newcap)
    1048             :  * @end multitable
    1049             :  *
    1050             :  * A temporary BAT is instantiated using COLnew with the type aliases
    1051             :  * of the required binary association. The aliases include the
    1052             :  * built-in types, such as TYPE_int....TYPE_ptr, and the atomic types
    1053             :  * introduced by the user. The initial capacity to be accommodated
    1054             :  * within a BAT is indicated by cap.  Their extend is automatically
    1055             :  * incremented upon storage overflow.  Failure to create the BAT
    1056             :  * results in a NULL pointer.
    1057             :  *
    1058             :  * The routine BATclone creates an empty BAT storage area with the
    1059             :  * properties inherited from its argument.
    1060             :  */
    1061             : gdk_export BAT *COLnew(oid hseq, int tltype, BUN capacity, role_t role)
    1062             :         __attribute__((__warn_unused_result__));
    1063             : gdk_export BAT *BATdense(oid hseq, oid tseq, BUN cnt)
    1064             :         __attribute__((__warn_unused_result__));
    1065             : gdk_export gdk_return BATextend(BAT *b, BUN newcap)
    1066             :         __attribute__((__warn_unused_result__));
    1067             : 
    1068             : /* internal */
    1069             : gdk_export uint8_t ATOMelmshift(int sz)
    1070             :         __attribute__((__const__));
    1071             : 
    1072             : gdk_export gdk_return GDKupgradevarheap(BAT *b, var_t v, BUN cap, BUN ncopy)
    1073             :         __attribute__((__warn_unused_result__));
    1074             : gdk_export gdk_return BUNappend(BAT *b, const void *right, bool force)
    1075             :         __attribute__((__warn_unused_result__));
    1076             : gdk_export gdk_return BUNappendmulti(BAT *b, const void *values, BUN count, bool force)
    1077             :         __attribute__((__warn_unused_result__));
    1078             : gdk_export gdk_return BATappend(BAT *b, BAT *n, BAT *s, bool force)
    1079             :         __attribute__((__warn_unused_result__));
    1080             : 
    1081             : gdk_export gdk_return BUNreplace(BAT *b, oid left, const void *right, bool force)
    1082             :         __attribute__((__warn_unused_result__));
    1083             : gdk_export gdk_return BUNreplacemulti(BAT *b, const oid *positions, const void *values, BUN count, bool force)
    1084             :         __attribute__((__warn_unused_result__));
    1085             : gdk_export gdk_return BUNreplacemultiincr(BAT *b, oid position, const void *values, BUN count, bool force)
    1086             :         __attribute__((__warn_unused_result__));
    1087             : 
    1088             : gdk_export gdk_return BUNdelete(BAT *b, oid o)
    1089             :         __attribute__((__warn_unused_result__));
    1090             : gdk_export gdk_return BATdel(BAT *b, BAT *d)
    1091             :         __attribute__((__warn_unused_result__));
    1092             : 
    1093             : gdk_export gdk_return BATreplace(BAT *b, BAT *p, BAT *n, bool force)
    1094             :         __attribute__((__warn_unused_result__));
    1095             : gdk_export gdk_return BATupdate(BAT *b, BAT *p, BAT *n, bool force)
    1096             :         __attribute__((__warn_unused_result__));
    1097             : gdk_export gdk_return BATreplacepos(BAT *b, const oid *positions, BAT *n, bool autoincr, bool force)
    1098             :         __attribute__((__warn_unused_result__));
    1099             : gdk_export gdk_return BATupdatepos(BAT *b, const oid *positions, BAT *n, bool autoincr, bool force)
    1100             :         __attribute__((__warn_unused_result__));
    1101             : 
    1102             : /* Functions to perform a binary search on a sorted BAT.
    1103             :  * See gdk_search.c for details. */
    1104             : gdk_export BUN SORTfnd(BAT *b, const void *v);
    1105             : gdk_export BUN SORTfndfirst(BAT *b, const void *v);
    1106             : gdk_export BUN SORTfndlast(BAT *b, const void *v);
    1107             : 
    1108             : gdk_export BUN ORDERfnd(BAT *b, Heap *oidxh, const void *v);
    1109             : gdk_export BUN ORDERfndfirst(BAT *b, Heap *oidxh, const void *v);
    1110             : gdk_export BUN ORDERfndlast(BAT *b, Heap *oidxh, const void *v);
    1111             : 
    1112             : gdk_export BUN BUNfnd(BAT *b, const void *right);
    1113             : 
    1114             : #define BUNfndVOID(b, v)                                                \
    1115             :         (((is_oid_nil(*(const oid*)(v)) ^ is_oid_nil((b)->tseqbase)) |       \
    1116             :                 (*(const oid*)(v) < (b)->tseqbase) |                      \
    1117             :                 (*(const oid*)(v) >= (b)->tseqbase + (b)->batCount)) ? \
    1118             :          BUN_NONE :                                                     \
    1119             :          (BUN) (*(const oid*)(v) - (b)->tseqbase))
    1120             : 
    1121             : #define BATttype(b)     (BATtdense(b) ? TYPE_oid : (b)->ttype)
    1122             : 
    1123             : #define Tsize(b)        ((b)->twidth)
    1124             : 
    1125             : #define tailsize(b,p)   ((b)->ttype ?                                \
    1126             :                          (ATOMstorage((b)->ttype) == TYPE_msk ?      \
    1127             :                           (((size_t) (p) + 31) / 32) * 4 :      \
    1128             :                           ((size_t) (p)) << (b)->tshift) :     \
    1129             :                          0)
    1130             : 
    1131             : #define Tloc(b,p)       ((void *)((b)->theap->base+(((size_t)(p)+(b)->tbaseoff)<<(b)->tshift)))
    1132             : 
    1133             : typedef var_t stridx_t;
    1134             : #define SIZEOF_STRIDX_T SIZEOF_VAR_T
    1135             : #define GDK_VARALIGN SIZEOF_STRIDX_T
    1136             : 
    1137             : #define BUNtvaroff(bi,p) VarHeapVal((bi).base, (p), (bi).width)
    1138             : 
    1139             : #define BUNtloc(bi,p)   (ATOMstorage((bi).type) == TYPE_msk ? Tmsk(&(bi), p) : (void *) ((char *) (bi).base + ((p) << (bi).shift)))
    1140             : #define BUNtpos(bi,p)   Tpos(&(bi),p)
    1141             : #define BUNtvar(bi,p)   (assert((bi).type && (bi).b->tvarsized), (void *) ((bi).vh->base+BUNtvaroff(bi,p)))
    1142             : #define BUNtail(bi,p)   ((bi).type?(bi).b->tvarsized?BUNtvar(bi,p):BUNtloc(bi,p):BUNtpos(bi,p))
    1143             : 
    1144             : #define BUNlast(b)      (assert((b)->batCount <= BUN_MAX), (b)->batCount)
    1145             : 
    1146             : #define BATcount(b)     ((b)->batCount)
    1147             : 
    1148             : #include "gdk_atoms.h"
    1149             : 
    1150             : #include "gdk_cand.h"
    1151             : 
    1152             : /*
    1153             :  * @- BAT properties
    1154             :  * @multitable @columnfractions 0.08 0.7
    1155             :  * @item BUN
    1156             :  * @tab BATcount (BAT *b)
    1157             :  * @item void
    1158             :  * @tab BATsetcapacity (BAT *b, BUN cnt)
    1159             :  * @item void
    1160             :  * @tab BATsetcount (BAT *b, BUN cnt)
    1161             :  * @item BAT *
    1162             :  * @tab BATkey (BAT *b, bool onoff)
    1163             :  * @item BAT *
    1164             :  * @tab BATmode (BAT *b, bool transient)
    1165             :  * @item BAT *
    1166             :  * @tab BATsetaccess (BAT *b, restrict_t mode)
    1167             :  * @item int
    1168             :  * @tab BATdirty (BAT *b)
    1169             :  * @item restrict_t
    1170             :  * @tab BATgetaccess (BAT *b)
    1171             :  * @end multitable
    1172             :  *
    1173             :  * The function BATcount returns the number of associations stored in
    1174             :  * the BAT.
    1175             :  *
    1176             :  * The BAT is given a new logical name using BBPrename.
    1177             :  *
    1178             :  * The integrity properties to be maintained for the BAT are
    1179             :  * controlled separately.  A key property indicates that duplicates in
    1180             :  * the association dimension are not permitted.
    1181             :  *
    1182             :  * The persistency indicator tells the retention period of BATs.  The
    1183             :  * system support two modes: PERSISTENT and TRANSIENT.
    1184             :  * The PERSISTENT BATs are automatically saved upon session boundary
    1185             :  * or transaction commit.  TRANSIENT BATs are removed upon transaction
    1186             :  * boundary.  All BATs are initially TRANSIENT unless their mode is
    1187             :  * changed using the routine BATmode.
    1188             :  *
    1189             :  * The BAT properties may be changed at any time using BATkey
    1190             :  * and BATmode.
    1191             :  *
    1192             :  * Valid BAT access properties can be set with BATsetaccess and
    1193             :  * BATgetaccess: BAT_READ, BAT_APPEND, and BAT_WRITE.  BATs can be
    1194             :  * designated to be read-only. In this case some memory optimizations
    1195             :  * may be made (slice and fragment bats can point to stable subsets of
    1196             :  * a parent bat).  A special mode is append-only. It is then allowed
    1197             :  * to insert BUNs at the end of the BAT, but not to modify anything
    1198             :  * that already was in there.
    1199             :  */
    1200             : gdk_export BUN BATcount_no_nil(BAT *b, BAT *s);
    1201             : gdk_export void BATsetcapacity(BAT *b, BUN cnt);
    1202             : gdk_export void BATsetcount(BAT *b, BUN cnt);
    1203             : gdk_export BUN BATgrows(BAT *b);
    1204             : gdk_export gdk_return BATkey(BAT *b, bool onoff);
    1205             : gdk_export gdk_return BATmode(BAT *b, bool transient);
    1206             : gdk_export gdk_return BATroles(BAT *b, const char *tnme);
    1207             : gdk_export void BAThseqbase(BAT *b, oid o);
    1208             : gdk_export void BATtseqbase(BAT *b, oid o);
    1209             : 
    1210             : /* The batRestricted field indicates whether a BAT is readonly.
    1211             :  * we have modes: BAT_WRITE  = all permitted
    1212             :  *                BAT_APPEND = append-only
    1213             :  *                BAT_READ   = read-only
    1214             :  * VIEW bats are always mapped read-only.
    1215             :  */
    1216             : typedef enum {
    1217             :         BAT_WRITE,                /* all kinds of access allowed */
    1218             :         BAT_READ,                 /* only read-access allowed */
    1219             :         BAT_APPEND,               /* only reads and appends allowed */
    1220             : } restrict_t;
    1221             : 
    1222             : gdk_export BAT *BATsetaccess(BAT *b, restrict_t mode)
    1223             :         __attribute__((__warn_unused_result__));
    1224             : gdk_export restrict_t BATgetaccess(BAT *b);
    1225             : 
    1226             : 
    1227             : #define BATdirty(b)     (!(b)->batCopiedtodisk ||                    \
    1228             :                          (b)->batDirtydesc ||                                \
    1229             :                          (b)->theap->dirty ||                             \
    1230             :                          ((b)->tvheap != NULL && (b)->tvheap->dirty))
    1231             : #define BATdirtydata(b) (!(b)->batCopiedtodisk ||                    \
    1232             :                          (b)->theap->dirty ||                             \
    1233             :                          ((b)->tvheap != NULL && (b)->tvheap->dirty))
    1234             : 
    1235             : #define BATcapacity(b)  (b)->batCapacity
    1236             : /*
    1237             :  * @- BAT manipulation
    1238             :  * @multitable @columnfractions 0.08 0.7
    1239             :  * @item BAT *
    1240             :  * @tab BATclear (BAT *b, bool force)
    1241             :  * @item BAT *
    1242             :  * @tab COLcopy (BAT *b, int tt, bool writeable, role_t role)
    1243             :  * @end multitable
    1244             :  *
    1245             :  * The routine BATclear removes the binary associations, leading to an
    1246             :  * empty, but (re-)initialized BAT. Its properties are retained.  A
    1247             :  * temporary copy is obtained with Colcopy. The new BAT has an unique
    1248             :  * name.
    1249             :  */
    1250             : gdk_export gdk_return BATclear(BAT *b, bool force);
    1251             : gdk_export BAT *COLcopy(BAT *b, int tt, bool writable, role_t role);
    1252             : 
    1253             : gdk_export gdk_return BATgroup(BAT **groups, BAT **extents, BAT **histo, BAT *b, BAT *s, BAT *g, BAT *e, BAT *h)
    1254             :         __attribute__((__warn_unused_result__));
    1255             : /*
    1256             :  * @- BAT Input/Output
    1257             :  * @multitable @columnfractions 0.08 0.7
    1258             :  * @item BAT *
    1259             :  * @tab BATload (str name)
    1260             :  * @item BAT *
    1261             :  * @tab BATsave (BAT *b)
    1262             :  * @item int
    1263             :  * @tab BATdelete (BAT *b)
    1264             :  * @end multitable
    1265             :  *
    1266             :  * A BAT created by COLnew is considered temporary until one calls the
    1267             :  * routine BATsave or BATmode.  This routine reserves disk space and
    1268             :  * checks for name clashes in the BAT directory. It also makes the BAT
    1269             :  * persistent. The empty BAT is initially marked as ordered on both
    1270             :  * columns.
    1271             :  *
    1272             :  * Failure to read or write the BAT results in a NULL, otherwise it
    1273             :  * returns the BAT pointer.
    1274             :  *
    1275             :  * @- Heap Storage Modes
    1276             :  * The discriminative storage modes are memory-mapped, compressed, or
    1277             :  * loaded in memory.  As can be seen in the bat record, each BAT has
    1278             :  * one BUN-heap (@emph{bn}), and possibly two heaps (@emph{hh} and
    1279             :  * @emph{th}) for variable-sized atoms.
    1280             :  */
    1281             : 
    1282             : gdk_export gdk_return BATsave(BAT *b)
    1283             :         __attribute__((__warn_unused_result__));
    1284             : gdk_export void BATmsync(BAT *b);
    1285             : 
    1286             : #define NOFARM (-1) /* indicate to GDKfilepath to create relative path */
    1287             : 
    1288             : gdk_export char *GDKfilepath(int farmid, const char *dir, const char *nme, const char *ext);
    1289             : gdk_export bool GDKinmemory(int farmid);
    1290             : gdk_export bool GDKembedded(void);
    1291             : gdk_export gdk_return GDKcreatedir(const char *nme);
    1292             : 
    1293             : gdk_export void OIDXdestroy(BAT *b);
    1294             : 
    1295             : /*
    1296             :  * @- Printing
    1297             :  * @multitable @columnfractions 0.08 0.7
    1298             :  * @item int
    1299             :  * @tab BATprintcolumns (stream *f, int argc, BAT *b[]);
    1300             :  * @end multitable
    1301             :  *
    1302             :  * The functions to convert BATs into ASCII. They are primarily meant for ease of
    1303             :  * debugging and to a lesser extent for output processing.  Printing a
    1304             :  * BAT is done essentially by looping through its components, printing
    1305             :  * each association.
    1306             :  *
    1307             :  */
    1308             : gdk_export gdk_return BATprintcolumns(stream *s, int argc, BAT *argv[]);
    1309             : gdk_export gdk_return BATprint(stream *s, BAT *b);
    1310             : 
    1311             : /*
    1312             :  * @- BAT clustering
    1313             :  * @multitable @columnfractions 0.08 0.7
    1314             :  * @item bool
    1315             :  * @tab BATordered (BAT *b)
    1316             :  * @end multitable
    1317             :  *
    1318             :  * When working in a main-memory situation, clustering of data on
    1319             :  * disk-pages is not important. Whenever mmap()-ed data is used
    1320             :  * intensively, reducing the number of page faults is a hot issue.
    1321             :  *
    1322             :  * The above functions rearrange data in MonetDB heaps (used for
    1323             :  * storing BUNs var-sized atoms, or accelerators). Applying these
    1324             :  * clusterings will allow that MonetDB's main-memory oriented
    1325             :  * algorithms work efficiently also in a disk-oriented context.
    1326             :  *
    1327             :  * BATordered starts a check on the tail values to see if they are
    1328             :  * ordered. The result is returned and stored in the tsorted field of
    1329             :  * the BAT.
    1330             :  */
    1331             : gdk_export bool BATordered(BAT *b);
    1332             : gdk_export bool BATordered_rev(BAT *b);
    1333             : gdk_export gdk_return BATsort(BAT **sorted, BAT **order, BAT **groups, BAT *b, BAT *o, BAT *g, bool reverse, bool nilslast, bool stable)
    1334             :         __attribute__((__warn_unused_result__));
    1335             : 
    1336             : 
    1337             : gdk_export void GDKqsort(void *restrict h, void *restrict t, const void *restrict base, size_t n, int hs, int ts, int tpe, bool reverse, bool nilslast);
    1338             : 
    1339             : #define BATtordered(b)  ((b)->tsorted)
    1340             : #define BATtrevordered(b) ((b)->trevsorted)
    1341             : /* BAT is dense (i.e., BATtvoid() is true and tseqbase is not NIL) */
    1342             : #define BATtdense(b)    (!is_oid_nil((b)->tseqbase) &&                       \
    1343             :                          ((b)->tvheap == NULL || (b)->tvheap->free == 0))
    1344             : /* BATtvoid: BAT can be (or actually is) represented by TYPE_void */
    1345             : #define BATtvoid(b)     (BATtdense(b) || (b)->ttype==TYPE_void)
    1346             : #define BATtkey(b)      ((b)->tkey || BATtdense(b))
    1347             : 
    1348             : /* set some properties that are trivial to deduce */
    1349             : static inline void
    1350    26162036 : BATsettrivprop(BAT *b)
    1351             : {
    1352    26162036 :         assert(!is_oid_nil(b->hseqbase));
    1353    26162036 :         assert(is_oid_nil(b->tseqbase) || ATOMtype(b->ttype) == TYPE_oid);
    1354    26162036 :         if (!b->batDirtydesc)
    1355             :                 return;
    1356    26162036 :         if (b->ttype == TYPE_void) {
    1357     1533005 :                 if (is_oid_nil(b->tseqbase)) {
    1358         523 :                         b->tnonil = b->batCount == 0;
    1359         523 :                         b->tnil = !b->tnonil;
    1360         523 :                         b->trevsorted = true;
    1361         523 :                         b->tkey = b->batCount <= 1;
    1362             :                 } else {
    1363     1532482 :                         b->tnonil = true;
    1364     1532482 :                         b->tnil = false;
    1365     1532482 :                         b->tkey = true;
    1366     1532482 :                         b->trevsorted = b->batCount <= 1;
    1367             :                 }
    1368     1533005 :                 b->tsorted = true;
    1369    24629031 :         } else if (b->batCount <= 1) {
    1370     3828175 :                 if (ATOMlinear(b->ttype)) {
    1371     3828008 :                         b->tsorted = true;
    1372     3828008 :                         b->trevsorted = true;
    1373             :                 }
    1374     3828175 :                 b->tkey = true;
    1375     3828175 :                 if (b->batCount == 0) {
    1376     2855838 :                         b->tnonil = true;
    1377     2855838 :                         b->tnil = false;
    1378     2855838 :                         if (b->ttype == TYPE_oid) {
    1379      203897 :                                 b->tseqbase = 0;
    1380             :                         }
    1381      972337 :                 } else if (b->ttype == TYPE_oid) {
    1382             :                         /* b->batCount == 1 */
    1383      131264 :                         oid sqbs = ((const oid *) b->theap->base)[b->tbaseoff];
    1384      131264 :                         if (is_oid_nil(sqbs)) {
    1385         478 :                                 b->tnonil = false;
    1386         478 :                                 b->tnil = true;
    1387             :                         } else {
    1388      130786 :                                 b->tnonil = true;
    1389      130786 :                                 b->tnil = false;
    1390      130786 :                                 b->tminpos = 0;
    1391      130786 :                                 b->tmaxpos = 0;
    1392             :                         }
    1393      131264 :                         b->tseqbase = sqbs;
    1394             :                 }
    1395    20800856 :         } else if (b->batCount == 2 && ATOMlinear(b->ttype)) {
    1396             :                 int c;
    1397      223932 :                 if (b->tvarsized)
    1398       55256 :                         c = ATOMcmp(b->ttype,
    1399             :                                     b->tvheap->base + VarHeapVal(Tloc(b, 0), 0, b->twidth),
    1400             :                                     b->tvheap->base + VarHeapVal(Tloc(b, 0), 1, b->twidth));
    1401             :                 else
    1402      168676 :                         c = ATOMcmp(b->ttype, Tloc(b, 0), Tloc(b, 1));
    1403      223938 :                 b->tsorted = c <= 0;
    1404      223938 :                 b->tnosorted = !b->tsorted;
    1405      223938 :                 b->trevsorted = c >= 0;
    1406      223938 :                 b->tnorevsorted = !b->trevsorted;
    1407      223938 :                 b->tkey = c != 0;
    1408      223938 :                 b->tnokey[0] = 0;
    1409      223938 :                 b->tnokey[1] = !b->tkey;
    1410    20576924 :         } else if (!ATOMlinear(b->ttype)) {
    1411           0 :                 b->tsorted = false;
    1412           0 :                 b->trevsorted = false;
    1413             :         }
    1414             : }
    1415             : 
    1416             : /*
    1417             :  * @+ BAT Buffer Pool
    1418             :  * @multitable @columnfractions 0.08 0.7
    1419             :  * @item int
    1420             :  * @tab BBPfix (bat bi)
    1421             :  * @item int
    1422             :  * @tab BBPunfix (bat bi)
    1423             :  * @item int
    1424             :  * @tab BBPretain (bat bi)
    1425             :  * @item int
    1426             :  * @tab BBPrelease (bat bi)
    1427             :  * @item str
    1428             :  * @tab BBPname (bat bi)
    1429             :  * @item bat
    1430             :  * @tab BBPindex  (str nme)
    1431             :  * @item BAT*
    1432             :  * @tab BATdescriptor (bat bi)
    1433             :  * @end multitable
    1434             :  *
    1435             :  * The BAT Buffer Pool module contains the code to manage the storage
    1436             :  * location of BATs.
    1437             :  *
    1438             :  * The remaining BBP tables contain status information to load, swap
    1439             :  * and migrate the BATs. The core table is BBPcache which contains a
    1440             :  * pointer to the BAT descriptor with its heaps.  A zero entry means
    1441             :  * that the file resides on disk. Otherwise it has been read or mapped
    1442             :  * into memory.
    1443             :  *
    1444             :  * BATs loaded into memory are retained in a BAT buffer pool.  They
    1445             :  * retain their position within the cache during their life cycle,
    1446             :  * which make indexing BATs a stable operation.
    1447             :  *
    1448             :  * The BBPindex routine checks if a BAT with a certain name is
    1449             :  * registered in the buffer pools. If so, it returns its BAT id.  The
    1450             :  * BATdescriptor routine has a BAT id parameter, and returns a pointer
    1451             :  * to the corresponding BAT record (after incrementing the reference
    1452             :  * count). The BAT will be loaded into memory, if necessary.
    1453             :  *
    1454             :  * The structure of the BBP file obeys the tuple format for GDK.
    1455             :  *
    1456             :  * The status and BAT persistency information is encoded in the status
    1457             :  * field.
    1458             :  */
    1459             : typedef struct {
    1460             :         BAT *cache;             /* if loaded: BAT* handle */
    1461             :         char *logical;          /* logical name (may point at bak) */
    1462             :         char bak[16];           /* logical name backup (tmp_%o) */
    1463             :         BAT *desc;              /* the BAT descriptor */
    1464             :         char *options;          /* A string list of options */
    1465             : #if SIZEOF_VOID_P == 4
    1466             :         char physical[20];      /* dir + basename for storage */
    1467             : #else
    1468             :         char physical[24];      /* dir + basename for storage */
    1469             : #endif
    1470             :         bat next;               /* next BBP slot in linked list */
    1471             :         int refs;               /* in-memory references on which the loaded status of a BAT relies */
    1472             :         int lrefs;              /* logical references on which the existence of a BAT relies */
    1473             :         ATOMIC_TYPE status;     /* status mask used for spin locking */
    1474             :         MT_Id pid;              /* creator of this bat while "private" */
    1475             : } BBPrec;
    1476             : 
    1477             : gdk_export bat BBPlimit;
    1478             : #if SIZEOF_VOID_P == 4
    1479             : #define N_BBPINIT       1000
    1480             : #define BBPINITLOG      11
    1481             : #else
    1482             : #define N_BBPINIT       10000
    1483             : #define BBPINITLOG      14
    1484             : #endif
    1485             : #define BBPINIT         (1 << BBPINITLOG)
    1486             : /* absolute maximum number of BATs is N_BBPINIT * BBPINIT
    1487             :  * this also gives the longest possible "physical" name and "bak" name
    1488             :  * of a BAT: the "bak" name is "tmp_%o", so at most 14 + \0 bytes on
    1489             :  * 64 bit architecture and 11 + \0 on 32 bit architecture; the
    1490             :  * physical name is a bit more complicated, but the longest possible
    1491             :  * name is 22 + \0 bytes (16 + \0 on 32 bits) */
    1492             : gdk_export BBPrec *BBP[N_BBPINIT];
    1493             : 
    1494             : /* fast defines without checks; internal use only  */
    1495             : #define BBP_record(i)   BBP[(i)>>BBPINITLOG][(i)&(BBPINIT-1)]
    1496             : #define BBP_cache(i)    BBP_record(i).cache
    1497             : #define BBP_logical(i)  BBP_record(i).logical
    1498             : #define BBP_bak(i)      BBP_record(i).bak
    1499             : #define BBP_next(i)     BBP_record(i).next
    1500             : #define BBP_physical(i) BBP_record(i).physical
    1501             : #define BBP_options(i)  BBP_record(i).options
    1502             : #define BBP_desc(i)     BBP_record(i).desc
    1503             : #define BBP_refs(i)     BBP_record(i).refs
    1504             : #define BBP_lrefs(i)    BBP_record(i).lrefs
    1505             : #define BBP_status(i)   ((unsigned) ATOMIC_GET(&BBP_record(i).status))
    1506             : #define BBP_pid(i)      BBP_record(i).pid
    1507             : #define BATgetId(b)     BBP_logical((b)->batCacheid)
    1508             : #define BBPvalid(i)     (BBP_logical(i) != NULL && *BBP_logical(i) != '.')
    1509             : 
    1510             : /* macros that nicely check parameters */
    1511             : #define BBPstatus(i)    (BBPcheck(i) ? BBP_status(i) : 0)
    1512             : #define BBPrefs(i)      (BBPcheck(i) ? BBP_refs(i) : -1)
    1513             : #define BBPcache(i)     (BBPcheck(i) ? BBP_cache(i) : (BAT*) NULL)
    1514             : #define BBPname(i)      (BBPcheck(i) ? BBP_logical(i) : "")
    1515             : 
    1516             : #define BBPRENAME_ALREADY       (-1)
    1517             : #define BBPRENAME_ILLEGAL       (-2)
    1518             : #define BBPRENAME_LONG          (-3)
    1519             : #define BBPRENAME_MEMORY        (-4)
    1520             : 
    1521             : gdk_export void BBPlock(void);
    1522             : 
    1523             : gdk_export void BBPunlock(void);
    1524             : 
    1525             : gdk_export BAT *BBPquickdesc(bat b);
    1526             : 
    1527             : /*
    1528             :  * @- GDK error handling
    1529             :  *  @multitable @columnfractions 0.08 0.7
    1530             :  * @item str
    1531             :  * @tab
    1532             :  *  GDKmessage
    1533             :  * @item bit
    1534             :  * @tab
    1535             :  *  GDKfatal(str msg)
    1536             :  * @item int
    1537             :  * @tab
    1538             :  *  GDKwarning(str msg)
    1539             :  * @item int
    1540             :  * @tab
    1541             :  *  GDKerror (str msg)
    1542             :  * @item int
    1543             :  * @tab
    1544             :  *  GDKgoterrors ()
    1545             :  * @item int
    1546             :  * @tab
    1547             :  *  GDKsyserror (str msg)
    1548             :  * @item str
    1549             :  * @tab
    1550             :  *  GDKerrbuf
    1551             :  *  @item
    1552             :  * @tab GDKsetbuf (str buf)
    1553             :  * @end multitable
    1554             :  *
    1555             :  * The error handling mechanism is not sophisticated yet. Experience
    1556             :  * should show if this mechanism is sufficient.  Most routines return
    1557             :  * a pointer with zero to indicate an error.
    1558             :  *
    1559             :  * The error messages are also copied to standard output.  The last
    1560             :  * error message is kept around in a global variable.
    1561             :  *
    1562             :  * Error messages can also be collected in a user-provided buffer,
    1563             :  * instead of being echoed to a stream. This is a thread-specific
    1564             :  * issue; you want to decide on the error mechanism on a
    1565             :  * thread-specific basis.  This effect is established with
    1566             :  * GDKsetbuf. The memory (de)allocation of this buffer, that must at
    1567             :  * least be 1024 chars long, is entirely by the user. A pointer to
    1568             :  * this buffer is kept in the pseudo-variable GDKerrbuf. Normally,
    1569             :  * this is a NULL pointer.
    1570             :  */
    1571             : #define GDKMAXERRLEN    10240
    1572             : #define GDKWARNING      "!WARNING: "
    1573             : #define GDKERROR        "!ERROR: "
    1574             : #define GDKMESSAGE      "!OS: "
    1575             : #define GDKFATAL        "!FATAL: "
    1576             : 
    1577             : /* Data Distilleries uses ICU for internationalization of some MonetDB error messages */
    1578             : 
    1579             : #include "gdk_tracer.h"
    1580             : 
    1581             : gdk_export gdk_return GDKtracer_fill_comp_info(BAT *id, BAT *component, BAT *log_level);
    1582             : 
    1583             : #define GDKerror(format, ...)                                   \
    1584             :         GDKtracer_log(__FILE__, __func__, __LINE__, M_ERROR,    \
    1585             :                       GDK, NULL, format, ##__VA_ARGS__)
    1586             : #define GDKsyserr(errno, format, ...)                                   \
    1587             :         GDKtracer_log(__FILE__, __func__, __LINE__, M_CRITICAL,         \
    1588             :                       GDK, GDKstrerror(errno, (char[64]){0}, 64),       \
    1589             :                       format, ##__VA_ARGS__)
    1590             : #define GDKsyserror(format, ...)        GDKsyserr(errno, format, ##__VA_ARGS__)
    1591             : 
    1592             : gdk_export _Noreturn void GDKfatal(_In_z_ _Printf_format_string_ const char *format, ...)
    1593             :         __attribute__((__format__(__printf__, 1, 2)));
    1594             :         /*
    1595             : gdk_export void GDKfatal(_In_z_ _Printf_format_string_ const char *format, ...)
    1596             :         __attribute__((__format__(__printf__, 1, 2)));
    1597             :         */
    1598             : gdk_export void GDKclrerr(void);
    1599             : 
    1600             : 
    1601             : /* tfastins* family: update a value at a particular location in the bat
    1602             :  * bunfastapp* family: append a value to the bat
    1603             :  * *_nocheck: do not check whether the capacity is large enough
    1604             :  * * (without _nocheck): check bat capacity and possibly extend
    1605             :  *
    1606             :  * This means, for tfastins* it is the caller's responsibility to set
    1607             :  * the batCount and theap->free values correctly (e.g. by calling
    1608             :  * BATsetcount(), and for *_nocheck to make sure there is enough space
    1609             :  * allocated in the theap (tvheap for variable-sized types is still
    1610             :  * extended if needed, making that these functions can fail).
    1611             :  */
    1612             : static inline gdk_return __attribute__((__warn_unused_result__))
    1613    73376050 : tfastins_nocheckVAR(BAT *b, BUN p, const void *v)
    1614             : {
    1615             :         var_t d;
    1616             :         gdk_return rc;
    1617    73376050 :         assert(b->tbaseoff == 0);
    1618    73376050 :         assert(b->theap->parentid == b->batCacheid);
    1619    73376050 :         if ((rc = ATOMputVAR(b, &d, v)) != GDK_SUCCEED)
    1620             :                 return rc;
    1621    74371391 :         if (b->twidth < SIZEOF_VAR_T &&
    1622    65262698 :             (b->twidth <= 2 ? d - GDK_VAROFFSET : d) >= ((size_t) 1 << (8 << b->tshift))) {
    1623             :                 /* doesn't fit in current heap, upgrade it */
    1624       12954 :                 rc = GDKupgradevarheap(b, d, 0, MAX(p, b->batCount));
    1625       12955 :                 if (rc != GDK_SUCCEED)
    1626             :                         return rc;
    1627             :         }
    1628    74371392 :         switch (b->twidth) {
    1629    25337653 :         case 1:
    1630    25337653 :                 ((uint8_t *) b->theap->base)[p] = (uint8_t) (d - GDK_VAROFFSET);
    1631    25337653 :                 break;
    1632    14979458 :         case 2:
    1633    14979458 :                 ((uint16_t *) b->theap->base)[p] = (uint16_t) (d - GDK_VAROFFSET);
    1634    14979458 :                 break;
    1635    25650629 :         case 4:
    1636    25650629 :                 ((uint32_t *) b->theap->base)[p] = (uint32_t) d;
    1637    25650629 :                 break;
    1638             : #if SIZEOF_VAR_T == 8
    1639     8403652 :         case 8:
    1640     8403652 :                 ((uint64_t *) b->theap->base)[p] = (uint64_t) d;
    1641     8403652 :                 break;
    1642             : #endif
    1643             :         }
    1644             :         return GDK_SUCCEED;
    1645             : }
    1646             : 
    1647             : static inline gdk_return __attribute__((__warn_unused_result__))
    1648   354330186 : tfastins_nocheckFIX(BAT *b, BUN p, const void *v)
    1649             : {
    1650   354330186 :         return ATOMputFIX(b->ttype, Tloc(b, p), v);
    1651             : }
    1652             : 
    1653             : static inline gdk_return __attribute__((__warn_unused_result__))
    1654   347814373 : tfastins_nocheck(BAT *b, BUN p, const void *v)
    1655             : {
    1656   347814373 :         assert(b->theap->parentid == b->batCacheid);
    1657   347814373 :         assert(b->tbaseoff == 0);
    1658   347814373 :         if (b->ttype == TYPE_void) {
    1659             :                 ;
    1660   349241991 :         } else if (ATOMstorage(b->ttype) == TYPE_msk) {
    1661           0 :                 mskSetVal(b, p, * (msk *) v);
    1662   349241991 :         } else if (b->tvarsized) {
    1663    26964759 :                 return tfastins_nocheckVAR(b, p, v);
    1664             :         } else {
    1665   322277232 :                 return tfastins_nocheckFIX(b, p, v);
    1666             :         }
    1667             :         return GDK_SUCCEED;
    1668             : }
    1669             : 
    1670             : static inline gdk_return __attribute__((__warn_unused_result__))
    1671   347432367 : tfastins(BAT *b, BUN p, const void *v)
    1672             : {
    1673   347432367 :         if (p > BATcapacity(b)) {
    1674           0 :                 if (p >= BUN_MAX) {
    1675           0 :                         GDKerror("tfastins: too many elements to accommodate (" BUNFMT ")\n", BUN_MAX);
    1676           0 :                         return GDK_FAIL;
    1677             :                 }
    1678           0 :                 BUN sz = BATgrows(b);
    1679           0 :                 if (sz <= p)
    1680           0 :                         sz = p + BATTINY;
    1681           0 :                 gdk_return rc = BATextend(b, sz);
    1682           0 :                 if (rc != GDK_SUCCEED)
    1683             :                         return rc;
    1684             :         }
    1685   347432367 :         return tfastins_nocheck(b, p, v);
    1686             : }
    1687             : 
    1688             : static inline gdk_return __attribute__((__warn_unused_result__))
    1689      144736 : bunfastapp_nocheck(BAT *b, const void *v)
    1690             : {
    1691      144736 :         BUN p = b->batCount;
    1692      144736 :         if (ATOMstorage(b->ttype) == TYPE_msk && p % 32 == 0)
    1693           0 :                 ((uint32_t *) b->theap->base)[p / 32] = 0;
    1694      144736 :         gdk_return rc = tfastins_nocheck(b, p, v);
    1695      144735 :         if (rc == GDK_SUCCEED) {
    1696      144735 :                 b->batCount++;
    1697      144735 :                 if (ATOMstorage(b->ttype) == TYPE_msk) {
    1698           0 :                         if (p % 32 == 0)
    1699           0 :                                 b->theap->free += 4;
    1700             :                 } else
    1701      144735 :                         b->theap->free += b->twidth;
    1702             :         }
    1703      144735 :         return rc;
    1704             : }
    1705             : 
    1706             : static inline gdk_return __attribute__((__warn_unused_result__))
    1707   347526968 : bunfastapp(BAT *b, const void *v)
    1708             : {
    1709   347526968 :         BUN p = b->batCount;
    1710   347526968 :         if (ATOMstorage(b->ttype) == TYPE_msk && p % 32 == 0)
    1711           0 :                 ((uint32_t *) b->theap->base)[p / 32] = 0;
    1712   347526968 :         gdk_return rc = tfastins(b, p, v);
    1713   337698462 :         if (rc == GDK_SUCCEED) {
    1714   335342676 :                 b->batCount++;
    1715   335342676 :                 if (ATOMstorage(b->ttype) == TYPE_msk) {
    1716           0 :                         if (p % 32 == 0)
    1717           0 :                                 b->theap->free += 4;
    1718             :                 } else
    1719   335342676 :                         b->theap->free += b->twidth;
    1720             :         }
    1721   337698462 :         return rc;
    1722             : }
    1723             : 
    1724             : #define bunfastappTYPE(TYPE, b, v)                                      \
    1725             :         (BATcount(b) >= BATcapacity(b) &&                            \
    1726             :          ((BATcount(b) == BUN_MAX &&                                    \
    1727             :            (GDKerror("bunfastapp: too many elements to accommodate (" BUNFMT ")\n", BUN_MAX), \
    1728             :             true)) ||                                                   \
    1729             :           BATextend((b), BATgrows(b)) != GDK_SUCCEED) ?                 \
    1730             :          GDK_FAIL :                                                     \
    1731             :          (assert((b)->theap->parentid == (b)->batCacheid),             \
    1732             :           (b)->theap->free += sizeof(TYPE),                               \
    1733             :           ((TYPE *) (b)->theap->base)[(b)->batCount++] = * (const TYPE *) (v), \
    1734             :           GDK_SUCCEED))
    1735             : 
    1736             : static inline gdk_return __attribute__((__warn_unused_result__))
    1737         342 : bunfastapp_nocheckVAR(BAT *b, const void *v)
    1738             : {
    1739             :         gdk_return rc;
    1740         342 :         rc = tfastins_nocheckVAR(b, b->batCount, v);
    1741         342 :         if (rc == GDK_SUCCEED) {
    1742         342 :                 b->batCount++;
    1743         342 :                 b->theap->free += b->twidth;
    1744             :         }
    1745         342 :         return rc;
    1746             : }
    1747             : 
    1748             : /*
    1749             :  * @- Column Imprints Functions
    1750             :  *
    1751             :  * @multitable @columnfractions 0.08 0.7
    1752             :  * @item BAT*
    1753             :  * @tab
    1754             :  *  BATimprints (BAT *b)
    1755             :  * @end multitable
    1756             :  *
    1757             :  * The column imprints index structure.
    1758             :  *
    1759             :  */
    1760             : 
    1761             : gdk_export gdk_return BATimprints(BAT *b);
    1762             : gdk_export void IMPSdestroy(BAT *b);
    1763             : gdk_export lng IMPSimprintsize(BAT *b);
    1764             : 
    1765             : /* The ordered index structure */
    1766             : 
    1767             : gdk_export gdk_return BATorderidx(BAT *b, bool stable);
    1768             : gdk_export gdk_return GDKmergeidx(BAT *b, BAT**a, int n_ar);
    1769             : gdk_export bool BATcheckorderidx(BAT *b);
    1770             : 
    1771             : #include "gdk_delta.h"
    1772             : #include "gdk_hash.h"
    1773             : #include "gdk_bbp.h"
    1774             : #include "gdk_utils.h"
    1775             : 
    1776             : /* functions defined in gdk_bat.c */
    1777             : gdk_export gdk_return void_inplace(BAT *b, oid id, const void *val, bool force)
    1778             :         __attribute__((__warn_unused_result__));
    1779             : gdk_export BAT *BATattach(int tt, const char *heapfile, role_t role);
    1780             : 
    1781             : #ifdef NATIVE_WIN32
    1782             : #ifdef _MSC_VER
    1783             : #define fileno _fileno
    1784             : #endif
    1785             : #define fdopen _fdopen
    1786             : #define putenv _putenv
    1787             : #endif
    1788             : 
    1789             : /* Return a pointer to the value contained in V.  Also see VALget
    1790             :  * which returns a void *. */
    1791             : static inline const void *
    1792   204606964 : VALptr(const ValRecord *v)
    1793             : {
    1794   204606964 :         switch (ATOMstorage(v->vtype)) {
    1795       22171 :         case TYPE_void: return (const void *) &v->val.oval;
    1796           0 :         case TYPE_msk: return (const void *) &v->val.mval;
    1797     6892562 :         case TYPE_bte: return (const void *) &v->val.btval;
    1798      381543 :         case TYPE_sht: return (const void *) &v->val.shval;
    1799    74781861 :         case TYPE_int: return (const void *) &v->val.ival;
    1800       12299 :         case TYPE_flt: return (const void *) &v->val.fval;
    1801      555763 :         case TYPE_dbl: return (const void *) &v->val.dval;
    1802    53244572 :         case TYPE_lng: return (const void *) &v->val.lval;
    1803             : #ifdef HAVE_HGE
    1804       16682 :         case TYPE_hge: return (const void *) &v->val.hval;
    1805             : #endif
    1806         508 :         case TYPE_uuid: return (const void *) &v->val.uval;
    1807      204171 :         case TYPE_ptr: return (const void *) &v->val.pval;
    1808    68492556 :         case TYPE_str: return (const void *) v->val.sval;
    1809        2276 :         default:       return (const void *) v->val.pval;
    1810             :         }
    1811             : }
    1812             : 
    1813             : /*
    1814             :  * The kernel maintains a central table of all active threads.  They
    1815             :  * are indexed by their tid. The structure contains information on the
    1816             :  * input/output file descriptors, which should be set before a
    1817             :  * database operation is started. It ensures that output is delivered
    1818             :  * to the proper client.
    1819             :  *
    1820             :  * The Thread structure should be ideally made directly accessible to
    1821             :  * each thread. This speeds up access to tid and file descriptors.
    1822             :  */
    1823             : #define THREADS 1024
    1824             : #define THREADDATA      3
    1825             : 
    1826             : typedef struct threadStruct {
    1827             :         int tid;                /* logical ID by MonetDB; val == index
    1828             :                                  * into this array + 1 (0 is
    1829             :                                  * invalid) */
    1830             :         ATOMIC_TYPE pid;        /* thread id, 0 = unallocated */
    1831             :         char name[MT_NAME_LEN];
    1832             :         void *data[THREADDATA];
    1833             :         uintptr_t sp;
    1834             : } *Thread;
    1835             : 
    1836             : 
    1837             : gdk_export int THRgettid(void);
    1838             : gdk_export Thread THRget(int tid);
    1839             : gdk_export MT_Id THRcreate(void (*f) (void *), void *arg, enum MT_thr_detach d, const char *name);
    1840             : gdk_export void THRdel(Thread t);
    1841             : gdk_export void THRsetdata(int, void *);
    1842             : gdk_export void *THRgetdata(int);
    1843             : gdk_export int THRhighwater(void);
    1844             : 
    1845             : gdk_export void *THRdata[THREADDATA];
    1846             : 
    1847             : #define GDKstdout       ((stream*)THRdata[0])
    1848             : #define GDKstdin        ((stream*)THRdata[1])
    1849             : 
    1850             : #define GDKerrbuf       ((char*)THRgetdata(2))
    1851             : #define GDKsetbuf(x)    THRsetdata(2,(void *)(x))
    1852             : 
    1853             : #define THRget_errbuf(t)        ((char*)t->data[2])
    1854             : #define THRset_errbuf(t,b)      (t->data[2] = b)
    1855             : 
    1856             : static inline bat
    1857   482718917 : BBPcheck(bat x)
    1858             : {
    1859   482718917 :         if (!is_bat_nil(x)) {
    1860   482185661 :                 assert(x > 0);
    1861             : 
    1862   482185661 :                 if (x < 0 || x >= getBBPsize() || BBP_logical(x) == NULL) {
    1863           0 :                         TRC_DEBUG(CHECK_, "range error %d\n", (int) x);
    1864             :                 } else {
    1865   480737161 :                         assert(BBP_pid(x) == 0 || BBP_pid(x) == MT_getpid());
    1866   480730553 :                         return x;
    1867             :                 }
    1868             :         }
    1869             :         return 0;
    1870             : }
    1871             : 
    1872             : static inline BAT *
    1873   104875485 : BATdescriptor(bat i)
    1874             : {
    1875             :         BAT *b = NULL;
    1876             : 
    1877   104875485 :         if (BBPcheck(i)) {
    1878   104236425 :                 if (BBPfix(i) <= 0)
    1879             :                         return NULL;
    1880   104302392 :                 b = BBP_cache(i);
    1881   104302392 :                 if (b == NULL)
    1882       15805 :                         b = BBPdescriptor(i);
    1883             :         }
    1884             :         return b;
    1885             : }
    1886             : 
    1887             : static inline void *
    1888      641667 : Tpos(BATiter *bi, BUN p)
    1889             : {
    1890      641667 :         assert(bi->base == NULL);
    1891      641667 :         if (bi->vh) {
    1892             :                 oid o;
    1893          17 :                 assert(!is_oid_nil(bi->tseq));
    1894          17 :                 if (((ccand_t *) bi->vh)->type == CAND_NEGOID) {
    1895          17 :                         BUN nexc = (bi->vhfree - sizeof(ccand_t)) / SIZEOF_OID;
    1896          17 :                         o = bi->tseq + p;
    1897          17 :                         if (nexc > 0) {
    1898          17 :                                 const oid *exc = (const oid *) (bi->vh->base + sizeof(ccand_t));
    1899          17 :                                 if (o >= exc[0]) {
    1900          17 :                                         if (o + nexc > exc[nexc - 1]) {
    1901             :                                                 o += nexc;
    1902             :                                         } else {
    1903             :                                                 BUN lo = 0;
    1904           0 :                                                 BUN hi = nexc - 1;
    1905           0 :                                                 while (hi - lo > 1) {
    1906           0 :                                                         BUN mid = (hi + lo) / 2;
    1907           0 :                                                         if (exc[mid] - mid > o)
    1908             :                                                                 hi = mid;
    1909             :                                                         else
    1910             :                                                                 lo = mid;
    1911             :                                                 }
    1912           0 :                                                 o += hi;
    1913             :                                         }
    1914             :                                 }
    1915             :                         }
    1916             :                 } else {
    1917           0 :                         const uint32_t *msk = (const uint32_t *) (bi->vh->base + sizeof(ccand_t));
    1918           0 :                         BUN nmsk = (bi->vhfree - sizeof(ccand_t)) / sizeof(uint32_t);
    1919             :                         o = 0;
    1920           0 :                         for (BUN i = 0; i < nmsk; i++) {
    1921           0 :                                 uint32_t m = candmask_pop(msk[i]);
    1922           0 :                                 if (o + m > p) {
    1923             :                                         m = msk[i];
    1924           0 :                                         for (i = 0; i < 32; i++) {
    1925           0 :                                                 if (m & (1U << i) && ++o == p)
    1926             :                                                         break;
    1927             :                                         }
    1928             :                                         break;
    1929             :                                 }
    1930             :                                 o += m;
    1931             :                         }
    1932             :                 }
    1933          17 :                 bi->tvid = o;
    1934      641650 :         } else if (is_oid_nil(bi->tseq)) {
    1935         558 :                 bi->tvid = oid_nil;
    1936             :         } else {
    1937      641092 :                 bi->tvid = bi->tseq + p;
    1938             :         }
    1939      641667 :         return (void *) &bi->tvid;
    1940             : }
    1941             : 
    1942             : static inline bool
    1943             : Tmskval(BATiter *bi, BUN p)
    1944             : {
    1945        4632 :         return ((uint32_t *) bi->base)[p / 32] & (1U << (p % 32));
    1946             : }
    1947             : 
    1948             : static inline void *
    1949             : Tmsk(BATiter *bi, BUN p)
    1950             : {
    1951       10092 :         bi->tmsk = Tmskval(bi, p);
    1952           0 :         return &bi->tmsk;
    1953             : }
    1954             : 
    1955             : /* return the oid value at BUN position p from the (v)oid bat b
    1956             :  * works with any TYPE_void or TYPE_oid bat */
    1957             : static inline oid
    1958    19543994 : BUNtoid(BAT *b, BUN p)
    1959             : {
    1960    19543994 :         assert(ATOMtype(b->ttype) == TYPE_oid);
    1961             :         /* BATcount is the number of valid entries, so with
    1962             :          * exceptions, the last value can well be larger than
    1963             :          * b->tseqbase + BATcount(b) */
    1964    19543994 :         assert(p < BATcount(b));
    1965    19543994 :         assert(b->ttype == TYPE_void || b->tvheap == NULL);
    1966    19543994 :         if (is_oid_nil(b->tseqbase)) {
    1967    19333237 :                 if (b->ttype == TYPE_void)
    1968           0 :                         return oid_nil;
    1969    19333237 :                 MT_lock_set(&b->theaplock);
    1970    19480029 :                 oid o = ((const oid *) b->theap->base)[p + b->tbaseoff];
    1971    19480029 :                 MT_lock_unset(&b->theaplock);
    1972    19693323 :                 return o;
    1973             :         }
    1974      210757 :         if (b->ttype == TYPE_oid || b->tvheap == NULL) {
    1975      210740 :                 return b->tseqbase + p;
    1976             :         }
    1977             :         /* b->tvheap != NULL, so we know there will be no parallel
    1978             :          * modifications (so no locking) */
    1979          17 :         BATiter bi = bat_iterator_nolock(b);
    1980          17 :         return * (oid *) Tpos(&bi, p);
    1981             : }
    1982             : 
    1983             : /*
    1984             :  * @+ Transaction Management
    1985             :  * @multitable @columnfractions 0.08 0.7
    1986             :  * @item int
    1987             :  * @tab
    1988             :  *  TMcommit ()
    1989             :  * @item int
    1990             :  * @tab
    1991             :  *  TMabort ()
    1992             :  * @item int
    1993             :  * @tab
    1994             :  *  TMsubcommit ()
    1995             :  * @end multitable
    1996             :  *
    1997             :  * MonetDB by default offers a global transaction environment.  The
    1998             :  * global transaction involves all activities on all persistent BATs
    1999             :  * by all threads.  Each global transaction ends with either TMabort
    2000             :  * or TMcommit, and immediately starts a new transaction.  TMcommit
    2001             :  * implements atomic commit to disk on the collection of all
    2002             :  * persistent BATs. For all persistent BATs, the global commit also
    2003             :  * flushes the delta status for these BATs (see
    2004             :  * BATcommit/BATabort). This allows to perform TMabort quickly in
    2005             :  * memory (without re-reading all disk images from disk).  The
    2006             :  * collection of which BATs is persistent is also part of the global
    2007             :  * transaction state. All BATs that where persistent at the last
    2008             :  * commit, but were made transient since then, are made persistent
    2009             :  * again by TMabort.  In other words, BATs that are deleted, are only
    2010             :  * physically deleted at TMcommit time. Until that time, rollback
    2011             :  * (TMabort) is possible.
    2012             :  *
    2013             :  * Use of TMabort is currently NOT RECOMMENDED due to two bugs:
    2014             :  *
    2015             :  * @itemize
    2016             :  * @item
    2017             :  * TMabort after a failed %TMcommit@ does not bring us back to the
    2018             :  * previous committed state; but to the state at the failed TMcommit.
    2019             :  * @item
    2020             :  * At runtime, TMabort does not undo BAT name changes, whereas a cold
    2021             :  * MonetDB restart does.
    2022             :  * @end itemize
    2023             :  *
    2024             :  * In effect, the problems with TMabort reduce the functionality of
    2025             :  * the global transaction mechanism to consistent checkpointing at
    2026             :  * each TMcommit. For many applications, consistent checkpointingis
    2027             :  * enough.
    2028             :  *
    2029             :  * Extension modules exist that provide fine grained locking (lock
    2030             :  * module) and Write Ahead Logging (sqlserver).  Applications that
    2031             :  * need more fine-grained transactions, should build this on top of
    2032             :  * these extension primitives.
    2033             :  *
    2034             :  * TMsubcommit is intended to quickly add or remove BATs from the
    2035             :  * persistent set. In both cases, rollback is not necessary, such that
    2036             :  * the commit protocol can be accelerated. It comes down to writing a
    2037             :  * new BBP.dir.
    2038             :  *
    2039             :  * Its parameter is a BAT-of-BATs (in the tail); the persistence
    2040             :  * status of that BAT is committed. We assume here that the calling
    2041             :  * thread has exclusive access to these bats.  An error is reported if
    2042             :  * you try to partially commit an already committed persistent BAT (it
    2043             :  * needs the rollback mechanism).
    2044             :  */
    2045             : gdk_export gdk_return TMcommit(void);
    2046             : gdk_export void TMabort(void);
    2047             : gdk_export gdk_return TMsubcommit(BAT *bl);
    2048             : gdk_export gdk_return TMsubcommit_list(bat *restrict subcommit, BUN *restrict sizes, int cnt, lng logno, lng transid);
    2049             : 
    2050             : /*
    2051             :  * @- Delta Management
    2052             :  *  @multitable @columnfractions 0.08 0.6
    2053             :  * @item BAT *
    2054             :  * @tab BATcommit (BAT *b)
    2055             :  * @item BAT *
    2056             :  * @tab BATfakeCommit (BAT *b)
    2057             :  * @item BAT *
    2058             :  * @tab BATundo (BAT *b)
    2059             :  * @end multitable
    2060             :  *
    2061             :  * The BAT keeps track of updates with respect to a 'previous state'.
    2062             :  * Do not confuse 'previous state' with 'stable' or
    2063             :  * 'commited-on-disk', because these concepts are not always the
    2064             :  * same. In particular, they diverge when BATcommit, BATfakecommit,
    2065             :  * and BATundo are called explictly, bypassing the normal global
    2066             :  * TMcommit protocol (some applications need that flexibility).
    2067             :  *
    2068             :  * BATcommit make the current BAT state the new 'stable state'.  This
    2069             :  * happens inside the global TMcommit on all persistent BATs previous
    2070             :  * to writing all bats to persistent storage using a BBPsync.
    2071             :  *
    2072             :  * EXPERT USE ONLY: The routine BATfakeCommit updates the delta
    2073             :  * information on BATs and clears the dirty bit. This avoids any
    2074             :  * copying to disk.  Expert usage only, as it bypasses the global
    2075             :  * commit protocol, and changes may be lost after quitting or crashing
    2076             :  * MonetDB.
    2077             :  *
    2078             :  * BATabort undo-s all changes since the previous state. The global
    2079             :  * TMabort achieves a rollback to the previously committed state by
    2080             :  * doing BATabort on all persistent bats.
    2081             :  *
    2082             :  * BUG: after a failed TMcommit, TMabort does not do anything because
    2083             :  * TMcommit does the BATcommits @emph{before} attempting to sync to
    2084             :  * disk instead of @sc{after} doing this.
    2085             :  */
    2086             : gdk_export void BATcommit(BAT *b, BUN size);
    2087             : gdk_export void BATfakeCommit(BAT *b);
    2088             : gdk_export void BATundo(BAT *b);
    2089             : 
    2090             : /*
    2091             :  * @+ BAT Alignment and BAT views
    2092             :  * @multitable @columnfractions 0.08 0.7
    2093             :  * @item int
    2094             :  * @tab ALIGNsynced (BAT* b1, BAT* b2)
    2095             :  * @item int
    2096             :  * @tab ALIGNsync   (BAT *b1, BAT *b2)
    2097             :  * @item int
    2098             :  * @tab ALIGNrelated (BAT *b1, BAT *b2)
    2099             :  *
    2100             :  * @item BAT*
    2101             :  * @tab VIEWcreate   (oid seq, BAT *b)
    2102             :  * @item int
    2103             :  * @tab isVIEW   (BAT *b)
    2104             :  * @item bat
    2105             :  * @tab VIEWhparent   (BAT *b)
    2106             :  * @item bat
    2107             :  * @tab VIEWtparent   (BAT *b)
    2108             :  * @end multitable
    2109             :  *
    2110             :  * Alignments of two columns of a BAT means that the system knows
    2111             :  * whether these two columns are exactly equal. Relatedness of two
    2112             :  * BATs means that one pair of columns (either head or tail) of both
    2113             :  * BATs is aligned. The first property is checked by ALIGNsynced, the
    2114             :  * latter by ALIGNrelated.
    2115             :  *
    2116             :  * All algebraic BAT commands propagate the properties - including
    2117             :  * alignment properly on their results.
    2118             :  *
    2119             :  * VIEW BATs are BATs that lend their storage from a parent BAT.  They
    2120             :  * are just a descriptor that points to the data in this parent BAT. A
    2121             :  * view is created with VIEWcreate. The cache id of the parent (if
    2122             :  * any) is returned by VIEWtparent (otherwise it returns 0).
    2123             :  *
    2124             :  * VIEW bats are read-only!!
    2125             :  */
    2126             : gdk_export int ALIGNsynced(BAT *b1, BAT *b2);
    2127             : 
    2128             : gdk_export void BATassertProps(BAT *b);
    2129             : 
    2130             : gdk_export BAT *VIEWcreate(oid seq, BAT *b);
    2131             : gdk_export void VIEWbounds(BAT *b, BAT *view, BUN l, BUN h);
    2132             : 
    2133             : #define ALIGNapp(x, f, e)                                               \
    2134             :         do {                                                            \
    2135             :                 if (!(f) && ((x)->batRestricted == BAT_READ ||               \
    2136             :                              (x)->batSharecnt > 0)) {                     \
    2137             :                         GDKerror("access denied to %s, aborting.\n",  \
    2138             :                                  BATgetId(x));                          \
    2139             :                         return (e);                                     \
    2140             :                 }                                                       \
    2141             :         } while (false)
    2142             : 
    2143             : /* the parentid in a VIEW is correct for the normal view. We must
    2144             :  * correct for the reversed view.
    2145             :  */
    2146             : #define isVIEW(x)                                                       \
    2147             :         (((x)->theap && (x)->theap->parentid != (x)->batCacheid) || \
    2148             :          ((x)->tvheap && (x)->tvheap->parentid != (x)->batCacheid))
    2149             : 
    2150             : #define VIEWtparent(x)  ((x)->theap == NULL || (x)->theap->parentid == (x)->batCacheid ? 0 : (x)->theap->parentid)
    2151             : #define VIEWvtparent(x) ((x)->tvheap == NULL || (x)->tvheap->parentid == (x)->batCacheid ? 0 : (x)->tvheap->parentid)
    2152             : 
    2153             : /*
    2154             :  * @+ BAT Iterators
    2155             :  *  @multitable @columnfractions 0.15 0.7
    2156             :  * @item BATloop
    2157             :  * @tab
    2158             :  *  (BAT *b; BUN p, BUN q)
    2159             :  * @item BATloopDEL
    2160             :  * @tab
    2161             :  *  (BAT *b; BUN p; BUN q; int dummy)
    2162             :  * @item HASHloop
    2163             :  * @tab
    2164             :  *  (BAT *b; Hash *h, size_t dummy; ptr value)
    2165             :  * @item HASHloop_bte
    2166             :  * @tab
    2167             :  *  (BAT *b; Hash *h, size_t idx; bte *value, BUN w)
    2168             :  * @item HASHloop_sht
    2169             :  * @tab
    2170             :  *  (BAT *b; Hash *h, size_t idx; sht *value, BUN w)
    2171             :  * @item HASHloop_int
    2172             :  * @tab
    2173             :  *  (BAT *b; Hash *h, size_t idx; int *value, BUN w)
    2174             :  * @item HASHloop_flt
    2175             :  * @tab
    2176             :  *  (BAT *b; Hash *h, size_t idx; flt *value, BUN w)
    2177             :  * @item HASHloop_lng
    2178             :  * @tab
    2179             :  *  (BAT *b; Hash *h, size_t idx; lng *value, BUN w)
    2180             :  * @item HASHloop_hge
    2181             :  * @tab
    2182             :  *  (BAT *b; Hash *h, size_t idx; hge *value, BUN w)
    2183             :  * @item HASHloop_dbl
    2184             :  * @tab
    2185             :  *  (BAT *b; Hash *h, size_t idx; dbl *value, BUN w)
    2186             :  * @item  HASHloop_str
    2187             :  * @tab
    2188             :  *  (BAT *b; Hash *h, size_t idx; str value, BUN w)
    2189             :  * @item HASHlooploc
    2190             :  * @tab
    2191             :  *  (BAT *b; Hash *h, size_t idx; ptr value, BUN w)
    2192             :  * @item HASHloopvar
    2193             :  * @tab
    2194             :  *  (BAT *b; Hash *h, size_t idx; ptr value, BUN w)
    2195             :  * @end multitable
    2196             :  *
    2197             :  * The @emph{BATloop()} looks like a function call, but is actually a
    2198             :  * macro.
    2199             :  *
    2200             :  * @- simple sequential scan
    2201             :  * The first parameter is a BAT, the p and q are BUN pointers, where p
    2202             :  * is the iteration variable.
    2203             :  */
    2204             : #define BATloop(r, p, q)                        \
    2205             :         for (q = BUNlast(r), p = 0; p < q; p++)
    2206             : 
    2207             : /*
    2208             :  * @+ Common BAT Operations
    2209             :  * Much used, but not necessarily kernel-operations on BATs.
    2210             :  *
    2211             :  * For each BAT we maintain its dimensions as separately accessible
    2212             :  * properties. They can be used to improve query processing at higher
    2213             :  * levels.
    2214             :  */
    2215             : enum prop_t {
    2216             :         CURRENTLY_NO_PROPERTIES_DEFINED,
    2217             : };
    2218             : 
    2219             : gdk_export ValPtr BATgetprop(BAT *b, enum prop_t idx);
    2220             : 
    2221             : /*
    2222             :  * @- BAT relational operators
    2223             :  *
    2224             :  * The full-materialization policy intermediate results in MonetDB
    2225             :  * means that a join can produce an arbitrarily large result and choke
    2226             :  * the system. The Data Distilleries tool therefore first computes the
    2227             :  * join result size before the actual join (better waste time than
    2228             :  * crash the server). To exploit that perfect result size knowledge,
    2229             :  * an result-size estimate parameter was added to all equi-join
    2230             :  * implementations.  TODO: add this for
    2231             :  * semijoin/select/unique/diff/intersect
    2232             :  *
    2233             :  * @- modes for thethajoin
    2234             :  */
    2235             : #define JOIN_EQ         0
    2236             : #define JOIN_LT         (-1)
    2237             : #define JOIN_LE         (-2)
    2238             : #define JOIN_GT         1
    2239             : #define JOIN_GE         2
    2240             : #define JOIN_BAND       3
    2241             : #define JOIN_NE         (-3)
    2242             : 
    2243             : gdk_export BAT *BATselect(BAT *b, BAT *s, const void *tl, const void *th, bool li, bool hi, bool anti);
    2244             : gdk_export BAT *BATthetaselect(BAT *b, BAT *s, const void *val, const char *op);
    2245             : 
    2246             : gdk_export BAT *BATconstant(oid hseq, int tt, const void *val, BUN cnt, role_t role);
    2247             : gdk_export gdk_return BATsubcross(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool max_one)
    2248             :         __attribute__((__warn_unused_result__));
    2249             : 
    2250             : gdk_export gdk_return BATleftjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, BUN estimate)
    2251             :         __attribute__((__warn_unused_result__));
    2252             : gdk_export gdk_return BATouterjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool match_one, BUN estimate)
    2253             :         __attribute__((__warn_unused_result__));
    2254             : gdk_export gdk_return BATthetajoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, int op, bool nil_matches, BUN estimate)
    2255             :         __attribute__((__warn_unused_result__));
    2256             : gdk_export gdk_return BATsemijoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool max_one, BUN estimate)
    2257             :         __attribute__((__warn_unused_result__));
    2258             : gdk_export BAT *BATintersect(BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool max_one, BUN estimate);
    2259             : gdk_export BAT *BATdiff(BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool not_in, BUN estimate);
    2260             : gdk_export gdk_return BATjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, BUN estimate)
    2261             :         __attribute__((__warn_unused_result__));
    2262             : gdk_export BUN BATguess_uniques(BAT *b, struct canditer *ci);
    2263             : gdk_export gdk_return BATbandjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, const void *c1, const void *c2, bool li, bool hi, BUN estimate)
    2264             :         __attribute__((__warn_unused_result__));
    2265             : gdk_export gdk_return BATrangejoin(BAT **r1p, BAT **r2p, BAT *l, BAT *rl, BAT *rh, BAT *sl, BAT *sr, bool li, bool hi, bool anti, bool symmetric, BUN estimate)
    2266             :         __attribute__((__warn_unused_result__));
    2267             : gdk_export BAT *BATproject(BAT *restrict l, BAT *restrict r);
    2268             : gdk_export BAT *BATproject2(BAT *restrict l, BAT *restrict r1, BAT *restrict r2);
    2269             : gdk_export BAT *BATprojectchain(BAT **bats);
    2270             : 
    2271             : gdk_export BAT *BATslice(BAT *b, BUN low, BUN high);
    2272             : 
    2273             : gdk_export BAT *BATunique(BAT *b, BAT *s);
    2274             : 
    2275             : gdk_export BAT *BATmergecand(BAT *a, BAT *b);
    2276             : gdk_export BAT *BATintersectcand(BAT *a, BAT *b);
    2277             : gdk_export BAT *BATdiffcand(BAT *a, BAT *b);
    2278             : 
    2279             : gdk_export gdk_return BATfirstn(BAT **topn, BAT **gids, BAT *b, BAT *cands, BAT *grps, BUN n, bool asc, bool nilslast, bool distinct)
    2280             :         __attribute__((__warn_unused_result__));
    2281             : 
    2282             : #include "gdk_calc.h"
    2283             : 
    2284             : /*
    2285             :  * @- BAT sample operators
    2286             :  *
    2287             :  * @multitable @columnfractions 0.08 0.7
    2288             :  * @item BAT *
    2289             :  * @tab BATsample (BAT *b, n)
    2290             :  * @end multitable
    2291             :  *
    2292             :  * The routine BATsample returns a random sample on n BUNs of a BAT.
    2293             :  *
    2294             :  */
    2295             : gdk_export BAT *BATsample(BAT *b, BUN n);
    2296             : gdk_export BAT *BATsample_with_seed(BAT *b, BUN n, uint64_t seed);
    2297             : 
    2298             : /*
    2299             :  *
    2300             :  */
    2301             : #define MAXPARAMS       32
    2302             : 
    2303             : #define CHECK_QRY_TIMEOUT_SHIFT 14
    2304             : #define CHECK_QRY_TIMEOUT_STEP  (1 << CHECK_QRY_TIMEOUT_SHIFT)
    2305             : #define CHECK_QRY_TIMEOUT_MASK  (CHECK_QRY_TIMEOUT_STEP - 1)
    2306             : 
    2307             : #define TIMEOUT_MSG "Timeout was reached!"
    2308             : 
    2309             : #define TIMEOUT_HANDLER(rtpe)                   \
    2310             :         do {                                    \
    2311             :                 GDKerror(TIMEOUT_MSG);          \
    2312             :                 return rtpe;                    \
    2313             :         } while(0)
    2314             : 
    2315             : #define GOTO_LABEL_TIMEOUT_HANDLER(label)       \
    2316             :         do {                                    \
    2317             :                 GDKerror(TIMEOUT_MSG);          \
    2318             :                 goto label;                     \
    2319             :         } while(0)
    2320             : 
    2321             : #define GDK_CHECK_TIMEOUT_BODY(timeoffset, callback)            \
    2322             :         do {                                                    \
    2323             :                 if (timeoffset && GDKusec() > timeoffset) {  \
    2324             :                         callback;                               \
    2325             :                 }                                               \
    2326             :         } while (0)
    2327             : 
    2328             : #define GDK_CHECK_TIMEOUT(timeoffset, counter, callback)                \
    2329             :         do {                                                            \
    2330             :                 if (timeoffset) {                                       \
    2331             :                         if (counter > CHECK_QRY_TIMEOUT_STEP) {              \
    2332             :                                 GDK_CHECK_TIMEOUT_BODY(timeoffset, callback); \
    2333             :                                 counter = 0;                            \
    2334             :                         } else {                                        \
    2335             :                                 counter++;                              \
    2336             :                         }                                               \
    2337             :                 }                                                       \
    2338             :         } while (0)
    2339             : 
    2340             : /* here are some useful construct to iterate a number of times (the
    2341             :  * REPEATS argument--only evaluated once) and checking for a timeout
    2342             :  * every once in a while; the TIMEOFFSET value is a variable of type lng
    2343             :  * which is either 0 or the GDKusec() compatible time after which the
    2344             :  * loop should terminate; check for this condition after the loop using
    2345             :  * the TIMEOUT_CHECK macro; in order to break out of any of these loops,
    2346             :  * use TIMEOUT_LOOP_BREAK since plain break won't do it; it is perfectly
    2347             :  * ok to use continue inside the body */
    2348             : 
    2349             : /* use IDX as a loop variable, initializing it to 0 and incrementing it
    2350             :  * on each iteration */
    2351             : #define TIMEOUT_LOOP_IDX(IDX, REPEATS, TIMEOFFSET)                      \
    2352             :         for (BUN REPS = (IDX = 0, (REPEATS)); REPS > 0; REPS = 0) /* "loops" at most once */ \
    2353             :                 for (BUN CTR1 = 0, END1 = (REPS + CHECK_QRY_TIMEOUT_MASK) >> CHECK_QRY_TIMEOUT_SHIFT; CTR1 < END1 && TIMEOFFSET >= 0; CTR1++, TIMEOFFSET = TIMEOFFSET > 0 && GDKusec() > TIMEOFFSET ? -1 : TIMEOFFSET) \
    2354             :                         for (BUN CTR2 = 0, END2 = CTR1 == END1 - 1 ? REPS & CHECK_QRY_TIMEOUT_MASK : CHECK_QRY_TIMEOUT_STEP; CTR2 < END2; CTR2++, IDX++)
    2355             : 
    2356             : /* declare and use IDX as a loop variable, initializing it to 0 and
    2357             :  * incrementing it on each iteration */
    2358             : #define TIMEOUT_LOOP_IDX_DECL(IDX, REPEATS, TIMEOFFSET)                 \
    2359             :         for (BUN IDX = 0, REPS = (REPEATS); REPS > 0; REPS = 0) /* "loops" at most once */ \
    2360             :                 for (BUN CTR1 = 0, END1 = (REPS + CHECK_QRY_TIMEOUT_MASK) >> CHECK_QRY_TIMEOUT_SHIFT; CTR1 < END1 && TIMEOFFSET >= 0; CTR1++, TIMEOFFSET = TIMEOFFSET > 0 && GDKusec() > TIMEOFFSET ? -1 : TIMEOFFSET) \
    2361             :                         for (BUN CTR2 = 0, END2 = CTR1 == END1 - 1 ? REPS & CHECK_QRY_TIMEOUT_MASK : CHECK_QRY_TIMEOUT_STEP; CTR2 < END2; CTR2++, IDX++)
    2362             : 
    2363             : /* there is no user-visible loop variable */
    2364             : #define TIMEOUT_LOOP(REPEATS, TIMEOFFSET)                               \
    2365             :         for (BUN CTR1 = 0, REPS = (REPEATS), END1 = (REPS + CHECK_QRY_TIMEOUT_MASK) >> CHECK_QRY_TIMEOUT_SHIFT; CTR1 < END1 && TIMEOFFSET >= 0; CTR1++, TIMEOFFSET = TIMEOFFSET > 0 && GDKusec() > TIMEOFFSET ? -1 : TIMEOFFSET) \
    2366             :                 for (BUN CTR2 = 0, END2 = CTR1 == END1 - 1 ? REPS & CHECK_QRY_TIMEOUT_MASK : CHECK_QRY_TIMEOUT_STEP; CTR2 < END2; CTR2++)
    2367             : 
    2368             : /* break out of the loop (cannot use do/while trick here) */
    2369             : #define TIMEOUT_LOOP_BREAK                      \
    2370             :         {                                       \
    2371             :                 END1 = END2 = 0;                \
    2372             :                 continue;                       \
    2373             :         }
    2374             : 
    2375             : /* check whether a timeout occurred, and execute the CALLBACK argument
    2376             :  * if it did */
    2377             : #define TIMEOUT_CHECK(TIMEOFFSET, CALLBACK)     \
    2378             :         do {                                    \
    2379             :                 if (TIMEOFFSET == -1)           \
    2380             :                         CALLBACK;               \
    2381             :         } while (0)
    2382             : 
    2383             : typedef struct gdk_callback {
    2384             :         char *name;
    2385             :         int argc;
    2386             :         int interval;  // units sec
    2387             :         lng last_called; // timestamp GDKusec
    2388             :         gdk_return (*func)(int argc, void *argv[]);
    2389             :         struct gdk_callback *next;
    2390             :         void *argv[FLEXIBLE_ARRAY_MEMBER];
    2391             : } gdk_callback;
    2392             : 
    2393             : typedef gdk_return gdk_callback_func(int argc, void *argv[]);
    2394             : 
    2395             : gdk_export gdk_return gdk_add_callback(char *name, gdk_callback_func *f, int argc, void
    2396             :                 *argv[], int interval);
    2397             : gdk_export gdk_return gdk_remove_callback(char *, gdk_callback_func *f);
    2398             : 
    2399             : #endif /* _GDK_H_ */

Generated by: LCOV version 1.14