tdb2: implement tdb_chainlock_read/tdb_chainunlock_read.
[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 unsigned long directly. */
11         BUILD_ASSERT(sizeof(unsigned long) == sizeof(Word_t));
12         /* We pack pointers into jbitset (in jbitset_type.h) */
13         BUILD_ASSERT(sizeof(Word_t) >= sizeof(void *));
14
15         set = malloc(sizeof(*set));
16         if (set) {
17                 set->judy = NULL;
18                 memset(&set->err, 0, sizeof(set->err));
19                 set->errstr = NULL;
20         }
21         return set;
22 }
23
24 const char *jbit_error_(struct jbitset *set)
25 {
26         char *str;
27         free((char *)set->errstr);
28         set->errstr = str = malloc(100);
29         if (!set->errstr)
30                 return "out of memory";
31
32         sprintf(str,
33                 "JU_ERRNO_* == %d, ID == %d\n",
34                 JU_ERRNO(&set->err), JU_ERRID(&set->err));
35         return str;
36 }
37
38 void jbit_free(const struct jbitset *set)
39 {
40         free((char *)set->errstr);
41         Judy1FreeArray((PPvoid_t)&set->judy, PJE0);
42         free((void *)set);
43 }