r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[abartlet/samba.git/.git] / source3 / lib / bitmap.c
index 8121c38bd5b1b0baa788b89faf7674b72f30bd82..a6b52efe096946c1bbef71769710f317504f6a56 100644 (file)
@@ -30,12 +30,12 @@ struct bitmap *bitmap_allocate(int n)
 {
        struct bitmap *bm;
 
-       bm = (struct bitmap *)malloc(sizeof(*bm));
+       bm = SMB_MALLOC_P(struct bitmap);
 
        if (!bm) return NULL;
        
        bm->n = n;
-       bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
+       bm->b = SMB_MALLOC_ARRAY(uint32, (n+31)/32);
        if (!bm->b) {
                SAFE_FREE(bm);
                return NULL;
@@ -59,6 +59,44 @@ void bitmap_free(struct bitmap *bm)
        SAFE_FREE(bm);
 }
 
+/****************************************************************************
+talloc a bitmap
+****************************************************************************/
+struct bitmap *bitmap_talloc(TALLOC_CTX *mem_ctx, int n)
+{
+       struct bitmap *bm;
+
+       if (!mem_ctx) return NULL;
+
+       bm = TALLOC_P(mem_ctx, struct bitmap);
+
+       if (!bm) return NULL;
+       
+       bm->n = n;
+       bm->b = TALLOC_ARRAY(mem_ctx, uint32, (n+31)/32);
+       if (!bm->b) {
+               return NULL;
+       }
+
+       memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
+
+       return bm;
+}
+
+/****************************************************************************
+copy as much of the source bitmap as will fit in the destination bitmap.
+****************************************************************************/
+
+int bitmap_copy(struct bitmap * const dst, const struct bitmap * const src)
+{
+        int count = MIN(dst->n, src->n);
+
+        SMB_ASSERT(dst->b != src->b);
+       memcpy(dst->b, src->b, sizeof(dst->b[0])*(count+31)/32);
+
+        return count;
+}
+
 /****************************************************************************
 set a bit in a bitmap
 ****************************************************************************/
@@ -105,7 +143,7 @@ wraparound
 ****************************************************************************/
 int bitmap_find(struct bitmap *bm, unsigned ofs)
 {
-       int i, j;
+       unsigned int i, j;
 
        if (ofs > bm->n) ofs = 0;