diff options
author | Kenneth Graunke <[email protected]> | 2015-03-27 19:24:33 -0700 |
---|---|---|
committer | Kenneth Graunke <[email protected]> | 2015-04-02 14:19:41 -0700 |
commit | 33f0f68d590a460f84a0df0de10f29c4a582d7e7 (patch) | |
tree | c9a0c4a69f1cc18bae2815d3c6f77e2f1f692b3e | |
parent | ca3b4d6d17a0f95b287e87888c9d893be94f0301 (diff) |
ralloc: Implement a new ralloc_adopt() API.
ralloc_adopt() reparents all children from one context to another.
Conceptually, ralloc_adopt(new_ctx, old_ctx) behaves like this
pseudocode:
foreach child of old_ctx:
ralloc_steal(new_ctx, child)
However, ralloc provides no way to iterate over a memory context's
children, and ralloc_adopt does this task more efficiently anyway.
One potential use of this is to implement a memory-sweeper pass: first,
steal all of a context's memory to a temporary context. Then, walk over
anything that should be kept, and ralloc_steal it back to the original
context. Finally, free the temporary context. This works when the
context is something that can't be freed (i.e. an important structure).
Signed-off-by: Kenneth Graunke <[email protected]>
Reviewed-by: Jason Ekstrand <[email protected]>
-rw-r--r-- | src/util/ralloc.c | 26 | ||||
-rw-r--r-- | src/util/ralloc.h | 7 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/util/ralloc.c b/src/util/ralloc.c index 36bc61fd075..01719c888b1 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -271,6 +271,32 @@ ralloc_steal(const void *new_ctx, void *ptr) add_child(parent, info); } +void +ralloc_adopt(const void *new_ctx, void *old_ctx) +{ + ralloc_header *new_info, *old_info, *child; + + if (unlikely(old_ctx == NULL)) + return; + + old_info = get_header(old_ctx); + new_info = get_header(new_ctx); + + /* If there are no children, bail. */ + if (unlikely(old_info->child == NULL)) + return; + + /* Set all the children's parent to new_ctx; get a pointer to the last child. */ + for (child = old_info->child; child->next != NULL; child = child->next) { + child->parent = new_info; + } + + /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */ + child->next = new_info->child; + new_info->child = old_info->child; + old_info->child = NULL; +} + void * ralloc_parent(const void *ptr) { diff --git a/src/util/ralloc.h b/src/util/ralloc.h index f088a36274d..01f102ba244 100644 --- a/src/util/ralloc.h +++ b/src/util/ralloc.h @@ -235,6 +235,13 @@ void ralloc_free(void *ptr); void ralloc_steal(const void *new_ctx, void *ptr); /** + * Reparent all children from one context to another. + * + * This effectively calls ralloc_steal(new_ctx, child) for all children of \p old_ctx. + */ +void ralloc_adopt(const void *new_ctx, void *old_ctx); + +/** * Return the given pointer's ralloc context. */ void *ralloc_parent(const void *ptr); |