@based/db 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  2. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  3. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  4. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  5. package/dist/lib/linux_aarch64/include/cdefs.h +320 -0
  6. package/dist/lib/linux_aarch64/include/jemalloc.h +468 -0
  7. package/dist/lib/linux_aarch64/include/libdeflate.h +345 -0
  8. package/dist/lib/linux_aarch64/include/libdeflate_strings.h +35 -0
  9. package/dist/lib/linux_aarch64/include/linker_set.h +109 -0
  10. package/dist/lib/linux_aarch64/include/queue.h +627 -0
  11. package/dist/lib/linux_aarch64/include/selva/_export.h +7 -0
  12. package/dist/lib/linux_aarch64/include/selva/align.h +9 -0
  13. package/dist/lib/linux_aarch64/include/selva/backoff_timeout.h +29 -0
  14. package/dist/lib/linux_aarch64/include/selva/bitmap.h +95 -0
  15. package/dist/lib/linux_aarch64/include/selva/colvec.h +71 -0
  16. package/dist/lib/linux_aarch64/include/selva/crc32c.h +17 -0
  17. package/dist/lib/linux_aarch64/include/selva/ctime.h +135 -0
  18. package/dist/lib/linux_aarch64/include/selva/db.h +430 -0
  19. package/dist/lib/linux_aarch64/include/selva/endian.h +301 -0
  20. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +27 -0
  21. package/dist/lib/linux_aarch64/include/selva/fast_memcmp.h +18 -0
  22. package/dist/lib/linux_aarch64/include/selva/fast_memmem.h +11 -0
  23. package/dist/lib/linux_aarch64/include/selva/fast_parsei.h +36 -0
  24. package/dist/lib/linux_aarch64/include/selva/fields.h +379 -0
  25. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  26. package/dist/lib/linux_aarch64/include/selva/hll.h +85 -0
  27. package/dist/lib/linux_aarch64/include/selva/lpf.h +28 -0
  28. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  29. package/dist/lib/linux_aarch64/include/selva/membar.h +32 -0
  30. package/dist/lib/linux_aarch64/include/selva/node_id_set.h +43 -0
  31. package/dist/lib/linux_aarch64/include/selva/poptop.h +114 -0
  32. package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +49 -0
  33. package/dist/lib/linux_aarch64/include/selva/selva_lang.h +105 -0
  34. package/dist/lib/linux_aarch64/include/selva/selva_math.h +37 -0
  35. package/dist/lib/linux_aarch64/include/selva/selva_string.h +683 -0
  36. package/dist/lib/linux_aarch64/include/selva/sort.h +140 -0
  37. package/dist/lib/linux_aarch64/include/selva/strsearch.h +43 -0
  38. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  39. package/dist/lib/linux_aarch64/include/selva/timestamp.h +25 -0
  40. package/dist/lib/linux_aarch64/include/selva/types.h +119 -0
  41. package/dist/lib/linux_aarch64/include/selva/vector.h +35 -0
  42. package/dist/lib/linux_aarch64/include/selva_error.h +140 -0
  43. package/dist/lib/linux_aarch64/include/selva_lang_code.h +160 -0
  44. package/dist/lib/linux_aarch64/include/tree.h +852 -0
  45. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  46. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  47. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  48. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  49. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  50. package/dist/lib/linux_aarch64/libselva.so +0 -0
  51. package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
  52. package/dist/lib/linux_x86_64/include/cdefs.h +320 -0
  53. package/dist/lib/linux_x86_64/include/jemalloc.h +468 -0
  54. package/dist/lib/linux_x86_64/include/libdeflate.h +345 -0
  55. package/dist/lib/linux_x86_64/include/libdeflate_strings.h +35 -0
  56. package/dist/lib/linux_x86_64/include/linker_set.h +109 -0
  57. package/dist/lib/linux_x86_64/include/queue.h +627 -0
  58. package/dist/lib/linux_x86_64/include/selva/_export.h +7 -0
  59. package/dist/lib/linux_x86_64/include/selva/align.h +9 -0
  60. package/dist/lib/linux_x86_64/include/selva/backoff_timeout.h +29 -0
  61. package/dist/lib/linux_x86_64/include/selva/bitmap.h +95 -0
  62. package/dist/lib/linux_x86_64/include/selva/colvec.h +71 -0
  63. package/dist/lib/linux_x86_64/include/selva/crc32c.h +17 -0
  64. package/dist/lib/linux_x86_64/include/selva/ctime.h +135 -0
  65. package/dist/lib/linux_x86_64/include/selva/db.h +430 -0
  66. package/dist/lib/linux_x86_64/include/selva/endian.h +301 -0
  67. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +27 -0
  68. package/dist/lib/linux_x86_64/include/selva/fast_memcmp.h +18 -0
  69. package/dist/lib/linux_x86_64/include/selva/fast_memmem.h +11 -0
  70. package/dist/lib/linux_x86_64/include/selva/fast_parsei.h +36 -0
  71. package/dist/lib/linux_x86_64/include/selva/fields.h +379 -0
  72. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  73. package/dist/lib/linux_x86_64/include/selva/hll.h +85 -0
  74. package/dist/lib/linux_x86_64/include/selva/lpf.h +28 -0
  75. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  76. package/dist/lib/linux_x86_64/include/selva/membar.h +32 -0
  77. package/dist/lib/linux_x86_64/include/selva/node_id_set.h +43 -0
  78. package/dist/lib/linux_x86_64/include/selva/poptop.h +114 -0
  79. package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +49 -0
  80. package/dist/lib/linux_x86_64/include/selva/selva_lang.h +105 -0
  81. package/dist/lib/linux_x86_64/include/selva/selva_math.h +37 -0
  82. package/dist/lib/linux_x86_64/include/selva/selva_string.h +683 -0
  83. package/dist/lib/linux_x86_64/include/selva/sort.h +140 -0
  84. package/dist/lib/linux_x86_64/include/selva/strsearch.h +43 -0
  85. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  86. package/dist/lib/linux_x86_64/include/selva/timestamp.h +25 -0
  87. package/dist/lib/linux_x86_64/include/selva/types.h +119 -0
  88. package/dist/lib/linux_x86_64/include/selva/vector.h +35 -0
  89. package/dist/lib/linux_x86_64/include/selva_error.h +140 -0
  90. package/dist/lib/linux_x86_64/include/selva_lang_code.h +160 -0
  91. package/dist/lib/linux_x86_64/include/tree.h +852 -0
  92. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  93. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  94. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  95. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  96. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  97. package/dist/lib/linux_x86_64/libselva.so +0 -0
  98. package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
  99. package/package.json +1 -1
@@ -0,0 +1,430 @@
1
+ /*
2
+ * Copyright (c) 2024-2025 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #pragma once
6
+
7
+ #include <assert.h>
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <sys/types.h>
11
+ #include "selva/_export.h"
12
+ #include "selva/types.h"
13
+
14
+ /*
15
+ * TODO Don't like this one but it compiles.
16
+ * We should have something with selva_hash_state;
17
+ */
18
+ struct XXH3_state_s;
19
+
20
+ struct selva_dump_common_data {
21
+ /**
22
+ * Pointer to data returned here when loading; Data read from here when saving.
23
+ */
24
+ const void *meta_data __pcounted_by(meta_len);
25
+ size_t meta_len;
26
+
27
+ /**
28
+ * Can be nullptr. Also set errlog_size to 0.
29
+ */
30
+ char *errlog_buf __pcounted_by(errlog_size);
31
+ size_t errlog_size;
32
+ };
33
+
34
+ /**
35
+ * Create a new DB instance.
36
+ */
37
+ SELVA_EXPORT
38
+ struct SelvaDb *selva_db_create(void);
39
+
40
+ /**
41
+ * Destroy a DB instance.
42
+ */
43
+ SELVA_EXPORT
44
+ void selva_db_destroy(struct SelvaDb *db) __attribute__((nonnull));
45
+
46
+ /**
47
+ * Create a new node type with a schema.
48
+ * @param type must not exist before.
49
+ */
50
+ SELVA_EXPORT
51
+ int selva_db_create_type(struct SelvaDb *db, node_type_t type, const uint8_t *schema_buf, size_t schema_len) __attribute__((nonnull));
52
+
53
+ /**
54
+ * Save the common/shared data of the database.
55
+ */
56
+ SELVA_EXPORT
57
+ int selva_dump_save_common(struct SelvaDb *db, struct selva_dump_common_data *com, const char *filename) __attribute__((nonnull));
58
+
59
+ /**
60
+ * Save a nodes block starting from start.
61
+ */
62
+ SELVA_EXPORT
63
+ int selva_dump_save_block(struct SelvaDb *db, struct SelvaTypeEntry *te, const char *filename, node_id_t start, selva_hash128_t *range_hash_out) __attribute__((nonnull));
64
+
65
+ /**
66
+ * **Usage:**
67
+ * ```c
68
+ * struct SelvaDb *db = selva_db_create();
69
+ * selva_dump_load_common(db, filename_common);
70
+ * selva_dump_load_block(db, filename_range_n);
71
+ * ```
72
+ */
73
+ SELVA_EXPORT
74
+ int selva_dump_load_common(struct SelvaDb *db, struct selva_dump_common_data *com, const char *filename) __attribute__((nonnull));
75
+
76
+ SELVA_EXPORT
77
+ int selva_dump_load_block(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
78
+
79
+ /**
80
+ * Find a type by type id.
81
+ */
82
+ SELVA_EXPORT
83
+ struct SelvaTypeEntry *selva_get_type_by_index(const struct SelvaDb *db, node_type_t type) __attribute__((nonnull));
84
+
85
+ /**
86
+ * Get the type for node.
87
+ */
88
+ SELVA_EXPORT
89
+ struct SelvaTypeEntry *selva_get_type_by_node(const struct SelvaDb *db, struct SelvaNode *node) __attribute__((nonnull, pure));
90
+
91
+ SELVA_EXPORT
92
+ inline block_id_t selva_get_block_capacity(const struct SelvaTypeEntry *te)
93
+ #ifndef __zig
94
+ {
95
+ return te->blocks->block_capacity;
96
+ }
97
+ #else
98
+ ;
99
+ #endif
100
+
101
+ inline block_id_t selva_node_id2block_i3(block_id_t block_capacity, node_id_t node_id)
102
+ {
103
+ assert(node_id > 0);
104
+ return ((node_id - 1) - ((node_id - 1) % block_capacity)) / block_capacity;
105
+ }
106
+
107
+ SELVA_EXPORT
108
+ inline block_id_t selva_node_id2block_i(const struct SelvaTypeBlocks *blocks, node_id_t node_id)
109
+ #ifndef __zig
110
+ {
111
+ return selva_node_id2block_i3(blocks->block_capacity, node_id);
112
+ }
113
+ #else
114
+ ;
115
+ #endif
116
+
117
+ SELVA_EXPORT
118
+ inline block_id_t selva_node_id2block_i2(const struct SelvaTypeEntry *te, node_id_t node_id)
119
+ #ifndef __zig
120
+ {
121
+ return selva_node_id2block_i(te->blocks, node_id);
122
+ }
123
+ #else
124
+ ;
125
+ #endif
126
+
127
+ /**
128
+ * Get the node schema for type.
129
+ */
130
+ SELVA_EXPORT
131
+ __attribute__((nonnull, pure))
132
+ inline const struct SelvaNodeSchema *selva_get_ns_by_te(const struct SelvaTypeEntry *te)
133
+ #ifndef __zig
134
+ {
135
+ return &te->ns;
136
+ }
137
+ #else
138
+ ;
139
+ #endif
140
+
141
+ SELVA_EXPORT
142
+ inline const struct SelvaFieldSchema *get_fs_by_fields_schema_field(const struct SelvaFieldsSchema *fields_schema, field_t field)
143
+ #ifndef __zig
144
+ {
145
+ if (!fields_schema || field >= fields_schema->nr_fields) {
146
+ return nullptr;
147
+ }
148
+
149
+ return &fields_schema->field_schemas[field];
150
+ }
151
+ #else
152
+ ;
153
+ #endif
154
+
155
+ /**
156
+ * Get the field schema for field.
157
+ */
158
+ SELVA_EXPORT
159
+ __attribute__((nonnull, pure))
160
+ inline const struct SelvaFieldSchema *selva_get_fs_by_te_field(const struct SelvaTypeEntry *te, field_t field)
161
+ #ifndef __zig
162
+ {
163
+ return get_fs_by_fields_schema_field(&te->ns.fields_schema, field);
164
+ }
165
+ #else
166
+ ;
167
+ #endif
168
+
169
+ /**
170
+ * Get the field schema for field.
171
+ */
172
+ SELVA_EXPORT
173
+ __attribute__((nonnull, pure))
174
+ inline const struct SelvaFieldSchema *selva_get_fs_by_ns_field(const struct SelvaNodeSchema *ns, field_t field)
175
+ #ifndef __zig
176
+ {
177
+ return get_fs_by_fields_schema_field(&ns->fields_schema, field);
178
+ }
179
+ #else
180
+ ;
181
+ #endif
182
+
183
+ /**
184
+ * Get the field schema for field.
185
+ */
186
+ SELVA_EXPORT
187
+ __attribute__((nonnull, pure))
188
+ inline const struct SelvaFieldSchema *selva_get_fs_by_node(struct SelvaDb *db, struct SelvaNode *node, field_t field)
189
+ #ifndef __zig
190
+ {
191
+ struct SelvaTypeEntry *type;
192
+
193
+ type = selva_get_type_by_node(db, node);
194
+ if (!type) {
195
+ return nullptr;
196
+ }
197
+
198
+ return selva_get_fs_by_ns_field(&type->ns, field);
199
+ }
200
+ #else
201
+ ;
202
+ #endif
203
+
204
+ SELVA_EXPORT
205
+ #if __has_c_attribute(reproducible)
206
+ [[reproducible]]
207
+ #endif
208
+ inline enum SelvaFieldType selva_get_fs_type(const struct SelvaFieldSchema *fs)
209
+ #ifndef __zig
210
+ {
211
+ return fs->type;
212
+ }
213
+ #else
214
+ ;
215
+ #endif
216
+
217
+ /**
218
+ * Get the EdgeFieldConstraint from a ref field schema.
219
+ * struct EdgeFieldConstraint *efc = selva_get_edge_field_constraint(src_fs);
220
+ * struct SelvaTypeEntry *dst_type = selva_get_type_by_index(db, efc->dst_node_type);
221
+ * struct SelvaFieldSchema *dst_fs = selva_get_fs_by_node(db, dst, efc->inverse_field);
222
+ */
223
+ SELVA_EXPORT
224
+ __attribute__((returns_nonnull))
225
+ __attribute__((nonnull))
226
+ inline const struct EdgeFieldConstraint *selva_get_edge_field_constraint(const struct SelvaFieldSchema *fs)
227
+ #ifndef __zig
228
+ {
229
+ assert(fs->type == SELVA_FIELD_TYPE_REFERENCE ||
230
+ fs->type == SELVA_FIELD_TYPE_REFERENCES ||
231
+ fs->type == SELVA_FIELD_TYPE_WEAK_REFERENCE ||
232
+ fs->type == SELVA_FIELD_TYPE_WEAK_REFERENCES);
233
+ return &fs->edge_constraint;
234
+ }
235
+ #else
236
+ ;
237
+ #endif
238
+
239
+ SELVA_EXPORT
240
+ inline const struct SelvaFieldsSchema *selva_get_edge_field_fields_schema(struct SelvaDb *db, const struct EdgeFieldConstraint *efc)
241
+ #ifndef __zig
242
+ {
243
+ struct SelvaTypeEntry *te = selva_get_type_by_index(db, efc->meta_node_type);
244
+
245
+ return (te) ? &selva_get_ns_by_te(te)->fields_schema : nullptr;
246
+ }
247
+ #else
248
+ ;
249
+ #endif
250
+
251
+ SELVA_EXPORT
252
+ void selva_expire_node(struct SelvaDb *db, node_type_t type, node_id_t node_id, int64_t ts);
253
+
254
+ SELVA_EXPORT
255
+ void selva_expire_node_cancel(struct SelvaDb *db, node_type_t type, node_id_t node_id);
256
+
257
+ SELVA_EXPORT
258
+ void selva_db_expire_tick(struct SelvaDb *db, selva_dirty_node_cb_t dirty_cb, void *dirty_ctx, int64_t now);
259
+
260
+ /**
261
+ * Delete a node.
262
+ * @param dirty_cb is called for any newly dirty nodes in addition to the node being deleted.
263
+ */
264
+ SELVA_EXPORT
265
+ void selva_del_node(struct SelvaDb *db, struct SelvaTypeEntry *type, struct SelvaNode *node, selva_dirty_node_cb_t dirty_cb, void *dirty_ctx) __attribute__((nonnull(1, 2, 3)));
266
+
267
+ SELVA_EXPORT
268
+ void selva_del_block(struct SelvaDb *db, struct SelvaTypeEntry *te, node_id_t start);
269
+
270
+ /**
271
+ * Get a node by id.
272
+ */
273
+ SELVA_EXPORT
274
+ struct SelvaNode *selva_find_node(struct SelvaTypeEntry *type, node_id_t node_id) __attribute__((nonnull));
275
+
276
+ /**
277
+ * Find the first node greater than or equal to the provided id, or NULL.
278
+ */
279
+ SELVA_EXPORT
280
+ struct SelvaNode *selva_nfind_node(struct SelvaTypeEntry *type, node_id_t node_id) __attribute__((nonnull));
281
+
282
+ /**
283
+ * Get or create a node by id.
284
+ */
285
+ SELVA_EXPORT
286
+ struct SelvaNode *selva_upsert_node(struct SelvaTypeEntry *type, node_id_t node_id) __attribute__((nonnull));
287
+
288
+ /**
289
+ * **Example**
290
+ * ```c
291
+ * for (struct SelvaNode *np = selva_min_node(type); np; np = selva_next_node(type, np))
292
+ * ```
293
+ */
294
+ SELVA_EXPORT
295
+ struct SelvaNode *selva_min_node(struct SelvaTypeEntry *type) __attribute__((nonnull));
296
+
297
+ /**
298
+ * **Example**
299
+ * ```c
300
+ * for (struct SelvaNode *np = selva_max_node(type); np; np = selva_prev_node(type, np))
301
+ * ```
302
+ */
303
+ SELVA_EXPORT
304
+ struct SelvaNode *selva_max_node(struct SelvaTypeEntry *type) __attribute__((nonnull));
305
+
306
+ /**
307
+ * Get previous node with a lower node id.
308
+ */
309
+ SELVA_EXPORT
310
+ struct SelvaNode *selva_prev_node(struct SelvaTypeEntry *type, struct SelvaNode *node) __attribute__((nonnull));
311
+
312
+ /**
313
+ * Get next node with higher node id.
314
+ */
315
+ SELVA_EXPORT
316
+ struct SelvaNode *selva_next_node(struct SelvaTypeEntry *type, struct SelvaNode *node) __attribute__((nonnull));
317
+
318
+ /**
319
+ * Total count of nodes of type.
320
+ */
321
+ SELVA_EXPORT
322
+ size_t selva_node_count(const struct SelvaTypeEntry *type) __attribute__((nonnull));
323
+
324
+ /**
325
+ * Get the node id of of node.
326
+ */
327
+ SELVA_EXPORT
328
+ __attribute__((nonnull, pure))
329
+ inline node_id_t selva_get_node_id(const struct SelvaNode *node)
330
+ #ifndef __zig
331
+ {
332
+ return node->node_id;
333
+ }
334
+ #else
335
+ ;
336
+ #endif
337
+
338
+ /**
339
+ * Get the type of of node.
340
+ */
341
+ SELVA_EXPORT
342
+ __attribute__((nonnull, pure))
343
+ inline node_type_t selva_get_node_type(const struct SelvaNode *node)
344
+ #ifndef __zig
345
+ {
346
+ return node->type;
347
+ }
348
+ #else
349
+ ;
350
+ #endif
351
+
352
+ /**
353
+ * \addtogroup node_hash
354
+ * @{
355
+ */
356
+
357
+ /**
358
+ * Calculate the node hash.
359
+ * Update node hash by using a temp hash state allocated earlier.
360
+ * @param tmp_hash_state is only used for computation and it's reset before use.
361
+ */
362
+ SELVA_EXPORT
363
+ selva_hash128_t selva_node_hash_update(struct SelvaDb *db, struct SelvaTypeEntry *type, struct SelvaNode *node, struct XXH3_state_s *tmp_hash_state);
364
+
365
+ SELVA_EXPORT
366
+ selva_hash128_t selva_node_hash(struct SelvaDb *db, struct SelvaTypeEntry *type, struct SelvaNode *node);
367
+
368
+ SELVA_EXPORT
369
+ int selva_node_hash_range(struct SelvaDb *db, struct SelvaTypeEntry *type, node_id_t start, node_id_t end, selva_hash128_t *hash_out) __attribute__((nonnull, warn_unused_result));
370
+
371
+ /**
372
+ * @}
373
+ */
374
+
375
+ /**
376
+ * Get the number of aliases under given type.
377
+ */
378
+ SELVA_EXPORT
379
+ size_t selva_alias_count(const struct SelvaAliases *aliases);
380
+
381
+ /**
382
+ * Set new alias.
383
+ * @param name is copied.
384
+ * @returns the previous node_id the alias was pointing to; Otherwise 0.
385
+ */
386
+ SELVA_EXPORT
387
+ node_id_t selva_set_alias(struct SelvaAliases *aliases, node_id_t dest, const char *name_str, size_t name_len);
388
+
389
+ /**
390
+ * Delete alias by name.
391
+ * @return the destination node_id the alias was pointing to; 0 if SELVA_ENOENT.
392
+ */
393
+ SELVA_EXPORT
394
+ node_id_t selva_del_alias_by_name(struct SelvaAliases *aliases, const char *name_str, size_t name_len);
395
+
396
+ /**
397
+ * Delete all aliases pointing to dest.
398
+ */
399
+ SELVA_EXPORT
400
+ void selva_del_alias_by_dest(struct SelvaAliases *aliases, node_id_t dest);
401
+
402
+ /**
403
+ * Get alias by name.
404
+ */
405
+ SELVA_EXPORT
406
+ struct SelvaNode *selva_get_alias(struct SelvaTypeEntry *type, struct SelvaAliases *aliases, const char *name_str, size_t name_len);
407
+
408
+ /**
409
+ * Get alias by destination id.
410
+ * This may not seem very useful but this is actually the way that allows you to
411
+ * traverse all aliases to the given node_id by following the `next` pointer or
412
+ * by calling selva_get_next_alias().
413
+ */
414
+ SELVA_EXPORT
415
+ const struct SelvaAlias *selva_get_alias_by_dest(struct SelvaAliases *aliases, node_id_t dest);
416
+
417
+ SELVA_EXPORT
418
+ const struct SelvaAlias *selva_get_next_alias(const struct SelvaAlias *alias);
419
+
420
+ SELVA_EXPORT
421
+ const char *selva_get_alias_name(const struct SelvaAlias *alias, size_t *len) __attribute__((nonnull, pure));
422
+
423
+ SELVA_EXPORT
424
+ struct SelvaAliases *selva_get_aliases(struct SelvaTypeEntry *type, field_t field);
425
+
426
+ /***
427
+ * Remove all aliases to the given node_id.
428
+ */
429
+ SELVA_EXPORT
430
+ void selva_remove_all_aliases(struct SelvaTypeEntry *type, node_id_t node_id);
@@ -0,0 +1,301 @@
1
+ /*
2
+ * Copyright (c) 2022-2024 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #pragma once
6
+ #ifndef _SELVA_ENDIAN_H_
7
+ #define _SELVA_ENDIAN_H_
8
+
9
+ #include <stddef.h>
10
+ #include <stdint.h>
11
+
12
+ /*
13
+ * NOTICE
14
+ * This header conflicts with `endian.h` on Linux/glibc and gets included at
15
+ * least by `sys/types.h`. This is not a big problem because we implement the
16
+ * same functionality + some extras.
17
+ * The original header could be included with `#include_next`. Isn't it pretty
18
+ * stupid that we can override system headers included by system headers in
19
+ * this way?
20
+ */
21
+
22
+ _Static_assert(sizeof(double) == 8, "Only 64bit doubles are supported");
23
+
24
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
25
+
26
+ #ifndef htobe16
27
+ #define htobe16(x) __builtin_bswap16(x)
28
+ #define htole16(x) (x)
29
+ #define be16toh(x) __builtin_bswap16(x)
30
+ #define le16toh(x) (x)
31
+ #endif
32
+
33
+ #ifndef htobe32
34
+ #define htobe32(x) __builtin_bswap32(x)
35
+ #define htole32(x) (x)
36
+ #define be32toh(x) __builtin_bswap32(x)
37
+ #define le32toh(x) (x)
38
+ #endif
39
+
40
+ #ifndef htobe64
41
+ #define htobe64(x) __builtin_bswap64(x)
42
+ #define htole64(x) (x)
43
+ #define be64toh(x) __builtin_bswap64(x)
44
+ #define le64toh(x) (x)
45
+ #endif
46
+
47
+ static inline void htoledouble(char buf[8], double x) {
48
+ #if __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
49
+ /*
50
+ * x: 4 5 6 7 0 1 2 3
51
+ * 0 1 2 3 4 5 6 7
52
+ */
53
+ char s[8];
54
+
55
+ __builtin_memcpy(s, &x, 8);
56
+ buf[0] = s[4];
57
+ buf[1] = s[5];
58
+ buf[2] = s[6];
59
+ buf[3] = s[7];
60
+ buf[4] = s[0];
61
+ buf[5] = s[1];
62
+ buf[6] = s[2];
63
+ buf[7] = s[3];
64
+ #else
65
+ __builtin_memcpy(buf, &x, 8);
66
+ #endif
67
+ }
68
+
69
+ static inline double ledoubletoh(const char buf[8]) {
70
+ #if __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
71
+ char s[8];
72
+ double x;
73
+
74
+ s[0] = buf[4];
75
+ s[1] = buf[5];
76
+ s[2] = buf[6];
77
+ s[3] = buf[7];
78
+ s[4] = buf[0];
79
+ s[5] = buf[1];
80
+ s[6] = buf[2];
81
+ s[7] = buf[3];
82
+
83
+ __builtin_memcpy(&x, s, sizeof(double));
84
+ return x;
85
+ #else
86
+ double x;
87
+
88
+ __builtin_memcpy(&x, buf, sizeof(double));
89
+
90
+ return x;
91
+ #endif
92
+ }
93
+
94
+ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
95
+
96
+ #ifndef htobe16
97
+ #define htobe16(x) (x)
98
+ #define htole16(x) __builtin_bswap16(x)
99
+ #define be16toh(x) (x)
100
+ #define le16toh(x) __builtin_bswap16(x)
101
+ #endif
102
+
103
+ #ifndef htobe32
104
+ #define htobe32(x) (x)
105
+ #define htole32(x) __builtin_bswap32(x)
106
+ #define be32toh(x) (x)
107
+ #define le32toh(x) __builtin_bswap32(x)
108
+ #endif
109
+
110
+ #ifndef htobe64
111
+ #define htobe64(x) (x)
112
+ #define htole64(x) __builtin_bswap64(x)
113
+ #define be64toh(x) (x)
114
+ #define le64toh(x) __builtin_bswap64(x)
115
+ #endif
116
+
117
+ static inline void htoledouble(char buf[8], double x) {
118
+ #if __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
119
+ /*
120
+ * x: 3 2 1 0 7 6 5 4
121
+ * s: 0 1 2 3 4 5 6 7
122
+ */
123
+ char s[8];
124
+
125
+ __builtin_memcpy(s, &x, 8);
126
+ buf[0] = s[3];
127
+ buf[1] = s[2];
128
+ buf[2] = s[1];
129
+ buf[3] = s[0];
130
+ buf[4] = s[7];
131
+ buf[5] = s[6];
132
+ buf[6] = s[5];
133
+ buf[7] = s[4];
134
+ #else
135
+ /*
136
+ * x: 7 6 5 4 3 2 1 0
137
+ * s: 0 1 2 3 4 5 6 7
138
+ */
139
+ char s[8];
140
+
141
+ __builtin_memcpy(s, &x, 8);
142
+ buf[0] = s[7];
143
+ buf[1] = s[6];
144
+ buf[2] = s[5];
145
+ buf[3] = s[4];
146
+ buf[4] = s[3];
147
+ buf[5] = s[2];
148
+ buf[6] = s[1];
149
+ buf[7] = s[0];
150
+ #endif
151
+ }
152
+
153
+ static inline double ledoubletoh(const char buf[8]) {
154
+ char s[8];
155
+ double x;
156
+
157
+ #if __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
158
+ s[0] = buf[3];
159
+ s[1] = buf[2];
160
+ s[2] = buf[1];
161
+ s[3] = buf[0];
162
+ s[4] = buf[7];
163
+ s[5] = buf[6];
164
+ s[6] = buf[5];
165
+ s[7] = buf[4];
166
+ #else
167
+ s[0] = buf[7];
168
+ s[1] = buf[6];
169
+ s[2] = buf[5];
170
+ s[3] = buf[4];
171
+ s[4] = buf[3];
172
+ s[5] = buf[2];
173
+ s[6] = buf[1];
174
+ s[7] = buf[0];
175
+ #endif
176
+
177
+ __builtin_memcpy(&x, s, sizeof(double));
178
+ return x;
179
+ }
180
+
181
+ #else
182
+ #error "Machine byte order not supported"
183
+ #endif
184
+
185
+ #if __linux__
186
+
187
+ /**
188
+ * Type generic Host to BE.
189
+ */
190
+ #define htobe(v) _Generic((v), \
191
+ uint16_t: htobe16(v), \
192
+ int16_t: (int16_t)htobe16(v), \
193
+ uint32_t: htobe32(v), \
194
+ int32_t: (int32_t)htobe32(v), \
195
+ uint64_t: htobe64(v), \
196
+ int64_t: (int64_t)htobe64(v), \
197
+ unsigned long long: (unsigned long long)htobe64(v), \
198
+ long long: (int64_t)htobe64(v))
199
+
200
+ /**
201
+ * Type generic Host to LE.
202
+ */
203
+ #define htole(v) _Generic((v), \
204
+ uint16_t: htole16(v), \
205
+ int16_t: (int16_t)htole16(v), \
206
+ uint32_t: htole32(v), \
207
+ int32_t: (int32_t)htole32(v), \
208
+ uint64_t: htole64(v), \
209
+ int64_t: (int64_t)htole64(v), \
210
+ unsigned long long: (unsigned long long)htole64(v), \
211
+ long long: (int64_t)htole64(v))
212
+
213
+ /**
214
+ * Type generic LE to Host.
215
+ */
216
+ #define letoh(v) _Generic((v), \
217
+ uint16_t: le16toh(v), \
218
+ int16_t: (int16_t)le16toh(v), \
219
+ uint32_t: le32toh(v), \
220
+ int32_t: (int32_t)le32toh(v), \
221
+ uint64_t: le64toh(v), \
222
+ int64_t: (int64_t)le64toh(v), \
223
+ unsigned long long: (unsigned long long)le64toh(v), \
224
+ long long: (int64_t)le64toh(v))
225
+
226
+ /**
227
+ * Type generic BE to Host.
228
+ */
229
+ #define betoh(v) _Generic((v), \
230
+ uint16_t: be16toh(v), \
231
+ int16_t: (int16_t)be16toh(v), \
232
+ uint32_t: be32toh(v), \
233
+ int32_t: (int32_t)be32toh(v), \
234
+ uint64_t: be64toh(v), \
235
+ int64_t: (int64_t)be64toh(v), \
236
+ unsigned long long: (unsigned long long)be64toh(v), \
237
+ long long: (int64_t)be64toh(v))
238
+
239
+ /* 128-bit machines not supported atm. */
240
+ static_assert(sizeof(long long) == sizeof(uint64_t));
241
+
242
+ #else
243
+
244
+ /**
245
+ * Type generic Host to BE.
246
+ */
247
+ #define htobe(v) _Generic((v), \
248
+ uint16_t: htobe16(v), \
249
+ int16_t: (int16_t)htobe16(v), \
250
+ uint32_t: htobe32(v), \
251
+ int32_t: (int32_t)htobe32(v), \
252
+ uint64_t: htobe64(v), \
253
+ size_t: htobe64(v), \
254
+ ssize_t: htobe64(v), \
255
+ int64_t: (int64_t)htobe64(v))
256
+
257
+ /**
258
+ * Type generic Host to LE.
259
+ */
260
+ #define htole(v) _Generic((v), \
261
+ uint16_t: htole16(v), \
262
+ int16_t: (int16_t)htole16(v), \
263
+ uint32_t: htole32(v), \
264
+ int32_t: (int32_t)htole32(v), \
265
+ uint64_t: htole64(v), \
266
+ size_t: htole64(v), \
267
+ ssize_t: htole64(v), \
268
+ int64_t: (int64_t)htole64(v))
269
+
270
+ /**
271
+ * Type generic LE to Host.
272
+ */
273
+ #define letoh(v) _Generic((v), \
274
+ uint16_t: le16toh(v), \
275
+ int16_t: (int16_t)le16toh(v), \
276
+ uint32_t: le32toh(v), \
277
+ int32_t: (int32_t)le32toh(v), \
278
+ uint64_t: le64toh(v), \
279
+ size_t: le64toh(v), \
280
+ ssize_t: le64toh(v), \
281
+ int64_t: (int64_t)le64toh(v))
282
+
283
+ /**
284
+ * Type generic BE to Host.
285
+ */
286
+ #define betoh(v) _Generic((v), \
287
+ uint16_t: be16toh(v), \
288
+ int16_t: (int16_t)be16toh(v), \
289
+ uint32_t: be32toh(v), \
290
+ int32_t: (int32_t)be32toh(v), \
291
+ uint64_t: be64toh(v), \
292
+ size_t: be64toh(v), \
293
+ ssize_t: be64toh(v), \
294
+ int64_t: (int64_t)be64toh(v))
295
+
296
+ #endif
297
+
298
+ /* If this fails then htole and letoh will need some adjustment. */
299
+ static_assert(sizeof(size_t) == sizeof(uint64_t));
300
+
301
+ #endif /* _SELVA_ENDIAN_H_ */