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