r2792: got rid of talloc_ldb_alloc() and instead created talloc_realloc_fn(),
authorAndrew Tridgell <tridge@samba.org>
Sun, 3 Oct 2004 06:39:19 +0000 (06:39 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:59:34 +0000 (12:59 -0500)
so talloc now doesn't contain any ldb specific functions.

allow NULL to be passed to a couple more talloc() functions

source/ldap_server/ldap_simple_ldb.c
source/lib/ldb/common/ldb_alloc.c
source/lib/ldb/include/ldb.h
source/lib/talloc.c
source/rpc_server/common/gendb.c
source/rpc_server/netlogon/schannel_state.c
source/rpc_server/samr/samdb.c
talloc_guide.txt

index d4c576a189d1f0c44ab607748938efadce63f9a2..7c6341b872a96726a6bea7e60e96ba0d549eeaaa 100644 (file)
@@ -141,7 +141,7 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_
                attrs[i] = NULL;
        }
 
-       ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+       ldb_set_alloc(ldb, talloc_realloc_fn, samdb);
        count = ldb_search(ldb, basedn, scope, r->filter, attrs, &res);
 
        for (i=0; i < count; i++) {
@@ -284,7 +284,7 @@ invalid_input:
        add_result->dn = talloc_steal(add_reply, dn);
 
        if (result == LDAP_SUCCESS) {
-               ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+               ldb_set_alloc(ldb, talloc_realloc_fn, samdb);
                ldb_ret = ldb_add(ldb, msg);
                if (ldb_ret == 0) {
                        result = LDAP_SUCCESS;
@@ -330,7 +330,7 @@ static NTSTATUS sldb_Del(struct ldapsrv_partition *partition, struct ldapsrv_cal
 
        DEBUG(10, ("sldb_Del: dn: [%s]\n", dn));
 
-       ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+       ldb_set_alloc(ldb, talloc_realloc_fn, samdb);
        ldb_ret = ldb_delete(ldb, dn);
 
        del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
@@ -449,7 +449,7 @@ invalid_input:
        modify_result->dn = talloc_steal(modify_reply, dn);
 
        if (result == LDAP_SUCCESS) {
-               ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+               ldb_set_alloc(ldb, talloc_realloc_fn, samdb);
                ldb_ret = ldb_modify(ldb, msg);
                if (ldb_ret == 0) {
                        result = LDAP_SUCCESS;
@@ -503,7 +503,7 @@ static NTSTATUS sldb_Compare(struct ldapsrv_partition *partition, struct ldapsrv
 
        attrs[0] = NULL;
 
-       ldb_set_alloc(ldb, talloc_ldb_alloc, samdb);
+       ldb_set_alloc(ldb, talloc_realloc_fn, samdb);
        count = ldb_search(ldb, dn, LDB_SCOPE_BASE, filter, attrs, &res);
 
        compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
index 6abd6fa8c3ca82f6c34f74c2ffdc8276648ece14..0053c5e73fa730d48a54ce678959df4f43f69a1b 100644 (file)
@@ -39,7 +39,7 @@
   this allows the user to choose their own allocation function
 */
 int ldb_set_alloc(struct ldb_context *ldb,
-                 void *(*alloc)(void *context, void *ptr, size_t size),
+                 void *(*alloc)(const void *context, void *ptr, size_t size),
                  void *context)
 {
        ldb->alloc_ops.alloc = alloc;
@@ -50,7 +50,7 @@ int ldb_set_alloc(struct ldb_context *ldb,
 /*
   this is the default memory allocation function
 */
-static void *ldb_default_alloc(void *context, void *ptr, size_t size)
+static void *ldb_default_alloc(const void *context, void *ptr, size_t size)
 {
        /* by setting LDB_ALLOC_OFS to non-zero the test suite can
           catch any places where we incorrectly use the libc alloc
index 241f186991287550c7577982562c0c70d6b9c424..4b1401f6735585aeb60d2c72ce5db9e62f89839d 100644 (file)
@@ -145,7 +145,7 @@ struct ldb_backend_ops {
   for pool allocators
 */
 struct ldb_alloc_ops {
-       void *(*alloc)(void *context, void *ptr, size_t size);
+       void *(*alloc)(const void *context, void *ptr, size_t size);
        void *context;
 };
 
@@ -320,7 +320,7 @@ const char *ldb_msg_find_string(const struct ldb_message *msg,
   which often take a context argument
 */
 int ldb_set_alloc(struct ldb_context *ldb,
-                 void *(*alloc)(void *context, void *ptr, size_t size),
+                 void *(*alloc)(const void *context, void *ptr, size_t size),
                  void *context);
 
 /*
index fc655460638b75f310a4dda50dcf29cbb8928f38..5c1f3e7dcd037009881c05c3b27c97e2240d0cb7 100644 (file)
@@ -273,6 +273,10 @@ int talloc_unlink(const void *context, void *ptr)
        struct talloc_chunk *tc_p, *new_p;
        void *new_parent;
 
+       if (context == NULL) {
+               context = null_context;
+       }
+
        if (talloc_unreference(context, ptr) == 0) {
                return 0;
        }
@@ -561,6 +565,10 @@ void *talloc_steal(const void *new_ctx, const void *ptr)
                return NULL;
        }
 
+       if (new_ctx == NULL) {
+               new_ctx = null_context;
+       }
+
        tc = talloc_chunk_from_ptr(ptr);
 
        if (new_ctx == NULL) {
@@ -948,16 +956,11 @@ void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned
 }
 
 /*
-  a alloc function for ldb that uses talloc
+  a function version of talloc_realloc(), so it can be passed as a function pointer
+  to libraries that want a realloc function (a realloc function encapsulates
+  all the basic capabilities of an allocation library, which is why this is useful)
 */
-void *talloc_ldb_alloc(void *context, void *ptr, size_t size)
+void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
 {
-       if (ptr == NULL) {
-               return talloc(context, size);
-       }
-       if (size == 0) {
-               talloc_free(ptr);
-               return NULL;
-       }
-       return talloc_realloc(context, ptr, size);
+       return _talloc_realloc(context, ptr, size, NULL);
 }
index d20aef5ff87de7b4eb76afed966b83edac16291a..0e6ddb0693bdfa5cf6bef66d5c00325ed2a66ccd 100644 (file)
@@ -42,7 +42,7 @@ int gendb_search_v(struct ldb_context *ldb,
                return -1;
        }
 
-       ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(ldb, talloc_realloc_fn, mem_ctx);
 
        count = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, attrs, res);
 
index f64aabb1bf1d653897d68046af432475a32d8b10..88eabc6d3792dafc0fd98fa32c42e51957841112 100644 (file)
@@ -43,7 +43,7 @@ static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
                return NULL;
        }
 
-       ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(ldb, talloc_realloc_fn, mem_ctx);
        
        return ldb;
 }
index 1628919f4e8f72d3a1010eff114d77331fa2e4c7..330741e29d418c1ef34e02747393bf8e494d49fe 100644 (file)
@@ -119,7 +119,7 @@ int samdb_search_free(void *ctx,
                      TALLOC_CTX *mem_ctx, struct ldb_message **res)
 {
        struct samdb_context *sam_ctx = ctx;
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_search_free(sam_ctx->ldb, res);
 }
 
@@ -746,7 +746,7 @@ int samdb_msg_add_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        if (s == NULL || a == NULL) {
                return -1;
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_string(sam_ctx->ldb, msg, a, s);
 }
 
@@ -761,7 +761,7 @@ int samdb_msg_add_delete(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        if (a == NULL) {
                return -1;
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        /* we use an empty replace rather than a delete, as it allows for 
           samdb_replace() to be used everywhere */
        return ldb_msg_add_empty(sam_ctx->ldb, msg, a, LDB_FLAG_MOD_REPLACE);
@@ -811,7 +811,7 @@ int samdb_msg_add_hash(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg,
                return -1;
        }
        memcpy(val.data, hash.hash, 16);
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -832,7 +832,7 @@ int samdb_msg_add_hashes(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        for (i=0;i<count;i++) {
                memcpy(i*16 + (char *)val.data, hashes[i].hash, 16);
        }
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -855,7 +855,7 @@ int samdb_msg_add_logon_hours(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message
        struct ldb_val val;
        val.length = hours.units_per_week / 8;
        val.data = hours.bitmap;
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_msg_add_value(sam_ctx->ldb, msg, attr_name, &val);
 }
 
@@ -868,7 +868,7 @@ int samdb_msg_set_string(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg
        struct samdb_context *sam_ctx = ctx;
        struct ldb_message_element *el;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
 
        el = ldb_msg_find_element(msg, attr_name);
        if (el) {
@@ -897,7 +897,7 @@ int samdb_add(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_add(sam_ctx->ldb, msg);
 }
 
@@ -908,7 +908,7 @@ int samdb_delete(void *ctx, TALLOC_CTX *mem_ctx, const char *dn)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_delete(sam_ctx->ldb, dn);
 }
 
@@ -919,7 +919,7 @@ int samdb_modify(void *ctx, TALLOC_CTX *mem_ctx, struct ldb_message *msg)
 {
        struct samdb_context *sam_ctx = ctx;
 
-       ldb_set_alloc(sam_ctx->ldb, talloc_ldb_alloc, mem_ctx);
+       ldb_set_alloc(sam_ctx->ldb, talloc_realloc_fn, mem_ctx);
        return ldb_modify(sam_ctx->ldb, msg);
 }
 
index 7742ade62356bd66bb0db3d47489cc6cba87f85b..484b4c8228582ebf7d034636bde180312ff7f16a 100644 (file)
@@ -458,3 +458,13 @@ The talloc_realloc_p() macro is equivalent to:
 except that it provides integer overflow protection for the multiply,
 returning NULL if the multiply overflows.
 
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size);
+
+This is a non-macro version of talloc_realloc(), which is useful 
+as libraries sometimes want a ralloc function pointer. A realloc()
+implementation encapsulates the functionality of malloc(), free() and
+realloc() in one call, which is why it is useful to be able to pass
+around a single function pointer.
+