https://github.com/esneider/talloc
Hierarchical memory allocator
https://github.com/esneider/talloc
Last synced: 3 months ago
JSON representation
Hierarchical memory allocator
- Host: GitHub
- URL: https://github.com/esneider/talloc
- Owner: esneider
- License: mit
- Created: 2011-09-05T17:39:12.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2015-06-02T16:21:45.000Z (over 9 years ago)
- Last Synced: 2024-07-16T11:59:19.080Z (7 months ago)
- Language: C
- Homepage:
- Size: 222 KB
- Stars: 69
- Watchers: 6
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# talloc
[](https://travis-ci.org/esneider/talloc)
[](https://coveralls.io/r/esneider/talloc?branch=master)
[](https://scan.coverity.com/projects/2438)Talloc is a hierarchical memory allocator. That means that it is similar to
*malloc*, but it can also track the natural tree-like structure of memory
dependencies. Freeing a *talloc-ed* chunk of memory will free all of its
dependencies.## Example
As an example we develop the allocation operations for a bi-dimensional matrix
of integers. For simplicity, the example does not handle out-of-memory errors.```c
struct matrix {
size_t rows;
size_t cols;
int **data;
};struct matrix *new_matrix(size_t rows, size_t cols) {
struct matrix *m = tzalloc(sizeof(*m), NULL);
m->rows = rows;
m->cols = cols;
m->data = tzalloc(rows * sizeof(*m->data), m);for (size_t i = 0; i < rows; i++)
m->data[i] = talloc(cols * sizeof(**m->data), m->data);return m;
}void free_matrix(struct matrix *m) {
tfree(m);
}
```## API
The API is really simple and each function does what you expect. For a detailed
explanation of each function, [see the inline
documentation](https://github.com/esneider/talloc/blob/master/src/talloc.h).The only detail is that you cannot mix the *malloc* and *talloc* families of
functions for a given chunk of memory. Once a chunk is allocated with *talloc*
(*malloc*), it can only be freed with *tfree* (*free*).### Basic
```c
void *talloc(size_t size, void *parent);void *tzalloc(size_t size, void *parent);
void *trealloc(void *mem, size_t size);
void *tfree(void *mem);
```### Advanced
```c
/**
* Get the parent of a talloc'ed memory chunk (the chunk on which it depends).
*/
void *talloc_get_parent(void *mem);/**
* Change the parent of a talloc'ed memory chunk. This will affect the
* dependencies of the entire subtree rooted at the given chunk.
*/
void talloc_set_parent(void *mem, void *parent);/**
* Remove a talloc'ed memory chunk from the dependency tree, taking care of its
* children (they now depend on parent).
*/
void talloc_steal(void *mem, void *parent);
```## Overhead
For each *talloc'ed* chunk of memory, 3 extra pointers are used to maintain the
internal structure. The time overhead for using *talloc* is quite small, since
([as you can see](https://github.com/esneider/talloc/blob/master/src/talloc.c)) the
code is extremely simple.## Credits
This library was created in an attempt to simplify the awesome but
unnecessarily bloated [Samba talloc](http://talloc.samba.org/).## Relevant

Source: [xkcd](http://xkcd.com/835/)