diff --git a/core/ptrbox.c b/core/ptrbox.c index 6db8e2c..0e10892 100644 --- a/core/ptrbox.c +++ b/core/ptrbox.c @@ -83,6 +83,28 @@ ptrbox_strdup(struct ptrbox *ptrbox, const char *s) return p; } +void * +ptrbox_malloc(struct ptrbox *ptrbox, size_t length) +{ + struct ptrbox_entry *entry; + void *mem; + + if (ptrbox == NULL) { + return NULL; + } + + if ((mem = malloc(length)) == NULL) { + return NULL; + } + + if ((entry = entry_from_data(mem)) == NULL) { + return NULL; + } + + ptrbox_add_entry(ptrbox, entry); + return mem; +} + int ptrbox_init(struct ptrbox *ptrbox) { diff --git a/include/common/ptrbox.h b/include/common/ptrbox.h index 1e4aa0d..dd4103f 100644 --- a/include/common/ptrbox.h +++ b/include/common/ptrbox.h @@ -60,6 +60,17 @@ int ptrbox_init(struct ptrbox *ptrbox); */ char *ptrbox_strdup(struct ptrbox *ptrbox, const char *s); +/* + * Allocate a number of bytes on the heap and store the reference + * + * @ptrbox: Pointer box + * @length: Number of bytes to allocate + * + * Returns the base of the allocated memory on success, otherwise + * NULL on failure. + */ +void *ptrbox_malloc(struct ptrbox *ptrbox, size_t length); + /* * Destroy all elements within a pointer box *