5 ms·
Hierarchical memory allocations are a great concept. But Samba's talloc is kind of bloated, with all the reference counting, destructors and stuff. Here is a s
by esneider 13y ago
Hierarchical memory allocations are a great concept. But Samba's talloc is kind of bloated, with all the reference counting, destructors and stuff.
Here is a simple, 200-line version of just the hierarchical allocator: https://github.com/esneider/talloc https://github.com/esneider/talloc
- huhtenberg 13y agoToo simple ;) void * p; p = talloc(1, NULL); talloc_set_parent(p, p); tfree(p);
- esneider 13y agoYep, you can also do void *p; free(p); free(p); Don't get me wrong, I get where you are going. But C philosophy is that libraries shouldn't do expensive checking to detect programmers' errors.
- huhtenberg 13y agoI'm not going anywhere. Double-free is a known pitfall. People watch for it and a lot of standard libraries have safeguards against it. On the other hand tfree() hangs if a prior call to talloc_set_parent() is passed certain arguments. Just more ammunition to shoot one's own foot. (p, p) might be a degenerate case, but real code will create cyclic references. In such case blaming programmer is the least productive way to handle it.
- esneider 13y agoFair enough... I thought it would be more difficult/inefficient to check for cyclic hierarchies, but it wasn't. Fixed! Thanks.