LCOV - code coverage report
Current view: top level - gdk - gdk_utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 743 1042 71.3 %
Date: 2024-12-20 20:06:10 Functions: 53 56 94.6 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * @a M. L. Kersten, P. Boncz, N. Nes
      15             :  *
      16             :  * @* Utilities
      17             :  * The utility section contains functions to initialize the Monet
      18             :  * database system, memory allocation details, and a basic system
      19             :  * logging scheme.
      20             :  */
      21             : #include "monetdb_config.h"
      22             : #include "monet_options.h"
      23             : 
      24             : #include "gdk.h"
      25             : #include "gdk_private.h"
      26             : #include "mutils.h"
      27             : 
      28             : static BAT *GDKkey = NULL;
      29             : static BAT *GDKval = NULL;
      30             : ATOMIC_TYPE GDKdebug = ATOMIC_VAR_INIT(0);
      31             : 
      32             : #include <signal.h>
      33             : 
      34             : #ifdef HAVE_FCNTL_H
      35             : #include <fcntl.h>
      36             : #endif
      37             : 
      38             : #ifdef HAVE_PWD_H
      39             : # include <pwd.h>
      40             : #endif
      41             : 
      42             : #ifdef HAVE_SYS_PARAM_H
      43             : # include <sys/param.h>  /* prerequisite of sys/sysctl on OpenBSD */
      44             : #endif
      45             : #ifdef BSD /* BSD macro is defined in sys/param.h */
      46             : # include <sys/sysctl.h>
      47             : #endif
      48             : #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
      49             : #include <sys/resource.h>
      50             : #endif
      51             : 
      52             : #ifdef __CYGWIN__
      53             : #include <sysinfoapi.h>
      54             : #endif
      55             : 
      56             : static ATOMIC_TYPE GDKstopped = ATOMIC_VAR_INIT(0);
      57             : static void GDKunlockHome(int farmid);
      58             : 
      59             : #undef malloc
      60             : #undef calloc
      61             : #undef realloc
      62             : #undef free
      63             : 
      64             : /* when the number of updates to a BAT is less than 1 in this number, we
      65             :  * keep the unique_est property */
      66             : BUN gdk_unique_estimate_keep_fraction = GDK_UNIQUE_ESTIMATE_KEEP_FRACTION; /* should become a define once */
      67             : /* if the number of unique values is less than 1 in this number, we
      68             :  * destroy the hash rather than update it in HASH{append,insert,delete} */
      69             : BUN hash_destroy_uniques_fraction = HASH_DESTROY_UNIQUES_FRACTION;     /* likewise */
      70             : /* if the estimated number of unique values is less than 1 in this
      71             :  * number, don't build a hash table to do a hashselect */
      72             : dbl no_hash_select_fraction = NO_HASH_SELECT_FRACTION;           /* same here */
      73             : /* if the hash chain is longer than this number, we delete the hash
      74             :  * rather than maintaining it in HASHdelete */
      75             : BUN hash_destroy_chain_length = HASH_DESTROY_CHAIN_LENGTH;
      76             : 
      77             : /*
      78             :  * @+ Monet configuration file
      79             :  * Parse a possible MonetDB config file (if specified by command line
      80             :  * option -c/--config) to extract pre-settings of system variables.
      81             :  * Un-recognized parameters are simply skipped, because they may be
      82             :  * picked up by other components of the system.  The consequence is
      83             :  * that making a typing error in the configuration file may be
      84             :  * unnoticed for a long time.  Syntax errors are immediately flagged,
      85             :  * though.
      86             :  *
      87             :  * Since the GDK kernel moves into the database directory, we need to
      88             :  * keep the absolute path to the MonetDB config file for top-levels to
      89             :  * access its information.
      90             :  */
      91             : 
      92             : static bool
      93         333 : GDKenvironment(const char *dbpath)
      94             : {
      95         333 :         if (dbpath == NULL) {
      96           0 :                 TRC_CRITICAL(GDK, "Database name missing.\n");
      97           0 :                 return false;
      98             :         }
      99         333 :         if (strlen(dbpath) >= FILENAME_MAX) {
     100           0 :                 TRC_CRITICAL(GDK, "Database name too long.\n");
     101           0 :                 return false;
     102             :         }
     103         333 :         if (!GDKembedded() && !MT_path_absolute(dbpath)) {
     104           0 :                 TRC_CRITICAL(GDK, "Directory not an absolute path: %s.\n", dbpath);
     105           0 :                 return false;
     106             :         }
     107             :         return true;
     108             : }
     109             : 
     110             : static struct orig_value {
     111             :         struct orig_value *next;
     112             :         char *value;
     113             :         char key[];
     114             : } *orig_value;
     115             : static MT_Lock GDKenvlock = MT_LOCK_INITIALIZER(GDKenvlock);
     116             : 
     117             : const char *
     118      468943 : GDKgetenv(const char *name)
     119             : {
     120      468943 :         MT_lock_set(&GDKenvlock);
     121      468944 :         for (struct orig_value *ov = orig_value; ov; ov = ov->next) {
     122           0 :                 if (strcmp(ov->key, name) == 0) {
     123           0 :                         MT_lock_unset(&GDKenvlock);
     124           0 :                         return ov->value;
     125             :                 }
     126             :         }
     127      468944 :         MT_lock_unset(&GDKenvlock);
     128      468944 :         if (GDKkey && GDKval) {
     129      468298 :                 BUN b = BUNfnd(GDKkey, name);
     130             : 
     131      468298 :                 if (b != BUN_NONE) {
     132      327287 :                         BATiter GDKenvi = bat_iterator(GDKval);
     133      327287 :                         const char *v = BUNtvar(GDKenvi, b);
     134      327287 :                         bat_iterator_end(&GDKenvi);
     135      327287 :                         return v;
     136             :                 }
     137             :         }
     138             :         return NULL;
     139             : }
     140             : 
     141             : bool
     142      283908 : GDKgetenv_istext(const char *name, const char *text)
     143             : {
     144      283908 :         const char *val = GDKgetenv(name);
     145             : 
     146      283908 :         return val && strcasecmp(val, text) == 0;
     147             : }
     148             : 
     149             : bool
     150       38204 : GDKgetenv_isyes(const char *name)
     151             : {
     152       38204 :         return GDKgetenv_istext(name, "yes");
     153             : }
     154             : 
     155             : bool
     156      245704 : GDKgetenv_istrue(const char *name)
     157             : {
     158      245704 :         return GDKgetenv_istext(name, "true");
     159             : }
     160             : 
     161             : int
     162       97218 : GDKgetenv_int(const char *name, int def)
     163             : {
     164       97218 :         const char *val = GDKgetenv(name);
     165             : 
     166       97218 :         if (val)
     167         320 :                 return atoi(val);
     168             :         return def;
     169             : }
     170             : 
     171             : #define ESCAPE_CHAR     '%'
     172             : 
     173             : static bool
     174        8089 : isutf8(const char *v, size_t *esclen)
     175             : {
     176        8089 :         size_t n = 1;
     177        8089 :         int nutf8 = 0;
     178        8089 :         int m = 0;
     179      151765 :         for (size_t i = 0; v[i]; i++) {
     180      143676 :                 if (nutf8 > 0) {
     181           8 :                         if ((v[i] & 0xC0) != 0x80 ||
     182           0 :                             (m != 0 && (v[i] & m) == 0))
     183           0 :                                 goto badutf8;
     184           8 :                         m = 0;
     185           8 :                         nutf8--;
     186      143668 :                 } else if ((v[i] & 0xE0) == 0xC0) {
     187           0 :                         nutf8 = 1;
     188           0 :                         if ((v[i] & 0x1E) == 0)
     189           0 :                                 goto badutf8;
     190      143668 :                 } else if ((v[i] & 0xF0) == 0xE0) {
     191           4 :                         nutf8 = 2;
     192           4 :                         if ((v[i] & 0x0F) == 0)
     193           0 :                                 m = 0x20;
     194      143664 :                 } else if ((v[i] & 0xF8) == 0xF0) {
     195           0 :                         nutf8 = 3;
     196           0 :                         if ((v[i] & 0x07) == 0)
     197           0 :                                 m = 0x30;
     198      143664 :                 } else if ((v[i] & 0x80) != 0) {
     199           0 :                         goto badutf8;
     200             :                 }
     201             :         }
     202        8089 :         *esclen = 0;
     203        8089 :         return true;
     204           0 :   badutf8:
     205           0 :         for (size_t i = 0; v[i]; i++) {
     206           0 :                 if (v[i] & 0x80 || v[i] == ESCAPE_CHAR)
     207           0 :                         n += 3;
     208             :                 else
     209           0 :                         n++;
     210             :         }
     211           0 :         *esclen = n;
     212           0 :         return false;
     213             : }
     214             : 
     215             : gdk_return
     216        8089 : GDKsetenv(const char *name, const char *value)
     217             : {
     218        8089 :         static const char hexdigits[] = "0123456789abcdef";
     219        8089 :         char *conval = NULL;
     220        8089 :         size_t esclen = 0;
     221        8089 :         if (!isutf8(value, &esclen)) {
     222           0 :                 size_t j = strlen(name) + 1;
     223           0 :                 struct orig_value *ov = GDKmalloc(offsetof(struct orig_value, key) + j + strlen(value) + 1);
     224           0 :                 if (ov == NULL)
     225             :                         return GDK_FAIL;
     226           0 :                 strcpy(ov->key, name);
     227           0 :                 ov->value = ov->key + j;
     228           0 :                 strcpy(ov->value, value);
     229           0 :                 conval = GDKmalloc(esclen);
     230           0 :                 if (conval == NULL) {
     231           0 :                         GDKfree(ov);
     232           0 :                         return GDK_FAIL;
     233             :                 }
     234             :                 j = 0;
     235           0 :                 for (size_t i = 0; value[i]; i++) {
     236           0 :                         if (value[i] & 0x80 || value[i] == ESCAPE_CHAR) {
     237           0 :                                 conval[j++] = ESCAPE_CHAR;
     238           0 :                                 conval[j++] = hexdigits[(unsigned char) value[i] >> 4];
     239           0 :                                 conval[j++] = hexdigits[(unsigned char) value[i] & 0xF];
     240             :                         } else {
     241           0 :                                 conval[j++] = value[i];
     242             :                         }
     243             :                 }
     244           0 :                 conval[j] = 0;
     245           0 :                 MT_lock_set(&GDKenvlock);
     246           0 :                 ov->next = orig_value;
     247           0 :                 orig_value = ov;
     248             :                 /* remove previous value if present (later in list) */
     249           0 :                 for (ov = orig_value; ov->next; ov = ov->next) {
     250           0 :                         if (strcmp(ov->next->key, name) == 0) {
     251           0 :                                 struct orig_value *ovn = ov->next;
     252           0 :                                 ov->next = ovn->next;
     253           0 :                                 GDKfree(ovn);
     254             :                         }
     255             :                 }
     256           0 :                 MT_lock_unset(&GDKenvlock);
     257             :         } else {
     258             :                 /* remove previous value if present */
     259        8089 :                 MT_lock_set(&GDKenvlock);
     260        8089 :                 for (struct orig_value **ovp = &orig_value; *ovp; ovp = &(*ovp)->next) {
     261           0 :                         if (strcmp((*ovp)->key, name) == 0) {
     262           0 :                                 struct orig_value *ov = *ovp;
     263           0 :                                 *ovp = ov->next;
     264           0 :                                 GDKfree(ov);
     265           0 :                                 break;
     266             :                         }
     267             :                 }
     268        8089 :                 MT_lock_unset(&GDKenvlock);
     269             :         }
     270        8089 :         BUN p = BUNfnd(GDKkey, name);
     271        8089 :         gdk_return rc;
     272        8089 :         if (p != BUN_NONE) {
     273        1914 :                 rc = BUNreplace(GDKval, p + GDKval->hseqbase,
     274             :                                 conval ? conval : value, false);
     275             :         } else {
     276        7132 :                 rc = BUNappend(GDKkey, name, false);
     277        7132 :                 if (rc == GDK_SUCCEED) {
     278       14264 :                         rc = BUNappend(GDKval, conval ? conval : value, false);
     279        7132 :                         if (rc != GDK_SUCCEED) {
     280             :                                 /* undo earlier successful append to
     281             :                                  * keep bats aligned (this can't really
     282             :                                  * fail, but we must check the result
     283             :                                  * anyway) */
     284           0 :                                 if (BUNdelete(GDKkey, GDKkey->hseqbase + GDKkey->batCount - 1) != GDK_SUCCEED)
     285           0 :                                         GDKerror("deleting key failed after failed value append");
     286             :                         }
     287             :                 }
     288             :         }
     289        8089 :         assert(BATcount(GDKval) == BATcount(GDKkey));
     290        8089 :         GDKfree(conval);
     291        8089 :         return rc;
     292             : }
     293             : 
     294             : gdk_return
     295          64 : GDKcopyenv(BAT **key, BAT **val, bool writable)
     296             : {
     297          64 :         BAT *k, *v;
     298             : 
     299          64 :         if (key == NULL || val == NULL) {
     300           0 :                 GDKerror("called incorrectly.\n");
     301           0 :                 return GDK_FAIL;
     302             :         }
     303          64 :         k = COLcopy(GDKkey, GDKkey->ttype, writable, TRANSIENT);
     304          64 :         v = COLcopy(GDKval, GDKval->ttype, writable, TRANSIENT);
     305          64 :         if (k == NULL || v == NULL) {
     306           0 :                 BBPreclaim(k);
     307           0 :                 BBPreclaim(v);
     308           0 :                 return GDK_FAIL;
     309             :         }
     310          64 :         *key = k;
     311          64 :         *val = v;
     312          64 :         return GDK_SUCCEED;
     313             : }
     314             : 
     315             : 
     316             : /*
     317             :  * @+ System logging
     318             :  * Per database a log file can be maintained for collection of system
     319             :  * management information. Its contents is driven by the upper layers,
     320             :  * which encode information such as who logged on and how long the
     321             :  * session went on.  The lower layers merely store error information
     322             :  * on the file.  It should not be used for crash recovery, because
     323             :  * this should be dealt with on a per client basis.
     324             :  *
     325             :  * A system log can be maintained in the database to keep track of
     326             :  * session and crash information. It should regularly be refreshed to
     327             :  * avoid disk overflow.
     328             :  */
     329             : #define GDKLOCK ".gdk_lock"
     330             : 
     331             : #define GET_GDKLOCK(x) BBPfarms[BBPselectfarm((x), 0, offheap)].lock_file
     332             : 
     333             : #define GDKLOGOFF       "LOGOFF"
     334             : #define GDKFOUNDDEAD    "FOUND     DEAD"
     335             : #define GDKLOGON        "LOGON"
     336             : #define GDKCRASH        "CRASH"
     337             : 
     338             : /*
     339             :  * Single-lined comments can now be logged safely, together with
     340             :  * process, thread and user ID, and the current time.
     341             :  */
     342             : static void __attribute__((__format__(__printf__, 2, 3)))
     343         827 : GDKlog(FILE *lockFile, const char *format, ...)
     344             : {
     345         827 :         va_list ap;
     346         827 :         char *p = 0, buf[1024];
     347         827 :         time_t tm = time(0);
     348             : #if defined(HAVE_CTIME_R3) || defined(HAVE_CTIME_R)
     349         827 :         char tbuf[26];
     350             : #endif
     351         827 :         char *ctm;
     352             : 
     353         827 :         if (MT_pagesize() == 0 || lockFile == NULL)
     354           1 :                 return;
     355             : 
     356         826 :         va_start(ap, format);
     357         826 :         vsnprintf(buf, sizeof(buf), format, ap);
     358         826 :         va_end(ap);
     359             : 
     360             :         /* remove forbidden characters from message */
     361         826 :         for (p = buf; (p = strchr(p, '\n')) != NULL; *p = ' ')
     362             :                 ;
     363         826 :         for (p = buf; (p = strchr(p, '@')) != NULL; *p = ' ')
     364             :                 ;
     365             : 
     366         826 :         fseek(lockFile, 0, SEEK_END);
     367             : #ifndef HAVE_GETUID
     368             : #define getuid() 0
     369             : #endif
     370             : #ifdef HAVE_CTIME_R3
     371             :         ctm = ctime_r(&tm, tbuf, sizeof(tbuf));
     372             : #else
     373         826 :         ctm = ctime_r(&tm, tbuf);
     374             : #endif
     375         826 :         fprintf(lockFile, "USR=%d PID=%d TIME=%.24s @ %s\n", (int) getuid(), (int) getpid(), ctm, buf);
     376         826 :         fflush(lockFile);
     377             : }
     378             : 
     379             : /*
     380             :  * @+ Interrupt handling
     381             :  */
     382             : #ifdef WIN32
     383             : static void
     384             : BATSIGabort(int nr)
     385             : {
     386             :         (void) nr;
     387             :         _Exit(3);               /* emulate Windows exit code without pop-up */
     388             : }
     389             : #endif
     390             : 
     391             : #ifndef NATIVE_WIN32
     392             : static void
     393         334 : BATSIGinit(void)
     394             : {
     395             : #ifdef HAVE_SIGACTION
     396         334 :         struct sigaction sa;
     397         334 :         sigemptyset(&sa.sa_mask);
     398         334 :         sa.sa_flags = 0;
     399             : #ifdef SIGPIPE
     400         334 :         sa.sa_handler = SIG_IGN;
     401         334 :         sigaction(SIGPIPE, &sa, NULL);
     402             : #endif
     403             : #ifdef SIGHUP
     404         334 :         sa.sa_handler = GDKtracer_reinit_basic;
     405         334 :         sigaction(SIGHUP, &sa, NULL);
     406             : #endif
     407             : #ifdef WIN32
     408             :         sa.sa_handler = BATSIGabort;
     409             :         sigaction(SIGABRT, &sa, NULL);
     410             : #endif
     411             : #else
     412             : #ifdef SIGPIPE
     413             :         (void) signal(SIGPIPE, SIG_IGN);
     414             : #endif
     415             : #ifdef SIGHUP
     416             :         // Register signal to GDKtracer (logrotate)
     417             :         (void) signal(SIGHUP, GDKtracer_reinit_basic);
     418             : #endif
     419             : #ifdef WIN32
     420             :         (void) signal(SIGABRT, BATSIGabort);
     421             : #endif
     422             : #endif
     423             : #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
     424             :         _set_abort_behavior(0, _CALL_REPORTFAULT | _WRITE_ABORT_MSG);
     425             :         _set_error_mode(_OUT_TO_STDERR);
     426             : #endif
     427         334 : }
     428             : #endif /* NATIVE_WIN32 */
     429             : 
     430             : /* memory thresholds; these values some "sane" constants only, really
     431             :  * set in GDKinit() */
     432             : #define MMAP_MINSIZE_PERSISTENT ((size_t) 1 << 18)
     433             : #if SIZEOF_SIZE_T == 4
     434             : #define MMAP_MINSIZE_TRANSIENT  ((size_t) 1 << 20)
     435             : #else
     436             : #define MMAP_MINSIZE_TRANSIENT  ((size_t) 1 << 32)
     437             : #endif
     438             : #define MMAP_PAGESIZE           ((size_t) 1 << 16)
     439             : size_t GDK_mmap_minsize_persistent = MMAP_MINSIZE_PERSISTENT;
     440             : size_t GDK_mmap_minsize_transient = MMAP_MINSIZE_TRANSIENT;
     441             : size_t GDK_mmap_pagesize = MMAP_PAGESIZE; /* mmap granularity */
     442             : size_t GDK_mem_maxsize = GDK_VM_MAXSIZE;
     443             : size_t GDK_vm_maxsize = GDK_VM_MAXSIZE;
     444             : 
     445             : #define SEG_SIZE(x)     ((ssize_t) (((x) + _MT_pagesize - 1) & ~(_MT_pagesize - 1)))
     446             : 
     447             : /* This block is to provide atomic addition and subtraction to select
     448             :  * variables.  We use intrinsic functions (recognized and inlined by
     449             :  * the compiler) for both the GNU C compiler and Microsoft Visual
     450             :  * Studio.  By doing this, we avoid locking overhead.  There is also a
     451             :  * fall-back for other compilers. */
     452             : #include "matomic.h"
     453             : static ATOMIC_TYPE GDK_mallocedbytes_estimate = ATOMIC_VAR_INIT(0);
     454             : static ATOMIC_TYPE GDK_vm_cursize = ATOMIC_VAR_INIT(0);
     455             : 
     456             : size_t _MT_pagesize = 0;        /* variable holding page size */
     457             : size_t _MT_npages = 0;          /* variable holding memory size in pages */
     458             : 
     459             : static lng programepoch;
     460             : 
     461             : void
     462         335 : MT_init(void)
     463             : {
     464         335 :         programepoch = GDKusec();
     465             : #ifdef _MSC_VER
     466             :         {
     467             :                 SYSTEM_INFO sysInfo;
     468             : 
     469             :                 GetSystemInfo(&sysInfo);
     470             :                 _MT_pagesize = sysInfo.dwPageSize;
     471             :         }
     472             : #elif defined(BSD) && defined(HW_PAGESIZE)
     473             :         {
     474             :                 int size;
     475             :                 size_t len = sizeof(int);
     476             :                 int mib[2];
     477             : 
     478             :                 /* Everyone should have permission to make this call,
     479             :                  * if we get a failure something is really wrong. */
     480             :                 mib[0] = CTL_HW;
     481             :                 mib[1] = HW_PAGESIZE;
     482             :                 sysctl(mib, 2, &size, &len, NULL, 0);
     483             :                 _MT_pagesize = size;
     484             :         }
     485             : #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
     486         335 :         _MT_pagesize = (size_t)sysconf(_SC_PAGESIZE);
     487             : #endif
     488         335 :         if (_MT_pagesize <= 0)
     489           0 :                 _MT_pagesize = 4096;    /* default */
     490             : 
     491             : #ifdef WIN32
     492             :         {
     493             :                 MEMORYSTATUSEX memStatEx;
     494             : 
     495             :                 memStatEx.dwLength = sizeof(memStatEx);
     496             :                 if (GlobalMemoryStatusEx(&memStatEx))
     497             :                         _MT_npages = (size_t) (memStatEx.ullTotalPhys / _MT_pagesize);
     498             :         }
     499             : #elif defined(BSD) && defined(HW_MEMSIZE) && SIZEOF_SIZE_T == SIZEOF_LNG
     500             :         /* Darwin, 64-bits */
     501             :         {
     502             :                 uint64_t size = 0;
     503             :                 size_t len = sizeof(size);
     504             :                 int mib[2];
     505             : 
     506             :                 /* Everyone should have permission to make this call,
     507             :                  * if we get a failure something is really wrong. */
     508             :                 mib[0] = CTL_HW;
     509             :                 mib[1] = HW_MEMSIZE;
     510             :                 sysctl(mib, 2, &size, &len, NULL, 0);
     511             :                 _MT_npages = size / _MT_pagesize;
     512             :         }
     513             : #elif defined(BSD) && defined (HW_PHYSMEM64) && SIZEOF_SIZE_T == SIZEOF_LNG
     514             :         /* OpenBSD, 64-bits */
     515             :         {
     516             :                 int64_t size = 0;
     517             :                 size_t len = sizeof(size);
     518             :                 int mib[2];
     519             : 
     520             :                 /* Everyone should have permission to make this call,
     521             :                  * if we get a failure something is really wrong. */
     522             :                 mib[0] = CTL_HW;
     523             :                 mib[1] = HW_PHYSMEM64;
     524             :                 sysctl(mib, 2, &size, &len, NULL, 0);
     525             :                 _MT_npages = size / _MT_pagesize;
     526             :         }
     527             : #elif defined(BSD) && defined(HW_PHYSMEM)
     528             :         /* NetBSD, OpenBSD, Darwin, 32-bits; FreeBSD 32 & 64-bits */
     529             :         {
     530             : # ifdef __FreeBSD__
     531             :                 unsigned long size = 0; /* type long required by sysctl() (?) */
     532             : # else
     533             :                 int size = 0;
     534             : # endif
     535             :                 size_t len = sizeof(size);
     536             :                 int mib[2];
     537             : 
     538             :                 /* Everyone should have permission to make this call,
     539             :                  * if we get a failure something is really wrong. */
     540             :                 mib[0] = CTL_HW;
     541             :                 mib[1] = HW_PHYSMEM;
     542             :                 sysctl(mib, 2, &size, &len, NULL, 0);
     543             :                 _MT_npages = size / _MT_pagesize;
     544             :         }
     545             : #elif defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
     546         335 :         _MT_npages = (size_t)sysconf(_SC_PHYS_PAGES);
     547             : # if SIZEOF_SIZE_T == SIZEOF_INT
     548             :         /* Bug #2935: the value returned here can be more than what can be
     549             :          * addressed on Solaris, so cap the value */
     550             :         if (UINT_MAX / _MT_pagesize < _MT_npages)
     551             :                 _MT_npages = UINT_MAX / _MT_pagesize;
     552             : # endif
     553             : #else
     554             : # error "don't know how to get the amount of physical memory for your OS"
     555             : #endif
     556             : 
     557             : #ifdef __linux__
     558             :         /* limit values to whatever cgroups gives us */
     559         335 :         FILE *fc;
     560         335 :         char buf[1024];
     561         335 :         char cgr1[1024] = "/sys/fs/cgroup/memory";
     562         335 :         char cgr2[1024] = "/sys/fs/cgroup";
     563         335 :         fc = fopen("/proc/self/mountinfo", "r");
     564         335 :         if (fc != NULL) {
     565       11055 :                 while (fgets(buf, (int) sizeof(buf), fc) != NULL) {
     566       10720 :                         char *p, *cgr;
     567       10720 :                         if ((p = strstr(buf, " - cgroup ")) != NULL &&
     568           0 :                             strstr(p, "memory") != NULL)
     569             :                                 cgr = cgr1;
     570       10720 :                         else if (strstr(buf, " - cgroup2 ") != NULL)
     571             :                                 cgr = cgr2;
     572             :                         else
     573       10385 :                                 continue;
     574             :                         /* buf points at mount ID */
     575         335 :                         p = strchr(buf, ' ');
     576         335 :                         if (p == NULL)
     577             :                                 break;
     578         335 :                         p++;
     579             :                         /* p points at parent ID */
     580         335 :                         p = strchr(p, ' ');
     581         335 :                         if (p == NULL)
     582             :                                 break;
     583         335 :                         p++;
     584             :                         /* p points at major:minor */
     585         335 :                         p = strchr(p, ' ');
     586         335 :                         if (p == NULL)
     587             :                                 break;
     588         335 :                         p++;
     589             :                         /* p points at root */
     590         335 :                         p = strchr(p, ' ');
     591         335 :                         if (p == NULL)
     592             :                                 break;
     593         335 :                         p++;
     594             :                         /* p points at mount point */
     595         335 :                         char *dir = p;
     596         335 :                         p = strchr(p, ' ');
     597         335 :                         if (p == NULL)
     598             :                                 break;
     599         335 :                         *p = 0;
     600         335 :                         strcpy_len(cgr, dir, 1024);
     601             :                 }
     602         335 :                 fclose(fc);
     603             :         }
     604         335 :         fc = fopen("/proc/self/cgroup", "r");
     605         335 :         if (fc != NULL) {
     606             :                 /* each line is of the form:
     607             :                  * hierarchy-ID:controller-list:cgroup-path
     608             :                  *
     609             :                  * For cgroup v1, the hierarchy-ID refers to the
     610             :                  * second column in /proc/cgroups (which we ignore)
     611             :                  * and the controller-list is a comma-separated list
     612             :                  * of the controllers bound to the hierarchy.  We look
     613             :                  * for the "memory" controller and use its
     614             :                  * cgroup-path.  We ignore the other lines.
     615             :                  *
     616             :                  * For cgroup v2, the hierarchy-ID is 0 and the
     617             :                  * controller-list is empty.  We just use the
     618             :                  * cgroup-path.
     619             :                  *
     620             :                  * We use the first line that we can match (either v1
     621             :                  * or v2) and for which we can open any of the files
     622             :                  * that we are looking for.
     623             :                  */
     624         335 :                 while (fgets(buf, (int) sizeof(buf), fc) != NULL) {
     625         335 :                         char pth[1024];
     626         335 :                         char *p, *q;
     627         335 :                         bool success = false; /* true if we can open any file */
     628         335 :                         FILE *f;
     629         335 :                         uint64_t mem;
     630             : 
     631         335 :                         p = strchr(buf, '\n');
     632         335 :                         if (p == NULL)
     633             :                                 break;
     634         335 :                         *p = 0;
     635         335 :                         if (strncmp(buf, "0::", 3) == 0) {
     636             :                                 /* cgroup v2 entry */
     637         335 :                                 p = stpcpy(pth, cgr2);
     638         335 :                                 q = stpcpy(stpcpy(p, buf + 3), "/");
     639             :                                 /* hard limit */
     640         335 :                                 strcpy(q, "memory.max");
     641         335 :                                 f = fopen(pth, "r");
     642         335 :                                 while (f == NULL && q > p) {
     643             :                                         /* go up the hierarchy until we
     644             :                                          * find the file or the
     645             :                                          * hierarchy runs out */
     646           0 :                                         *--q = 0; /* zap the slash */
     647           0 :                                         q = strrchr(p, '/');
     648           0 :                                         if (q == NULL || q == p) {
     649             :                                                 /* position after the slash */
     650           0 :                                                 q = p + 1;
     651           0 :                                                 break;
     652             :                                         }
     653           0 :                                         strcpy(++q, "memory.max");
     654           0 :                                         f = fopen(pth, "r");
     655             :                                 }
     656         335 :                                 if (f != NULL) {
     657         335 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     658           0 :                                             && mem > 0
     659           0 :                                             && mem < (uint64_t) _MT_pagesize * _MT_npages) {
     660           0 :                                                 _MT_npages = (size_t) (mem / _MT_pagesize);
     661             :                                         }
     662         335 :                                         success = true;
     663             :                                         /* assume "max" if not a number */
     664         335 :                                         fclose(f);
     665             :                                 }
     666             :                                 /* soft high limit */
     667         335 :                                 strcpy(q, "memory.high");
     668         335 :                                 f = fopen(pth, "r");
     669         335 :                                 if (f != NULL) {
     670         335 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     671           0 :                                             && mem > 0
     672           0 :                                             && mem < (uint64_t) _MT_pagesize * _MT_npages) {
     673           0 :                                                 _MT_npages = (size_t) (mem / _MT_pagesize);
     674             :                                         }
     675         335 :                                         success = true;
     676             :                                         /* assume "max" if not a number */
     677         335 :                                         fclose(f);
     678             :                                 }
     679             :                                 /* limit of swap usage, hard limit
     680             :                                  * we use this, together with
     681             :                                  * memory.high, as maximum virtual
     682             :                                  * memory size */
     683         335 :                                 strcpy(q, "memory.swap.max");
     684         335 :                                 f = fopen(pth, "r");
     685         335 :                                 if (f != NULL) {
     686         335 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     687           0 :                                             && mem > 0
     688           0 :                                             && (mem += _MT_npages * _MT_pagesize) < (uint64_t) GDK_vm_maxsize) {
     689           0 :                                                 GDK_vm_maxsize = (size_t) mem;
     690             :                                         }
     691         335 :                                         success = true;
     692         335 :                                         fclose(f);
     693             :                                 }
     694             : #if 0 /* not sure about using this one */
     695             :                                 /* limit of swap usage, soft limit */
     696             :                                 strcpy(q, "memory.swap.high");
     697             :                                 f = fopen(pth, "r");
     698             :                                 if (f != NULL) {
     699             :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     700             :                                             && mem > 0
     701             :                                             && (mem += _MT_npages * _MT_pagesize) < (uint64_t) GDK_vm_maxsize) {
     702             :                                                 GDK_vm_maxsize = (size_t) mem;
     703             :                                         }
     704             :                                         success = true;
     705             :                                         fclose(f);
     706             :                                 }
     707             : #endif
     708             :                         } else {
     709             :                                 /* cgroup v1 entry */
     710           0 :                                 p = strchr(buf, ':');
     711           0 :                                 if (p == NULL)
     712             :                                         break;
     713           0 :                                 q = p + 1;
     714           0 :                                 p = strchr(q, ':');
     715           0 :                                 if (p == NULL)
     716             :                                         break;
     717           0 :                                 *p++ = 0;
     718           0 :                                 if (strstr(q, "memory") == NULL)
     719           0 :                                         continue;
     720             :                                 /* limit of memory usage */
     721           0 :                                 strconcat_len(pth, sizeof(pth),
     722             :                                               cgr1, p,
     723             :                                               "/memory.limit_in_bytes",
     724             :                                               NULL);
     725           0 :                                 f = fopen(pth, "r");
     726           0 :                                 if (f == NULL) {
     727           0 :                                         strconcat_len(pth, sizeof(pth),
     728             :                                                       cgr1,
     729             :                                                       "/memory.limit_in_bytes",
     730             :                                                       NULL);
     731           0 :                                         f = fopen(pth, "r");
     732             :                                 }
     733           0 :                                 if (f != NULL) {
     734           0 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     735           0 :                                             && mem > 0
     736           0 :                                             && mem < (uint64_t) _MT_pagesize * _MT_npages) {
     737           0 :                                                 _MT_npages = (size_t) (mem / _MT_pagesize);
     738             :                                         }
     739           0 :                                         success = true;
     740           0 :                                         fclose(f);
     741             :                                 }
     742             :                                 /* soft limit of memory usage */
     743           0 :                                 strconcat_len(pth, sizeof(pth),
     744             :                                               cgr1, p,
     745             :                                               "/memory.soft_limit_in_bytes",
     746             :                                               NULL);
     747           0 :                                 f = fopen(pth, "r");
     748           0 :                                 if (f == NULL) {
     749           0 :                                         strconcat_len(pth, sizeof(pth),
     750             :                                                       cgr1,
     751             :                                                       "/memory.soft_limit_in_bytes",
     752             :                                                       NULL);
     753           0 :                                         f = fopen(pth, "r");
     754             :                                 }
     755           0 :                                 if (f != NULL) {
     756           0 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     757           0 :                                             && mem > 0
     758           0 :                                             && mem < (uint64_t) _MT_pagesize * _MT_npages) {
     759           0 :                                                 _MT_npages = (size_t) (mem / _MT_pagesize);
     760             :                                         }
     761           0 :                                         success = true;
     762           0 :                                         fclose(f);
     763             :                                 }
     764             :                                 /* limit of memory+swap usage
     765             :                                  * we use this as maximum virtual memory size */
     766           0 :                                 strconcat_len(pth, sizeof(pth),
     767             :                                               cgr1, p,
     768             :                                               "/memory.memsw.limit_in_bytes",
     769             :                                               NULL);
     770           0 :                                 f = fopen(pth, "r");
     771           0 :                                 if (f == NULL) {
     772           0 :                                         strconcat_len(pth, sizeof(pth),
     773             :                                                       cgr1,
     774             :                                                       "/memory.memsw.limit_in_bytes",
     775             :                                                       NULL);
     776           0 :                                         f = fopen(pth, "r");
     777             :                                 }
     778           0 :                                 if (f != NULL) {
     779           0 :                                         if (fscanf(f, "%" SCNu64, &mem) == 1
     780           0 :                                             && mem > 0
     781           0 :                                             && mem < (uint64_t) GDK_vm_maxsize) {
     782           0 :                                                 GDK_vm_maxsize = (size_t) mem;
     783             :                                         }
     784           0 :                                         success = true;
     785           0 :                                         fclose(f);
     786             :                                 }
     787             :                         }
     788         335 :                         if (success)
     789             :                                 break;
     790             :                 }
     791         335 :                 fclose(fc);
     792             :         }
     793             : #endif
     794             : 
     795             : #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(RLIMIT_AS)
     796         335 :         struct rlimit l;
     797             :         /* address space (virtual memory) limit */
     798         335 :         if (getrlimit(RLIMIT_AS, &l) == 0
     799         335 :             && l.rlim_cur != (rlim_t)RLIM_INFINITY
     800           0 :             && (size_t)l.rlim_cur < GDK_vm_maxsize) {
     801           0 :                 GDK_vm_maxsize = l.rlim_cur;
     802             :         }
     803             : #endif
     804         335 : }
     805             : 
     806             : /*
     807             :  * @+ Session Initialization
     808             :  * The interface code to the operating system is highly dependent on
     809             :  * the processing environment. It can be filtered away with
     810             :  * compile-time flags.  Suicide is necessary due to some system
     811             :  * implementation errors.
     812             :  *
     813             :  * The kernel requires file descriptors for I/O with the user.  They
     814             :  * are thread specific and should be obtained by a function.
     815             :  *
     816             :  * The arguments relevant for the kernel are extracted from the list.
     817             :  * Their value is turned into a blanc space.
     818             :  */
     819             : 
     820             : #define CATNAP          50      /* time to sleep in ms for catnaps */
     821             : 
     822             : static int THRinit(void);
     823             : static gdk_return GDKlockHome(int farmid);
     824             : 
     825             : void
     826         341 : GDKsetdebug(unsigned debug)
     827             : {
     828         341 :         ATOMIC_SET(&GDKdebug, debug);
     829         341 :         if (debug & ACCELMASK)
     830           0 :                 GDKtracer_set_component_level("accelerator", "debug");
     831             :         else
     832         341 :                 GDKtracer_reset_component_level("accelerator");
     833         341 :         if (debug & ALGOMASK)
     834           0 :                 GDKtracer_set_component_level("algo", "debug");
     835             :         else
     836         341 :                 GDKtracer_reset_component_level("algo");
     837         341 :         if (debug & ALLOCMASK)
     838           0 :                 GDKtracer_set_component_level("alloc", "debug");
     839             :         else
     840         341 :                 GDKtracer_reset_component_level("alloc");
     841         341 :         if (debug & BATMASK)
     842           0 :                 GDKtracer_set_component_level("bat", "debug");
     843             :         else
     844         341 :                 GDKtracer_reset_component_level("bat");
     845         341 :         if (debug & CHECKMASK)
     846         329 :                 GDKtracer_set_component_level("check", "debug");
     847             :         else
     848          12 :                 GDKtracer_reset_component_level("check");
     849         341 :         if (debug & DELTAMASK)
     850           0 :                 GDKtracer_set_component_level("delta", "debug");
     851             :         else
     852         341 :                 GDKtracer_reset_component_level("delta");
     853         341 :         if (debug & HEAPMASK)
     854           0 :                 GDKtracer_set_component_level("heap", "debug");
     855             :         else
     856         341 :                 GDKtracer_reset_component_level("heap");
     857         341 :         if (debug & IOMASK)
     858           0 :                 GDKtracer_set_component_level("io", "debug");
     859             :         else
     860         341 :                 GDKtracer_reset_component_level("io");
     861         341 :         if (debug & PARMASK)
     862           0 :                 GDKtracer_set_component_level("par", "debug");
     863             :         else
     864         341 :                 GDKtracer_reset_component_level("par");
     865         341 :         if (debug & PERFMASK)
     866           0 :                 GDKtracer_set_component_level("perf", "debug");
     867             :         else
     868         341 :                 GDKtracer_reset_component_level("perf");
     869         341 :         if (debug & TEMMASK)
     870           0 :                 GDKtracer_set_component_level("tem", "debug");
     871             :         else
     872         341 :                 GDKtracer_reset_component_level("tem");
     873         341 :         if (debug & THRDMASK)
     874           0 :                 GDKtracer_set_component_level("thrd", "debug");
     875             :         else
     876         341 :                 GDKtracer_reset_component_level("thrd");
     877         341 : }
     878             : 
     879             : unsigned
     880          19 : GDKgetdebug(void)
     881             : {
     882          19 :         ATOMIC_BASE_TYPE debug = ATOMIC_GET(&GDKdebug);
     883          19 :         const char *lvl;
     884          19 :         lvl = GDKtracer_get_component_level("accelerator");
     885          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     886           0 :                 debug |= ACCELMASK;
     887          19 :         lvl = GDKtracer_get_component_level("algo");
     888          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     889           0 :                 debug |= ALGOMASK;
     890          19 :         lvl = GDKtracer_get_component_level("alloc");
     891          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     892           0 :                 debug |= ALLOCMASK;
     893          19 :         lvl = GDKtracer_get_component_level("bat");
     894          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     895           0 :                 debug |= BATMASK;
     896          19 :         lvl = GDKtracer_get_component_level("check");
     897          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     898           0 :                 debug |= CHECKMASK;
     899          19 :         lvl = GDKtracer_get_component_level("delta");
     900          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     901           0 :                 debug |= DELTAMASK;
     902          19 :         lvl = GDKtracer_get_component_level("heap");
     903          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     904           0 :                 debug |= HEAPMASK;
     905          19 :         lvl = GDKtracer_get_component_level("io");
     906          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     907           0 :                 debug |= IOMASK;
     908          19 :         lvl = GDKtracer_get_component_level("par");
     909          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     910           0 :                 debug |= PARMASK;
     911          19 :         lvl = GDKtracer_get_component_level("perf");
     912          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     913           0 :                 debug |= PERFMASK;
     914          19 :         lvl = GDKtracer_get_component_level("tem");
     915          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     916           0 :                 debug |= TEMMASK;
     917          19 :         lvl = GDKtracer_get_component_level("thrd");
     918          19 :         if (lvl && strcmp(lvl, "debug") == 0)
     919           0 :                 debug |= THRDMASK;
     920          19 :         return (unsigned) debug;
     921             : }
     922             : 
     923             : static bool Mbedded = true;
     924             : bool
     925    24403470 : GDKembedded(void)
     926             : {
     927    24403470 :         return Mbedded;
     928             : }
     929             : 
     930             : static MT_Id mainpid;
     931             : 
     932             : gdk_return
     933         334 : GDKinit(opt *set, int setlen, bool embedded, const char *caller_revision)
     934             : {
     935         334 :         static bool first = true;
     936         334 :         const char *dbpath;
     937         334 :         const char *dbtrace;
     938         334 :         const char *p;
     939         334 :         opt *n;
     940         334 :         int i, nlen = 0;
     941         334 :         char buf[16];
     942             : 
     943         334 :         if (caller_revision) {
     944         323 :                 p = mercurial_revision();
     945         323 :                 if (p && strcmp(p, caller_revision) != 0) {
     946           0 :                         GDKerror("incompatible versions: caller is %s, GDK is %s\n", caller_revision, p);
     947           0 :                         return GDK_FAIL;
     948             :                 }
     949             :         }
     950             : 
     951         334 :         ATOMIC_SET(&GDKstopped, 0);
     952             : 
     953         334 :         if (BBPchkfarms() != GDK_SUCCEED)
     954             :                 return GDK_FAIL;
     955             : 
     956         334 :         if (GDKinmemory(0)) {
     957             :                 dbpath = dbtrace = NULL;
     958             :         } else {
     959         333 :                 dbpath = mo_find_option(set, setlen, "gdk_dbpath");
     960         333 :                 dbtrace = mo_find_option(set, setlen, "gdk_dbtrace");
     961             :         }
     962         334 :         Mbedded = embedded;
     963             :         /* some sanity checks (should also find if symbols are not defined) */
     964         334 :         static_assert(sizeof(int) == sizeof(int32_t),
     965             :                       "int is not equal in size to int32_t");
     966         334 :         static_assert(sizeof(char) == SIZEOF_CHAR,
     967             :                       "error in configure: bad value for SIZEOF_CHAR");
     968         334 :         static_assert(sizeof(short) == SIZEOF_SHORT,
     969             :                       "error in configure: bad value for SIZEOF_SHORT");
     970         334 :         static_assert(sizeof(int) == SIZEOF_INT,
     971             :                       "error in configure: bad value for SIZEOF_INT");
     972         334 :         static_assert(sizeof(long) == SIZEOF_LONG,
     973             :                       "error in configure: bad value for SIZEOF_LONG");
     974         334 :         static_assert(sizeof(lng) == SIZEOF_LNG,
     975             :                       "error in configure: bad value for SIZEOF_LNG");
     976             : #ifdef HAVE_HGE
     977         334 :         static_assert(sizeof(hge) == SIZEOF_HGE,
     978             :                       "error in configure: bad value for SIZEOF_HGE");
     979             : #endif
     980         334 :         static_assert(sizeof(dbl) == SIZEOF_DOUBLE,
     981             :                       "error in configure: bad value for SIZEOF_DOUBLE");
     982         334 :         static_assert(sizeof(oid) == SIZEOF_OID,
     983             :                       "error in configure: bad value for SIZEOF_OID");
     984         334 :         static_assert(sizeof(void *) == SIZEOF_VOID_P,
     985             :                       "error in configure: bad value for SIZEOF_VOID_P");
     986         334 :         static_assert(sizeof(size_t) == SIZEOF_SIZE_T,
     987             :                       "error in configure: bad value for SIZEOF_SIZE_T");
     988         334 :         static_assert(SIZEOF_OID == SIZEOF_INT || SIZEOF_OID == SIZEOF_LNG,
     989             :                       "SIZEOF_OID should be equal to SIZEOF_INT or SIZEOF_LNG");
     990         334 :         static_assert(sizeof(uuid) == 16,
     991             :                       "sizeof(uuid) should be equal to 16");
     992             : 
     993         334 :         if (first) {
     994             :                 /* some things are really only initialized once */
     995         324 :                 if (!MT_thread_init()) {
     996           0 :                         TRC_CRITICAL(GDK, "MT_thread_init failed\n");
     997           0 :                         return GDK_FAIL;
     998             :                 }
     999             : 
    1000     2654532 :                 for (i = 0; i <= BBP_BATMASK; i++) {
    1001     2654208 :                         char name[MT_NAME_LEN];
    1002     2654208 :                         snprintf(name, sizeof(name), "GDKswapLock%d", i);
    1003     2654208 :                         MT_lock_init(&GDKbatLock[i].swap, name);
    1004             :                 }
    1005         324 :                 if (mnstr_init() < 0) {
    1006           0 :                         TRC_CRITICAL(GDK, "mnstr_init failed\n");
    1007           0 :                         return GDK_FAIL;
    1008             :                 }
    1009             :         } else {
    1010             :                 /* BBP was locked by BBPexit() */
    1011             :                 //BBPunlock();
    1012         334 :         }
    1013         334 :         mainpid = MT_getpid();
    1014             : 
    1015         334 :         GDKtracer_init(dbpath, dbtrace);
    1016         334 :         errno = 0;
    1017         334 :         if (!GDKinmemory(0) && !GDKenvironment(dbpath))
    1018             :                 return GDK_FAIL;
    1019             : 
    1020         334 :         MT_init_posix();
    1021         334 :         if (THRinit() < 0)
    1022             :                 return GDK_FAIL;
    1023             : #ifndef NATIVE_WIN32
    1024         334 :         BATSIGinit();
    1025             : #endif
    1026         334 :         MT_init();
    1027             : 
    1028             :         /* now try to lock the database: go through all farms, and if
    1029             :          * we see a new directory, lock it */
    1030       10958 :         for (int farmid = 0; farmid < MAXFARMS; farmid++) {
    1031       10626 :                 if (BBPfarms[farmid].dirname != NULL) {
    1032        1316 :                         bool skip = false;
    1033        1316 :                         for (int j = 0; j < farmid; j++) {
    1034         817 :                                 if (BBPfarms[j].dirname != NULL &&
    1035         817 :                                     strcmp(BBPfarms[farmid].dirname, BBPfarms[j].dirname) == 0) {
    1036             :                                         skip = true;
    1037             :                                         break;
    1038             :                                 }
    1039             :                         }
    1040         984 :                         if (!skip && GDKlockHome(farmid) != GDK_SUCCEED)
    1041             :                                 return GDK_FAIL;
    1042             :                 }
    1043             :         }
    1044             : 
    1045             :         /* Mserver by default takes 80% of all memory as a default */
    1046             : #if SIZEOF_SIZE_T == 4
    1047             :         if ((double) MT_npages() * (double) MT_pagesize() * 0.815 >= (double) GDK_VM_MAXSIZE)
    1048             :                 GDK_mem_maxsize = GDK_VM_MAXSIZE;
    1049             :         else
    1050             : #endif
    1051         332 :         GDK_mem_maxsize = (size_t) ((double) MT_npages() * (double) MT_pagesize() * 0.815);
    1052         332 :         const char *allow = mo_find_option(set, setlen, "allow_hge_upgrade");
    1053         656 :         if (BBPinit(allow && strcmp(allow, "yes") == 0) != GDK_SUCCEED)
    1054             :                 return GDK_FAIL;
    1055         332 :         first = false;
    1056             : 
    1057         332 :         if (GDK_mem_maxsize / 16 < GDK_mmap_minsize_transient) {
    1058         322 :                 GDK_mmap_minsize_transient = MAX(MMAP_PAGESIZE, GDK_mem_maxsize / 16);
    1059         322 :                 if (GDK_mmap_minsize_persistent > GDK_mmap_minsize_transient)
    1060           0 :                         GDK_mmap_minsize_persistent = GDK_mmap_minsize_transient;
    1061             :         }
    1062             : 
    1063         332 :         n = (opt *) malloc(setlen * sizeof(opt));
    1064         332 :         if (n == NULL) {
    1065           0 :                 GDKsyserror("malloc failed\n");
    1066           0 :                 return GDK_FAIL;
    1067             :         }
    1068             : 
    1069        3989 :         for (i = 0; i < setlen; i++) {
    1070       16354 :                 bool done = false;
    1071             : 
    1072       16354 :                 for (int j = 0; j < nlen; j++) {
    1073       13351 :                         if (strcmp(n[j].name, set[i].name) == 0) {
    1074         654 :                                 if (n[j].kind < set[i].kind) {
    1075         654 :                                         n[j] = set[i];
    1076             :                                 }
    1077             :                                 done = true;
    1078             :                                 break;
    1079             :                         }
    1080             :                 }
    1081        3003 :                 if (!done) {
    1082        3003 :                         n[nlen] = set[i];
    1083        3003 :                         nlen++;
    1084             :                 }
    1085             :         }
    1086             :         /* check some options before creating our first BAT */
    1087        3335 :         for (i = 0; i < nlen; i++) {
    1088        3003 :                 if (strcmp("gdk_mem_maxsize", n[i].name) == 0) {
    1089           0 :                         GDK_mem_maxsize = (size_t) strtoll(n[i].value, NULL, 10);
    1090           0 :                         GDK_mem_maxsize = MAX(1 << 26, GDK_mem_maxsize);
    1091           0 :                         if (GDK_mem_maxsize / 16 < GDK_mmap_minsize_transient)
    1092           0 :                                 GDK_mmap_minsize_transient = MAX(MMAP_PAGESIZE, GDK_mem_maxsize / 16);
    1093           0 :                         if (GDK_mmap_minsize_persistent > GDK_mmap_minsize_transient)
    1094           0 :                                 GDK_mmap_minsize_persistent = GDK_mmap_minsize_transient;
    1095        3003 :                 } else if (strcmp("gdk_vm_maxsize", n[i].name) == 0) {
    1096         172 :                         GDK_vm_maxsize = (size_t) strtoll(n[i].value, NULL, 10);
    1097         172 :                         GDK_vm_maxsize = MAX(1 << 30, GDK_vm_maxsize);
    1098        2831 :                 } else if (strcmp("gdk_mmap_minsize_persistent", n[i].name) == 0) {
    1099           0 :                         GDK_mmap_minsize_persistent = (size_t) strtoll(n[i].value, NULL, 10);
    1100        2831 :                 } else if (strcmp("gdk_mmap_minsize_transient", n[i].name) == 0) {
    1101           0 :                         GDK_mmap_minsize_transient = (size_t) strtoll(n[i].value, NULL, 10);
    1102        2831 :                 } else if (strcmp("gdk_mmap_pagesize", n[i].name) == 0) {
    1103           0 :                         GDK_mmap_pagesize = (size_t) strtoll(n[i].value, NULL, 10);
    1104           0 :                         if (GDK_mmap_pagesize < 1 << 12 ||
    1105           0 :                             GDK_mmap_pagesize > 1 << 20 ||
    1106             :                             /* x & (x - 1): turn off rightmost 1 bit;
    1107             :                              * i.e. if result is zero, x is power of
    1108             :                              * two */
    1109           0 :                             (GDK_mmap_pagesize & (GDK_mmap_pagesize - 1)) != 0) {
    1110           0 :                                 free(n);
    1111           0 :                                 TRC_CRITICAL(GDK, "gdk_mmap_pagesize must be power of 2 between 2**12 and 2**20\n");
    1112           0 :                                 return GDK_FAIL;
    1113             :                         }
    1114             :                 }
    1115             :         }
    1116             : 
    1117         332 :         GDKkey = COLnew(0, TYPE_str, 100, TRANSIENT);
    1118         332 :         GDKval = COLnew(0, TYPE_str, 100, TRANSIENT);
    1119         332 :         if (GDKkey == NULL || GDKval == NULL) {
    1120           0 :                 free(n);
    1121           0 :                 TRC_CRITICAL(GDK, "Could not create environment BATs");
    1122           0 :                 return GDK_FAIL;
    1123             :         }
    1124         664 :         if (BBPrename(GDKkey, "environment_key") != 0 ||
    1125         332 :             BBPrename(GDKval, "environment_val") != 0) {
    1126           0 :                 free(n);
    1127           0 :                 TRC_CRITICAL(GDK, "BBPrename of environment BATs failed");
    1128           0 :                 return GDK_FAIL;
    1129             :         }
    1130         332 :         BBP_pid(GDKkey->batCacheid) = 0;
    1131         332 :         BBP_pid(GDKval->batCacheid) = 0;
    1132             : 
    1133             :         /* store options into environment BATs */
    1134        3335 :         for (i = 0; i < nlen; i++)
    1135        3003 :                 if (GDKsetenv(n[i].name, n[i].value) != GDK_SUCCEED) {
    1136           0 :                         TRC_CRITICAL(GDK, "GDKsetenv %s failed", n[i].name);
    1137           0 :                         free(n);
    1138           0 :                         return GDK_FAIL;
    1139             :                 }
    1140         332 :         free(n);
    1141             : 
    1142         332 :         GDKnr_threads = GDKgetenv_int("gdk_nr_threads", 0);
    1143         332 :         if (GDKnr_threads == 0) {
    1144         331 :                 GDKnr_threads = MT_check_nr_cores();
    1145         331 :                 snprintf(buf, sizeof(buf), "%d", GDKnr_threads);
    1146         331 :                 if (GDKsetenv("gdk_nr_threads", buf) != GDK_SUCCEED) {
    1147           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_nr_threads failed");
    1148           0 :                         return GDK_FAIL;
    1149             :                 }
    1150             :         }
    1151         332 :         if (GDKnr_threads > THREADS)
    1152           0 :                 GDKnr_threads = THREADS;
    1153             : 
    1154         332 :         if (!GDKinmemory(0)) {
    1155         331 :                 if ((p = GDKgetenv("gdk_dbpath")) != NULL &&
    1156         331 :                         (p = strrchr(p, DIR_SEP)) != NULL) {
    1157         331 :                         if (GDKsetenv("gdk_dbname", p + 1) != GDK_SUCCEED) {
    1158           0 :                                 TRC_CRITICAL(GDK, "GDKsetenv gdk_dbname failed");
    1159           0 :                                 return GDK_FAIL;
    1160             :                         }
    1161             : #if DIR_SEP != '/'              /* on Windows look for different separator */
    1162             :                 } else if ((p = GDKgetenv("gdk_dbpath")) != NULL &&
    1163             :                                    (p = strrchr(p, '/')) != NULL) {
    1164             :                         if (GDKsetenv("gdk_dbname", p + 1) != GDK_SUCCEED) {
    1165             :                                 TRC_CRITICAL(GDK, "GDKsetenv gdk_dbname failed");
    1166             :                                 return GDK_FAIL;
    1167             :                         }
    1168             : #endif
    1169             :                 }
    1170           1 :         } else if (GDKgetenv("gdk_dbname") == NULL) {
    1171           1 :                 if (GDKsetenv("gdk_dbname", "in-memory") != GDK_SUCCEED) {
    1172           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_dbname failed");
    1173           0 :                         return GDK_FAIL;
    1174             :                 }
    1175             :         }
    1176         332 :         if (GDKgetenv("gdk_vm_maxsize") == NULL) {
    1177         160 :                 snprintf(buf, sizeof(buf), "%zu", GDK_vm_maxsize);
    1178         160 :                 if (GDKsetenv("gdk_vm_maxsize", buf) != GDK_SUCCEED) {
    1179           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_vm_maxsize failed");
    1180           0 :                         return GDK_FAIL;
    1181             :                 }
    1182             :         }
    1183         332 :         if (GDKgetenv("gdk_mem_maxsize") == NULL) {
    1184         332 :                 snprintf(buf, sizeof(buf), "%zu", GDK_mem_maxsize);
    1185         332 :                 if (GDKsetenv("gdk_mem_maxsize", buf) != GDK_SUCCEED) {
    1186           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_mem_maxsize failed");
    1187           0 :                         return GDK_FAIL;
    1188             :                 }
    1189             :         }
    1190         332 :         if (GDKgetenv("gdk_mmap_minsize_persistent") == NULL) {
    1191         332 :                 snprintf(buf, sizeof(buf), "%zu", GDK_mmap_minsize_persistent);
    1192         332 :                 if (GDKsetenv("gdk_mmap_minsize_persistent", buf) != GDK_SUCCEED) {
    1193           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_mmap_minsize_persistent failed");
    1194           0 :                         return GDK_FAIL;
    1195             :                 }
    1196             :         }
    1197         332 :         if (GDKgetenv("gdk_mmap_minsize_transient") == NULL) {
    1198         332 :                 snprintf(buf, sizeof(buf), "%zu", GDK_mmap_minsize_transient);
    1199         332 :                 if (GDKsetenv("gdk_mmap_minsize_transient", buf) != GDK_SUCCEED) {
    1200           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_mmap_minsize_transient failed");
    1201           0 :                         return GDK_FAIL;
    1202             :                 }
    1203             :         }
    1204         332 :         if (GDKgetenv("gdk_mmap_pagesize") == NULL) {
    1205         332 :                 snprintf(buf, sizeof(buf), "%zu", GDK_mmap_pagesize);
    1206         332 :                 if (GDKsetenv("gdk_mmap_pagesize", buf) != GDK_SUCCEED) {
    1207           0 :                         TRC_CRITICAL(GDK, "GDKsetenv gdk_mmap_pagesize failed");
    1208           0 :                         return GDK_FAIL;
    1209             :                 }
    1210             :         }
    1211         332 :         if (GDKgetenv("monet_pid") == NULL) {
    1212         332 :                 snprintf(buf, sizeof(buf), "%d", (int) getpid());
    1213         332 :                 if (GDKsetenv("monet_pid", buf) != GDK_SUCCEED) {
    1214           0 :                         TRC_CRITICAL(GDK, "GDKsetenv monet_pid failed");
    1215           0 :                         return GDK_FAIL;
    1216             :                 }
    1217             :         }
    1218         332 :         if (GDKsetenv("revision", mercurial_revision()) != GDK_SUCCEED) {
    1219           0 :                 TRC_CRITICAL(GDK, "GDKsetenv revision failed");
    1220           0 :                 return GDK_FAIL;
    1221             :         }
    1222         332 :         gdk_unique_estimate_keep_fraction = 0;
    1223         332 :         if ((p = GDKgetenv("gdk_unique_estimate_keep_fraction")) != NULL)
    1224           0 :                 gdk_unique_estimate_keep_fraction = (BUN) strtoll(p, NULL, 10);
    1225         332 :         if (gdk_unique_estimate_keep_fraction == 0)
    1226         332 :                 gdk_unique_estimate_keep_fraction = GDK_UNIQUE_ESTIMATE_KEEP_FRACTION;
    1227         332 :         hash_destroy_uniques_fraction = 0;
    1228         332 :         if ((p = GDKgetenv("hash_destroy_uniques_fraction")) != NULL)
    1229           0 :                 hash_destroy_uniques_fraction = (BUN) strtoll(p, NULL, 10);
    1230         332 :         if (hash_destroy_uniques_fraction == 0)
    1231         332 :                 hash_destroy_uniques_fraction = HASH_DESTROY_UNIQUES_FRACTION;
    1232         332 :         no_hash_select_fraction = 0;
    1233         332 :         if ((p = GDKgetenv("no_hash_select_fraction")) != NULL)
    1234           0 :                 no_hash_select_fraction = (dbl) strtoll(p, NULL, 10);
    1235         332 :         if (no_hash_select_fraction == 0)
    1236         332 :                 no_hash_select_fraction = NO_HASH_SELECT_FRACTION;
    1237         332 :         hash_destroy_chain_length = 0;
    1238         332 :         if ((p = GDKgetenv("hash_destroy_chain_length")) != NULL)
    1239           0 :                 hash_destroy_chain_length = (BUN) strtoll(p, NULL, 10);
    1240         332 :         if (hash_destroy_chain_length == 0)
    1241         332 :                 hash_destroy_chain_length = HASH_DESTROY_CHAIN_LENGTH;
    1242             : 
    1243             :         return GDK_SUCCEED;
    1244             : }
    1245             : 
    1246             : int GDKnr_threads = 0;
    1247             : static ATOMIC_TYPE GDKnrofthreads = ATOMIC_VAR_INIT(0);
    1248             : 
    1249             : bool
    1250    20349837 : GDKexiting(void)
    1251             : {
    1252    20349837 :         return (bool) (ATOMIC_GET(&GDKstopped) > 0);
    1253             : }
    1254             : 
    1255             : void
    1256         331 : GDKprepareExit(void)
    1257             : {
    1258         331 :         ATOMIC_ADD(&GDKstopped, 1);
    1259             : 
    1260         331 :         if (MT_getpid() == mainpid) {
    1261         330 :                 TRC_DEBUG_IF(THRD)
    1262           0 :                         dump_threads();
    1263         330 :                 join_detached_threads();
    1264             :         }
    1265         331 : }
    1266             : 
    1267             : void
    1268         330 : GDKreset(int status)
    1269             : {
    1270         330 :         assert(GDKexiting());
    1271             : 
    1272         330 :         if (GDKembedded())
    1273             :                 // In the case of a restarted embedded database, GDKstopped has to be reset as well.
    1274          11 :                 ATOMIC_SET(&GDKstopped, 0);
    1275             : 
    1276         330 :         if (GDKkey) {
    1277         330 :                 BBPunfix(GDKkey->batCacheid);
    1278         330 :                 GDKkey = NULL;
    1279             :         }
    1280         330 :         if (GDKval) {
    1281         330 :                 BBPunfix(GDKval->batCacheid);
    1282         330 :                 GDKval = NULL;
    1283             :         }
    1284             : 
    1285         330 :         join_detached_threads();
    1286             : 
    1287         330 :         MT_lock_set(&GDKenvlock);
    1288         330 :         while (orig_value) {
    1289           0 :                 struct orig_value *ov = orig_value;
    1290           0 :                 orig_value = orig_value->next;
    1291           0 :                 GDKfree(ov);
    1292             :         }
    1293         330 :         MT_lock_unset(&GDKenvlock);
    1294             : 
    1295         330 :         if (status == 0) {
    1296             :                 /* they had their chance, now kill them */
    1297         330 :                 bool killed = MT_kill_threads();
    1298             :                 /* all threads ceased running, now we can clean up */
    1299         330 :                 if (!killed) {
    1300             :                         /* we can't clean up after killing threads */
    1301         330 :                         BBPexit();
    1302             :                 }
    1303         330 :                 GDKlog(GET_GDKLOCK(PERSISTENT), GDKLOGOFF);
    1304             : 
    1305       10890 :                 for (int farmid = 0; farmid < MAXFARMS; farmid++) {
    1306       10560 :                         if (BBPfarms[farmid].dirname != NULL) {
    1307        1944 :                                 bool skip = false;
    1308        1944 :                                 for (int j = 0; j < farmid; j++) {
    1309         967 :                                         if (BBPfarms[j].dirname != NULL &&
    1310           0 :                                             strcmp(BBPfarms[farmid].dirname, BBPfarms[j].dirname) == 0) {
    1311             :                                                 skip = true;
    1312             :                                                 break;
    1313             :                                         }
    1314             :                                 }
    1315         977 :                                 if (!skip)
    1316         977 :                                         GDKunlockHome(farmid);
    1317         977 :                                 if (BBPfarms[farmid].dirname) {
    1318         977 :                                         GDKfree((char*)BBPfarms[farmid].dirname);
    1319         977 :                                         BBPfarms[farmid].dirname = NULL;
    1320             :                                 }
    1321             :                         }
    1322             :                 }
    1323             : 
    1324             : #ifdef LOCK_STATS
    1325             :                 TRC_DEBUG_IF(TEM) GDKlockstatistics(1);
    1326             : #endif
    1327         330 :                 ATOMIC_SET(&GDKdebug, 0);
    1328         330 :                 GDK_mmap_minsize_persistent = MMAP_MINSIZE_PERSISTENT;
    1329         330 :                 GDK_mmap_minsize_transient = MMAP_MINSIZE_TRANSIENT;
    1330         330 :                 GDK_mmap_pagesize = MMAP_PAGESIZE;
    1331         330 :                 GDK_mem_maxsize = (size_t) ((double) MT_npages() * (double) MT_pagesize() * 0.815);
    1332         330 :                 GDK_vm_maxsize = GDK_VM_MAXSIZE;
    1333         330 :                 GDKatomcnt = TYPE_blob + 1;
    1334             : 
    1335         330 :                 if (GDK_mem_maxsize / 16 < GDK_mmap_minsize_transient) {
    1336         330 :                         GDK_mmap_minsize_transient = GDK_mem_maxsize / 16;
    1337         330 :                         if (GDK_mmap_minsize_persistent > GDK_mmap_minsize_transient)
    1338           0 :                                 GDK_mmap_minsize_persistent = GDK_mmap_minsize_transient;
    1339             :                 }
    1340             : 
    1341         330 :                 GDKnr_threads = 0;
    1342         330 :                 ATOMIC_SET(&GDKnrofthreads, 0);
    1343         330 :                 close_stream(GDKstdout);
    1344         330 :                 close_stream(GDKstdin);
    1345         330 :                 GDKstdout = NULL;
    1346         330 :                 GDKstdin = NULL;
    1347             : 
    1348         330 :                 gdk_bbp_reset();
    1349             :         }
    1350         330 :         ATOMunknown_clean();
    1351             : 
    1352             :         /* stop GDKtracer */
    1353         330 :         GDKtracer_stop();
    1354         330 : }
    1355             : 
    1356             : /*
    1357             :  * All semaphores used by the application should be mentioned here.
    1358             :  * They are initialized during system initialization.
    1359             :  */
    1360             : 
    1361             : batlock_t GDKbatLock[BBP_BATMASK + 1];
    1362             : 
    1363             : /*
    1364             :  * @+ Concurrency control
    1365             :  * Concurrency control requires actions at several levels of the
    1366             :  * system.  First, it should be ensured that each database is
    1367             :  * controlled by a single server process (group). Subsequent attempts
    1368             :  * should be stopped.  This is regulated through file locking against
    1369             :  * ".gdk_lock".
    1370             :  *
    1371             :  * Before the locks and threads are initiated, we cannot use the
    1372             :  * normal routines yet. So we have a local fatal here instead of
    1373             :  * GDKfatal.
    1374             :  */
    1375             : static gdk_return
    1376         499 : GDKlockHome(int farmid)
    1377             : {
    1378         499 :         int fd;
    1379         499 :         struct stat st;
    1380         499 :         char *gdklockpath;
    1381         499 :         FILE *GDKlockFile;
    1382             : 
    1383         499 :         assert(BBPfarms[farmid].dirname != NULL);
    1384         499 :         assert(BBPfarms[farmid].lock_file == NULL);
    1385             : 
    1386         499 :         if ((gdklockpath = GDKfilepath(farmid, NULL, GDKLOCK, NULL)) == NULL) {
    1387             :                 return GDK_FAIL;
    1388             :         }
    1389             : 
    1390             :         /*
    1391             :          * Obtain the global database lock.
    1392             :          */
    1393         499 :         if (MT_stat(BBPfarms[farmid].dirname, &st) < 0 &&
    1394           0 :             GDKcreatedir(gdklockpath) != GDK_SUCCEED) {
    1395           0 :                 TRC_CRITICAL(GDK, "could not create %s\n",
    1396             :                          BBPfarms[farmid].dirname);
    1397           0 :                 GDKfree(gdklockpath);
    1398           0 :                 return GDK_FAIL;
    1399             :         }
    1400         499 :         if ((fd = MT_lockf(gdklockpath, F_TLOCK)) < 0) {
    1401           2 :                 TRC_CRITICAL(GDK, "Database lock '%s' denied\n",
    1402             :                          gdklockpath);
    1403           2 :                 GDKfree(gdklockpath);
    1404           2 :                 return GDK_FAIL;
    1405             :         }
    1406             : 
    1407             :         /* now we have the lock on the database and are the only
    1408             :          * process allowed in this section */
    1409             : 
    1410         497 :         if ((GDKlockFile = fdopen(fd, "r+")) == NULL) {
    1411           0 :                 GDKsyserror("Could not fdopen %s\n", gdklockpath);
    1412           0 :                 close(fd);
    1413           0 :                 GDKfree(gdklockpath);
    1414           0 :                 return GDK_FAIL;
    1415             :         }
    1416             : 
    1417             :         /*
    1418             :          * Print the new process list in the global lock file.
    1419             :          */
    1420         497 :         if (fseek(GDKlockFile, 0, SEEK_SET) == -1) {
    1421           0 :                 fclose(GDKlockFile);
    1422           0 :                 TRC_CRITICAL(GDK, "Error while setting the file pointer on %s\n", gdklockpath);
    1423           0 :                 GDKfree(gdklockpath);
    1424           0 :                 return GDK_FAIL;
    1425             :         }
    1426         497 :         if (ftruncate(fileno(GDKlockFile), 0) < 0) {
    1427           0 :                 fclose(GDKlockFile);
    1428           0 :                 TRC_CRITICAL(GDK, "Could not truncate %s\n", gdklockpath);
    1429           0 :                 GDKfree(gdklockpath);
    1430           0 :                 return GDK_FAIL;
    1431             :         }
    1432         497 :         if (fflush(GDKlockFile) == EOF) {
    1433           0 :                 fclose(GDKlockFile);
    1434           0 :                 TRC_CRITICAL(GDK, "Could not flush %s\n", gdklockpath);
    1435           0 :                 GDKfree(gdklockpath);
    1436           0 :                 return GDK_FAIL;
    1437             :         }
    1438         497 :         GDKlog(GDKlockFile, GDKLOGON);
    1439         497 :         GDKfree(gdklockpath);
    1440         497 :         BBPfarms[farmid].lock_file = GDKlockFile;
    1441         497 :         return GDK_SUCCEED;
    1442             : }
    1443             : 
    1444             : 
    1445             : static void
    1446         977 : GDKunlockHome(int farmid)
    1447             : {
    1448         977 :         if (BBPfarms[farmid].lock_file) {
    1449         495 :                 char *gdklockpath = GDKfilepath(farmid, NULL, GDKLOCK, NULL);
    1450         495 :                 if (gdklockpath)
    1451         495 :                         MT_lockf(gdklockpath, F_ULOCK);
    1452         495 :                 fclose(BBPfarms[farmid].lock_file);
    1453         495 :                 BBPfarms[farmid].lock_file = NULL;
    1454         495 :                 GDKfree(gdklockpath);
    1455             :         }
    1456         977 : }
    1457             : 
    1458             : /*
    1459             :  * @+ Error handling
    1460             :  * Errors come in three flavors: warnings, non-fatal and fatal errors.
    1461             :  * A fatal error leaves a core dump behind after trying to safe the
    1462             :  * content of the relation.  A non-fatal error returns a message to
    1463             :  * the user and aborts the current transaction.  Fatal errors are also
    1464             :  * recorded on the system log for post-mortem analysis.
    1465             :  * In non-silent mode the errors are immediately sent to output, which
    1466             :  * makes it hard for upper layers to detect if an error was produced
    1467             :  * in the process. To facilitate such testing, a global error count is
    1468             :  * maintained on a thread basis, which can be read out by the function
    1469             :  * GDKerrorCount(); Furthermore, threads may have set their private
    1470             :  * error buffer.
    1471             :  */
    1472             : 
    1473             : #define GDKERRLEN       (1024+512)
    1474             : 
    1475             : void
    1476     8316740 : GDKclrerr(void)
    1477             : {
    1478     8316740 :         char *buf;
    1479             : 
    1480     8316740 :         buf = GDKerrbuf;
    1481     8316170 :         if (buf)
    1482     8266429 :                 *buf = 0;
    1483     8316170 : }
    1484             : 
    1485             : jmp_buf GDKfataljump;
    1486             : str GDKfatalmsg;
    1487             : bit GDKfataljumpenable = 0;
    1488             : 
    1489             : /* coverity[+kill] */
    1490             : void
    1491           0 : GDKfatal(const char *format, ...)
    1492             : {
    1493           0 :         char message[GDKERRLEN];
    1494           0 :         size_t len = strlen(GDKFATAL);
    1495           0 :         va_list ap;
    1496             : 
    1497           0 :         GDKtracer_set_component_level("io", "debug");
    1498             : #ifndef NATIVE_WIN32
    1499           0 :         BATSIGinit();
    1500             : #endif
    1501           0 :         if (!strncmp(format, GDKFATAL, len)) {
    1502             :                 len = 0;
    1503             :         } else {
    1504           0 :                 strcpy(message, GDKFATAL);
    1505             :         }
    1506           0 :         va_start(ap, format);
    1507           0 :         vsnprintf(message + len, sizeof(message) - (len + 2), format, ap);
    1508           0 :         va_end(ap);
    1509             : 
    1510             : #ifndef __COVERITY__
    1511           0 :         if (GDKfataljumpenable) {
    1512             :                 // in embedded mode, we really don't want to kill our host
    1513           0 :                 GDKfatalmsg = GDKstrdup(message);
    1514           0 :                 longjmp(GDKfataljump, 42);
    1515             :         } else
    1516             : #endif
    1517             :         {
    1518           0 :                 fputs(message, stderr);
    1519           0 :                 fputs("\n", stderr);
    1520           0 :                 fflush(stderr);
    1521             : 
    1522             :                 /*
    1523             :                  * Real errors should be saved in the log file for post-crash
    1524             :                  * inspection.
    1525             :                  */
    1526           0 :                 if (GDKexiting()) {
    1527           0 :                         fflush(stdout);
    1528           0 :                         exit(1);
    1529             :                 } else {
    1530           0 :                         GDKlog(GET_GDKLOCK(PERSISTENT), "%s", message);
    1531             : #ifdef COREDUMP
    1532             :                         abort();
    1533             : #else
    1534           0 :                         exit(1);
    1535             : #endif
    1536             :                 }
    1537             :         }
    1538             : }
    1539             : 
    1540             : 
    1541             : lng
    1542   157703528 : GDKusec(void)
    1543             : {
    1544             :         /* Return the time in microseconds since an epoch.  The epoch
    1545             :          * is currently midnight at the start of January 1, 1970, UTC. */
    1546             : #if defined(NATIVE_WIN32)
    1547             :         FILETIME ft;
    1548             :         ULARGE_INTEGER f;
    1549             :         GetSystemTimeAsFileTime(&ft); /* time since Jan 1, 1601 */
    1550             :         f.LowPart = ft.dwLowDateTime;
    1551             :         f.HighPart = ft.dwHighDateTime;
    1552             :         /* there are 369 years, of which 89 are leap years from
    1553             :          * January 1, 1601 to January 1, 1970 which makes 134774 days;
    1554             :          * multiply that with the number of seconds in a day and the
    1555             :          * number of 100ns units in a second; subtract that from the
    1556             :          * value for the current time since January 1, 1601 to get the
    1557             :          * time since the Unix epoch */
    1558             :         f.QuadPart -= LL_CONSTANT(134774) * 24 * 60 * 60 * 10000000;
    1559             :         /* and convert to microseconds */
    1560             :         return (lng) (f.QuadPart / 10);
    1561             : #elif defined(HAVE_CLOCK_GETTIME)
    1562   157703528 :         struct timespec ts;
    1563   157703528 :         (void) clock_gettime(CLOCK_REALTIME, &ts);
    1564   157703490 :         return (lng) (ts.tv_sec * LL_CONSTANT(1000000) + ts.tv_nsec / 1000);
    1565             : #elif defined(HAVE_GETTIMEOFDAY)
    1566             :         struct timeval tv;
    1567             :         gettimeofday(&tv, NULL);
    1568             :         return (lng) (tv.tv_sec * LL_CONSTANT(1000000) + tv.tv_usec);
    1569             : #elif defined(HAVE_FTIME)
    1570             :         struct timeb tb;
    1571             :         ftime(&tb);
    1572             :         return (lng) (tb.time * LL_CONSTANT(1000000) + tb.millitm * LL_CONSTANT(1000));
    1573             : #else
    1574             :         /* last resort */
    1575             :         return (lng) (time(NULL) * LL_CONSTANT(1000000));
    1576             : #endif
    1577             : }
    1578             : 
    1579             : 
    1580             : int
    1581           0 : GDKms(void)
    1582             : {
    1583             :         /* wraps around after a bit over 24 days */
    1584           0 :         return (int) ((GDKusec() - programepoch) / 1000);
    1585             : }
    1586             : 
    1587             : 
    1588             : /*
    1589             :  * @+ Logical Thread management
    1590             :  *
    1591             :  * All semaphores used by the application should be mentioned here.
    1592             :  * They are initialized during system initialization.
    1593             :  *
    1594             :  * The first action upon thread creation is to add it to the pool of
    1595             :  * known threads. This should be done by the thread itself.
    1596             :  * Note that the users should have gained exclusive access already.  A
    1597             :  * new entry is initialized automatically when not found.  Its file
    1598             :  * descriptors are the same as for the server and should be
    1599             :  * subsequently reset.
    1600             :  */
    1601             : stream *GDKstdout;
    1602             : stream *GDKstdin;
    1603             : 
    1604             : static int
    1605         334 : THRinit(void)
    1606             : {
    1607         334 :         if ((GDKstdout = stdout_wastream()) == NULL) {
    1608           0 :                 TRC_CRITICAL(GDK, "malloc for stdout failed\n");
    1609           0 :                 return -1;
    1610             :         }
    1611         334 :         if ((GDKstdin = stdin_rastream()) == NULL) {
    1612           0 :                 TRC_CRITICAL(GDK, "malloc for stdin failed\n");
    1613           0 :                 mnstr_destroy(GDKstdout);
    1614           0 :                 GDKstdout = NULL;
    1615           0 :                 return -1;
    1616             :         }
    1617         334 :         struct freebats *t = MT_thread_getfreebats();
    1618         334 :         t->freebats = 0;
    1619         334 :         t->nfreebats = 0;
    1620         334 :         return 0;
    1621             : }
    1622             : 
    1623             : const char *
    1624         321 : GDKversion(void)
    1625             : {
    1626         321 :         return MONETDB_VERSION;
    1627             : }
    1628             : 
    1629             : const char *
    1630         653 : GDKlibversion(void)
    1631             : {
    1632         653 :         return GDK_VERSION;
    1633             : }
    1634             : 
    1635             : inline size_t
    1636   151386506 : GDKmem_cursize(void)
    1637             : {
    1638             :         /* RAM/swapmem that Monet is really using now */
    1639   151386506 :         return (size_t) ATOMIC_GET(&GDK_mallocedbytes_estimate);
    1640             : }
    1641             : 
    1642             : inline size_t
    1643   143954160 : GDKvm_cursize(void)
    1644             : {
    1645             :         /* current Monet VM address space usage */
    1646   143954160 :         return (size_t) ATOMIC_GET(&GDK_vm_cursize) + GDKmem_cursize();
    1647             : }
    1648             : 
    1649             : #define heapinc(_memdelta)                                              \
    1650             :         ATOMIC_ADD(&GDK_mallocedbytes_estimate, _memdelta)
    1651             : #ifndef NDEBUG
    1652             : #define heapdec(_memdelta)                                                      \
    1653             :         do {                                                            \
    1654             :                 ATOMIC_BASE_TYPE old = ATOMIC_SUB(&GDK_mallocedbytes_estimate, _memdelta); \
    1655             :                 assert(old >= (ATOMIC_BASE_TYPE) _memdelta);         \
    1656             :         } while (0)
    1657             : #else
    1658             : #define heapdec(_memdelta)                                              \
    1659             :         ATOMIC_SUB(&GDK_mallocedbytes_estimate, _memdelta)
    1660             : #endif
    1661             : 
    1662             : #define meminc(vmdelta)                                                 \
    1663             :         ATOMIC_ADD(&GDK_vm_cursize, SEG_SIZE(vmdelta))
    1664             : #ifndef NDEBUG
    1665             : #define memdec(vmdelta)                                                 \
    1666             :         do {                                                            \
    1667             :                 ssize_t diff = SEG_SIZE(vmdelta);                       \
    1668             :                 ATOMIC_BASE_TYPE old = ATOMIC_SUB(&GDK_vm_cursize, diff); \
    1669             :                 assert(old >= (ATOMIC_BASE_TYPE) diff);                      \
    1670             :         } while (0)
    1671             : #else
    1672             : #define memdec(vmdelta)                                                 \
    1673             :         ATOMIC_SUB(&GDK_vm_cursize, SEG_SIZE(vmdelta))
    1674             : #endif
    1675             : 
    1676             : /* Memory allocation
    1677             :  *
    1678             :  * The functions GDKmalloc, GDKzalloc, GDKrealloc, GDKstrdup, and
    1679             :  * GDKfree are used throughout to allocate and free memory.  These
    1680             :  * functions are almost directly mapped onto the system
    1681             :  * malloc/realloc/free functions, but they give us some extra
    1682             :  * debugging hooks.
    1683             :  *
    1684             :  * When allocating memory, we allocate a bit more than was asked for.
    1685             :  * The extra space is added onto the front of the memory area that is
    1686             :  * returned, and in debug builds also some at the end.  The area in
    1687             :  * front is used to store the actual size of the allocated area.  The
    1688             :  * most important use is to be able to keep statistics on how much
    1689             :  * memory is being used.  In debug builds, the size is also used to
    1690             :  * make sure that we don't write outside of the allocated arena.  This
    1691             :  * is also where the extra space at the end comes in.
    1692             :  */
    1693             : 
    1694             : /* we allocate extra space and return a pointer offset by this amount */
    1695             : #define MALLOC_EXTRA_SPACE      (2 * SIZEOF_VOID_P)
    1696             : 
    1697             : #if defined(NDEBUG) || defined(SANITIZER)
    1698             : #define DEBUG_SPACE     0
    1699             : #else
    1700             : #define DEBUG_SPACE     16
    1701             : #endif
    1702             : 
    1703             : /* malloc smaller than this aren't subject to the GDK_vm_maxsize test */
    1704             : #define SMALL_MALLOC    256
    1705             : 
    1706             : static void *
    1707   165419622 : GDKmalloc_internal(size_t size, bool clear)
    1708             : {
    1709   165419622 :         void *s;
    1710   165419622 :         size_t nsize;
    1711             : 
    1712   165419622 :         assert(size != 0);
    1713             : #ifndef SIZE_CHECK_IN_HEAPS_ONLY
    1714             :         if (size > SMALL_MALLOC &&
    1715             :             GDKvm_cursize() + size >= GDK_vm_maxsize &&
    1716             :             !MT_thread_override_limits()) {
    1717             :                 GDKerror("allocating too much memory\n");
    1718             :                 return NULL;
    1719             :         }
    1720             : #endif
    1721             : 
    1722             :         /* pad to multiple of eight bytes and add some extra space to
    1723             :          * write real size in front; when debugging, also allocate
    1724             :          * extra space for check bytes */
    1725   165419622 :         nsize = (size + 7) & ~7;
    1726   165419622 :         if (clear)
    1727    29477881 :                 s = calloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE, 1);
    1728             :         else
    1729   135941741 :                 s = malloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
    1730   165419622 :         if (s == NULL) {
    1731           0 :                 GDKsyserror("malloc failed; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", size, GDKmem_cursize(), GDKvm_cursize());;
    1732           0 :                 return NULL;
    1733             :         }
    1734   165419622 :         s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
    1735             : 
    1736   165419622 :         heapinc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
    1737             : 
    1738             :         /* just before the pointer that we return, write how much we
    1739             :          * asked of malloc */
    1740   165419622 :         ((size_t *) s)[-1] = nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE;
    1741             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1742             :         /* just before that, write how much was asked of us */
    1743   165419622 :         ((size_t *) s)[-2] = size;
    1744             :         /* write pattern to help find out-of-bounds writes */
    1745   165419622 :         memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
    1746             : #endif
    1747   165419622 :         return s;
    1748             : }
    1749             : 
    1750             : #undef GDKmalloc
    1751             : void *
    1752   128201652 : GDKmalloc(size_t size)
    1753             : {
    1754   128201652 :         void *s;
    1755             : 
    1756   128201652 :         if ((s = GDKmalloc_internal(size, false)) == NULL)
    1757             :                 return NULL;
    1758             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1759             :         /* write a pattern to help make sure all data is properly
    1760             :          * initialized by the caller */
    1761   128185964 :         DEADBEEFCHK memset(s, '\xBD', size);
    1762             : #endif
    1763             :         return s;
    1764             : }
    1765             : 
    1766             : #undef GDKzalloc
    1767             : void *
    1768    29478289 : GDKzalloc(size_t size)
    1769             : {
    1770    29478289 :         return GDKmalloc_internal(size, true);
    1771             : }
    1772             : 
    1773             : #undef GDKstrdup
    1774             : char *
    1775     8366080 : GDKstrdup(const char *s)
    1776             : {
    1777     8366080 :         size_t size;
    1778     8366080 :         char *p;
    1779             : 
    1780     8366080 :         if (s == NULL)
    1781             :                 return NULL;
    1782     7749310 :         size = strlen(s) + 1;
    1783             : 
    1784     7749310 :         if ((p = GDKmalloc_internal(size, false)) == NULL)
    1785             :                 return NULL;
    1786     7749374 :         memcpy(p, s, size);     /* including terminating NULL byte */
    1787     7749374 :         return p;
    1788             : }
    1789             : 
    1790             : #undef GDKstrndup
    1791             : char *
    1792         126 : GDKstrndup(const char *s, size_t size)
    1793             : {
    1794         126 :         char *p;
    1795             : 
    1796         126 :         if (s == NULL)
    1797             :                 return NULL;
    1798         126 :         if ((p = GDKmalloc_internal(size + 1, false)) == NULL)
    1799             :                 return NULL;
    1800         126 :         if (size > 0)
    1801         126 :                 memcpy(p, s, size);
    1802         126 :         p[size] = '\0';         /* make sure it's NULL terminated */
    1803         126 :         return p;
    1804             : }
    1805             : 
    1806             : #undef GDKfree
    1807             : void
    1808   321313653 : GDKfree(void *s)
    1809             : {
    1810   321313653 :         size_t asize;
    1811             : 
    1812   321313653 :         if (s == NULL)
    1813             :                 return;
    1814             : 
    1815   165292934 :         asize = ((size_t *) s)[-1]; /* how much allocated last */
    1816             : 
    1817             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1818   165292934 :         size_t *p = s;
    1819   165292934 :         assert((asize & 2) == 0);   /* check against duplicate free */
    1820   165292934 :         size_t size = p[-2];
    1821   165292934 :         assert(((size + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
    1822             :         /* check for out-of-bounds writes */
    1823  3185030891 :         for (size_t i = size; i < asize - MALLOC_EXTRA_SPACE; i++)
    1824  3019737957 :                 assert(((char *) s)[i] == '\xBD');
    1825   165292934 :         p[-1] |= 2;             /* indicate area is freed */
    1826             : 
    1827             :         /* overwrite memory that is to be freed with a pattern that
    1828             :          * will help us recognize access to already freed memory in
    1829             :          * the debugger */
    1830   165292934 :         DEADBEEFCHK memset(s, '\xDB', asize - MALLOC_EXTRA_SPACE);
    1831             : #endif
    1832             : 
    1833   165292934 :         free((char *) s - MALLOC_EXTRA_SPACE);
    1834   165292934 :         heapdec((ssize_t) asize);
    1835             : }
    1836             : 
    1837             : #undef GDKrealloc
    1838             : void *
    1839      240363 : GDKrealloc(void *s, size_t size)
    1840             : {
    1841      240363 :         size_t nsize, asize;
    1842             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1843      240363 :         size_t osize;
    1844             : #endif
    1845      240363 :         size_t *os = s;
    1846             : 
    1847      240363 :         assert(size != 0);
    1848             : 
    1849      240363 :         if (s == NULL)
    1850         843 :                 return GDKmalloc(size);
    1851             : 
    1852      239520 :         nsize = (size + 7) & ~7;
    1853      239520 :         asize = os[-1];         /* how much allocated last */
    1854             : 
    1855             : #ifndef SIZE_CHECK_IN_HEAPS_ONLY
    1856             :         if (size > SMALL_MALLOC &&
    1857             :             nsize > asize &&
    1858             :             GDKvm_cursize() + nsize - asize >= GDK_vm_maxsize &&
    1859             :             !MT_thread_override_limits()) {
    1860             :                 GDKerror("allocating too much memory\n");
    1861             :                 return NULL;
    1862             :         }
    1863             : #endif
    1864             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1865      239520 :         assert((asize & 2) == 0);   /* check against duplicate free */
    1866             :         /* check for out-of-bounds writes */
    1867      239520 :         osize = os[-2];         /* how much asked for last */
    1868      239520 :         assert(((osize + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
    1869     4141548 :         for (size_t i = osize; i < asize - MALLOC_EXTRA_SPACE; i++)
    1870     3902028 :                 assert(((char *) s)[i] == '\xBD');
    1871             :         /* if shrinking, write debug pattern into to-be-freed memory */
    1872      239520 :         DEADBEEFCHK if (size < osize)
    1873       35003 :                 memset((char *) s + size, '\xDB', osize - size);
    1874      239520 :         os[-1] |= 2;            /* indicate area is freed */
    1875             : #endif
    1876      239520 :         s = realloc((char *) s - MALLOC_EXTRA_SPACE,
    1877             :                     nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
    1878      239520 :         if (s == NULL) {
    1879             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1880           0 :                 os[-1] &= ~2;       /* not freed after all */
    1881           0 :                 assert(os[-1] == asize);
    1882           0 :                 assert(os[-2] == osize);
    1883             : #endif
    1884           0 :                 GDKsyserror("realloc failed; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", size, GDKmem_cursize(), GDKvm_cursize());;
    1885           0 :                 return NULL;
    1886             :         }
    1887      239520 :         s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
    1888             :         /* just before the pointer that we return, write how much we
    1889             :          * asked of malloc */
    1890      239520 :         ((size_t *) s)[-1] = nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE;
    1891             : #if !defined(NDEBUG) && !defined(SANITIZER)
    1892             :         /* just before that, write how much was asked of us */
    1893      239520 :         ((size_t *) s)[-2] = size;
    1894             :         /* if growing, initialize new memory with debug pattern */
    1895      239520 :         DEADBEEFCHK if (size > osize)
    1896      204441 :                 memset((char *) s + osize, '\xBD', size - osize);
    1897             :         /* write pattern to help find out-of-bounds writes */
    1898      239520 :         memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
    1899             : #endif
    1900             : 
    1901      239520 :         heapinc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
    1902      239520 :         heapdec((ssize_t) asize);
    1903             : 
    1904             :         return s;
    1905             : }
    1906             : 
    1907             : /* return how much memory was allocated; the argument must be a value
    1908             :  * returned by GDKmalloc, GDKzalloc, GDKrealloc, GDKstrdup, or
    1909             :  * GDKstrndup */
    1910             : size_t
    1911      191760 : GDKmallocated(const void *s)
    1912             : {
    1913      191760 :         return ((const size_t *) s)[-1]; /* how much allocated last */
    1914             : }
    1915             : 
    1916             : /*
    1917             :  * @- virtual memory
    1918             :  * allocations affect only the logical VM resources.
    1919             :  */
    1920             : #undef GDKmmap
    1921             : void *
    1922        3077 : GDKmmap(const char *path, int mode, size_t len)
    1923             : {
    1924        3077 :         void *ret;
    1925             : 
    1926             : #ifndef SIZE_CHECK_IN_HEAPS_ONLY
    1927             :         if (GDKvm_cursize() + len >= GDK_vm_maxsize &&
    1928             :             !MT_thread_override_limits()) {
    1929             :                 GDKerror("requested too much virtual memory; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", len, GDKmem_cursize(), GDKvm_cursize());
    1930             :                 return NULL;
    1931             :         }
    1932             : #endif
    1933        3077 :         ret = MT_mmap(path, mode, len);
    1934        3077 :         if (ret != NULL) {
    1935        3077 :                 if (mode & MMAP_COPY)
    1936           0 :                         heapinc(len);
    1937             :                 else
    1938        3077 :                         meminc(len);
    1939             :         } else
    1940           0 :                 GDKerror("requesting virtual memory failed; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", len, GDKmem_cursize(), GDKvm_cursize());
    1941        3077 :         return ret;
    1942             : }
    1943             : 
    1944             : #undef GDKmunmap
    1945             : gdk_return
    1946        3077 : GDKmunmap(void *addr, int mode, size_t size)
    1947             : {
    1948        3077 :         int ret;
    1949             : 
    1950        3077 :         ret = MT_munmap(addr, size);
    1951        3077 :         if (ret == 0) {
    1952        3077 :                 if (mode & MMAP_COPY)
    1953           0 :                         heapdec(size);
    1954             :                 else
    1955        3077 :                         memdec(size);
    1956             :         }
    1957        3077 :         return ret == 0 ? GDK_SUCCEED : GDK_FAIL;
    1958             : }
    1959             : 
    1960             : #undef GDKmremap
    1961             : void *
    1962         592 : GDKmremap(const char *path, int mode, void *old_address, size_t old_size, size_t *new_size)
    1963             : {
    1964         592 :         void *ret;
    1965             : 
    1966             : #ifndef SIZE_CHECK_IN_HEAPS_ONLY
    1967             :         if (*new_size > old_size &&
    1968             :             GDKvm_cursize() + *new_size - old_size >= GDK_vm_maxsize &&
    1969             :             !MT_thread_override_limits()) {
    1970             :                 GDKerror("requested too much virtual memory; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", *new_size, GDKmem_cursize(), GDKvm_cursize());
    1971             :                 return NULL;
    1972             :         }
    1973             : #endif
    1974         592 :         ret = MT_mremap(path, mode, old_address, old_size, new_size);
    1975         592 :         if (ret != NULL) {
    1976         592 :                 if (mode & MMAP_COPY) {
    1977           0 :                         heapdec(old_size);
    1978           0 :                         heapinc(*new_size);
    1979             :                 } else {
    1980         592 :                         memdec(old_size);
    1981         592 :                         meminc(*new_size);
    1982             :                 }
    1983             :         } else {
    1984           0 :                 GDKerror("requesting virtual memory failed; memory requested: %zu, memory in use: %zu, virtual memory in use: %zu\n", *new_size, GDKmem_cursize(), GDKvm_cursize());
    1985             :         }
    1986         592 :         return ret;
    1987             : }
    1988             : 
    1989             : /* print some potentially interesting information */
    1990             : struct prinfocb {
    1991             :         struct prinfocb *next;
    1992             :         void (*func)(void);
    1993             : } *prinfocb;
    1994             : 
    1995             : void
    1996         331 : GDKprintinforegister(void (*func)(void))
    1997             : {
    1998         331 :         struct prinfocb *p = GDKmalloc(sizeof(struct prinfocb));
    1999         331 :         if (p == NULL) {
    2000           0 :                 GDKerror("cannot register USR1 printing function.\n");
    2001           0 :                 return;
    2002             :         }
    2003         331 :         p->func = func;
    2004         331 :         p->next = NULL;
    2005         331 :         struct prinfocb **pp = &prinfocb;
    2006         386 :         while (*pp != NULL)
    2007          55 :                 pp = &(*pp)->next;
    2008         331 :         *pp = p;
    2009             : }
    2010             : 
    2011             : void
    2012         116 : GDKprintinfo(void)
    2013             : {
    2014         116 :         size_t allocated = (size_t) ATOMIC_GET(&GDK_mallocedbytes_estimate);
    2015         116 :         size_t vmallocated = (size_t) ATOMIC_GET(&GDK_vm_cursize);
    2016             : 
    2017         116 :         printf("SIGUSR1 info start\n");
    2018         116 :         printf("Virtual memory allocated: %zu, of which %zu with malloc\n",
    2019             :                vmallocated + allocated, allocated);
    2020         116 :         printf("gdk_vm_maxsize: %zu, gdk_mem_maxsize: %zu\n",
    2021             :                GDK_vm_maxsize, GDK_mem_maxsize);
    2022         116 :         printf("gdk_mmap_minsize_persistent %zu, gdk_mmap_minsize_transient %zu\n",
    2023             :                GDK_mmap_minsize_persistent, GDK_mmap_minsize_transient);
    2024             : #ifdef __linux__
    2025         116 :         int fd = open("/proc/self/statm", O_RDONLY | O_CLOEXEC);
    2026         116 :         if (fd >= 0) {
    2027         116 :                 char buf[512];
    2028         116 :                 ssize_t s = read(fd, buf, sizeof(buf) - 1);
    2029         116 :                 close(fd);
    2030         116 :                 if (s > 0) {
    2031         116 :                         assert((size_t) s < sizeof(buf));
    2032         116 :                         size_t size, resident, shared;
    2033         116 :                         buf[s] = 0;
    2034         116 :                         if (sscanf(buf, "%zu %zu %zu", &size, &resident, &shared) == 3) {
    2035         116 :                                 size *= MT_pagesize();
    2036         116 :                                 resident *= MT_pagesize();
    2037         116 :                                 shared *= MT_pagesize();
    2038         116 :                                 printf("Virtual size: %zu, anonymous RSS: %zu, shared RSS: %zu (together: %zu)\n",
    2039             :                                        size, resident - shared, shared, resident);
    2040             :                         }
    2041             :                 }
    2042             :         }
    2043             : #endif
    2044         116 :         BBPprintinfo();
    2045             : #ifdef LOCK_STATS
    2046             :         GDKlockstatistics(3);
    2047             : #endif
    2048         116 :         dump_threads();
    2049         232 :         for (struct prinfocb *p = prinfocb; p; p = p->next)
    2050         116 :                 (*p->func)();
    2051         116 :         printf("SIGUSR1 info end\n");
    2052         116 : }
    2053             : 
    2054             : exception_buffer *
    2055      685188 : eb_init(exception_buffer *eb)
    2056             : {
    2057      685188 :         if (eb) {
    2058      685188 :                 eb->enabled = 0;
    2059      685188 :                 eb->code = 0;
    2060      685188 :                 eb->msg = NULL;
    2061             :         }
    2062      685188 :         return eb;
    2063             : }
    2064             : 
    2065             : void
    2066           3 : eb_error( exception_buffer *eb, char *msg, int val )
    2067             : {
    2068           3 :         eb->code = val;
    2069           3 :         eb->msg = msg;
    2070           3 :         eb->enabled = 0;                     /* not any longer... */
    2071             : #ifdef HAVE_SIGLONGJMP
    2072           3 :         siglongjmp(eb->state, eb->code);
    2073             : #else
    2074             :         longjmp(eb->state, eb->code);
    2075             : #endif
    2076             : }
    2077             : 
    2078             : #define SA_BLOCK (64*1024)
    2079             : 
    2080             : typedef struct freed_t {
    2081             :         struct freed_t *n;
    2082             :         size_t sz;
    2083             : } freed_t;
    2084             : 
    2085             : static void
    2086       70338 : sa_destroy_freelist( freed_t *f )
    2087             : {
    2088       79248 :         while(f) {
    2089        8910 :                 freed_t *n = f->n;
    2090        8910 :                 GDKfree(f);
    2091        8910 :                 f = n;
    2092             :         }
    2093             : }
    2094             : 
    2095             : static void
    2096      191760 : sa_free(allocator *pa, void *blk)
    2097             : {
    2098      191760 :         assert(!pa->pa);
    2099             :         size_t i;
    2100             : 
    2101    47568872 :         for(i = 0; i < pa->nr; i++) {
    2102    47568872 :                 if (pa->blks[i] == blk)
    2103             :                         break;
    2104             :         }
    2105      191760 :         assert (i < pa->nr);
    2106     1995265 :         for (; i < pa->nr-1; i++)
    2107     1803505 :                 pa->blks[i] = pa->blks[i+1];
    2108      191760 :         pa->nr--;
    2109             : 
    2110      191760 :         size_t sz = GDKmallocated(blk);
    2111      191760 :         if (sz > (SA_BLOCK + 32)) {
    2112          90 :                 GDKfree(blk);
    2113             :         } else {
    2114      191670 :                 freed_t *f = blk;
    2115      191670 :                 f->n = pa->freelist;
    2116      191670 :                 f->sz = sz;
    2117             : 
    2118      191670 :                 pa->freelist = f;
    2119             :         }
    2120      191760 : }
    2121             : 
    2122             : static void *
    2123      182760 : sa_use_freed(allocator *pa, size_t sz)
    2124             : {
    2125      182760 :         (void)sz;
    2126             : 
    2127      182760 :         freed_t *f = pa->freelist;
    2128      182760 :         pa->freelist = f->n;
    2129      182760 :         return f;
    2130             : }
    2131             : 
    2132             : allocator *
    2133      277008 : sa_create(allocator *pa)
    2134             : {
    2135      277008 :         allocator *sa = (pa)?(allocator*)sa_alloc(pa, sizeof(allocator)):(allocator*)GDKmalloc(sizeof(allocator));
    2136      277004 :         if (sa == NULL)
    2137             :                 return NULL;
    2138      277004 :         eb_init(&sa->eb);
    2139      277005 :         sa->pa = pa;
    2140      277005 :         sa->size = 64;
    2141      277005 :         sa->nr = 1;
    2142      277005 :         sa->blks = pa?(char**)sa_alloc(pa, sizeof(char*) * sa->size):(char**)GDKmalloc(sizeof(char*) * sa->size);
    2143      277002 :         sa->freelist = NULL;
    2144      277002 :         if (sa->blks == NULL) {
    2145           0 :                 if (!pa)
    2146           0 :                         GDKfree(sa);
    2147           0 :                 return NULL;
    2148             :         }
    2149      277002 :         sa->blks[0] = pa?(char*)sa_alloc(pa, SA_BLOCK):(char*)GDKmalloc(SA_BLOCK);
    2150      277008 :         sa->usedmem = SA_BLOCK;
    2151      277008 :         if (sa->blks[0] == NULL) {
    2152           0 :                 if (!pa)
    2153           0 :                         GDKfree(sa->blks);
    2154           0 :                 if (!pa)
    2155           0 :                         GDKfree(sa);
    2156           0 :                 return NULL;
    2157             :         }
    2158      277008 :         sa->used = 0;
    2159      277008 :         return sa;
    2160             : }
    2161             : 
    2162     1885822 : allocator *sa_reset( allocator *sa )
    2163             : {
    2164     1885822 :         size_t i ;
    2165             : 
    2166     1946188 :         for (i = 1; i<sa->nr; i++) {
    2167       60361 :                 if (!sa->pa)
    2168           0 :                         GDKfree(sa->blks[i]);
    2169             :                 else
    2170       60361 :                         sa_free(sa->pa, sa->blks[i]);
    2171             :         }
    2172     1885827 :         sa->nr = 1;
    2173     1885827 :         sa->used = 0;
    2174     1885827 :         sa->usedmem = SA_BLOCK;
    2175     1885827 :         return sa;
    2176             : }
    2177             : 
    2178             : #undef sa_realloc
    2179             : #undef sa_alloc
    2180             : void *
    2181          33 : sa_realloc( allocator *sa, void *p, size_t sz, size_t oldsz )
    2182             : {
    2183          33 :         void *r = sa_alloc(sa, sz);
    2184             : 
    2185          33 :         if (r)
    2186          33 :                 memcpy(r, p, oldsz);
    2187          33 :         return r;
    2188             : }
    2189             : 
    2190             : #define round16(sz) ((sz+15)&~15)
    2191             : void *
    2192   241394345 : sa_alloc( allocator *sa, size_t sz )
    2193             : {
    2194   241394345 :         char *r;
    2195   241394345 :         sz = round16(sz);
    2196   241394345 :         if (sz > (SA_BLOCK-sa->used)) {
    2197      514172 :                 if (sa->pa)
    2198       60590 :                         r = (char*)sa_alloc(sa->pa, (sz > SA_BLOCK ? sz : SA_BLOCK));
    2199      453582 :                 else if (sz <= SA_BLOCK && sa->freelist) {
    2200      182760 :                         r = sa_use_freed(sa, SA_BLOCK);
    2201             :                 } else
    2202      270822 :                         r = GDKmalloc(sz > SA_BLOCK ? sz : SA_BLOCK);
    2203      514175 :                 if (r == NULL) {
    2204           0 :                         if (sa->eb.enabled)
    2205           0 :                                 eb_error(&sa->eb, "out of memory", 1000);
    2206             :                         return NULL;
    2207             :                 }
    2208      514175 :                 if (sa->nr >= sa->size) {
    2209         951 :                         char **tmp;
    2210         951 :                         size_t osz = sa->size;
    2211         951 :                         sa->size *=2;
    2212         951 :                         if (sa->pa)
    2213          19 :                                 tmp = (char**)sa_realloc(sa->pa, sa->blks, sizeof(char*) * sa->size, sizeof(char*) * osz);
    2214             :                         else
    2215         932 :                                 tmp = GDKrealloc(sa->blks, sizeof(char*) * sa->size);
    2216         951 :                         if (tmp == NULL) {
    2217           0 :                                 sa->size /= 2; /* undo */
    2218           0 :                                 if (sa->eb.enabled)
    2219           0 :                                         eb_error(&sa->eb, "out of memory", 1000);
    2220           0 :                                 if (!sa->pa)
    2221           0 :                                         GDKfree(r);
    2222           0 :                                 return NULL;
    2223             :                         }
    2224         951 :                         sa->blks = tmp;
    2225             :                 }
    2226      514175 :                 if (sz > SA_BLOCK) {
    2227         527 :                         sa->blks[sa->nr] = sa->blks[sa->nr-1];
    2228         527 :                         sa->blks[sa->nr-1] = r;
    2229         527 :                         sa->nr ++;
    2230         527 :                         sa->usedmem += sz;
    2231             :                 } else {
    2232      513648 :                         sa->blks[sa->nr] = r;
    2233      513648 :                         sa->nr ++;
    2234      513648 :                         sa->used = sz;
    2235      513648 :                         sa->usedmem += SA_BLOCK;
    2236             :                 }
    2237             :         } else {
    2238   240880173 :                 r = sa->blks[sa->nr-1] + sa->used;
    2239   240880173 :                 sa->used += sz;
    2240             :         }
    2241             :         return r;
    2242             : }
    2243             : 
    2244             : #undef sa_zalloc
    2245    10286554 : void *sa_zalloc( allocator *sa, size_t sz )
    2246             : {
    2247    10286554 :         void *r = sa_alloc(sa, sz);
    2248             : 
    2249    10286498 :         if (r)
    2250    10286498 :                 memset(r, 0, sz);
    2251    10286498 :         return r;
    2252             : }
    2253             : 
    2254      201737 : void sa_destroy( allocator *sa )
    2255             : {
    2256      201737 :         if (sa->pa) {
    2257      131399 :                 sa_reset(sa);
    2258      131399 :                 sa_free(sa->pa, sa->blks[0]);
    2259      131399 :                 return;
    2260             :         }
    2261             : 
    2262       70338 :         sa_destroy_freelist(sa->freelist);
    2263      402494 :         for (size_t i = 0; i<sa->nr; i++) {
    2264      332156 :                 GDKfree(sa->blks[i]);
    2265             :         }
    2266       70338 :         GDKfree(sa->blks);
    2267       70338 :         GDKfree(sa);
    2268             : }
    2269             : 
    2270             : #undef sa_strndup
    2271    13202448 : char *sa_strndup( allocator *sa, const char *s, size_t l)
    2272             : {
    2273    13202448 :         char *r = sa_alloc(sa, l+1);
    2274             : 
    2275    13202373 :         if (r) {
    2276    13202373 :                 memcpy(r, s, l);
    2277    13202373 :                 r[l] = 0;
    2278             :         }
    2279    13202373 :         return r;
    2280             : }
    2281             : 
    2282             : #undef sa_strdup
    2283     7839649 : char *sa_strdup( allocator *sa, const char *s )
    2284             : {
    2285     7839649 :         return sa_strndup( sa, s, strlen(s));
    2286             : }
    2287             : 
    2288       42737 : char *sa_strconcat( allocator *sa, const char *s1, const char *s2 )
    2289             : {
    2290       42737 :         size_t l1 = strlen(s1);
    2291       42737 :         size_t l2 = strlen(s2);
    2292       42737 :         char *r = sa_alloc(sa, l1+l2+1);
    2293             : 
    2294       42737 :         if (l1)
    2295       42737 :                 memcpy(r, s1, l1);
    2296       42737 :         if (l2)
    2297       42737 :                 memcpy(r+l1, s2, l2);
    2298       42737 :         r[l1+l2] = 0;
    2299       42737 :         return r;
    2300             : }
    2301             : 
    2302           0 : size_t sa_size( allocator *sa )
    2303             : {
    2304           0 :         return sa->usedmem;
    2305             : }

Generated by: LCOV version 1.14