omap-iter-impl.c 1.48 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include <stdlib.h>
#include <string.h>
#include "libmobject-store.h"
Matthieu Dorier's avatar
Matthieu Dorier committed
9 10 11
#include "src/omap-iter/omap-iter-impl.h"
#include "src/util/utlist.h"
#include "src/util/log.h"
12 13 14 15

void omap_iter_create(mobject_store_omap_iter_t* iter)
{
	*iter = (mobject_store_omap_iter_t)calloc(1, sizeof(**iter));
16
	(*iter)->ref_count = 1;
17 18 19 20
}

void omap_iter_free(mobject_store_omap_iter_t iter)
{
21
	if(!iter) return;
22 23
	iter->ref_count -= 1;
	if(iter->ref_count > 0) return;
24

25 26 27 28
	omap_iter_node_t elt, tmp;

	DL_FOREACH_SAFE(iter->head, elt, tmp) {
		DL_DELETE(iter->head, elt);
29 30
		if(elt->key)   free((void*)(elt->key));
		if(elt->value) free((void*)(elt->value));
31
		free((void*)(elt));
32
	}
33 34 35 36

	free(iter);
}

37 38 39 40 41 42
void omap_iter_incr_ref(mobject_store_omap_iter_t iter)
{
	if(!iter) return;
	iter->ref_count += 1;
}

43 44 45 46
void omap_iter_append(mobject_store_omap_iter_t iter, 
                      const char* key, const char* val, 
                      size_t val_size)
{
47 48
	MOBJECT_ASSERT(iter, "trying to append to a NULL iterator");

49
	omap_iter_node_t item = (omap_iter_node_t)calloc(1, sizeof(*item));
50 51
	item->key             = key ? strdup(key) : NULL;
	item->key_size        = key ? strlen(key)+1 : 0;
52
	item->value_size      = val_size;
53 54
	item->value           = val_size ? (char*)malloc(val_size) : NULL;
	if(val) memcpy(item->value, val, val_size);
55 56 57 58 59

	DL_APPEND(iter->head, item);

	if(iter->current == NULL)
		iter->current = iter->head;
60 61

	iter->num_items += 1;
62
}