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 331 : GDKenvironment(const char *dbpath)
94 : {
95 331 : if (dbpath == NULL) {
96 0 : TRC_CRITICAL(GDK, "Database name missing.\n");
97 0 : return false;
98 : }
99 331 : if (strlen(dbpath) >= FILENAME_MAX) {
100 0 : TRC_CRITICAL(GDK, "Database name too long.\n");
101 0 : return false;
102 : }
103 331 : 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 471233 : GDKgetenv(const char *name)
119 : {
120 471233 : MT_lock_set(&GDKenvlock);
121 471309 : 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 471309 : MT_lock_unset(&GDKenvlock);
128 471309 : if (GDKkey && GDKval) {
129 470667 : BUN b = BUNfnd(GDKkey, name);
130 :
131 470666 : if (b != BUN_NONE) {
132 330179 : BATiter GDKenvi = bat_iterator(GDKval);
133 330180 : const char *v = BUNtvar(GDKenvi, b);
134 330180 : bat_iterator_end(&GDKenvi);
135 330180 : return v;
136 : }
137 : }
138 : return NULL;
139 : }
140 :
141 : bool
142 285860 : GDKgetenv_istext(const char *name, const char *text)
143 : {
144 285860 : const char *val = GDKgetenv(name);
145 :
146 285868 : return val && strcasecmp(val, text) == 0;
147 : }
148 :
149 : bool
150 39167 : GDKgetenv_isyes(const char *name)
151 : {
152 39167 : return GDKgetenv_istext(name, "yes");
153 : }
154 :
155 : bool
156 246698 : GDKgetenv_istrue(const char *name)
157 : {
158 246698 : return GDKgetenv_istext(name, "true");
159 : }
160 :
161 : int
162 95760 : GDKgetenv_int(const char *name, int def)
163 : {
164 95760 : const char *val = GDKgetenv(name);
165 :
166 95762 : if (val)
167 318 : return atoi(val);
168 : return def;
169 : }
170 :
171 : #define ESCAPE_CHAR '%'
172 :
173 : static bool
174 8040 : isutf8(const char *v, size_t *esclen)
175 : {
176 8040 : size_t n = 1;
177 8040 : int nutf8 = 0;
178 8040 : int m = 0;
179 151556 : for (size_t i = 0; v[i]; i++) {
180 143516 : 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 143508 : } else if ((v[i] & 0xE0) == 0xC0) {
187 0 : nutf8 = 1;
188 0 : if ((v[i] & 0x1E) == 0)
189 0 : goto badutf8;
190 143508 : } else if ((v[i] & 0xF0) == 0xE0) {
191 4 : nutf8 = 2;
192 4 : if ((v[i] & 0x0F) == 0)
193 0 : m = 0x20;
194 143504 : } else if ((v[i] & 0xF8) == 0xF0) {
195 0 : nutf8 = 3;
196 0 : if ((v[i] & 0x07) == 0)
197 0 : m = 0x30;
198 143504 : } else if ((v[i] & 0x80) != 0) {
199 0 : goto badutf8;
200 : }
201 : }
202 8040 : *esclen = 0;
203 8040 : 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 8040 : GDKsetenv(const char *name, const char *value)
217 : {
218 8040 : static const char hexdigits[] = "0123456789abcdef";
219 8040 : char *conval = NULL;
220 8040 : size_t esclen = 0;
221 8040 : 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 8040 : MT_lock_set(&GDKenvlock);
260 8040 : 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 8040 : MT_lock_unset(&GDKenvlock);
269 : }
270 8040 : BUN p = BUNfnd(GDKkey, name);
271 8040 : gdk_return rc;
272 8040 : if (p != BUN_NONE) {
273 1902 : rc = BUNreplace(GDKval, p + GDKval->hseqbase,
274 : conval ? conval : value, false);
275 : } else {
276 7089 : rc = BUNappend(GDKkey, name, false);
277 7089 : if (rc == GDK_SUCCEED) {
278 14178 : rc = BUNappend(GDKval, conval ? conval : value, false);
279 7089 : 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 8040 : assert(BATcount(GDKval) == BATcount(GDKkey));
290 8040 : GDKfree(conval);
291 8040 : return rc;
292 : }
293 :
294 : gdk_return
295 65 : GDKcopyenv(BAT **key, BAT **val, bool writable)
296 : {
297 65 : BAT *k, *v;
298 :
299 65 : if (key == NULL || val == NULL) {
300 0 : GDKerror("called incorrectly.\n");
301 0 : return GDK_FAIL;
302 : }
303 65 : k = COLcopy(GDKkey, GDKkey->ttype, writable, TRANSIENT);
304 65 : v = COLcopy(GDKval, GDKval->ttype, writable, TRANSIENT);
305 65 : if (k == NULL || v == NULL) {
306 0 : BBPreclaim(k);
307 0 : BBPreclaim(v);
308 0 : return GDK_FAIL;
309 : }
310 65 : *key = k;
311 65 : *val = v;
312 65 : 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 822 : GDKlog(FILE *lockFile, const char *format, ...)
344 : {
345 822 : va_list ap;
346 822 : char *p = 0, buf[1024];
347 822 : time_t tm = time(0);
348 : #if defined(HAVE_CTIME_R3) || defined(HAVE_CTIME_R)
349 822 : char tbuf[26];
350 : #endif
351 822 : char *ctm;
352 :
353 822 : if (MT_pagesize() == 0 || lockFile == NULL)
354 1 : return;
355 :
356 821 : va_start(ap, format);
357 821 : vsnprintf(buf, sizeof(buf), format, ap);
358 821 : va_end(ap);
359 :
360 : /* remove forbidden characters from message */
361 821 : for (p = buf; (p = strchr(p, '\n')) != NULL; *p = ' ')
362 : ;
363 821 : for (p = buf; (p = strchr(p, '@')) != NULL; *p = ' ')
364 : ;
365 :
366 821 : 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 821 : ctm = ctime_r(&tm, tbuf);
374 : #endif
375 821 : fprintf(lockFile, "USR=%d PID=%d TIME=%.24s @ %s\n", (int) getuid(), (int) getpid(), ctm, buf);
376 821 : 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 332 : BATSIGinit(void)
394 : {
395 : #ifdef HAVE_SIGACTION
396 332 : struct sigaction sa;
397 332 : sigemptyset(&sa.sa_mask);
398 332 : sa.sa_flags = 0;
399 : #ifdef SIGPIPE
400 332 : sa.sa_handler = SIG_IGN;
401 332 : sigaction(SIGPIPE, &sa, NULL);
402 : #endif
403 : #ifdef SIGHUP
404 332 : sa.sa_handler = GDKtracer_reinit_basic;
405 332 : 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 332 : }
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 333 : MT_init(void)
463 : {
464 333 : 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 333 : _MT_pagesize = (size_t)sysconf(_SC_PAGESIZE);
487 : #endif
488 333 : 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 333 : _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 333 : FILE *fc;
560 333 : char buf[1024];
561 333 : char cgr1[1024] = "/sys/fs/cgroup/memory";
562 333 : char cgr2[1024] = "/sys/fs/cgroup";
563 333 : fc = fopen("/proc/self/mountinfo", "r");
564 333 : if (fc != NULL) {
565 10989 : while (fgets(buf, (int) sizeof(buf), fc) != NULL) {
566 10656 : char *p, *cgr;
567 10656 : if ((p = strstr(buf, " - cgroup ")) != NULL &&
568 0 : strstr(p, "memory") != NULL)
569 : cgr = cgr1;
570 10656 : else if (strstr(buf, " - cgroup2 ") != NULL)
571 : cgr = cgr2;
572 : else
573 10323 : continue;
574 : /* buf points at mount ID */
575 333 : p = strchr(buf, ' ');
576 333 : if (p == NULL)
577 : break;
578 333 : p++;
579 : /* p points at parent ID */
580 333 : p = strchr(p, ' ');
581 333 : if (p == NULL)
582 : break;
583 333 : p++;
584 : /* p points at major:minor */
585 333 : p = strchr(p, ' ');
586 333 : if (p == NULL)
587 : break;
588 333 : p++;
589 : /* p points at root */
590 333 : p = strchr(p, ' ');
591 333 : if (p == NULL)
592 : break;
593 333 : p++;
594 : /* p points at mount point */
595 333 : char *dir = p;
596 333 : p = strchr(p, ' ');
597 333 : if (p == NULL)
598 : break;
599 333 : *p = 0;
600 333 : strcpy_len(cgr, dir, 1024);
601 : }
602 333 : fclose(fc);
603 : }
604 333 : fc = fopen("/proc/self/cgroup", "r");
605 333 : 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 333 : while (fgets(buf, (int) sizeof(buf), fc) != NULL) {
625 333 : char pth[1024];
626 333 : char *p, *q;
627 333 : bool success = false; /* true if we can open any file */
628 333 : FILE *f;
629 333 : uint64_t mem;
630 :
631 333 : p = strchr(buf, '\n');
632 333 : if (p == NULL)
633 : break;
634 333 : *p = 0;
635 333 : if (strncmp(buf, "0::", 3) == 0) {
636 : /* cgroup v2 entry */
637 333 : p = stpcpy(pth, cgr2);
638 333 : q = stpcpy(stpcpy(p, buf + 3), "/");
639 : /* hard limit */
640 333 : strcpy(q, "memory.max");
641 333 : f = fopen(pth, "r");
642 333 : 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 333 : if (f != NULL) {
657 333 : 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 333 : success = true;
663 : /* assume "max" if not a number */
664 333 : fclose(f);
665 : }
666 : /* soft high limit */
667 333 : strcpy(q, "memory.high");
668 333 : f = fopen(pth, "r");
669 333 : if (f != NULL) {
670 333 : 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 333 : success = true;
676 : /* assume "max" if not a number */
677 333 : 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 333 : strcpy(q, "memory.swap.max");
684 333 : f = fopen(pth, "r");
685 333 : if (f != NULL) {
686 333 : 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 333 : success = true;
692 333 : 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 333 : if (success)
789 : break;
790 : }
791 333 : fclose(fc);
792 : }
793 : #endif
794 :
795 : #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(RLIMIT_AS)
796 333 : struct rlimit l;
797 : /* address space (virtual memory) limit */
798 333 : if (getrlimit(RLIMIT_AS, &l) == 0
799 333 : && 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 333 : }
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 338 : GDKsetdebug(unsigned debug)
827 : {
828 338 : ATOMIC_SET(&GDKdebug, debug);
829 338 : if (debug & ACCELMASK)
830 0 : GDKtracer_set_component_level("accelerator", "debug");
831 : else
832 338 : GDKtracer_reset_component_level("accelerator");
833 338 : if (debug & ALGOMASK)
834 0 : GDKtracer_set_component_level("algo", "debug");
835 : else
836 338 : GDKtracer_reset_component_level("algo");
837 338 : if (debug & ALLOCMASK)
838 0 : GDKtracer_set_component_level("alloc", "debug");
839 : else
840 338 : GDKtracer_reset_component_level("alloc");
841 338 : if (debug & BATMASK)
842 0 : GDKtracer_set_component_level("bat", "debug");
843 : else
844 338 : GDKtracer_reset_component_level("bat");
845 338 : if (debug & CHECKMASK)
846 326 : GDKtracer_set_component_level("check", "debug");
847 : else
848 12 : GDKtracer_reset_component_level("check");
849 338 : if (debug & DELTAMASK)
850 0 : GDKtracer_set_component_level("delta", "debug");
851 : else
852 338 : GDKtracer_reset_component_level("delta");
853 338 : if (debug & HEAPMASK)
854 0 : GDKtracer_set_component_level("heap", "debug");
855 : else
856 338 : GDKtracer_reset_component_level("heap");
857 338 : if (debug & IOMASK)
858 0 : GDKtracer_set_component_level("io", "debug");
859 : else
860 338 : GDKtracer_reset_component_level("io");
861 338 : if (debug & PARMASK)
862 0 : GDKtracer_set_component_level("par", "debug");
863 : else
864 338 : GDKtracer_reset_component_level("par");
865 338 : if (debug & PERFMASK)
866 0 : GDKtracer_set_component_level("perf", "debug");
867 : else
868 338 : GDKtracer_reset_component_level("perf");
869 338 : if (debug & TEMMASK)
870 0 : GDKtracer_set_component_level("tem", "debug");
871 : else
872 338 : GDKtracer_reset_component_level("tem");
873 338 : if (debug & THRDMASK)
874 0 : GDKtracer_set_component_level("thrd", "debug");
875 : else
876 338 : GDKtracer_reset_component_level("thrd");
877 338 : }
878 :
879 : unsigned
880 18 : GDKgetdebug(void)
881 : {
882 18 : ATOMIC_BASE_TYPE debug = ATOMIC_GET(&GDKdebug);
883 18 : const char *lvl;
884 18 : lvl = GDKtracer_get_component_level("accelerator");
885 18 : if (lvl && strcmp(lvl, "debug") == 0)
886 0 : debug |= ACCELMASK;
887 18 : lvl = GDKtracer_get_component_level("algo");
888 18 : if (lvl && strcmp(lvl, "debug") == 0)
889 0 : debug |= ALGOMASK;
890 18 : lvl = GDKtracer_get_component_level("alloc");
891 18 : if (lvl && strcmp(lvl, "debug") == 0)
892 0 : debug |= ALLOCMASK;
893 18 : lvl = GDKtracer_get_component_level("bat");
894 18 : if (lvl && strcmp(lvl, "debug") == 0)
895 0 : debug |= BATMASK;
896 18 : lvl = GDKtracer_get_component_level("check");
897 18 : if (lvl && strcmp(lvl, "debug") == 0)
898 0 : debug |= CHECKMASK;
899 18 : lvl = GDKtracer_get_component_level("delta");
900 18 : if (lvl && strcmp(lvl, "debug") == 0)
901 0 : debug |= DELTAMASK;
902 18 : lvl = GDKtracer_get_component_level("heap");
903 18 : if (lvl && strcmp(lvl, "debug") == 0)
904 0 : debug |= HEAPMASK;
905 18 : lvl = GDKtracer_get_component_level("io");
906 18 : if (lvl && strcmp(lvl, "debug") == 0)
907 0 : debug |= IOMASK;
908 18 : lvl = GDKtracer_get_component_level("par");
909 18 : if (lvl && strcmp(lvl, "debug") == 0)
910 0 : debug |= PARMASK;
911 18 : lvl = GDKtracer_get_component_level("perf");
912 18 : if (lvl && strcmp(lvl, "debug") == 0)
913 0 : debug |= PERFMASK;
914 18 : lvl = GDKtracer_get_component_level("tem");
915 18 : if (lvl && strcmp(lvl, "debug") == 0)
916 0 : debug |= TEMMASK;
917 18 : lvl = GDKtracer_get_component_level("thrd");
918 18 : if (lvl && strcmp(lvl, "debug") == 0)
919 0 : debug |= THRDMASK;
920 18 : return (unsigned) debug;
921 : }
922 :
923 : static bool Mbedded = true;
924 : bool
925 25642943 : GDKembedded(void)
926 : {
927 25642943 : return Mbedded;
928 : }
929 :
930 : static MT_Id mainpid;
931 :
932 : gdk_return
933 332 : GDKinit(opt *set, int setlen, bool embedded, const char *caller_revision)
934 : {
935 332 : static bool first = true;
936 332 : const char *dbpath;
937 332 : const char *dbtrace;
938 332 : const char *p;
939 332 : opt *n;
940 332 : int i, nlen = 0;
941 332 : char buf[16];
942 :
943 332 : if (caller_revision) {
944 321 : p = mercurial_revision();
945 321 : 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 332 : ATOMIC_SET(&GDKstopped, 0);
952 :
953 332 : if (BBPchkfarms() != GDK_SUCCEED)
954 : return GDK_FAIL;
955 :
956 332 : if (GDKinmemory(0)) {
957 : dbpath = dbtrace = NULL;
958 : } else {
959 331 : dbpath = mo_find_option(set, setlen, "gdk_dbpath");
960 331 : dbtrace = mo_find_option(set, setlen, "gdk_dbtrace");
961 : }
962 332 : Mbedded = embedded;
963 : /* some sanity checks (should also find if symbols are not defined) */
964 332 : static_assert(sizeof(int) == sizeof(int32_t),
965 : "int is not equal in size to int32_t");
966 332 : static_assert(sizeof(char) == SIZEOF_CHAR,
967 : "error in configure: bad value for SIZEOF_CHAR");
968 332 : static_assert(sizeof(short) == SIZEOF_SHORT,
969 : "error in configure: bad value for SIZEOF_SHORT");
970 332 : static_assert(sizeof(int) == SIZEOF_INT,
971 : "error in configure: bad value for SIZEOF_INT");
972 332 : static_assert(sizeof(long) == SIZEOF_LONG,
973 : "error in configure: bad value for SIZEOF_LONG");
974 332 : static_assert(sizeof(lng) == SIZEOF_LNG,
975 : "error in configure: bad value for SIZEOF_LNG");
976 : #ifdef HAVE_HGE
977 332 : static_assert(sizeof(hge) == SIZEOF_HGE,
978 : "error in configure: bad value for SIZEOF_HGE");
979 : #endif
980 332 : static_assert(sizeof(dbl) == SIZEOF_DOUBLE,
981 : "error in configure: bad value for SIZEOF_DOUBLE");
982 332 : static_assert(sizeof(oid) == SIZEOF_OID,
983 : "error in configure: bad value for SIZEOF_OID");
984 332 : static_assert(sizeof(void *) == SIZEOF_VOID_P,
985 : "error in configure: bad value for SIZEOF_VOID_P");
986 332 : static_assert(sizeof(size_t) == SIZEOF_SIZE_T,
987 : "error in configure: bad value for SIZEOF_SIZE_T");
988 332 : static_assert(SIZEOF_OID == SIZEOF_INT || SIZEOF_OID == SIZEOF_LNG,
989 : "SIZEOF_OID should be equal to SIZEOF_INT or SIZEOF_LNG");
990 332 : static_assert(sizeof(uuid) == 16,
991 : "sizeof(uuid) should be equal to 16");
992 :
993 332 : if (first) {
994 : /* some things are really only initialized once */
995 322 : if (!MT_thread_init()) {
996 0 : TRC_CRITICAL(GDK, "MT_thread_init failed\n");
997 0 : return GDK_FAIL;
998 : }
999 :
1000 2638146 : for (i = 0; i <= BBP_BATMASK; i++) {
1001 2637824 : char name[MT_NAME_LEN];
1002 2637824 : snprintf(name, sizeof(name), "GDKswapLock%d", i);
1003 2637824 : MT_lock_init(&GDKbatLock[i].swap, name);
1004 : }
1005 322 : 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 332 : }
1013 332 : mainpid = MT_getpid();
1014 :
1015 332 : GDKtracer_init(dbpath, dbtrace);
1016 332 : errno = 0;
1017 332 : if (!GDKinmemory(0) && !GDKenvironment(dbpath))
1018 : return GDK_FAIL;
1019 :
1020 332 : MT_init_posix();
1021 332 : if (THRinit() < 0)
1022 : return GDK_FAIL;
1023 : #ifndef NATIVE_WIN32
1024 332 : BATSIGinit();
1025 : #endif
1026 332 : 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 10892 : for (int farmid = 0; farmid < MAXFARMS; farmid++) {
1031 10562 : if (BBPfarms[farmid].dirname != NULL) {
1032 1308 : bool skip = false;
1033 1308 : for (int j = 0; j < farmid; j++) {
1034 812 : if (BBPfarms[j].dirname != NULL &&
1035 812 : strcmp(BBPfarms[farmid].dirname, BBPfarms[j].dirname) == 0) {
1036 : skip = true;
1037 : break;
1038 : }
1039 : }
1040 978 : 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 330 : GDK_mem_maxsize = (size_t) ((double) MT_npages() * (double) MT_pagesize() * 0.815);
1052 330 : const char *allow = mo_find_option(set, setlen, "allow_hge_upgrade");
1053 652 : if (BBPinit(allow && strcmp(allow, "yes") == 0) != GDK_SUCCEED)
1054 : return GDK_FAIL;
1055 330 : first = false;
1056 :
1057 330 : if (GDK_mem_maxsize / 16 < GDK_mmap_minsize_transient) {
1058 320 : GDK_mmap_minsize_transient = MAX(MMAP_PAGESIZE, GDK_mem_maxsize / 16);
1059 320 : if (GDK_mmap_minsize_persistent > GDK_mmap_minsize_transient)
1060 0 : GDK_mmap_minsize_persistent = GDK_mmap_minsize_transient;
1061 : }
1062 :
1063 330 : n = (opt *) malloc(setlen * sizeof(opt));
1064 330 : if (n == NULL) {
1065 0 : GDKsyserror("malloc failed\n");
1066 0 : return GDK_FAIL;
1067 : }
1068 :
1069 3965 : for (i = 0; i < setlen; i++) {
1070 16257 : bool done = false;
1071 :
1072 16257 : for (int j = 0; j < nlen; j++) {
1073 13272 : if (strcmp(n[j].name, set[i].name) == 0) {
1074 650 : if (n[j].kind < set[i].kind) {
1075 650 : n[j] = set[i];
1076 : }
1077 : done = true;
1078 : break;
1079 : }
1080 : }
1081 2985 : if (!done) {
1082 2985 : n[nlen] = set[i];
1083 2985 : nlen++;
1084 : }
1085 : }
1086 : /* check some options before creating our first BAT */
1087 3315 : for (i = 0; i < nlen; i++) {
1088 2985 : 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 2985 : } else if (strcmp("gdk_vm_maxsize", n[i].name) == 0) {
1096 171 : GDK_vm_maxsize = (size_t) strtoll(n[i].value, NULL, 10);
1097 171 : GDK_vm_maxsize = MAX(1 << 30, GDK_vm_maxsize);
1098 2814 : } 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 2814 : } 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 2814 : } 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 330 : GDKkey = COLnew(0, TYPE_str, 100, TRANSIENT);
1118 330 : GDKval = COLnew(0, TYPE_str, 100, TRANSIENT);
1119 330 : 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 660 : if (BBPrename(GDKkey, "environment_key") != 0 ||
1125 330 : 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 330 : BBP_pid(GDKkey->batCacheid) = 0;
1131 330 : BBP_pid(GDKval->batCacheid) = 0;
1132 :
1133 : /* store options into environment BATs */
1134 3315 : for (i = 0; i < nlen; i++)
1135 2985 : 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 330 : free(n);
1141 :
1142 330 : GDKnr_threads = GDKgetenv_int("gdk_nr_threads", 0);
1143 330 : if (GDKnr_threads == 0) {
1144 329 : GDKnr_threads = MT_check_nr_cores();
1145 329 : snprintf(buf, sizeof(buf), "%d", GDKnr_threads);
1146 329 : 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 330 : if (GDKnr_threads > THREADS)
1152 0 : GDKnr_threads = THREADS;
1153 :
1154 330 : if (!GDKinmemory(0)) {
1155 329 : if ((p = GDKgetenv("gdk_dbpath")) != NULL &&
1156 329 : (p = strrchr(p, DIR_SEP)) != NULL) {
1157 329 : 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 330 : if (GDKgetenv("gdk_vm_maxsize") == NULL) {
1177 159 : snprintf(buf, sizeof(buf), "%zu", GDK_vm_maxsize);
1178 159 : 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 330 : if (GDKgetenv("gdk_mem_maxsize") == NULL) {
1184 330 : snprintf(buf, sizeof(buf), "%zu", GDK_mem_maxsize);
1185 330 : 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 330 : if (GDKgetenv("gdk_mmap_minsize_persistent") == NULL) {
1191 330 : snprintf(buf, sizeof(buf), "%zu", GDK_mmap_minsize_persistent);
1192 330 : 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 330 : if (GDKgetenv("gdk_mmap_minsize_transient") == NULL) {
1198 330 : snprintf(buf, sizeof(buf), "%zu", GDK_mmap_minsize_transient);
1199 330 : 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 330 : if (GDKgetenv("gdk_mmap_pagesize") == NULL) {
1205 330 : snprintf(buf, sizeof(buf), "%zu", GDK_mmap_pagesize);
1206 330 : 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 330 : if (GDKgetenv("monet_pid") == NULL) {
1212 330 : snprintf(buf, sizeof(buf), "%d", (int) getpid());
1213 330 : if (GDKsetenv("monet_pid", buf) != GDK_SUCCEED) {
1214 0 : TRC_CRITICAL(GDK, "GDKsetenv monet_pid failed");
1215 0 : return GDK_FAIL;
1216 : }
1217 : }
1218 330 : if (GDKsetenv("revision", mercurial_revision()) != GDK_SUCCEED) {
1219 0 : TRC_CRITICAL(GDK, "GDKsetenv revision failed");
1220 0 : return GDK_FAIL;
1221 : }
1222 330 : gdk_unique_estimate_keep_fraction = 0;
1223 330 : if ((p = GDKgetenv("gdk_unique_estimate_keep_fraction")) != NULL)
1224 0 : gdk_unique_estimate_keep_fraction = (BUN) strtoll(p, NULL, 10);
1225 330 : if (gdk_unique_estimate_keep_fraction == 0)
1226 330 : gdk_unique_estimate_keep_fraction = GDK_UNIQUE_ESTIMATE_KEEP_FRACTION;
1227 330 : hash_destroy_uniques_fraction = 0;
1228 330 : if ((p = GDKgetenv("hash_destroy_uniques_fraction")) != NULL)
1229 0 : hash_destroy_uniques_fraction = (BUN) strtoll(p, NULL, 10);
1230 330 : if (hash_destroy_uniques_fraction == 0)
1231 330 : hash_destroy_uniques_fraction = HASH_DESTROY_UNIQUES_FRACTION;
1232 330 : no_hash_select_fraction = 0;
1233 330 : if ((p = GDKgetenv("no_hash_select_fraction")) != NULL)
1234 0 : no_hash_select_fraction = (dbl) strtoll(p, NULL, 10);
1235 330 : if (no_hash_select_fraction == 0)
1236 330 : no_hash_select_fraction = NO_HASH_SELECT_FRACTION;
1237 330 : hash_destroy_chain_length = 0;
1238 330 : if ((p = GDKgetenv("hash_destroy_chain_length")) != NULL)
1239 0 : hash_destroy_chain_length = (BUN) strtoll(p, NULL, 10);
1240 330 : if (hash_destroy_chain_length == 0)
1241 330 : 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 18512175 : GDKexiting(void)
1251 : {
1252 18512175 : return (bool) (ATOMIC_GET(&GDKstopped) > 0);
1253 : }
1254 :
1255 : void
1256 329 : GDKprepareExit(void)
1257 : {
1258 329 : ATOMIC_ADD(&GDKstopped, 1);
1259 :
1260 329 : if (MT_getpid() == mainpid) {
1261 328 : TRC_DEBUG_IF(THRD)
1262 0 : dump_threads();
1263 328 : join_detached_threads();
1264 : }
1265 329 : }
1266 :
1267 : void
1268 328 : GDKreset(int status)
1269 : {
1270 328 : assert(GDKexiting());
1271 :
1272 328 : 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 328 : if (GDKkey) {
1277 328 : BBPunfix(GDKkey->batCacheid);
1278 328 : GDKkey = NULL;
1279 : }
1280 328 : if (GDKval) {
1281 328 : BBPunfix(GDKval->batCacheid);
1282 328 : GDKval = NULL;
1283 : }
1284 :
1285 328 : join_detached_threads();
1286 :
1287 328 : MT_lock_set(&GDKenvlock);
1288 328 : while (orig_value) {
1289 0 : struct orig_value *ov = orig_value;
1290 0 : orig_value = orig_value->next;
1291 0 : GDKfree(ov);
1292 : }
1293 328 : MT_lock_unset(&GDKenvlock);
1294 :
1295 328 : if (status == 0) {
1296 : /* they had their chance, now kill them */
1297 328 : bool killed = MT_kill_threads();
1298 : /* all threads ceased running, now we can clean up */
1299 328 : if (!killed) {
1300 : /* we can't clean up after killing threads */
1301 328 : BBPexit();
1302 : }
1303 328 : GDKlog(GET_GDKLOCK(PERSISTENT), GDKLOGOFF);
1304 :
1305 10824 : for (int farmid = 0; farmid < MAXFARMS; farmid++) {
1306 10496 : if (BBPfarms[farmid].dirname != NULL) {
1307 1932 : bool skip = false;
1308 1932 : for (int j = 0; j < farmid; j++) {
1309 961 : if (BBPfarms[j].dirname != NULL &&
1310 0 : strcmp(BBPfarms[farmid].dirname, BBPfarms[j].dirname) == 0) {
1311 : skip = true;
1312 : break;
1313 : }
1314 : }
1315 971 : if (!skip)
1316 971 : GDKunlockHome(farmid);
1317 971 : if (BBPfarms[farmid].dirname) {
1318 971 : GDKfree((char*)BBPfarms[farmid].dirname);
1319 971 : BBPfarms[farmid].dirname = NULL;
1320 : }
1321 : }
1322 : }
1323 :
1324 : #ifdef LOCK_STATS
1325 : TRC_DEBUG_IF(TEM) GDKlockstatistics(1);
1326 : #endif
1327 328 : ATOMIC_SET(&GDKdebug, 0);
1328 328 : GDK_mmap_minsize_persistent = MMAP_MINSIZE_PERSISTENT;
1329 328 : GDK_mmap_minsize_transient = MMAP_MINSIZE_TRANSIENT;
1330 328 : GDK_mmap_pagesize = MMAP_PAGESIZE;
1331 328 : GDK_mem_maxsize = (size_t) ((double) MT_npages() * (double) MT_pagesize() * 0.815);
1332 328 : GDK_vm_maxsize = GDK_VM_MAXSIZE;
1333 328 : GDKatomcnt = TYPE_blob + 1;
1334 :
1335 328 : if (GDK_mem_maxsize / 16 < GDK_mmap_minsize_transient) {
1336 328 : GDK_mmap_minsize_transient = GDK_mem_maxsize / 16;
1337 328 : if (GDK_mmap_minsize_persistent > GDK_mmap_minsize_transient)
1338 0 : GDK_mmap_minsize_persistent = GDK_mmap_minsize_transient;
1339 : }
1340 :
1341 328 : GDKnr_threads = 0;
1342 328 : ATOMIC_SET(&GDKnrofthreads, 0);
1343 328 : close_stream(GDKstdout);
1344 328 : close_stream(GDKstdin);
1345 328 : GDKstdout = NULL;
1346 328 : GDKstdin = NULL;
1347 :
1348 328 : gdk_bbp_reset();
1349 : }
1350 328 : ATOMunknown_clean();
1351 :
1352 : /* stop GDKtracer */
1353 328 : GDKtracer_stop();
1354 328 : }
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 496 : GDKlockHome(int farmid)
1377 : {
1378 496 : int fd;
1379 496 : struct stat st;
1380 496 : char *gdklockpath;
1381 496 : FILE *GDKlockFile;
1382 :
1383 496 : assert(BBPfarms[farmid].dirname != NULL);
1384 496 : assert(BBPfarms[farmid].lock_file == NULL);
1385 :
1386 496 : if ((gdklockpath = GDKfilepath(farmid, NULL, GDKLOCK, NULL)) == NULL) {
1387 : return GDK_FAIL;
1388 : }
1389 :
1390 : /*
1391 : * Obtain the global database lock.
1392 : */
1393 496 : 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 496 : 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 494 : 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 494 : 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 494 : 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 494 : 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 494 : GDKlog(GDKlockFile, GDKLOGON);
1439 494 : GDKfree(gdklockpath);
1440 494 : BBPfarms[farmid].lock_file = GDKlockFile;
1441 494 : return GDK_SUCCEED;
1442 : }
1443 :
1444 :
1445 : static void
1446 971 : GDKunlockHome(int farmid)
1447 : {
1448 971 : if (BBPfarms[farmid].lock_file) {
1449 492 : char *gdklockpath = GDKfilepath(farmid, NULL, GDKLOCK, NULL);
1450 492 : if (gdklockpath)
1451 492 : MT_lockf(gdklockpath, F_ULOCK);
1452 492 : fclose(BBPfarms[farmid].lock_file);
1453 492 : BBPfarms[farmid].lock_file = NULL;
1454 492 : GDKfree(gdklockpath);
1455 : }
1456 971 : }
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 9456944 : GDKclrerr(void)
1477 : {
1478 9456944 : char *buf;
1479 :
1480 9456944 : buf = GDKerrbuf;
1481 9473729 : if (buf)
1482 9465721 : *buf = 0;
1483 9473729 : }
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 157970970 : 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 157970970 : struct timespec ts;
1563 157970970 : (void) clock_gettime(CLOCK_REALTIME, &ts);
1564 158277989 : 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 332 : THRinit(void)
1606 : {
1607 332 : if ((GDKstdout = stdout_wastream()) == NULL) {
1608 0 : TRC_CRITICAL(GDK, "malloc for stdout failed\n");
1609 0 : return -1;
1610 : }
1611 332 : 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 332 : struct freebats *t = MT_thread_getfreebats();
1618 332 : t->freebats = 0;
1619 332 : t->nfreebats = 0;
1620 332 : return 0;
1621 : }
1622 :
1623 : const char *
1624 319 : GDKversion(void)
1625 : {
1626 319 : return MONETDB_VERSION;
1627 : }
1628 :
1629 : const char *
1630 649 : GDKlibversion(void)
1631 : {
1632 649 : return GDK_VERSION;
1633 : }
1634 :
1635 : inline size_t
1636 169169309 : GDKmem_cursize(void)
1637 : {
1638 : /* RAM/swapmem that Monet is really using now */
1639 169169309 : return (size_t) ATOMIC_GET(&GDK_mallocedbytes_estimate);
1640 : }
1641 :
1642 : inline size_t
1643 160788501 : GDKvm_cursize(void)
1644 : {
1645 : /* current Monet VM address space usage */
1646 160788501 : 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 180892543 : GDKmalloc_internal(size_t size, bool clear)
1708 : {
1709 180892543 : void *s;
1710 180892543 : size_t nsize;
1711 :
1712 180892543 : 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 180892543 : nsize = (size + 7) & ~7;
1726 180892543 : if (clear)
1727 30202821 : s = calloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE, 1);
1728 : else
1729 150689722 : s = malloc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
1730 180892543 : 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 180892543 : s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
1735 :
1736 180892543 : 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 180892543 : ((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 180892543 : ((size_t *) s)[-2] = size;
1744 : /* write pattern to help find out-of-bounds writes */
1745 180892543 : memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
1746 : #endif
1747 180892543 : return s;
1748 : }
1749 :
1750 : #undef GDKmalloc
1751 : void *
1752 142955201 : GDKmalloc(size_t size)
1753 : {
1754 142955201 : void *s;
1755 :
1756 142955201 : 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 143217984 : DEADBEEFCHK memset(s, '\xBD', size);
1762 : #endif
1763 : return s;
1764 : }
1765 :
1766 : #undef GDKzalloc
1767 : void *
1768 30204235 : GDKzalloc(size_t size)
1769 : {
1770 30204235 : return GDKmalloc_internal(size, true);
1771 : }
1772 :
1773 : #undef GDKstrdup
1774 : char *
1775 8465698 : GDKstrdup(const char *s)
1776 : {
1777 8465698 : size_t size;
1778 8465698 : char *p;
1779 :
1780 8465698 : if (s == NULL)
1781 : return NULL;
1782 7827601 : size = strlen(s) + 1;
1783 :
1784 7827601 : if ((p = GDKmalloc_internal(size, false)) == NULL)
1785 : return NULL;
1786 7828496 : memcpy(p, s, size); /* including terminating NULL byte */
1787 7828496 : 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 339107438 : GDKfree(void *s)
1809 : {
1810 339107438 : size_t asize;
1811 :
1812 339107438 : if (s == NULL)
1813 : return;
1814 :
1815 180744301 : asize = ((size_t *) s)[-1]; /* how much allocated last */
1816 :
1817 : #if !defined(NDEBUG) && !defined(SANITIZER)
1818 180744301 : size_t *p = s;
1819 180744301 : assert((asize & 2) == 0); /* check against duplicate free */
1820 180744301 : size_t size = p[-2];
1821 180744301 : assert(((size + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
1822 : /* check for out-of-bounds writes */
1823 3483385615 : for (size_t i = size; i < asize - MALLOC_EXTRA_SPACE; i++)
1824 3302641314 : assert(((char *) s)[i] == '\xBD');
1825 180744301 : 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 180744301 : DEADBEEFCHK memset(s, '\xDB', asize - MALLOC_EXTRA_SPACE);
1831 : #endif
1832 :
1833 180744301 : free((char *) s - MALLOC_EXTRA_SPACE);
1834 180744301 : heapdec((ssize_t) asize);
1835 : }
1836 :
1837 : #undef GDKrealloc
1838 : void *
1839 303262 : GDKrealloc(void *s, size_t size)
1840 : {
1841 303262 : size_t nsize, asize;
1842 : #if !defined(NDEBUG) && !defined(SANITIZER)
1843 303262 : size_t osize;
1844 : #endif
1845 303262 : size_t *os = s;
1846 :
1847 303262 : assert(size != 0);
1848 :
1849 303262 : if (s == NULL)
1850 933 : return GDKmalloc(size);
1851 :
1852 302329 : nsize = (size + 7) & ~7;
1853 302329 : 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 302329 : assert((asize & 2) == 0); /* check against duplicate free */
1866 : /* check for out-of-bounds writes */
1867 302329 : osize = os[-2]; /* how much asked for last */
1868 302329 : assert(((osize + 7) & ~7) + MALLOC_EXTRA_SPACE + DEBUG_SPACE == asize);
1869 5206911 : for (size_t i = osize; i < asize - MALLOC_EXTRA_SPACE; i++)
1870 4904582 : assert(((char *) s)[i] == '\xBD');
1871 : /* if shrinking, write debug pattern into to-be-freed memory */
1872 302329 : DEADBEEFCHK if (size < osize)
1873 42720 : memset((char *) s + size, '\xDB', osize - size);
1874 302329 : os[-1] |= 2; /* indicate area is freed */
1875 : #endif
1876 302329 : s = realloc((char *) s - MALLOC_EXTRA_SPACE,
1877 : nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
1878 302329 : 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 302329 : s = (void *) ((char *) s + MALLOC_EXTRA_SPACE);
1888 : /* just before the pointer that we return, write how much we
1889 : * asked of malloc */
1890 302329 : ((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 302329 : ((size_t *) s)[-2] = size;
1894 : /* if growing, initialize new memory with debug pattern */
1895 302329 : DEADBEEFCHK if (size > osize)
1896 259563 : memset((char *) s + osize, '\xBD', size - osize);
1897 : /* write pattern to help find out-of-bounds writes */
1898 302329 : memset((char *) s + size, '\xBD', nsize + DEBUG_SPACE - size);
1899 : #endif
1900 :
1901 302329 : heapinc(nsize + MALLOC_EXTRA_SPACE + DEBUG_SPACE);
1902 302329 : 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 192014 : GDKmallocated(const void *s)
1912 : {
1913 192014 : 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 2470 : GDKmmap(const char *path, int mode, size_t len)
1923 : {
1924 2470 : 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 2470 : ret = MT_mmap(path, mode, len);
1934 2499 : if (ret != NULL) {
1935 2499 : if (mode & MMAP_COPY)
1936 0 : heapinc(len);
1937 : else
1938 2499 : 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 2499 : return ret;
1942 : }
1943 :
1944 : #undef GDKmunmap
1945 : gdk_return
1946 2496 : GDKmunmap(void *addr, int mode, size_t size)
1947 : {
1948 2496 : int ret;
1949 :
1950 2496 : ret = MT_munmap(addr, size);
1951 2499 : if (ret == 0) {
1952 2499 : if (mode & MMAP_COPY)
1953 0 : heapdec(size);
1954 : else
1955 2499 : memdec(size);
1956 : }
1957 2499 : return ret == 0 ? GDK_SUCCEED : GDK_FAIL;
1958 : }
1959 :
1960 : #undef GDKmremap
1961 : void *
1962 465 : GDKmremap(const char *path, int mode, void *old_address, size_t old_size, size_t *new_size)
1963 : {
1964 465 : 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 465 : ret = MT_mremap(path, mode, old_address, old_size, new_size);
1975 465 : if (ret != NULL) {
1976 465 : if (mode & MMAP_COPY) {
1977 0 : heapdec(old_size);
1978 0 : heapinc(*new_size);
1979 : } else {
1980 465 : memdec(old_size);
1981 465 : 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 465 : 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 329 : GDKprintinforegister(void (*func)(void))
1997 : {
1998 329 : struct prinfocb *p = GDKmalloc(sizeof(struct prinfocb));
1999 329 : if (p == NULL) {
2000 0 : GDKerror("cannot register USR1 printing function.\n");
2001 0 : return;
2002 : }
2003 329 : p->func = func;
2004 329 : p->next = NULL;
2005 329 : struct prinfocb **pp = &prinfocb;
2006 384 : while (*pp != NULL)
2007 55 : pp = &(*pp)->next;
2008 329 : *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 685797 : eb_init(exception_buffer *eb)
2056 : {
2057 685797 : if (eb) {
2058 685797 : eb->enabled = 0;
2059 685797 : eb->code = 0;
2060 685797 : eb->msg = NULL;
2061 : }
2062 685797 : 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 71139 : sa_destroy_freelist( freed_t *f )
2087 : {
2088 79469 : while(f) {
2089 8330 : freed_t *n = f->n;
2090 8330 : GDKfree(f);
2091 8330 : f = n;
2092 : }
2093 : }
2094 :
2095 : static void
2096 192014 : sa_free(allocator *pa, void *blk)
2097 : {
2098 192014 : assert(!pa->pa);
2099 : size_t i;
2100 :
2101 47955329 : for(i = 0; i < pa->nr; i++) {
2102 47955329 : if (pa->blks[i] == blk)
2103 : break;
2104 : }
2105 192014 : assert (i < pa->nr);
2106 1996263 : for (; i < pa->nr-1; i++)
2107 1804249 : pa->blks[i] = pa->blks[i+1];
2108 192014 : pa->nr--;
2109 :
2110 192014 : size_t sz = GDKmallocated(blk);
2111 192014 : if (sz > (SA_BLOCK + 32)) {
2112 90 : GDKfree(blk);
2113 : } else {
2114 191924 : freed_t *f = blk;
2115 191924 : f->n = pa->freelist;
2116 191924 : f->sz = sz;
2117 :
2118 191924 : pa->freelist = f;
2119 : }
2120 192014 : }
2121 :
2122 : static void *
2123 183594 : sa_use_freed(allocator *pa, size_t sz)
2124 : {
2125 183594 : (void)sz;
2126 :
2127 183594 : freed_t *f = pa->freelist;
2128 183594 : pa->freelist = f->n;
2129 183594 : return f;
2130 : }
2131 :
2132 : allocator *
2133 280100 : sa_create(allocator *pa)
2134 : {
2135 280100 : allocator *sa = (pa)?(allocator*)sa_alloc(pa, sizeof(allocator)):(allocator*)GDKmalloc(sizeof(allocator));
2136 280101 : if (sa == NULL)
2137 : return NULL;
2138 280101 : eb_init(&sa->eb);
2139 280099 : sa->pa = pa;
2140 280099 : sa->size = 64;
2141 280099 : sa->nr = 1;
2142 280099 : sa->blks = pa?(char**)sa_alloc(pa, sizeof(char*) * sa->size):(char**)GDKmalloc(sizeof(char*) * sa->size);
2143 280098 : sa->freelist = NULL;
2144 280098 : if (sa->blks == NULL) {
2145 0 : if (!pa)
2146 0 : GDKfree(sa);
2147 0 : return NULL;
2148 : }
2149 280098 : sa->blks[0] = pa?(char*)sa_alloc(pa, SA_BLOCK):(char*)GDKmalloc(SA_BLOCK);
2150 280084 : sa->usedmem = SA_BLOCK;
2151 280084 : 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 280084 : sa->used = 0;
2159 280084 : return sa;
2160 : }
2161 :
2162 1901076 : allocator *sa_reset( allocator *sa )
2163 : {
2164 1901076 : size_t i ;
2165 :
2166 1961580 : for (i = 1; i<sa->nr; i++) {
2167 60251 : if (!sa->pa)
2168 0 : GDKfree(sa->blks[i]);
2169 : else
2170 60251 : sa_free(sa->pa, sa->blks[i]);
2171 : }
2172 1901329 : sa->nr = 1;
2173 1901329 : sa->used = 0;
2174 1901329 : sa->usedmem = SA_BLOCK;
2175 1901329 : 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 242231588 : sa_alloc( allocator *sa, size_t sz )
2193 : {
2194 242231588 : char *r;
2195 242231588 : sz = round16(sz);
2196 242231588 : if (sz > (SA_BLOCK-sa->used)) {
2197 517489 : if (sa->pa)
2198 60479 : r = (char*)sa_alloc(sa->pa, (sz > SA_BLOCK ? sz : SA_BLOCK));
2199 457010 : else if (sz <= SA_BLOCK && sa->freelist) {
2200 183594 : r = sa_use_freed(sa, SA_BLOCK);
2201 : } else
2202 273416 : r = GDKmalloc(sz > SA_BLOCK ? sz : SA_BLOCK);
2203 517462 : if (r == NULL) {
2204 0 : if (sa->eb.enabled)
2205 0 : eb_error(&sa->eb, "out of memory", 1000);
2206 : return NULL;
2207 : }
2208 517462 : if (sa->nr >= sa->size) {
2209 947 : char **tmp;
2210 947 : size_t osz = sa->size;
2211 947 : sa->size *=2;
2212 947 : if (sa->pa)
2213 19 : tmp = (char**)sa_realloc(sa->pa, sa->blks, sizeof(char*) * sa->size, sizeof(char*) * osz);
2214 : else
2215 928 : tmp = GDKrealloc(sa->blks, sizeof(char*) * sa->size);
2216 947 : 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 947 : sa->blks = tmp;
2225 : }
2226 517462 : if (sz > SA_BLOCK) {
2227 525 : sa->blks[sa->nr] = sa->blks[sa->nr-1];
2228 525 : sa->blks[sa->nr-1] = r;
2229 525 : sa->nr ++;
2230 525 : sa->usedmem += sz;
2231 : } else {
2232 516937 : sa->blks[sa->nr] = r;
2233 516937 : sa->nr ++;
2234 516937 : sa->used = sz;
2235 516937 : sa->usedmem += SA_BLOCK;
2236 : }
2237 : } else {
2238 241714099 : r = sa->blks[sa->nr-1] + sa->used;
2239 241714099 : sa->used += sz;
2240 : }
2241 : return r;
2242 : }
2243 :
2244 : #undef sa_zalloc
2245 10293238 : void *sa_zalloc( allocator *sa, size_t sz )
2246 : {
2247 10293238 : void *r = sa_alloc(sa, sz);
2248 :
2249 10293308 : if (r)
2250 10293308 : memset(r, 0, sz);
2251 10293308 : return r;
2252 : }
2253 :
2254 202902 : void sa_destroy( allocator *sa )
2255 : {
2256 202902 : if (sa->pa) {
2257 131763 : sa_reset(sa);
2258 131763 : sa_free(sa->pa, sa->blks[0]);
2259 131763 : return;
2260 : }
2261 :
2262 71139 : sa_destroy_freelist(sa->freelist);
2263 407278 : for (size_t i = 0; i<sa->nr; i++) {
2264 336139 : GDKfree(sa->blks[i]);
2265 : }
2266 71139 : GDKfree(sa->blks);
2267 71139 : GDKfree(sa);
2268 : }
2269 :
2270 : #undef sa_strndup
2271 13281777 : char *sa_strndup( allocator *sa, const char *s, size_t l)
2272 : {
2273 13281777 : char *r = sa_alloc(sa, l+1);
2274 :
2275 13281431 : if (r) {
2276 13281431 : memcpy(r, s, l);
2277 13281431 : r[l] = 0;
2278 : }
2279 13281431 : return r;
2280 : }
2281 :
2282 : #undef sa_strdup
2283 7916518 : char *sa_strdup( allocator *sa, const char *s )
2284 : {
2285 7916518 : return sa_strndup( sa, s, strlen(s));
2286 : }
2287 :
2288 42691 : char *sa_strconcat( allocator *sa, const char *s1, const char *s2 )
2289 : {
2290 42691 : size_t l1 = strlen(s1);
2291 42691 : size_t l2 = strlen(s2);
2292 42691 : char *r = sa_alloc(sa, l1+l2+1);
2293 :
2294 42692 : if (l1)
2295 42690 : memcpy(r, s1, l1);
2296 42692 : if (l2)
2297 42692 : memcpy(r+l1, s2, l2);
2298 42692 : r[l1+l2] = 0;
2299 42692 : return r;
2300 : }
2301 :
2302 0 : size_t sa_size( allocator *sa )
2303 : {
2304 0 : return sa->usedmem;
2305 : }
|