svn commit: samba r7778 - in branches/SAMBA_4_0/source/lib/talloc: .

tridge at samba.org tridge at samba.org
Mon Jun 20 05:03:55 GMT 2005


Author: tridge
Date: 2005-06-20 05:03:54 +0000 (Mon, 20 Jun 2005)
New Revision: 7778

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=7778

Log:
added talloc_find_parent_bytype() and talloc_find_parent_byname()

These provide a way to find a parent of a ptr that is of a given
type. I will be using this to find the event context in smbd, relying
on the fact that everything is a child of the top level event
context. I did look at the alternatives, and found that passing the
event context to just about every call in smbd was getting way too
complex (we need to get it to anything that can do a ldb operation, as
that can invoke ldap). 

So this method avoids a global, and seems to work nicely

Modified:
   branches/SAMBA_4_0/source/lib/talloc/talloc.c
   branches/SAMBA_4_0/source/lib/talloc/talloc.h
   branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt


Changeset:
Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-06-20 04:59:10 UTC (rev 7777)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-06-20 05:03:54 UTC (rev 7778)
@@ -1108,3 +1108,27 @@
 
 	return tc->size;
 }
+
+/*
+  find a parent of this context that has the given name, if any
+*/
+void *talloc_find_parent_byname(const void *context, const char *name)
+{
+	struct talloc_chunk *tc;
+
+	if (context == NULL) {
+		return NULL;
+	}
+
+	tc = talloc_chunk_from_ptr(context);
+	while (tc->prev) {
+		tc = tc->prev;
+	}
+	while (tc->parent && (!tc->name || strcmp(tc->name, name))) {
+		tc = tc->parent;
+	}
+	if (tc == NULL) {
+		return NULL;
+	}
+	return (void *)(tc+1);
+}

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.h
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-06-20 04:59:10 UTC (rev 7777)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-06-20 05:03:54 UTC (rev 7778)
@@ -65,7 +65,9 @@
 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
 
+#define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
 
+
 #if TALLOC_DEPRECATED
 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
 #define talloc_p(ctx, type) talloc(ctx, type)
@@ -127,6 +129,7 @@
 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
 void *talloc_autofree_context(void);
 size_t talloc_get_size(const void *ctx);
+void *talloc_find_parent_byname(const void *ctx, const char *name);
 
 #endif
 

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-06-20 04:59:10 UTC (rev 7777)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-06-20 05:03:54 UTC (rev 7778)
@@ -567,3 +567,16 @@
 this context. It does NOT account for subcontext memory.
 This can be used to calculate the size of an array.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void *talloc_find_parent_byname(const void *ctx, const char *name);
+
+Find a parent memory context of the current context that has the given
+name. This can be very useful in complex programs where it may be
+difficult to pass all information down to the level you need, but you
+know the structure you want is a parent of another context.
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(type *)talloc_find_parent_bytype(ctx, type);
+
+Like talloc_find_parent_byname() but takes a type, making it typesafe.
+



More information about the samba-cvs mailing list