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, 2025 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #ifndef _GDK_ATOMS_H_
14 : #define _GDK_ATOMS_H_
15 :
16 : /* atomFromStr returns the number of bytes of the input string that
17 : * were processed. atomToStr returns the length of the string
18 : * produced. Both functions return -1 on (any kind of) failure. If
19 : * *dst is not NULL, *len specifies the available space. If there is
20 : * not enough space, or if *dst is NULL, *dst will be freed (if not
21 : * NULL) and a new buffer will be allocated and returned in *dst.
22 : * *len will be set to reflect the actual size allocated. If
23 : * allocation fails, *dst will be NULL on return and *len is
24 : * undefined. In any case, if the function returns, *buf is either
25 : * NULL or a valid pointer and then *len is the size of the area *buf
26 : * points to.
27 : *
28 : * atomCmp returns a value less than zero/equal to zero/greater than
29 : * zer if the first argument points to a values which is deemed
30 : * smaller/equal to/larger than the value pointed to by the second
31 : * argument.
32 : *
33 : * atomHash calculates a hash function for the value pointed to by the
34 : * argument.
35 : */
36 :
37 : #define IDLENGTH 64 /* maximum BAT id length */
38 :
39 : typedef struct {
40 : /* simple attributes */
41 : char name[IDLENGTH];
42 : uint8_t storage; /* stored as another type? */
43 : bool linear; /* atom can be ordered linearly */
44 : uint16_t size; /* fixed size of atom */
45 :
46 : /* automatically generated fields */
47 : const void *atomNull; /* global nil value */
48 :
49 : /* generic (fixed + varsized atom) ADT functions */
50 : ssize_t (*atomFromStr) (const char *src, size_t *len, void **dst, bool external);
51 : ssize_t (*atomToStr) (char **dst, size_t *len, const void *src, bool external);
52 : void *(*atomRead) (void *dst, size_t *dstlen, stream *s, size_t cnt);
53 : gdk_return (*atomWrite) (const void *src, stream *s, size_t cnt);
54 : int (*atomCmp) (const void *v1, const void *v2);
55 : BUN (*atomHash) (const void *v);
56 :
57 : /* varsized atom-only ADT functions */
58 : var_t (*atomPut) (BAT *, var_t *off, const void *src);
59 : void (*atomDel) (Heap *, var_t *atom);
60 : size_t (*atomLen) (const void *atom);
61 : gdk_return (*atomHeap) (Heap *, size_t);
62 : } atomDesc;
63 :
64 : #define MAXATOMS 128
65 :
66 : gdk_export atomDesc BATatoms[MAXATOMS];
67 : gdk_export int GDKatomcnt;
68 :
69 : gdk_export int ATOMallocate(const char *nme);
70 : gdk_export int ATOMindex(const char *nme);
71 :
72 : gdk_export const char *ATOMname(int id);
73 : gdk_export size_t ATOMlen(int id, const void *v);
74 : gdk_export void *ATOMnil(int id)
75 : __attribute__((__malloc__));
76 : gdk_export int ATOMprint(int id, const void *val, stream *fd);
77 : gdk_export char *ATOMformat(int id, const void *val)
78 : __attribute__((__warn_unused_result__));
79 :
80 : gdk_export void *ATOMdup(int id, const void *val);
81 :
82 : /*
83 : * @- maximum atomic string lengths
84 : */
85 : #define bitStrlen 8
86 : #define bteStrlen 8
87 : #define shtStrlen 12
88 : #define intStrlen 24
89 : #if SIZEOF_OID == SIZEOF_INT
90 : #define oidStrlen 24
91 : #else
92 : #define oidStrlen 48
93 : #endif
94 : #if SIZEOF_PTR == SIZEOF_INT
95 : #define ptrStrlen 24
96 : #else
97 : #define ptrStrlen 48
98 : #endif
99 : #define lngStrlen 48
100 : #ifdef HAVE_HGE
101 : #define hgeStrlen 96
102 : #endif
103 : #define fltStrlen 48
104 : #define dblStrlen 96
105 :
106 : /*
107 : * The system comes with the traditional atomic types: int (4 bytes),
108 : * bool(1 byte) and str (variable). In addition, we support the notion
109 : * of an OID type, which ensures uniqueness of its members. This
110 : * leads to the following type descriptor table.
111 : */
112 :
113 : #ifdef HAVE_HGE
114 : gdk_export ssize_t hgeFromStr(const char *src, size_t *len, hge **dst, bool external);
115 : gdk_export ssize_t hgeToStr(str *dst, size_t *len, const hge *src, bool external);
116 : #endif
117 : gdk_export ssize_t lngFromStr(const char *src, size_t *len, lng **dst, bool external);
118 : gdk_export ssize_t lngToStr(str *dst, size_t *len, const lng *src, bool external);
119 : gdk_export ssize_t intFromStr(const char *src, size_t *len, int **dst, bool external);
120 : gdk_export ssize_t intToStr(str *dst, size_t *len, const int *src, bool external);
121 : gdk_export ssize_t ptrFromStr(const char *src, size_t *len, ptr **dst, bool external);
122 : gdk_export ssize_t ptrToStr(str *dst, size_t *len, const ptr *src, bool external);
123 : gdk_export ssize_t bitFromStr(const char *src, size_t *len, bit **dst, bool external);
124 : gdk_export ssize_t bitToStr(str *dst, size_t *len, const bit *src, bool external);
125 : gdk_export ssize_t OIDfromStr(const char *src, size_t *len, oid **dst, bool external);
126 : gdk_export ssize_t OIDtoStr(str *dst, size_t *len, const oid *src, bool external);
127 : gdk_export ssize_t shtFromStr(const char *src, size_t *len, sht **dst, bool external);
128 : gdk_export ssize_t shtToStr(str *dst, size_t *len, const sht *src, bool external);
129 : gdk_export ssize_t bteFromStr(const char *src, size_t *len, bte **dst, bool external);
130 : gdk_export ssize_t bteToStr(str *dst, size_t *len, const bte *src, bool external);
131 : gdk_export ssize_t fltFromStr(const char *src, size_t *len, flt **dst, bool external);
132 : gdk_export ssize_t fltToStr(str *dst, size_t *len, const flt *src, bool external);
133 : gdk_export ssize_t dblFromStr(const char *src, size_t *len, dbl **dst, bool external);
134 : gdk_export ssize_t dblToStr(str *dst, size_t *len, const dbl *src, bool external);
135 : gdk_export ssize_t GDKstrFromStr(unsigned char *restrict dst, const unsigned char *restrict src, ssize_t len, char quote);
136 : gdk_export ssize_t strFromStr(const char *restrict src, size_t *restrict len, str *restrict dst, bool external);
137 : gdk_export size_t escapedStrlen(const char *restrict src, const char *sep1, const char *sep2, int quote);
138 : gdk_export size_t escapedStr(char *restrict dst, const char *restrict src, size_t dstlen, const char *sep1, const char *sep2, int quote);
139 : /*
140 : * @- nil values
141 : * All types have a single value designated as a NIL value. It
142 : * designates a missing value and it is ignored (forbidden) in several
143 : * primitives. The current policy is to use the smallest value in any
144 : * ordered domain. The routine atomnil returns a pointer to the nil
145 : * value representation.
146 : */
147 : #define GDK_bit_max ((bit) 1)
148 : #define GDK_bit_min ((bit) 0)
149 : #define GDK_bte_max ((bte) INT8_MAX)
150 : #define GDK_bte_min ((bte) INT8_MIN+1)
151 : #define GDK_sht_max ((sht) INT16_MAX)
152 : #define GDK_sht_min ((sht) INT16_MIN+1)
153 : #define GDK_int_max ((int) INT32_MAX)
154 : #define GDK_int_min ((int) INT32_MIN+1)
155 : #define GDK_lng_max ((lng) INT64_MAX)
156 : #define GDK_lng_min ((lng) INT64_MIN+1)
157 : #ifdef HAVE_HGE
158 : #define GDK_hge_max ((((hge) 1) << 126) - 1 + (((hge) 1) << 126))
159 : #define GDK_hge_min (-GDK_hge_max)
160 : #endif
161 : #define GDK_flt_max ((flt) FLT_MAX)
162 : #define GDK_flt_min ((flt) -FLT_MAX)
163 : #define GDK_dbl_max ((dbl) DBL_MAX)
164 : #define GDK_dbl_min ((dbl) -DBL_MAX)
165 : #define GDK_oid_max (((oid) 1 << ((8 * SIZEOF_OID) - 1)) - 1)
166 : #define GDK_oid_min ((oid) 0)
167 : /* representation of the nil */
168 : gdk_export const bte bte_nil;
169 : gdk_export const sht sht_nil;
170 : gdk_export const int int_nil;
171 : #ifdef NAN_CANNOT_BE_USED_AS_INITIALIZER
172 : /* Definition of NAN is seriously broken on Intel compiler (at least
173 : * in some versions), so we work around it. */
174 : union _flt_nil_t {
175 : uint32_t l;
176 : flt f;
177 : };
178 : gdk_export const union _flt_nil_t _flt_nil_;
179 : #define flt_nil (_flt_nil_.f)
180 : union _dbl_nil_t {
181 : uint64_t l;
182 : dbl d;
183 : };
184 : gdk_export const union _dbl_nil_t _dbl_nil_;
185 : #define dbl_nil (_dbl_nil_.d)
186 : #else
187 : gdk_export const flt flt_nil;
188 : gdk_export const dbl dbl_nil;
189 : #endif
190 : gdk_export const lng lng_nil;
191 : #ifdef HAVE_HGE
192 : gdk_export const hge hge_nil;
193 : #endif
194 : gdk_export const oid oid_nil;
195 : gdk_export const char str_nil[2];
196 : gdk_export const ptr ptr_nil;
197 : gdk_export const uuid uuid_nil;
198 :
199 : /* derived NIL values - OIDDEPEND */
200 : #define bit_nil ((bit) bte_nil)
201 : #define bat_nil ((bat) int_nil)
202 :
203 : #define void_nil oid_nil
204 :
205 : #define is_bit_nil(v) ((v) == GDK_bte_min-1)
206 : #define is_bte_nil(v) ((v) == GDK_bte_min-1)
207 : #define is_sht_nil(v) ((v) == GDK_sht_min-1)
208 : #define is_int_nil(v) ((v) == GDK_int_min-1)
209 : #define is_lng_nil(v) ((v) == GDK_lng_min-1)
210 : #ifdef HAVE_HGE
211 : #define is_hge_nil(v) ((v) == GDK_hge_min-1)
212 : #endif
213 : #define is_oid_nil(v) ((v) == ((oid) 1 << ((8 * SIZEOF_OID) - 1)))
214 : #define is_flt_nil(v) isnan(v)
215 : #define is_dbl_nil(v) isnan(v)
216 : #define is_bat_nil(v) (((v) & 0x7FFFFFFF) == 0) /* v == bat_nil || v == 0 */
217 :
218 : #include <math.h>
219 :
220 : #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && _MSC_VER < 1800
221 : #include <float.h>
222 : #define isnan(x) _isnan(x)
223 : #define isinf(x) (_fpclass(x) & (_FPCLASS_NINF | _FPCLASS_PINF))
224 : #define isfinite(x) _finite(x)
225 : #endif
226 :
227 : #ifdef HAVE_HGE
228 : #define is_uuid_nil(x) ((x).h == 0)
229 : #else
230 : #define is_uuid_nil(x) (memcmp((x).u, uuid_nil.u, UUID_SIZE) == 0)
231 : #endif
232 :
233 : #define is_blob_nil(x) ((x)->nitems == ~(size_t)0)
234 :
235 : /*
236 : * @- Derived types
237 : * In all algorithms across GDK, you will find switches on the types
238 : * (bte, sht, int, flt, dbl, lng, hge, str). They respectively
239 : * represent an octet, a 16-bit int, a 32-bit int, a 32-bit float, a
240 : * 64-bit double, a 64-bit int, a 128-bit int, and a pointer-sized location
241 : * of a char-buffer (ended by a zero char).
242 : *
243 : * In contrast, the types (bit, ptr, bat, oid) are derived types. They
244 : * do not occur in the switches. The ATOMstorage macro maps them
245 : * respectively onto a @code{ bte}, @code{ int} (pointers are 32-bit),
246 : * @code{ int}, and @code{ int}. OIDs are 32-bit.
247 : *
248 : * This approach makes it tractable to switch to 64-bits OIDs, or to a
249 : * fully 64-bits OS easily. One only has to map the @code{ oid} and
250 : * @code{ ptr} types to @code{ lng} instead of @code{ int}.
251 : *
252 : * Derived types mimic their fathers in many ways. They inherit the
253 : * @code{ size}, @code{ linear}, and @code{ null}
254 : * properties of their father. The same goes for the
255 : * ADT functions HASH, CMP, PUT, NULL, DEL, LEN, and HEAP. So, a
256 : * derived type differs in only two ways from its father:
257 : * @table @code
258 : * @item [string representation]
259 : * the only two ADT operations specific for a derived type are FROMSTR
260 : * and TOSTR.
261 : * @item [identity]
262 : * (a @code{ bit} is really of a different type than @code{ bte}). The
263 : * set of operations on derived type values or BATs of such types may
264 : * differ from the sets of operations on the father type.
265 : * @end table
266 : */
267 : /* use "do ... while(0)" so that lhs can safely be used in if statements */
268 : #define ATOMstorage(t) BATatoms[t].storage
269 : #define ATOMsize(t) BATatoms[t].size
270 : #define ATOMfromstr(t,s,l,src,ext) BATatoms[t].atomFromStr(src,l,s,ext)
271 : #define ATOMnilptr(t) BATatoms[t].atomNull
272 : #define ATOMcompare(t) BATatoms[t].atomCmp
273 : #define ATOMcmp(t,l,r) ((*ATOMcompare(t))(l, r))
274 : #define ATOMhash(t,src) BATatoms[t].atomHash(src)
275 : #define ATOMdel(t,hp,src) do if (BATatoms[t].atomDel) BATatoms[t].atomDel(hp,src); while (0)
276 : #define ATOMvarsized(t) (BATatoms[t].atomPut != NULL)
277 : #define ATOMlinear(t) BATatoms[t].linear
278 : #define ATOMtype(t) ((t) == TYPE_void ? TYPE_oid : (t))
279 :
280 : /* The base type is the storage type if the comparison function, the
281 : * hash function, and the nil value are the same as those of the
282 : * storage type; otherwise it is the type itself. */
283 : #define ATOMbasetype(t) ((t) != ATOMstorage(t) && \
284 : ATOMnilptr(t) == ATOMnilptr(ATOMstorage(t)) && \
285 : ATOMcompare(t) == ATOMcompare(ATOMstorage(t)) && \
286 : BATatoms[t].atomHash == BATatoms[ATOMstorage(t)].atomHash ? \
287 : ATOMstorage(t) : (t))
288 :
289 : /*
290 : * In case that atoms are added to a bat, their logical reference
291 : * count should be incremented (and decremented if deleted). Notice
292 : * that BATs with atomic types that have logical references (e.g. BATs
293 : * of BATs but also BATs of ODMG odSet) can never be persistent, as
294 : * this would make the commit tremendously complicated.
295 : */
296 :
297 : __attribute__((__warn_unused_result__))
298 : static inline gdk_return
299 102477141 : ATOMputVAR(BAT *b, var_t *dst, const void *src)
300 : {
301 102477141 : assert(BATatoms[b->ttype].atomPut != NULL);
302 102477141 : if ((*BATatoms[b->ttype].atomPut)(b, dst, src) == (var_t) -1)
303 0 : return GDK_FAIL;
304 : return GDK_SUCCEED;
305 : }
306 :
307 :
308 : __attribute__((__warn_unused_result__))
309 : static inline gdk_return
310 492320816 : ATOMputFIX(int type, void *dst, const void *src)
311 : {
312 492320816 : assert(BATatoms[type].atomPut == NULL);
313 492320816 : switch (ATOMsize(type)) {
314 : case 0: /* void */
315 : break;
316 39970904 : case 1:
317 39970904 : * (bte *) dst = * (bte *) src;
318 39970904 : break;
319 17367521 : case 2:
320 17367521 : * (sht *) dst = * (sht *) src;
321 17367521 : break;
322 389069688 : case 4:
323 389069688 : * (int *) dst = * (int *) src;
324 389069688 : break;
325 29476695 : case 8:
326 29476695 : * (lng *) dst = * (lng *) src;
327 29476695 : break;
328 16436008 : case 16:
329 : #ifdef HAVE_HGE
330 16436008 : * (hge *) dst = * (hge *) src;
331 : #else
332 : * (uuid *) dst = * (uuid *) src;
333 : #endif
334 16436008 : break;
335 0 : default:
336 0 : memcpy(dst, src, ATOMsize(type));
337 0 : break;
338 : }
339 492320816 : return GDK_SUCCEED;
340 : }
341 :
342 : __attribute__((__warn_unused_result__))
343 : static inline gdk_return
344 1143303 : ATOMreplaceVAR(BAT *b, var_t *dst, const void *src)
345 : {
346 1143303 : var_t loc = *dst;
347 1143303 : int type = b->ttype;
348 :
349 1143303 : assert(BATatoms[type].atomPut != NULL);
350 1143303 : if ((*BATatoms[type].atomPut)(b, &loc, src) == (var_t) -1)
351 : return GDK_FAIL;
352 1143273 : ATOMdel(type, b->tvheap, dst);
353 1143323 : *dst = loc;
354 1143323 : return GDK_SUCCEED;
355 : }
356 :
357 : /* string heaps:
358 : * - strings are 8 byte aligned
359 : * - start with a 1024 bucket hash table
360 : * - heaps < 64KiB are fully duplicate eliminated with this hash tables
361 : * - heaps >= 64KiB are opportunistically (imperfect) duplicate
362 : * eliminated as only the last 128KiB chunk is considered and there
363 : * is no linked list
364 : * - buckets and next pointers are unsigned short "indices"
365 : * - indices should be multiplied by 8 and takes from ELIMBASE to get
366 : * an offset
367 : * Note that a 64KiB chunk of the heap contains at most 8K 8-byte
368 : * aligned strings. The 1K bucket list means that in worst load, the
369 : * list length is 8 (OK).
370 : */
371 : #define GDK_STRHASHTABLE (1<<10) /* 1024 */
372 : #define GDK_STRHASHMASK (GDK_STRHASHTABLE-1)
373 : #define GDK_STRHASHSIZE (GDK_STRHASHTABLE * sizeof(stridx_t))
374 : #define GDK_ELIMPOWER 16 /* 64KiB is the threshold */
375 : #define GDK_ELIMDOUBLES(h) ((h)->free < GDK_ELIMLIMIT)
376 : #define GDK_ELIMLIMIT (1<<GDK_ELIMPOWER) /* equivalently: ELIMBASE == 0 */
377 : #define GDK_ELIMBASE(x) (((x) >> GDK_ELIMPOWER) << GDK_ELIMPOWER)
378 : #define GDK_VAROFFSET ((var_t) GDK_STRHASHSIZE)
379 :
380 : /*
381 : * @- String Comparison, NILs and UTF-8
382 : *
383 : * Using the char* type for strings is handy as this is the type of
384 : * any constant strings in a C/C++ program. Therefore, MonetDB uses
385 : * this definition for str. However, different compilers and
386 : * platforms use either signed or unsigned characters for the char
387 : * type. It is required that string ordering in MonetDB is consistent
388 : * over platforms though.
389 : *
390 : * As for the choice how strings should be ordered, our support for
391 : * UTF-8 actually imposes that it should follow 'unsigned char'
392 : * doctrine (like in the AIX native compiler). In this semantics,
393 : * though we have to take corrective action to ensure that str(nil) is
394 : * the smallest value of the domain.
395 : */
396 : __attribute__((__pure__))
397 : static inline bool
398 340041 : strEQ(const char *l, const char *r)
399 : {
400 340041 : return strcmp(l, r) == 0;
401 : }
402 :
403 : __attribute__((__pure__))
404 : static inline bool
405 2640948999 : strNil(const char *s)
406 : {
407 2655827693 : return s == NULL || (s[0] == '\200' && s[1] == '\0');
408 : }
409 :
410 : __attribute__((__pure__))
411 : static inline size_t
412 64755502 : strLen(const char *s)
413 : {
414 128522499 : return strNil(s) ? 2 : strlen(s) + 1;
415 : }
416 :
417 : __attribute__((__pure__))
418 : static inline int
419 833474581 : strCmp(const char *l, const char *r)
420 : {
421 1663712501 : return strNil(r)
422 408953824 : ? !strNil(l)
423 822184898 : : strNil(l) ? -1 : strcmp(l, r);
424 : }
425 :
426 : __attribute__((__pure__))
427 : static inline size_t
428 732188900 : VarHeapVal(const void *b, BUN p, int w)
429 : {
430 732188900 : switch (w) {
431 265867396 : case 1:
432 265867396 : return (size_t) ((const uint8_t *) b)[p] + GDK_VAROFFSET;
433 164956652 : case 2:
434 164956652 : return (size_t) ((const uint16_t *) b)[p] + GDK_VAROFFSET;
435 255161212 : case 4:
436 255161212 : return (size_t) ((const uint32_t *) b)[p];
437 : #if SIZEOF_VAR_T == 8
438 46203640 : case 8:
439 46203640 : return (size_t) ((const uint64_t *) b)[p];
440 : #endif
441 : default:
442 0 : MT_UNREACHABLE();
443 : }
444 : }
445 :
446 : __attribute__((__pure__))
447 : static inline BUN
448 258536205 : strHash(const char *key)
449 : {
450 258536205 : BUN y = 0;
451 :
452 4742727200 : for (BUN i = 0; key[i]; i++) {
453 4484190995 : y += key[i];
454 4484190995 : y += (y << 10);
455 4484190995 : y ^= (y >> 6);
456 : }
457 258536205 : y += (y << 3);
458 258536205 : y ^= (y >> 11);
459 258536205 : y += (y << 15);
460 258536205 : return y;
461 : }
462 :
463 : #endif /* _GDK_ATOMS_H_ */
|