Merge branch 'judy'
[ccan] / ccan / jbitset / jbitset.c
1 #include <ccan/jbitset/jbitset.h>
2 #include <ccan/build_assert/build_assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 struct jbitset *jbit_new(void)
7 {
8         struct jbitset *set;
9
10         /* Judy uses Word_t, we use size_t. */
11         BUILD_ASSERT(sizeof(size_t) == sizeof(Word_t));
12
13         set = malloc(sizeof(*set));
14         if (set) {
15                 set->judy = NULL;
16                 memset(&set->err, 0, sizeof(set->err));
17                 set->errstr = NULL;
18         }
19         return set;
20 }
21
22 const char *jbit_error_(struct jbitset *set)
23 {
24         char *str;
25         free((char *)set->errstr);
26         set->errstr = str = malloc(100);
27         if (!set->errstr)
28                 return "out of memory";
29
30         sprintf(str,
31                 "JU_ERRNO_* == %d, ID == %d\n",
32                 JU_ERRNO(&set->err), JU_ERRID(&set->err));
33         return str;
34 }
35
36 void jbit_free(const struct jbitset *set)
37 {
38         free((char *)set->errstr);
39         Judy1FreeArray((PPvoid_t)&set->judy, PJE0);
40         free((void *)set);
41 }