Big renaming. Uses the bp namespace everywhere
[SXSI/libbp.git] / bp-utils.c
1 #include <stdio.h>
2 #include "bp-utils.h"
3
4 static size_t allocated = 0;
5
6 void * bp_malloc(size_t n)
7 {
8   void * res = malloc(n);
9   if (!res) {
10     fprintf(stderr, __FILE__  ": failure to allocate %lu bytes\n", n);
11     exit(1);
12   };
13   allocated += n;
14   return res;
15 }
16
17 void bp_free(void * p)
18 {
19   if (p) free(p);
20 }
21 size_t bp_get_alloc_stats(void)
22 {
23   return allocated;
24 }
25 void bp_reset_alloc_states(void)
26 {
27   allocated = 0;
28 }
29
30 int bp_setbit(unsigned int *B, int i,int x)
31 {
32   int j,l;
33
34   j = i / D;
35   l = i % D;
36   if (x==0) B[j] &= (~(1<<(D-1-l)));
37   else if (x==1) B[j] |= (1<<(D-1-l));
38   else {
39     printf("error setbit x=%d\n",x);
40     exit(1);
41   }
42   return x;
43 }
44
45 int bp_setbits(unsigned int *B, int i, int d, int x)
46 {
47   int j;
48
49   for (j=0; j<d; j++) {
50     bp_setbit(B,i+j,(x>>(d-j-1))&1);
51   }
52   return x;
53 }