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