aboutsummaryrefslogtreecommitdiff
path: root/include/cu/res.h
blob: e1a1fc3223e3cbfcba5815ca70a5d666df7ccb44 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */

/**
 * @file res.h
 * Simple resource manager header.
 */

#ifndef CT_RES_H
#define CT_RES_H

#include <stddef.h>

/**
 * Very simple resource manager.
 * Keeps track of all allocations and frees them all at once.
 */
struct res {
	/** Number of allocations. */
	size_t n;
	/** Maximum number of allocations. */
	size_t max;
	/** Pointer to array of pointers to allocations. */
	void **p;
};

/**
 * Create new resource manager.
 *
 * @return Pointer to resource manager.
 */
struct res *res_create();

/**
 * Resource manager wrapper around malloc().
 *
 * @param r Resource manager.
 * @param size Size of allocation.
 * @return Pointer to newly allocated area.
 */
void *res_alloc(struct res *r, size_t size);

/**
 * Add already alloced pointer to manager.
 *
 * @param r Resource manager.
 * @param p Pointer to manage.
 */
void res_add(struct res *r, void *p);

/**
 * Destroy resource manager and free all associated allocations.
 *
 * @param r Resource manager to destroy.
 */
void res_destroy(struct res *r);

#endif /* CT_RES_H */