container_of: don't put member_ptr in container_off.
[ccan] / ccan / jmap / jmap.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include <ccan/jmap/jmap.h>
3 #include <ccan/build_assert/build_assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 struct jmap *jmap_new_(size_t size)
8 {
9         struct jmap *map;
10
11         /* Judy uses unsigned long for Word_t, we use unsigned long. */
12         BUILD_ASSERT(sizeof(Word_t) == sizeof(unsigned long));
13         /* We also put pointers into Judy, in jmap_types.h */
14         BUILD_ASSERT(sizeof(Word_t) >= sizeof(void *));
15
16         assert(size >= sizeof(*map));
17         map = malloc(size);
18         if (map) {
19                 map->judy = NULL;
20                 memset(&map->err, 0, sizeof(map->err));
21                 map->errstr = NULL;
22                 map->num_accesses = 0;
23                 map->acc_value = NULL;
24                 map->acc_index = 0;
25                 map->funcname = NULL;
26         }
27         return map;
28 }
29
30 const char *jmap_error_str_(struct jmap *map)
31 {
32         char *str;
33         free((char *)map->errstr);
34         map->errstr = str = malloc(100);
35         if (!map->errstr)
36                 return "out of memory";
37
38         sprintf(str,
39                 "JU_ERRNO_* == %d, ID == %d\n",
40                 JU_ERRNO(&map->err), JU_ERRID(&map->err));
41         return str;
42 }
43
44 void jmap_free_(const struct jmap *map)
45 {
46         free((char *)map->errstr);
47         JudyLFreeArray((PPvoid_t)&map->judy, PJE0);
48         free((void *)map);
49 }