#include <stdlib.h>
#include <assert.h>
+#include <string.h>
#include "macros.h"
#include "ralloc.h"
return ht;
}
+struct set *
+_mesa_set_clone(struct set *set, void *dst_mem_ctx)
+{
+ struct set *clone;
+
+ clone = ralloc(dst_mem_ctx, struct set);
+ if (clone == NULL)
+ return NULL;
+
+ memcpy(clone, set, sizeof(struct set));
+
+ clone->table = ralloc_array(clone, 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));
+
+ return clone;
+}
+
/**
* Frees the given set.
*
uint32_t (*key_hash_function)(const void *key),
bool (*key_equals_function)(const void *a,
const void *b));
+struct set *
+_mesa_set_clone(struct set *set, void *dst_mem_ctx);
+
void
_mesa_set_destroy(struct set *set,
void (*delete_function)(struct set_entry *entry));
_mesa_set_destroy(s, NULL);
}
+
+TEST(set, clone)
+{
+ struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer,
+ _mesa_key_pointer_equal);
+ struct set_entry *entry;
+
+ const void *a = (const void *)10;
+ const void *b = (const void *)20;
+ const void *c = (const void *)30;
+
+ _mesa_set_add(s, a);
+ _mesa_set_add(s, b);
+ _mesa_set_add(s, c);
+
+ entry = _mesa_set_search(s, c);
+ EXPECT_TRUE(entry);
+ EXPECT_EQ(entry->key, c);
+
+ _mesa_set_remove(s, entry);
+ EXPECT_EQ(s->entries, 2);
+
+ struct set *clone = _mesa_set_clone(s, NULL);
+ EXPECT_EQ(clone->entries, 2);
+
+ EXPECT_TRUE(_mesa_set_search(clone, a));
+ EXPECT_TRUE(_mesa_set_search(clone, b));
+ EXPECT_FALSE(_mesa_set_search(clone, c));
+
+ _mesa_set_destroy(s, NULL);
+ _mesa_set_destroy(clone, NULL);
+}