/* SPDX-License-Identifier: GPL-3.0-or-later */ /** * @file res.c * Simple resource manager implementation. */ #include #include #include /** * Grow resource manager buffer. * Doubles every call. * * @param r Resource manager to expand. */ static void res_expand(struct res *r) { r->max *= 2; r->p = realloc(r->p, r->max * sizeof(void *)); } struct res *res_create() { struct res *r = malloc(sizeof(struct res)); r->n = 0; /* arbitrary number */ r->max = 1024; r->p = calloc(1, r->max * sizeof(void *)); return r; } void res_add(struct res *r, void *p) { if (r->n >= r->max) res_expand(r); r->p[r->n++] = p; } void *res_alloc(struct res *r, size_t size) { void *p = malloc(size); if (!p) return NULL; res_add(r, p); return p; } void res_destroy(struct res *r) { for (size_t i = 0; i < r->n; ++i) free(r->p[i]); free(r->p); free(r); }