[03/18] util: Add a pointer map clone function

Submitted by Thomas Helland on April 11, 2018, 6:48 p.m.

Details

Message ID 20180411184827.877-4-thomashelland90@gmail.com
State New
Headers show
Series "Pointer specific data structures" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Thomas Helland April 11, 2018, 6:48 p.m.
---
 src/util/pointer_map.c | 23 +++++++++++++++++++++++
 src/util/pointer_map.h |  3 +++
 2 files changed, 26 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/util/pointer_map.c b/src/util/pointer_map.c
index 8076bd827f..463fa19282 100644
--- a/src/util/pointer_map.c
+++ b/src/util/pointer_map.c
@@ -102,6 +102,29 @@  _mesa_pointer_map_create(void *mem_ctx)
 
    return map;
 }
+struct pointer_map *
+_mesa_pointer_map_clone(struct pointer_map *src, void *dst_mem_ctx)
+{
+   struct pointer_map *pm = ralloc(dst_mem_ctx, struct pointer_map);
+
+   if (pm == NULL)
+      return NULL;
+
+   memcpy(pm, src, sizeof(struct pointer_map));
+
+   pm->map = ralloc_array(pm, struct map_entry, pm->size);
+   pm->metadata = ralloc_array(pm, uint8_t, pm->size);
+
+   if (pm->map == NULL || pm->metadata == NULL) {
+      ralloc_free(pm);
+      return NULL;
+   }
+
+   memcpy(pm->map, src->map, pm->size * sizeof(struct map_entry));
+   memcpy(pm->metadata, src->metadata, pm->size * sizeof(uint8_t));
+
+   return pm;
+}
 
 /**
  * Frees the pointer map.
diff --git a/src/util/pointer_map.h b/src/util/pointer_map.h
index e1cef418d8..4bfc306a5f 100644
--- a/src/util/pointer_map.h
+++ b/src/util/pointer_map.h
@@ -55,6 +55,9 @@  struct pointer_map {
 struct pointer_map *
 _mesa_pointer_map_create(void *mem_ctx);
 
+struct pointer_map *
+_mesa_pointer_map_clone(struct pointer_map *, void *dst_mem_ctx);
+
 void _mesa_pointer_map_destroy(struct pointer_map *map,
                                void (*delete_function)(struct map_entry *entry));