core: ptrbox: Add ptrbox_malloc() function

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-03-21 19:01:30 -04:00
parent 7c0f40abbf
commit 2e4f11bfcc
2 changed files with 33 additions and 0 deletions

View File

@@ -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)
{

View File

@@ -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
*