Memory Management

From ITRS
Jump to: navigation, search

Resource Handles

Solutions to the "resource handles have to be closed" problem:

Hierarchical resource pools are different from the other solutions above. Instead of closing resources when leaving a lexical scope, resources are closed when the pool is destroyed. Resource pools are hierarchical because a pool could be further split into sub-pools (e.g. Whole Program > Translation Unit > Function). Destroying a parent pool destroys all its descendants. All high level resource allocating functions (e.g. opening a file, a database connection) take an explicit resource pool parameter. This is similar to dynamic scoping vs. lexical scoping in programming languages.

C++ Destructor with C Syntax

Define a macro, SCOPED which can be used like:

SCOPED(int, vector[]) = malloc(sizeof(*vector)*n);

for GCC/clang it would be expanded to:

int vector[] __attribute__((__cleanup__(free_cleanup)));

and for Visual C++ would be expanded to:

int vector[] = malloc(sizeof(vector[0]*n);
Defer vector_defer(free_cleanup, &vector);

Virtual Memory

Garbage Collection

Tools

See Also

Comments

blog comments powered by Disqus