aboutsummaryrefslogtreecommitdiff
path: root/src/res.c
blob: 12d25ce57f0002febee7b05a394bb0413f8b1b6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file res.c
 * Simple resource manager implementation.
 */

#include <stdint.h>
#include <stdlib.h>

#include <ek/res.h>

/**
 * 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);
}