/* * Copyright (c) 2026, Apollo Telephone Laboratories. * Provided under the BSD-3 clause. */ #include #include #include "defconf/ptrbox.h" static inline void ptrbox_insert(struct ptrbox *ptrbox, struct ptrbox_entry *entry) { if (ptrbox == NULL || entry == NULL) { return; } TAILQ_INSERT_TAIL(&ptrbox->entries, entry, link); ++ptrbox->entry_count; } /* * Allocate a new pointer box entry * * @data: Data to be associated with pointer box entry */ static struct ptrbox_entry * ptrbox_alloc_entry(void *data) { struct ptrbox_entry *entry; if (data == NULL) { return NULL; } entry = malloc(sizeof(*entry)); if (entry == NULL) { return NULL; } entry->data = data; return entry; } char * ptrbox_strdup(struct ptrbox *ptrbox, const char *s) { struct ptrbox_entry *entry; char *dup; if (ptrbox == NULL || s == NULL) { return NULL; } if ((dup = strdup(s)) == NULL) { return NULL; } if ((entry = ptrbox_alloc_entry(dup)) == NULL) { free(dup); return NULL; } ptrbox_insert(ptrbox, entry); return dup; } int ptrbox_init(struct ptrbox *ptrbox) { if (ptrbox == NULL) { return -1; } TAILQ_INIT(&ptrbox->entries); ptrbox->entry_count = 0; return 0; } void ptrbox_destroy(struct ptrbox *ptrbox) { struct ptrbox_entry *entry, *tmp; if (ptrbox == NULL) { return; } entry = TAILQ_FIRST(&ptrbox->entries); while (entry != NULL) { if (entry->data != NULL) { free(entry->data); } tmp = entry; entry = TAILQ_NEXT(entry, link); free(tmp); } }