]> git.ozlabs.org Git - ccan/blob - ccan/jbitset/_info
tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / jbitset / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * jbitset - variable-length bitset (based on libJudy)
7  *
8  * This provides a convenient wrapper for using Judy bitsets; using
9  * integers or pointers as an index, Judy arrays provide an efficient
10  * bit array or bit map of variable size.
11  *
12  * jbitset.h simply contains wrappers for a size_t-indexed bitset, and
13  * jbitset_type.h contain a wrapper macro for pointer bitsets.
14  *
15  * Example:
16  * // Simple analysis of one-byte mallocs.
17  * #include <stdlib.h>
18  * #include <stdio.h>
19  * #include <err.h>
20  * #include <ccan/jbitset/jbitset_type.h>
21  * 
22  * // Define jbit_char_<op> and jbitset_char, for char * bitset.
23  * JBIT_DEFINE_TYPE(char, char);
24  * 
25  * int main(int argc, char *argv[])
26  * {
27  *      unsigned int i, runs, reuse;
28  *      size_t mindist = -1;
29  *      struct jbitset_char *set = jbit_char_new();
30  *      char *p, *prev;
31  * 
32  *      runs = (argc == 1 ? 1000 : atoi(argv[1]));
33  *      if (!runs)
34  *              errx(1, "Invalid number of allocations '%s'", argv[1]);
35  * 
36  *      for (i = 0; i < runs; i++)
37  *              if (!jbit_char_set(set, malloc(1)))
38  *                      errx(1, "same pointer allocated twice!");
39  * 
40  *      // Calculate minimum distance
41  *      prev = jbit_char_first(set)+1;
42  *      for (p = jbit_char_first(set); p; prev = p, p = jbit_char_next(set,p))
43  *              if (p - prev < mindist)
44  *                      mindist = p - prev;
45  * 
46  *      // Free them all, see how many we reallocate.
47  *      for (p = jbit_char_first(set); p; p = jbit_char_next(set, p))
48  *              free(p);
49  *      for (reuse = 0, i = 0; i < runs; i++)
50  *              reuse += jbit_char_test(set, malloc(1));
51  * 
52  *      printf("Allocation density (bytes): %zu\n"
53  *             "Minimum inter-pointer distance: %zu\n"
54  *             "Reuse rate: %.0f%%\n",
55  *             (jbit_char_last(set) - jbit_char_first(set)) / runs,
56  *             mindist,
57  *             100.0 * reuse / runs);
58  *      return 0;
59  * }
60  *
61  * License: LGPL (v2.1 or any later version)
62  * Author: Rusty Russell <rusty@rustcorp.com.au>
63  */
64 int main(int argc, char *argv[])
65 {
66         if (argc != 2)
67                 return 1;
68
69         if (strcmp(argv[1], "depends") == 0) {
70                 printf("ccan/build_assert\n");
71                 printf("ccan/compiler\n");
72                 printf("Judy\n");
73                 return 0;
74         }
75
76         if (strcmp(argv[1], "libs") == 0) {
77                 printf("Judy\n");
78                 return 0;
79         }
80
81         return 1;
82 }