mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2025-12-24 15:20:10 +01:00
util/set: add _mesa_set_copy, a cloning helper without allocation
Reviewed-by: Gert Wollny <gert.wollny@collabora.com> Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36728>
This commit is contained in:
parent
ed246aafd8
commit
06cef7bcc2
2 changed files with 29 additions and 15 deletions
|
|
@ -203,6 +203,29 @@ _mesa_set_create_u32_keys(void *mem_ctx)
|
|||
return _mesa_set_create(mem_ctx, key_u32_hash, key_u32_equals);
|
||||
}
|
||||
|
||||
/* Copy the set from src to dst. */
|
||||
bool
|
||||
_mesa_set_copy(struct set *dst, struct set *src, void *dst_mem_ctx)
|
||||
{
|
||||
/* Copy the structure except the initial storage. */
|
||||
memcpy(dst, src, offsetof(struct set, _initial_storage));
|
||||
dst->mem_ctx = dst_mem_ctx;
|
||||
|
||||
if (src->table != src->_initial_storage) {
|
||||
dst->table = ralloc_array(dst_mem_ctx, struct set_entry, dst->size);
|
||||
if (dst->table == NULL)
|
||||
return false;
|
||||
|
||||
memcpy(dst->table, src->table, dst->size * sizeof(struct set_entry));
|
||||
} else {
|
||||
dst->table = dst->_initial_storage;
|
||||
memcpy(dst->table, src->_initial_storage, sizeof(src->_initial_storage));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* It's preferred to use _mesa_set_copy instead of this to skip ralloc. */
|
||||
struct set *
|
||||
_mesa_set_clone(struct set *set, void *dst_mem_ctx)
|
||||
{
|
||||
|
|
@ -212,21 +235,9 @@ _mesa_set_clone(struct set *set, void *dst_mem_ctx)
|
|||
if (clone == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Copy the whole structure except the initial storage. */
|
||||
memcpy(clone, set, offsetof(struct set, _initial_storage));
|
||||
clone->mem_ctx = dst_mem_ctx;
|
||||
|
||||
if (set->table != set->_initial_storage) {
|
||||
clone->table = ralloc_array(dst_mem_ctx, struct set_entry, clone->size);
|
||||
if (clone->table == NULL) {
|
||||
ralloc_free(clone);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(clone->table, set->table, clone->size * sizeof(struct set_entry));
|
||||
} else {
|
||||
clone->table = clone->_initial_storage;
|
||||
memcpy(clone->table, set->_initial_storage, sizeof(set->_initial_storage));
|
||||
if (!_mesa_set_copy(clone, set, dst_mem_ctx)) {
|
||||
ralloc_free(clone);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return clone;
|
||||
|
|
|
|||
|
|
@ -88,6 +88,9 @@ _mesa_set_create(void *mem_ctx,
|
|||
struct set *
|
||||
_mesa_set_create_u32_keys(void *mem_ctx);
|
||||
|
||||
bool
|
||||
_mesa_set_copy(struct set *dst, struct set *src, void *dst_mem_ctx);
|
||||
|
||||
struct set *
|
||||
_mesa_set_clone(struct set *set, void *dst_mem_ctx);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue