Browse Source Download (without any required ccan dependencies)
jset
set of pointers (based on libJudy)
Rusty Russell <rusty@rustcorp.com.au>
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.
// 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;
}
LGPL (v2.1 or any later version)