Initial commit
[cgperf.git] / bool-array.c
blob88f3c5a85178081ba8ed82cbe444e70364e8f9ac
1 #ifndef CGPERF_BOOL_ARRAY_C
2 #define CGPERF_BOOL_ARRAY_C
3 #include <stdbool.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "c_fixing.h"
7 #include "globals.h"
8 #include "options.h"
9 #include "bool-array.h"
10 /*------------------------------------------------------------------------------------------------*/
11 #include "namespace/globals.h"
12 #include "namespace/options.h"
13 #include "namespace/bool-array.h"
14 /*------------------------------------------------------------------------------------------------*/
15 /*{{{ ba_new */
16 static struct Bool_Array *ba_new(u32 size)
18 struct Bool_Array *t;
20 t = calloc(1, sizeof(*t));
21 t->size = size;
22 t->iteration_number = 1;
23 t->storage_array = calloc(size, sizeof(*(t->storage_array)));
24 if (OPTS(DEBUG))
25 fprintf (stderr, "\nbool array size = %d, total bytes = %d\n", t->size, (u32)(t->size * sizeof(t->storage_array[0])));
26 return t;
27 }/*}}}*/
28 /*{{{ ba_del */
29 static void ba_del(struct Bool_Array *t)
31 if (OPTS(DEBUG))
32 fprintf(stderr, "\ndumping boolean array information\nsize = %d\niteration number = %d\nend of array dump\n", t->size, t->iteration_number);
33 free(t->storage_array);
34 free(t);
35 }/*}}}*/
36 /*{{{ ba_clear */
37 /* resets all bits to zero */
38 static void ba_clear(struct Bool_Array *t)
41 * If we wrap around it's time to zero things out again! However, this only occurs once
42 * about every 2^32 iterations, so it will not happen more frequently than once per second.
44 ++(t->iteration_number);
45 if (t->iteration_number == 0) {
46 t->iteration_number = 1;
47 memset(t->storage_array, 0, t->size * sizeof(*(t->storage_array)));
48 if (OPTS(DEBUG)) {
49 fprintf(stderr, "(re-initialized bool_array)\n");
50 fflush(stderr);
53 }/*}}}*/
54 /*{{{ ba_set_bit */
56 * Sets the specified bit to true.
57 * Returns its previous value (false or true).
59 static bool ba_set_bit(struct Bool_Array *t, u32 index)
61 if (t->storage_array[index] == t->iteration_number)
62 /* the bit was set since the last clear() call */
63 return true;
64 else {
65 /* the last operation on this bit was clear(). Set it now. */
66 t->storage_array[index] = t->iteration_number;
67 return false;
69 }/*}}}*/
70 /*------------------------------------------------------------------------------------------------*/
71 #define EPILOG
72 #include "namespace/globals.h"
73 #include "namespace/options.h"
74 #include "namespace/bool-array.h"
75 #undef EPILOG
76 /*------------------------------------------------------------------------------------------------*/
77 #endif