-
Notifications
You must be signed in to change notification settings - Fork 4
/
nalloc.h
83 lines (76 loc) · 2.56 KB
/
nalloc.h
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* \brief A structure aware memory allocator.
*
* Nalloc is a nested replacement of for the standard memory allocation
* functions, tracking the natural structure aware tree-like representation
* of memory dependencies. Freeing a alloced chunk of memory would release
* all of its dependencies.
*
* @note You can not mix malloc and nalloc families of functions for
* a given chunk of memory. Once a chunk is allocated with *nalloc*,
* it can only be freed with *nfree*.
*
* Use:
* @code
* struct matrix { size_t rows, cols; int **data; };
* struct matrix *matrix_new(size_t rows, size_t cols) {
* struct matrix *m = ncalloc(sizeof(*m), NULL);
* m->rows = rows; m->cols = cols;
* m->data = ncalloc(rows * sizeof(*m->data), m);
* for (size_t i = 0; i < rows; i++)
* m->data[i] = nalloc(cols * sizeof(**m->data), m->data);
* return m;
* }
* void matrix_delete(struct matrix *m) { nfree(m); }
* @endcode
*/
#pragma once
#include <stddef.h>
/**
* Allocate a (contiguous) memory chunk.
*
* @param size amount of memory requested (in bytes).
* @param parent pointer to allocated memory chunk from which this
* chunk depends, or NULL.
*
* @return pointer to the allocated memory chunk, or NULL if there was an error.
*/
void *nalloc(size_t size, void *parent);
/**
* Allocate a zeroed (contiguous) memory chunk.
*
* @param count amount of objects
* @param size amount of each memory object requested (in bytes).
* @param parent pointer to allocated memory chunk from which this
* chunk depends, or NULL.
*
* @return pointer to the allocated memory chunk, or NULL if there was an error.
*/
void *ncalloc(size_t count, size_t size, void *parent);
/**
* Modify the size of a memory chunk.
*
* @param mem pointer to allocated memory chunk.
* @param size amount of memory requested (in bytes).
*
* @return pointer to the allocated memory chunk.
* @return NULL if there was an error.
*/
void *nrealloc(void *mem, size_t size);
/**
* Deallocate a memory chunk and all the chunks depending on it.
*
* @param mem pointer to allocated memory chunk.
*
* @return always NULL, can be safely ignored.
*/
void *nfree(void *mem);
/**
* Change the parent of a memory chunk. This will affect the
* dependencies of the entire subtree rooted at the given chunk.
*
* @param mem pointer to allocated memory chunk.
* @param parent pointer to allocated memory chunk from which this
* chunk depends, or NULL.
*/
void nalloc_set_parent(void *mem, void *parent);