[netfilter-cvslog] r3365 - in trunk/nfsim: core kernelenv/include

rusty at netfilter.org rusty at netfilter.org
Tue Dec 14 05:52:49 CET 2004


Author: rusty at netfilter.org
Date: 2004-12-14 05:52:48 +0100 (Tue, 14 Dec 2004)
New Revision: 3365

Modified:
   trunk/nfsim/core/talloc.c
   trunk/nfsim/core/talloc.h
   trunk/nfsim/kernelenv/include/kernelenv.h
Log:
Updated talloc from junkcode (gets talloc_unlink() right for real parent)


Modified: trunk/nfsim/core/talloc.c
===================================================================
--- trunk/nfsim/core/talloc.c	2004-12-14 04:42:44 UTC (rev 3364)
+++ trunk/nfsim/core/talloc.c	2004-12-14 04:52:48 UTC (rev 3365)
@@ -242,7 +242,7 @@
   talloc_reference() has done. The context and pointer arguments
   must match those given to a talloc_reference()
 */
-void *talloc_unreference(const void *context, const void *ptr)
+static int talloc_unreference(const void *context, const void *ptr)
 {
 	struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
 	struct talloc_reference_handle *h;
@@ -256,16 +256,65 @@
 		if ((p==NULL && context==NULL) || p+1 == context) break;
 	}
 	if (h == NULL) {
-		return NULL;
+		return -1;
 	}
 
 	talloc_set_destructor(h, NULL);
 	_TLIST_REMOVE(tc->refs, h);
 	talloc_free(h);
-	return discard_const_p(void, ptr);
+	return 0;
 }
 
 /*
+  remove a specific parent context from a pointer. This is a more
+  controlled varient of talloc_free()
+*/
+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;
+	}
+
+	if (context == NULL) {
+		if (talloc_parent_chunk(ptr) != NULL) {
+			return -1;
+		}
+	} else {
+		if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
+			return -1;
+		}
+	}
+	
+	tc_p = talloc_chunk_from_ptr(ptr);
+
+	if (tc_p->refs == NULL) {
+		return talloc_free(ptr);
+	}
+
+	new_p = talloc_parent_chunk(tc_p->refs);
+	if (new_p) {
+		new_parent = new_p+1;
+	} else {
+		new_parent = NULL;
+	}
+
+	if (talloc_unreference(new_parent, ptr) != 0) {
+		return -1;
+	}
+
+	talloc_steal(new_parent, ptr);
+
+	return 0;
+}
+
+/*
   add a name to an existing pointer - va_list version
 */
 static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
@@ -393,7 +442,7 @@
 
 	if (tc->refs) {
 		talloc_reference_destructor(tc->refs);
-		return 0;
+		return -1;
 	}
 
 	if (tc->destructor) {
@@ -421,11 +470,13 @@
 			struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
 			if (p) new_parent = p+1;
 		}
-		if (new_parent == null_context) {
-			struct talloc_chunk *p = talloc_parent_chunk(ptr);
-			if (p) new_parent = p+1;
+		if (talloc_free(child) == -1) {
+			if (new_parent == null_context) {
+				struct talloc_chunk *p = talloc_parent_chunk(ptr);
+				if (p) new_parent = p+1;
+			}
+			talloc_steal(new_parent, child);
 		}
-		talloc_free(talloc_steal(new_parent, child));
 	}
 
 	if (tc->parent) {
@@ -517,6 +568,10 @@
 		return NULL;
 	}
 
+	if (new_ctx == NULL) {
+		new_ctx = null_context;
+	}
+
 	tc = talloc_chunk_from_ptr(ptr);
 
 	if (new_ctx == NULL) {
@@ -888,22 +943,41 @@
 */
 void *talloc_array_size(const void *ctx, size_t el_size, unsigned count, const char *name)
 {
-	if (count == 0 ||
-	    count >= MAX_TALLOC_SIZE/el_size) {
+	if (count >= MAX_TALLOC_SIZE/el_size) {
 		return NULL;
 	}
 	return talloc_named_const(ctx, el_size * count, name);
 }
 
+/*
+  alloc an zero array, checking for integer overflow in the array size
+*/
+void *talloc_zero_array_size(const void *ctx, size_t el_size, unsigned count, const char *name)
+{
+	if (count >= MAX_TALLOC_SIZE/el_size) {
+		return NULL;
+	}
+	return talloc_zero_named_const(ctx, el_size * count, name);
+}
 
+
 /*
   realloc an array, checking for integer overflow in the array size
 */
 void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
 {
-	if (count == 0 ||
-	    count >= MAX_TALLOC_SIZE/el_size) {
+	if (count >= MAX_TALLOC_SIZE/el_size) {
 		return NULL;
 	}
 	return _talloc_realloc(ctx, ptr, el_size * count, name);
 }
+
+/*
+  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_realloc_fn(const void *context, void *ptr, size_t size)
+{
+	return _talloc_realloc(context, ptr, size, NULL);
+}

Modified: trunk/nfsim/core/talloc.h
===================================================================
--- trunk/nfsim/core/talloc.h	2004-12-14 04:42:44 UTC (rev 3364)
+++ trunk/nfsim/core/talloc.h	2004-12-14 04:52:48 UTC (rev 3365)
@@ -33,6 +33,7 @@
 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
 #define talloc_zero(ctx, type) (type *)talloc_zero_named_const(ctx, sizeof(type), #type)
 #define talloc_array(ctx, type, count) (type *)talloc_array_size(ctx, sizeof(type), count, __location__)
+#define talloc_zero_array(ctx, type, count) (type *)talloc_zero_array_size(ctx, sizeof(type), count, __location__)
 #define talloc_realloc(ctx, p, type, count) (type *)talloc_realloc_array(ctx, p, sizeof(type), count, __location__)
 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
@@ -54,7 +55,7 @@
 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
 void talloc_increase_ref_count(const void *ptr);
 void *talloc_reference(const void *context, const void *ptr);
-void *talloc_unreference(const void *context, const void *ptr);
+int talloc_unlink(const void *context, void *ptr);
 void talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
 void talloc_set_name_const(const void *ptr, const char *name);
 void *talloc_named(const void *context, size_t size, 
@@ -80,6 +81,8 @@
 char *talloc_asprintf_append(char *s,
 			     const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
 void *talloc_array_size(const void *ctx, size_t el_size, unsigned count, const char *name);
+void *talloc_zero_array_size(const void *ctx, size_t el_size, unsigned count, const char *name);
 void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
+void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
 #endif
 

Modified: trunk/nfsim/kernelenv/include/kernelenv.h
===================================================================
--- trunk/nfsim/kernelenv/include/kernelenv.h	2004-12-14 04:42:44 UTC (rev 3364)
+++ trunk/nfsim/kernelenv/include/kernelenv.h	2004-12-14 04:52:48 UTC (rev 3365)
@@ -114,7 +114,7 @@
 		      : talloc_size(__kmalloc_ctx, (s)))
 #endif
 
-#define vfree(p)   talloc_unreference(__vmalloc_ctx, (p))
+#define vfree(p)   talloc_unlink(__vmalloc_ctx, (p))
 #define kfree(p)   talloc_free(p)
 
 #define synchronize_net() 




More information about the netfilter-cvslog mailing list