summaryrefslogtreecommitdiff
path: root/ccan/jmap/jmap.c
blob: 8982a7155ca73bc7059c441d4cdff539e13c40f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Licensed under LGPLv2.1+ - see LICENSE file for details */
#include <ccan/jmap/jmap.h>
#include <ccan/build_assert/build_assert.h>
#include <stdlib.h>
#include <string.h>

struct jmap *jmap_new_(size_t size)
{
	struct jmap *map;

	/* Judy uses unsigned long for Word_t, we use unsigned long. */
	BUILD_ASSERT(sizeof(Word_t) == sizeof(unsigned long));
	/* We also put pointers into Judy, in jmap_types.h */
	BUILD_ASSERT(sizeof(Word_t) >= sizeof(void *));

	assert(size >= sizeof(*map));
	map = malloc(size);
	if (map) {
		map->judy = NULL;
		memset(&map->err, 0, sizeof(map->err));
		map->errstr = NULL;
		map->num_accesses = 0;
		map->acc_value = NULL;
		map->acc_index = 0;
		map->funcname = NULL;
	}
	return map;
}

const char *jmap_error_str_(struct jmap *map)
{
	char *str;
	free((char *)map->errstr);
	map->errstr = str = malloc(100);
	if (!map->errstr)
		return "out of memory";

	sprintf(str,
		"JU_ERRNO_* == %d, ID == %d\n",
		JU_ERRNO(&map->err), JU_ERRID(&map->err));
	return str;
}

void jmap_free_(const struct jmap *map)
{
	free((char *)map->errstr);
	JudyLFreeArray((PPvoid_t)&map->judy, PJE0);
	free((void *)map);
}