diff options
| author | Chloe M. <chloe@aptel.org> | 2026-08-23 05:40:51 +0000 |
|---|---|---|
| committer | Chloe M. <chloe@aptel.org> | 2026-08-23 05:40:51 +0000 |
| commit | 6153bf6edb45e4ad1759e3db69e3a7358ae28b4e (patch) | |
| tree | c6cad42382502e7f3a9cc840b6df12480d07f1ef /head | |
| parent | 78b925a4d86b57e58e6322b7cad7bc47098618fd (diff) | |
core: Add support for pointer boxes
Pointer boxes are a way to manage memory and have all references
released after operation, this way they do not need to be manually
managed.
Signed-off-by: Chloe M. <chloe@aptel.org>
Diffstat (limited to 'head')
| -rw-r--r-- | head/defconf/ptrbox.h | 58 | ||||
| -rw-r--r-- | head/defconf/state.h | 3 |
2 files changed, 61 insertions, 0 deletions
diff --git a/head/defconf/ptrbox.h b/head/defconf/ptrbox.h new file mode 100644 index 0000000..3358595 --- /dev/null +++ b/head/defconf/ptrbox.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#ifndef DEFCONF_PTRBOX_H +#define DEFCONF_PTRBOX_H 1 + +#include <sys/queue.h> +#include <stdint.h> +#include <stddef.h> + +/* + * Represents a pointer box entry + * + * @data: Allocated data + * @link: TAILQ link + */ +struct ptrbox_entry { + void *data; + TAILQ_ENTRY(ptrbox_entry) link; +}; + +/* + * A pointer box stores references to memory to be freed later on + * + * @entries: Entries within pointer box + * @entry_count: Number of pointer box entries + */ +struct ptrbox { + TAILQ_HEAD(, ptrbox_entry) entries; + size_t entry_count; +}; + +/* + * Wrapper over strdup() but saved in a pointer box + * + * @ptrbox: Pointer box to allocate into + * @s: String to dup + * + * Returns the base of the dup'd string on success, otherwise + * NULL on failure. + */ +char *ptrbox_strdup(struct ptrbox *ptrbox, const char *s); + +/* + * Initialize a pointer box + */ +int ptrbox_init(struct ptrbox *ptrbox); + +/* + * Destroy a pointer box and all of its entries + * + * @ptrbox: Pointer box to destroy + */ +void ptrbox_destroy(struct ptrbox *ptrbox); + +#endif /* !DEFCONF_PTRBOX_H */ diff --git a/head/defconf/state.h b/head/defconf/state.h index 524af3b..aa99105 100644 --- a/head/defconf/state.h +++ b/head/defconf/state.h @@ -9,16 +9,19 @@ #include <stdint.h> #include <stddef.h> #include <stdio.h> +#include "defconf/ptrbox.h" /* * DEFCONF state machine * * @in_fp: Input file pointer * @lex_cache: Putback cache + * @ptrbox: Global pointer box */ struct defconf_state { FILE *in_fp; int lex_cache; + struct ptrbox ptrbox; }; /* |
