xref: /openbsd/usr.sbin/nsd/udb.h (revision 15ed76cb)
1 /* udb.h - u(micro) data base, stores data and index information in mmap file.
2  * By W.C.A. Wijngaards
3  * Copyright 2010, NLnet Labs.
4  * BSD, see LICENSE.
5  */
6 #ifndef UDB_H
7 #define UDB_H
8 #include <assert.h>
9 
10 /**
11  * The micro data base UDB.
12  *
13  * File data.udb is mmapped and used to lookup and edit.
14  * it contains a header with space-allocation-info, and a reference to the
15  * base information, an object that is the entry point for the file.
16  * Then it contains a lot of data and index objects.
17  *
18  * The space allocator is 'buddy system', 1megareas, larger get own area.
19  * So worst case is 2xdata filesize (+header).  Growth semi-linear.
20  * Chunks have size and type (for recovery).  Call to reserve space.
21  * Call to 'realloc-in-place', if space permits.
22  *
23  * Usually you want a record-type and its indexes (sorted) to be stored in
24  * the file.  This is a table (named by string).  The record is opaque
25  * data.
26  *
27  * To be able to use pointers in the mmapped file, there is conversion of
28  * relative-pointers(to file base) to system-pointers.
29  *
30  * If an item is moved its internal pointers need to be recalculated.
31  * Thus a recordtype (that has internal pointers) must provide a routine.
32  * Structures that are 'on-disk', are denoted with _d. Except rel_ptr which
33  * is also on-disk.
34  *
35  * About 64-bit trouble.  The pointer-size which which the application is
36  * compiled determines the file layout, because this makes it perform well
37  * in a mmap.  It could in theory be converted if you really wanted to.
38  * Nonpointer data is best stored as a fixed bitsize (uint8, 16, 32, 64).
39  */
40 typedef struct udb_base udb_base;
41 typedef struct udb_alloc udb_alloc;
42 
43 /** these checks are very slow, disabled by default */
44 #if 0
45 /** perform extra checks (when --enable-checking is used) */
46 #ifndef NDEBUG
47 #define UDB_CHECK 1
48 #endif
49 #endif
50 
51 /** pointers are stored like this */
52 typedef uint64_t udb_void;
53 
54 /** convert relptr to usable pointer */
55 #define UDB_REL(base, relptr) ((base) + (relptr))
56 /** from system pointer to relative pointer */
57 #define UDB_SYSTOREL(base, ptr) ((udb_void)((void*)(ptr) - (base)))
58 
59 /** MAX 2**x exponent of alloced chunks, for 1Mbytes.  The smallest
60  * chunk is 16bytes (8preamble+8data), so 0-3 is unused. */
61 #define UDB_ALLOC_CHUNKS_MAX 20
62 /** size of areas that are subdivided */
63 #define UDB_ALLOC_CHUNK_SIZE ((uint64_t)1<<UDB_ALLOC_CHUNKS_MAX)
64 /** the minimum alloc in exp, 2**x.  32bytes because of chunk_free_d size (8aligned) */
65 #define UDB_ALLOC_CHUNK_MINEXP 5
66 /** size of minimum alloc */
67 #define UDB_ALLOC_CHUNK_MINSIZE ((uint64_t)1<<UDB_ALLOC_CHUNK_MINEXP)
68 /** exp size used to mark the header (cannot be reallocated) */
69 #define UDB_EXP_HEADER 0
70 /** exp size used to mark XL(extralarge) allocations (in whole mbs) */
71 #define UDB_EXP_XL 1
72 
73 typedef struct udb_ptr udb_ptr;
74 /**
75  * This structure is there for when you want to have a pointer into
76  * the mmap-ed file.  It is kept track of.  Set it to NULL to unlink it.
77  * For pointers to the mmap-ed file from within the mmap-ed file, use the
78  * rel_pre construct below.
79  */
80 struct udb_ptr {
81 	/** the data segment it points to (relative file offset) */
82 	uint64_t data;
83 	/** pointer to the base pointer (for convenience) */
84 	void** base;
85 	/** prev in udb_ptr list for this data segment */
86 	udb_ptr* prev;
87 	/** next in udb_ptr list for this data segment */
88 	udb_ptr* next;
89 };
90 
91 typedef struct udb_rel_ptr udb_rel_ptr;
92 /**
93  * A relative pointer that keeps track of the list of pointers,
94  * so that it can be reallocated.
95  */
96 struct udb_rel_ptr {
97 	/** the relative pointer to the data itself (subtract chunk_d size
98 	 * to get the chunk_d type, this is for usage speed in dereferencing
99 	 * to the userdata). */
100 	udb_void data;
101 	/** udb_rel_ptr* prev in relptr list */
102 	udb_void prev;
103 	/** udb_rel_ptr* next in relptr list */
104 	udb_void next;
105 };
106 
107 /**
108  * This is the routine that is called for every relptr
109  * @param base: the baseptr for REL.
110  * @param p: the relptr, a real pointer to it.
111  * @param arg: user argument.
112  */
113 typedef void udb_walk_relptr_cb(void*, udb_rel_ptr*, void*);
114 
115 /**
116  * This routine calls the callback for every relptr in a datablock
117  * params in order:
118  * base: the baseptr for REL macro.
119  * warg: the walkfunc user argument.
120  * t: the type of the chunk.
121  * d: pointer to the data part of the chunk (real pointer).
122  * s: max size of the data part.
123  * cb: the callback to call for every element.
124  * arg: user argument to pass to the callback.
125  */
126 typedef void udb_walk_relptr_func(void*, void*, uint8_t, void*, uint64_t,
127 	udb_walk_relptr_cb*, void*);
128 
129 /** What sort of salvage should be performed by alloc */
130 enum udb_dirty_alloc {
131 	udb_dirty_clean = 0, /* all clean */
132 	udb_dirty_fl,        /* allocs, freelists are messed up */
133 	udb_dirty_fsize,     /* file size and fsize are messed up */
134 	udb_dirty_compact    /* allocs, freelists and relptrs are messed up */
135 };
136 
137 typedef struct udb_glob_d udb_glob_d;
138 /**
139  * The UDB global data for a file.  This structure is mmapped.
140  * Make sure it has no structure-padding problems.
141  */
142 struct udb_glob_d {
143 	/** size of header in the file (offset to the first alloced chunk) */
144 	uint64_t hsize;
145 	/** version number of this file */
146 	uint8_t version;
147 	/** was the file cleanly closed, 0 is not clean, 1 is clean */
148 	uint8_t clean_close;
149 	/** an allocation operation was in progress, file needs to be salvaged
150 	 * type enum udb_dirty_alloc */
151 	uint8_t dirty_alloc;
152 	/** user flags */
153 	uint8_t userflags;
154 	/** padding to 8-bytes alignment */
155 	uint8_t pad1[4];
156 	/** size to mmap */
157 	uint64_t fsize;
158 	/** chunk move rollback info: oldchunk (0 is nothing).
159 	 * volatile because these values prevent dataloss, they need to be
160 	 * written immediately. */
161 	volatile udb_void rb_old;
162 	/** chunk move rollback info: newchunk (0 is nothing) */
163 	volatile udb_void rb_new;
164 	/** size of move rollback chunks */
165 	volatile uint64_t rb_size;
166 	/** segment of move rollback, for an XL chunk that overlaps. */
167 	volatile uint64_t rb_seg;
168 	/** linked list for content-listing, 0 if empty */
169 	udb_rel_ptr content_list;
170 	/** user global data pointer */
171 	udb_rel_ptr user_global;
172 };
173 
174 /**
175  * The UDB database file.  Contains all the data
176  */
177 struct udb_base {
178 	/** name of the file, alloced */
179 	char* fname;
180 
181 	/** mmap base pointer (or NULL) */
182 	void* base;
183 	/** size of mmap */
184 	size_t base_size;
185 	/** fd of mmap (if -1, closed). */
186 	int fd;
187 
188 	/** space allocator that is used for this base */
189 	udb_alloc* alloc;
190 	/** real pointer to the global data in the file */
191 	udb_glob_d* glob_data;
192 
193 	/** store all linked udb_ptrs in this table, by hash(offset).
194 	 * then a linked list of ptrs (all that match the hash).
195 	 * this avoids buckets, and thus memory allocation. */
196 	udb_ptr** ram_hash;
197 	/** size of the current udb_ptr hashtable array */
198 	size_t ram_size;
199 	/** mask for the curren udb_ptr hashtable lookups */
200 	int ram_mask;
201 	/** number of ptrs in ram, used to decide when to grow */
202 	size_t ram_num;
203 	/** for relocation, this walks through all relptrs in chunk */
204 	udb_walk_relptr_func* walkfunc;
205 	/** user data for walkfunc */
206 	void* walkarg;
207 
208 	/** compaction is inhibited */
209 	int inhibit_compact;
210 	/** compaction is useful; deletions performed. */
211 	int useful_compact;
212 };
213 
214 typedef enum udb_chunk_type udb_chunk_type;
215 /** chunk type enum, setting these types help recovery and debug */
216 enum udb_chunk_type {
217 	udb_chunk_type_free = 0,
218 	udb_chunk_type_data, /* alloced data */
219 	udb_chunk_type_index,
220 	udb_chunk_type_radtree,
221 	udb_chunk_type_radnode,
222 	udb_chunk_type_radarray,
223 	udb_chunk_type_zone,
224 	udb_chunk_type_domain,
225 	udb_chunk_type_rrset,
226 	udb_chunk_type_rr,
227 	udb_chunk_type_task,
228 	udb_chunk_type_internal
229 };
230 
231 typedef struct udb_chunk_d udb_chunk_d;
232 /**
233  * UDB chunk info (prepended for every allocated chunk).
234  * The chunks are in doublelinkedlists per size.
235  * At the end of the chunk another exp uint8 is stored (to walk backwards).
236  * 17 bytes overhead, datasize for 32byte chunk is 15.
237  */
238 struct udb_chunk_d {
239 	/** the size of this chunk (i.e. 2**x) */
240 	uint8_t exp;
241 	/** type for this chunk (enum chunktype; free, data or index) */
242 	uint8_t type;
243 	/** flags for this chunk */
244 	uint8_t flags;
245 	/** padding onto 8-alignment */
246 	uint8_t pad[5];
247 	/** udb_rel_ptr* first in list of rel-ptrs that point back here
248 	 * In the free chunk this is the previous pointer. */
249 	udb_void ptrlist;
250 	/* user data space starts here, 64-bit aligned */
251 	uint8_t data[0];
252 	/* last octet: exp of chunk */
253 };
254 
255 typedef struct udb_free_chunk_d udb_free_chunk_d;
256 /**
257  * A free chunk.  Same start as the udb_chunk_d. minsize is 32 bytes.
258  */
259 struct udb_free_chunk_d {
260 	/** the size of this chunk (i.e. 2**x) */
261 	uint8_t exp;
262 	/** type for this chunk (enum chunktype; free, data or index) */
263 	uint8_t type;
264 	/** flags for this chunk */
265 	uint8_t flags;
266 	/** padding onto 8-alignment */
267 	uint8_t pad[5];
268 	/** udb_chunk_d* prev of free list for this size */
269 	udb_void prev;
270 	/** udb_chunk_d* next of free list for this size */
271 	udb_void next;
272 	/* empty stuff */
273 	/* last octet: exp of chunk */
274 };
275 
276 typedef struct udb_xl_chunk_d udb_xl_chunk_d;
277 /**
278  * an Extra Large (XL) chunk.  Same start as the udb_chunk_d.  Allocated in whole
279  * MAX_CHUNK_SIZE parts, whole megabytes.  overhead is 5x8=40 bytes.
280  */
281 struct udb_xl_chunk_d {
282 	/** the size of this chunk (i.e. 2**x): special XL value */
283 	uint8_t exp;
284 	/** type for this chunk (enum chunktype; free, data or index) */
285 	uint8_t type;
286 	/** flags for this chunk */
287 	uint8_t flags;
288 	/** padding onto 8-alignment */
289 	uint8_t pad[5];
290 	/** udb_rel_ptr* first in list of rel-ptrs that point back here
291 	 * In the free chunk this is the previous pointer. */
292 	udb_void ptrlist;
293 	/** size of this chunk in bytes */
294 	uint64_t size;
295 	/** data of the XL chunk */
296 	uint8_t data[0];
297 	/* uint64_t endsize: before last octet the size again. */
298 	/* uint8_t pad[7]: padding to make last octet last. */
299 	/* last octet: exp of chunk: special XL value */
300 };
301 
302 typedef struct udb_alloc_d udb_alloc_d;
303 /**
304  * UDB alloc info on disk.
305  */
306 struct udb_alloc_d {
307 	/** stats: number of data bytes allocated, sum of sizes passed to alloc */
308 	uint64_t stat_data;
309 	/** stats: number of bytes in free chunks, sum of their 2**x size */
310 	uint64_t stat_free;
311 	/** stats: number of bytes in alloced chunks, sum of their 2**x size */
312 	uint64_t stat_alloc;
313 	/** offset to create next chunk at. can be before file-end, or be
314 	 * fsize, volatile because it is used as a 'commit', and thus we want
315 	 * this to be written to memory (and thus disk) immediately. */
316 	volatile uint64_t nextgrow;
317 	/** fixed size array the points to the 2**x size chunks in the file,
318 	 * This is the start of the doublelinked list, ptr to udb_free_chunk_d.
319 	 * array starts at UDB_ALLOC_CHUNK_MINEXP entry as [0]. */
320 	udb_void free[UDB_ALLOC_CHUNKS_MAX-UDB_ALLOC_CHUNK_MINEXP+1];
321 };
322 
323 /**
324  * The UDB space allocator.  Assigns space in the file.
325  */
326 struct udb_alloc {
327 	/** the base this is part of */
328 	udb_base* udb;
329 	/** real pointer to space allocation info on disk; fixedsize struct */
330 	udb_alloc_d* disk;
331 };
332 
333 /**
334  * file header length, the file start with
335  * 64bit: magic number to identify file (and prevent stupid mistakes)
336  * globdata: global data. Fixed size segment. (starts with size uint64)
337  * allocdata: alloc global data. Fixed size segment.
338  * size and 0 byte: end marker for reverse search.
339  */
340 #define UDB_HEADER_SIZE (sizeof(uint64_t)+sizeof(udb_glob_d)+ \
341 	sizeof(udb_alloc_d)+sizeof(uint64_t)*2)
342 /** magic string that starts an UDB file, uint64_t, note first byte=0, to mark
343  * header start as a chunk. */
344 #define UDB_MAGIC (((uint64_t)'u'<<48)|((uint64_t)'d'<<40)|((uint64_t)'b' \
345 	<<32)|((uint64_t)'v'<<24)|((uint64_t)'0'<<16)|((uint64_t)'a'<<8))
346 
347 /* UDB BASE */
348 /**
349  * Create udb base structure and attempt to read the file.
350  * @param fname: file name.
351  * @param walkfunc: function to walk through relptrs in chunk.
352  * @param arg: user argument to pass to walkfunc
353  * @return base structure or NULL on failure.
354  */
355 udb_base* udb_base_create_read(const char* fname, udb_walk_relptr_func walkfunc,
356 	void* arg);
357 
358 /**
359  * Create udb base structure and create a new file.
360  * @param fname: file name.
361  * @param walkfunc: function to walk through relptrs in chunk.
362  * @param arg: user argument to pass to walkfunc
363  * @return base structure or NULL on failure.
364  */
365 udb_base* udb_base_create_new(const char* fname, udb_walk_relptr_func walkfunc,
366 	void* arg);
367 
368 /**
369  * Create udb from (O_RDWR) fd.
370  * @param fname: file name.
371  * @param fd: file descriptor.
372  * @param walkfunc: function to walk through relptrs in chunk.
373  * @param arg: user argument to pass to walkfunc
374  * @return base structure or NULL on failure.
375  */
376 udb_base* udb_base_create_fd(const char* fname, int fd,
377 	udb_walk_relptr_func walkfunc, void* arg);
378 
379 /**
380  * Properly close the UDB base file.  Separate from delete so the
381  * most important bits (write to disk, sockets) can be done first.
382  * @param udb: the udb.
383  */
384 void udb_base_close(udb_base* udb);
385 
386 /**
387  * Free the data structure (and close if not already) the udb.
388  * @param udb: the udb.
389  */
390 void udb_base_free(udb_base* udb);
391 
392 /**
393  * Free the udb, but keep mmap mapped for others.
394  * @param udb: the udb.
395  */
396 void udb_base_free_keep_mmap(udb_base* udb);
397 
398 /**
399  * Sync the mmap.
400  * @param udb: the udb.
401  * @param wait: if true, the call blocks until synced.
402  */
403 void udb_base_sync(udb_base* udb, int wait);
404 
405 /**
406  * The mmap size is updated to reflect changes by another process.
407  * @param udb: the udb.
408  */
409 void udb_base_remap_process(udb_base* udb);
410 
411 /**
412  * get the user data (relative) pointer.
413  * @param udb: the udb.
414  * @return the userdata relative pointer, 0 means nothing.
415  */
416 udb_rel_ptr* udb_base_get_userdata(udb_base* udb);
417 
418 /**
419  * Set the user data (relative) pointer.
420  * @param udb: the udb.
421  * @param user: user data. offset-pointer (or 0).
422  */
423 void udb_base_set_userdata(udb_base* udb, udb_void user);
424 
425 /**
426  * Set the user flags (to any value, uint8).
427  * @param udb: the udb.
428  * @param v: new value.
429  */
430 void udb_base_set_userflags(udb_base* udb, uint8_t v);
431 
432 /**
433  * Get the user flags.
434  * @param udb: the udb.
435  * @param v: new value.
436  */
437 uint8_t udb_base_get_userflags(udb_base* udb);
438 
439 /**
440  * Not for users of udb_base, but for udb_ptr.
441  * Link in a new ptr that references a data segment.
442  * @param udb: the udb.
443  * @param ptr: to link in.
444  */
445 void udb_base_link_ptr(udb_base* udb, udb_ptr* ptr);
446 
447 /**
448  * Not for users of udb_base, but for udb_ptr.
449  * Unlink a ptr that references a data segment.
450  * @param udb: the udb.
451  * @param ptr: to unlink.
452  */
453 void udb_base_unlink_ptr(udb_base* udb, udb_ptr* ptr);
454 
455 /* UDB ALLOC */
456 /**
457  * Utility for alloc, find 2**x size that is bigger than the given size.
458  * Does not work for amount==0.
459  * @param amount: amount of memory.
460  * @return x; the exponent where 2**x >= amount.
461  */
462 int udb_exp_size(uint64_t amount);
463 
464 /**
465  * Utility for alloc, what is the size that the current offset supports
466  * as a maximum 2**x chunk.
467  * Does not work for offset = 0 (result is infinite).
468  * @param offset: the offset into the memory region.
469  * @return maximum exponent where 2**x is fits the offset, thus
470  * 	offset % (2**x) == 0 and x cannot be larger.
471  */
472 int udb_exp_offset(uint64_t offset);
473 
474 /**
475  * Convert pointer to the data part to a pointer to the base of the chunk.
476  * @param data: data part.
477  * @return pointer to the base of the chunk.
478  */
479 udb_void chunk_from_dataptr_ext(udb_void data);
480 
481 /**
482  * Create empty UDB allocate structure to write to disk to initialize file.
483  * @param a: allocation structure to initialize.  system pointer.
484  */
485 void udb_alloc_init_new(udb_alloc_d* a);
486 
487 /**
488  * Create new udb allocator, with specific data on disk
489  * @param udb: the udb.
490  * @param disk: disk data.
491  * @return udb allocator or NULL on (malloc) failure.
492  */
493 udb_alloc* udb_alloc_create(udb_base* udb, udb_alloc_d* disk);
494 
495 /**
496  * Free the udb allocator from memory.
497  * @param alloc: the udb space allocator.
498  */
499 void udb_alloc_delete(udb_alloc* alloc);
500 
501 /**
502  * Allocate space on the disk.
503  * This may involve closing and reopening the mmap.
504  * @param alloc: the udb space allocator.
505  * @param sz: size you want to use.
506  * @return relative pointer (or 0 on alloc failure).
507  */
508 udb_void udb_alloc_space(udb_alloc* alloc, size_t sz);
509 
510 /**
511  * Allocate space on disk, give already the data you want there.
512  * This may involve closing and reopening the mmap.
513  * @param alloc: the udb space allocator.
514  * @param d: data you want there (system pointer).
515  * @param sz: size you want to use.
516  * @return relative pointer (or 0 on alloc failure).
517  */
518 udb_void udb_alloc_init(udb_alloc* alloc, void* d, size_t sz);
519 
520 /**
521  * free allocated space.  It may shrink the file.
522  * This may involve closing and reopening the mmap.
523  * @param alloc: the udb space allocator.
524  * @param r: relative pointer to data you want to free.
525  * @param sz: the size of the data you stop using.
526  * @return false if the free failed, it failed the close and mmap.
527  */
528 int udb_alloc_free(udb_alloc* alloc, udb_void r, size_t sz);
529 
530 /**
531  * realloc an existing allocated space.  It may grow the file.
532  * This may involve closing and reopening the mmap.
533  * It could also use the existing space where it is now.
534  * @param alloc: the udb space allocator.
535  * @param r: relative pointer to data you want to realloc.
536  *	if 0 then this is alloc_space(), and osz is ignored.
537  * @param osz: the old size of the data.
538  * @param sz: the size of the data you want to get.
539  *	if this is 0 then a free() is done, but please do it directly,
540  *	as you then get a returnvalue (file errors).
541  * @return relative pointer (0 on alloc failure, same if not moved).
542  */
543 udb_void udb_alloc_realloc(udb_alloc* alloc, udb_void r, size_t osz,
544 	size_t sz);
545 
546 /**
547  * Prepare for a lot of new entries.  Grow space for that.
548  * This can involve closing and reopening the mmap.
549  * This space (if large) is going to be released on next free() or close().
550  * @param alloc: the udb space allocator.
551  * @param sz: size of the entries.
552  * @param num: number of entries.
553  * @return false on failure to grow or re-mmap.
554  */
555 int udb_alloc_grow(udb_alloc* alloc, size_t sz, size_t num);
556 
557 /**
558  * attempt to compact the data and move free space to the end
559  * can shrink the db, which calls sync on the db (for portability).
560  * @param udb: the udb base.
561  * @return 0 on failure (to remap the (possibly) changed udb base).
562  */
563 int udb_compact(udb_base* udb);
564 
565 /**
566  * set the udb to inhibit or uninhibit compaction.  Does not perform
567  * the compaction itself if enabled, for that call udb_compact.
568  * @param udb: the udb base
569  * @param inhibit: 0 or 1.
570  */
571 void udb_compact_inhibited(udb_base* udb, int inhibit);
572 
573 /**
574  * Set the alloc type for a newly alloced piece of data
575  * @param alloc: the udb space allocator.
576  * @param r: relativeptr to the data.
577  * @param tp: the type of that block.
578  */
579 void udb_alloc_set_type(udb_alloc* alloc, udb_void r, udb_chunk_type tp);
580 
581 /**
582  * See if a pointer could be valid (it points within valid space),
583  * for the given type side.  For debug checks.
584  * @param udb: the udb
585  * @param to: the ptr (offset).
586  * @param destsize: the size_of of the destination of the pointer.
587  * @return true if it points to a valid region.
588  */
589 int udb_valid_offset(udb_base* udb, udb_void to, size_t destsize);
590 
591 /**
592  * See if a pointer is valid (it points to a chunk).  For debug checks.
593  * @param udb: the udb.
594  * @param to: the ptr (offset).
595  * @return true if it points to the start of a chunks data region.
596  */
597 int udb_valid_dataptr(udb_base* udb, udb_void to);
598 
599 /**
600  * See if a pointer is on the relptrlist for dataptr.  For debug checks.
601  * @param udb: the udb.
602  * @param rptr: the rel_ptr (offset).
603  * @param to: dataptr of the chunk on which ptrlist the rptr is searched.
604  * @return true if rptr is valid and on the ptrlist.
605  */
606 int udb_valid_rptr(udb_base* udb, udb_void rptr, udb_void to);
607 
608 /*** UDB_REL_PTR ***/
609 /**
610  * Init a new UDB rel ptr at NULL.
611  * @param ptr: sysptr, becomes inited.
612  */
613 void udb_rel_ptr_init(udb_rel_ptr* ptr);
614 
615 /**
616  * Unlink a UDB rel ptr.
617  * @param base: the udb base
618  * @param ptr: sysptr, unlinked
619  */
620 void udb_rel_ptr_unlink(void* base, udb_rel_ptr* ptr);
621 
622 /**
623  * Link a UDB rel ptr to a new chunk
624  * @param base: the udb base
625  * @param ptr: sysptr, linked to new value.
626  * @param to: the data to point to (relative ptr).
627  */
628 void udb_rel_ptr_link(void* base, udb_rel_ptr* ptr, udb_void to);
629 
630 /**
631  * Change rel ptr to a new value (point to another record)
632  * @param base: the udb base
633  * @param ptr: sysptr, points to new value.
634  * @param to: the data to point to (relative ptr).
635  */
636 void udb_rel_ptr_set(void* base, udb_rel_ptr* ptr, udb_void to);
637 
638 /**
639  * A chunk has moved and now edit all the relptrs in list to fix them up
640  * @param base: the udb base
641  * @param list: start of the ptr list
642  * @param to: where the chunk has moved to relptr to its userdata.
643  */
644 void udb_rel_ptr_edit(void* base, udb_void list, udb_void to);
645 
646 /**
647  * Get system pointer.  Assumes there is a variable named 'base'
648  * that points to the udb base.
649  * @param ptr: the relative pointer (a sysptr to it).
650  * @return void* to the data.
651  */
652 #define UDB_SYSPTR(ptr) UDB_REL(base, (ptr)->data)
653 
654 /** get sys ptr for char* string */
655 #define UDB_CHAR(ptr) ((char*)UDB_REL(base, ptr))
656 /** get sys ptr for udb_rel_ptr */
657 #define UDB_REL_PTR(ptr) ((udb_rel_ptr*)UDB_REL(base, ptr))
658 /** get sys ptr for udb_glob_d */
659 #define UDB_GLOB(ptr) ((udb_glob_d*)UDB_REL(base, ptr))
660 /** get sys ptr for udb_chunk_d */
661 #define UDB_CHUNK(ptr) ((udb_chunk_d*)UDB_REL(base, ptr))
662 /** get sys ptr for udb_free_chunk_d */
663 #define UDB_FREE_CHUNK(ptr) ((udb_free_chunk_d*)UDB_REL(base, ptr))
664 /** get sys ptr for udb_xl_chunk_d */
665 #define UDB_XL_CHUNK(ptr) ((udb_xl_chunk_d*)UDB_REL(base, ptr))
666 
667 /* udb_ptr */
668 /**
669  * Initialize an udb ptr.  Set to NULL.  (and thus not linked can be deleted).
670  * You MUST set it to 0 before you stop using the ptr.
671  * @param ptr: the ptr to initialise (caller has allocated it).
672  * @param udb: the udb base to link it to.
673  */
674 void udb_ptr_init(udb_ptr* ptr, udb_base* udb);
675 
676 /**
677  * Set udp ptr to a new value.  If set to NULL you can delete it.
678  * @param ptr: the ptr.
679  * @param udb: the udb base to link up with that data segment's administration.
680  * @param newval: new value to point to (udb_void relative file offset to data).
681  */
682 void udb_ptr_set(udb_ptr* ptr, udb_base* udb, udb_void newval);
683 
684 /** dereference udb_ptr */
685 #define UDB_PTR(ptr) (UDB_REL(*((ptr)->base), (ptr)->data))
686 
687 /**
688  * Ease of use udb ptr, allocate space and return ptr to it
689  * You MUST udb_ptr_set it to 0 before you stop using the ptr.
690  * @param base: udb base to use.
691  * @param ptr: ptr is overwritten, can be uninitialised.
692  * @param type: type of the allocation.
693  * 	You need a special type if the block contains udb_rel_ptr's.
694  * 	You can use udb_type_data for plain data.
695  * @param sz: amount to allocate.
696  * @return 0 on alloc failure.
697  */
698 int udb_ptr_alloc_space(udb_ptr* ptr, udb_base* udb, udb_chunk_type type,
699 	size_t sz);
700 
701 /**
702  * Ease of use udb ptr, free space and set ptr to NULL (to it can be deleted).
703  * The space is freed on disk.
704  * @param ptr: the ptr.
705  * @param udb: udb base.
706  * @param sz: the size of the data you stop using.
707  */
708 void udb_ptr_free_space(udb_ptr* ptr, udb_base* udb, size_t sz);
709 
710 /**
711  * Get pointer to the data of the ptr.  or use a macro to cast UDB_PTR to
712  * the type of your structure(.._d)
713  */
714 static inline uint8_t* udb_ptr_data(udb_ptr* ptr) {
715 	return (uint8_t*)UDB_PTR(ptr);
716 }
717 
718 /**
719  * See if udb ptr is null
720  */
721 static inline int udb_ptr_is_null(udb_ptr* ptr) {
722 	return (ptr->data == 0);
723 }
724 
725 /**
726  * Get the type of a udb_ptr chunk.
727  * @param ptr: udb pointer
728  * @return type of chunk */
729 udb_chunk_type udb_ptr_get_type(udb_ptr* ptr);
730 
731 /** Ease of use, create new pointer to destination relptr
732  * You MUST udb_ptr_set it to 0 before you stop using the ptr. */
733 static inline void udb_ptr_new(udb_ptr* ptr, udb_base* udb, udb_rel_ptr* d) {
734 	udb_ptr_init(ptr, udb);
735 	udb_ptr_set(ptr, udb, d->data);
736 }
737 
738 /** Ease of use.  Stop using this ptr */
739 static inline void udb_ptr_unlink(udb_ptr* ptr, udb_base* udb) {
740 	if(ptr->data)
741 		udb_base_unlink_ptr(udb, ptr);
742 }
743 
744 /* Ease of use.  Assign rptr from rptr */
745 static inline void udb_rptr_set_rptr(udb_rel_ptr* dest, udb_base* udb,
746 	udb_rel_ptr* p) {
747 #ifdef UDB_CHECK
748 	if(dest->data) { assert(udb_valid_rptr(udb,
749 		UDB_SYSTOREL(udb->base, dest), dest->data)); }
750 	if(p->data) { assert(udb_valid_rptr(udb,
751 		UDB_SYSTOREL(udb->base, p), p->data)); }
752 #endif
753 	udb_rel_ptr_set(udb->base, dest, p->data);
754 }
755 
756 /* Ease of use.  Assign rptr from ptr */
757 static inline void udb_rptr_set_ptr(udb_rel_ptr* dest, udb_base* udb,
758 	udb_ptr* p) {
759 #ifdef UDB_CHECK
760 	if(dest->data) { assert(udb_valid_rptr(udb,
761 		UDB_SYSTOREL(udb->base, dest), dest->data)); }
762 	if(p->data) { assert(udb_valid_dataptr(udb, p->data)); }
763 #endif
764 	udb_rel_ptr_set(udb->base, dest, p->data);
765 }
766 
767 /* Ease of use.  Assign ptr from rptr */
768 static inline void udb_ptr_set_rptr(udb_ptr* dest, udb_base* udb,
769 	udb_rel_ptr* p) {
770 #ifdef UDB_CHECK
771 	if(p->data) { assert(udb_valid_rptr(udb,
772 		UDB_SYSTOREL(udb->base, p), p->data)); }
773 #endif
774 	udb_ptr_set(dest, udb, p->data);
775 }
776 
777 /* Ease of use.  Assign ptr from ptr */
778 static inline void udb_ptr_set_ptr(udb_ptr* dest, udb_base* udb, udb_ptr* p) {
779 	udb_ptr_set(dest, udb, p->data);
780 }
781 
782 /* Ease of use, zero rptr.  You use this to zero an existing pointer.
783  * A new rptr should be rel_ptr_init-ed before it is taken into use. */
784 static inline void udb_rptr_zero(udb_rel_ptr* dest, udb_base* udb) {
785 #ifdef UDB_CHECK
786 	if(dest->data) { assert(udb_valid_rptr(udb,
787 		UDB_SYSTOREL(udb->base, dest), dest->data)); }
788 #endif
789 	udb_rel_ptr_set(udb->base, dest, 0);
790 }
791 
792 /* Ease of use, zero ptr */
793 static inline void udb_ptr_zero(udb_ptr* dest, udb_base* udb) {
794 	udb_ptr_set(dest, udb, 0);
795 }
796 
797 /** ease of use, delete memory pointed at by relptr */
798 static inline void udb_rel_ptr_free_space(udb_rel_ptr* ptr, udb_base* udb,
799 	size_t sz) {
800 	udb_void d = ptr->data;
801 #ifdef UDB_CHECK
802 	if(d) { assert(udb_valid_rptr(udb, UDB_SYSTOREL(udb->base, ptr), d)); }
803 #endif
804 	udb_rel_ptr_set(udb->base, ptr, 0);
805 	udb_alloc_free(udb->alloc, d, sz);
806 }
807 
808 #endif /* UDB_H */
809