From 6153bf6edb45e4ad1759e3db69e3a7358ae28b4e Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sun, 23 Aug 2026 05:40:51 +0000 Subject: 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. --- head/defconf/ptrbox.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ head/defconf/state.h | 3 +++ 2 files changed, 61 insertions(+) create mode 100644 head/defconf/ptrbox.h (limited to 'head') 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 +#include +#include + +/* + * 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 #include #include +#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; }; /* -- cgit v1.2.3