[PATCH] Add _sticky variants of references
Sam Liddicott
sam at liddicott.com
Wed Jul 29 01:13:47 MDT 2009
Sticky references will not be considered a problem when talloc_free
is called. Non-sticky references will.
---
lib/talloc/talloc.c | 27 +++++++++++++++++++++++++--
lib/talloc/talloc.h | 2 ++
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c
index 76351fe..9f21ca4 100644
--- a/lib/talloc/talloc.c
+++ b/lib/talloc/talloc.c
@@ -7,6 +7,7 @@
Copyright (C) Andrew Tridgell 2004
Copyright (C) Stefan Metzmacher 2006
+ Copyright (C) Sam Liddicott 2009
** NOTE! The following LGPL license applies to the talloc
** library. This does NOT imply that all of Samba is released
@@ -110,6 +111,7 @@ static inline int _talloc_free(void *ptr);
struct talloc_reference_handle {
struct talloc_reference_handle *next, *prev;
void *ptr;
+ bool sticky;
};
typedef int (*talloc_destructor_t)(void *);
@@ -410,7 +412,7 @@ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
}
/*
- increase the reference count on a piece of memory.
+ slippy increase the reference count on a piece of memory.
*/
int talloc_increase_ref_count(const void *ptr)
{
@@ -421,6 +423,17 @@ int talloc_increase_ref_count(const void *ptr)
}
/*
+ sticky increase the reference count on a piece of memory.
+*/
+int talloc_increase_ref_count_sticky(const void *ptr)
+{
+ if (unlikely(!talloc_reference_sticky(null_context, ptr))) {
+ return -1;
+ }
+ return 0;
+}
+
+/*
helper for talloc_reference()
this is referenced by a function pointer and should not be inline
@@ -473,7 +486,7 @@ static inline void *_talloc_named_const(const void *context, size_t size, const
same underlying data, and you want to be able to free the two instances separately,
and in either order
*/
-void *_talloc_reference(const void *context, const void *ptr)
+void *__talloc_reference(const void *context, const void *ptr, bool sticky)
{
struct talloc_chunk *tc;
struct talloc_reference_handle *handle;
@@ -491,9 +504,19 @@ void *_talloc_reference(const void *context, const void *ptr)
talloc_set_destructor(handle, talloc_reference_destructor);
handle->ptr = discard_const_p(void, ptr);
_TLIST_ADD(tc->refs, handle);
+ handle->sticky = sticky;
return handle->ptr;
}
+void *_talloc_reference(const void *context, const void *ptr)
+{
+ return __talloc_reference(context, ptr, false);
+}
+
+void *_talloc_reference_sticky(const void *context, const void *ptr)
+{
+ return __talloc_reference(context, ptr, false);
+}
/*
internal talloc_free call
diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h
index a4b33c3..8150cea 100644
--- a/lib/talloc/talloc.h
+++ b/lib/talloc/talloc.h
@@ -78,6 +78,7 @@ typedef void TALLOC_CTX;
#endif
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
+#define talloc_reference_sticky(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_sticky((ctx),(ptr))
#define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
/* useful macros for creating type checked pointers */
@@ -123,6 +124,7 @@ void *_talloc(const void *context, size_t size);
void *talloc_pool(const void *context, size_t size);
void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
int talloc_increase_ref_count(const void *ptr);
+int talloc_increase_ref_count_sticky(const void *ptr);
size_t talloc_reference_count(const void *ptr);
void *_talloc_reference(const void *context, const void *ptr);
int talloc_unlink(const void *context, void *ptr);
--
1.6.0.3
--------------030508000909030907000202
Content-Type: text/x-diff;
name="0002-Attempt-to-bring-in-5fe1d8dc1275e43d96da1297f5fb0d00.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename*0="0002-Attempt-to-bring-in-5fe1d8dc1275e43d96da1297f5fb0d00.pa";
filename*1="tch"
More information about the samba-technical
mailing list