Big renaming. Uses the bp namespace everywhere
[SXSI/libbp.git] / bp-utils.c
diff --git a/bp-utils.c b/bp-utils.c
new file mode 100644 (file)
index 0000000..b1d68ce
--- /dev/null
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include "bp-utils.h"
+
+static size_t allocated = 0;
+
+void * bp_malloc(size_t n)
+{
+  void * res = malloc(n);
+  if (!res) {
+    fprintf(stderr, __FILE__  ": failure to allocate %lu bytes\n", n);
+    exit(1);
+  };
+  allocated += n;
+  return res;
+}
+
+void bp_free(void * p)
+{
+  if (p) free(p);
+}
+size_t bp_get_alloc_stats(void)
+{
+  return allocated;
+}
+void bp_reset_alloc_states(void)
+{
+  allocated = 0;
+}
+
+int bp_setbit(unsigned int *B, int i,int x)
+{
+  int j,l;
+
+  j = i / D;
+  l = i % D;
+  if (x==0) B[j] &= (~(1<<(D-1-l)));
+  else if (x==1) B[j] |= (1<<(D-1-l));
+  else {
+    printf("error setbit x=%d\n",x);
+    exit(1);
+  }
+  return x;
+}
+
+int bp_setbits(unsigned int *B, int i, int d, int x)
+{
+  int j;
+
+  for (j=0; j<d; j++) {
+    bp_setbit(B,i+j,(x>>(d-j-1))&1);
+  }
+  return x;
+}