⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talloc_guide.txt

📁 samba服务器!
💻 TXT
📖 第 1 页 / 共 2 页
字号:
Using talloc in Samba4----------------------Andrew TridgellSeptember 2004The most current version of this document is available at   http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txtIf you are used to the "old" talloc from Samba3 before 3.0.20 then please readthis carefully, as talloc has changed a lot. With 3.0.20 (or 3.0.14?) theSamba4 talloc has been ported back to Samba3, so this guide applies to both.The new talloc is a hierarchical, reference counted memory pool systemwith destructors. Quite a mounthful really, but not too bad once youget used to it.Perhaps the biggest change from Samba3 is that there is no distinctionbetween a "talloc context" and a "talloc pointer". Any pointerreturned from talloc() is itself a valid talloc context. This meansyou can do this:  struct foo *X = talloc(mem_ctx, struct foo);  X->name = talloc_strdup(X, "foo");and the pointer X->name would be a "child" of the talloc context "X"which is itself a child of mem_ctx. So if you do talloc_free(mem_ctx)then it is all destroyed, whereas if you do talloc_free(X) then just Xand X->name are destroyed, and if you do talloc_free(X->name) thenjust the name element of X is destroyed.If you think about this, then what this effectively gives you is ann-ary tree, where you can free any part of the tree withtalloc_free().If you find this confusing, then I suggest you run the testsuite towatch talloc in action. You may also like to add your own tests totestsuite.c to clarify how some particular situation is handled.Performance-----------All the additional features of talloc() over malloc() do come at aprice. We have a simple performance test in Samba4 that measurestalloc() versus malloc() performance, and it seems that talloc() isabout 4% slower than malloc() on my x86 Debian Linux box. For Samba,the great reduction in code complexity that we get by using tallocmakes this worthwhile, especially as the total overhead oftalloc/malloc in Samba is already quite small.talloc API----------The following is a complete guide to the talloc API. Read it all atleast twice.Multi-threading---------------talloc itself does not deal with threads. It is thread-safe (assuming  the underlying "malloc" is), as long as each thread uses different  memory contexts.If two threads uses the same context then they need to synchronize in  order to be safe. In particular:- when using talloc_enable_leak_report(), giving directly NULL as a  parent context implicitly refers to a hidden "null context" global  variable, so this should not be used in a multi-threaded environment  without proper synchronization ;- the context returned by talloc_autofree_context() is also global so  shouldn't be used by several threads simultaneously without  synchronization.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-(type *)talloc(const void *context, type);The talloc() macro is the core of the talloc library. It takes amemory context and a type, and returns a pointer to a new area ofmemory of the given type.The returned pointer is itself a talloc context, so you can use it asthe context argument to more calls to talloc if you wish.The returned pointer is a "child" of the supplied context. This meansthat if you talloc_free() the context then the new child disappears aswell. Alternatively you can free just the child.The context argument to talloc() can be NULL, in which case a new toplevel context is created. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_size(const void *context, size_t size);The function talloc_size() should be used when you don't have aconvenient type to pass to talloc(). Unlike talloc(), it is not typesafe (as it returns a void *), so you are on your own for type checking.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);The talloc_ptrtype() macro should be used when you have a pointer andwant to allocate memory to point at with this pointer. When compilingwith gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()and talloc_get_name() will return the current location in the source file.and not the type.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-int talloc_free(void *ptr);The talloc_free() function frees a piece of talloc memory, and all itschildren. You can call talloc_free() on any pointer returned bytalloc().The return value of talloc_free() indicates success or failure, with 0returned for success and -1 for failure. The only possible failurecondition is if the pointer had a destructor attached to it and thedestructor returned -1. See talloc_set_destructor() for details ondestructors.If this pointer has an additional parent when talloc_free() is calledthen the memory is not actually released, but instead the mostrecently established parent is destroyed. See talloc_reference() fordetails on establishing additional parents.For more control on which parent is removed, see talloc_unlink()talloc_free() operates recursively on its children.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-int talloc_free_children(void *ptr);The talloc_free_children() walks along the list of all children of atalloc context and talloc_free()s only the children, not the contextitself.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_reference(const void *context, const void *ptr);The talloc_reference() function makes "context" an additional parentof "ptr".The return value of talloc_reference() is always the original pointer"ptr", unless talloc ran out of memory in creating the reference inwhich case it will return NULL (each additional reference consumesaround 48 bytes of memory on intel x86 platforms).If "ptr" is NULL, then the function is a no-op, and simply returns NULL.After creating a reference you can free it in one of the followingways:  - you can talloc_free() any parent of the original pointer. That    will reduce the number of parents of this pointer by 1, and will    cause this pointer to be freed if it runs out of parents.  - you can talloc_free() the pointer itself. That will destroy the    most recently established parent to the pointer and leave the    pointer as a child of its current parent.For more control on which parent to remove, see talloc_unlink()=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-int talloc_unlink(const void *context, const void *ptr);The talloc_unlink() function removes a specific parent from ptr. Thecontext passed must either be a context used in talloc_reference()with this pointer, or must be a direct parent of ptr. Note that if the parent has already been removed using talloc_free()then this function will fail and will return -1.  Likewise, if "ptr"is NULL, then the function will make no modifications and return -1.Usually you can just use talloc_free() instead of talloc_unlink(), butsometimes it is useful to have the additional control on which parentis removed.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void talloc_set_destructor(const void *ptr, int (*destructor)(void *));The function talloc_set_destructor() sets the "destructor" for thepointer "ptr". A destructor is a function that is called when thememory used by a pointer is about to be released. The destructorreceives the pointer as an argument, and should return 0 for successand -1 for failure.The destructor can do anything it wants to, including freeing otherpieces of memory. A common use for destructors is to clean upoperating system resources (such as open file descriptors) containedin the structure the destructor is placed on.You can only place one destructor on a pointer. If you need more thanone destructor then you can create a zero-length child of the pointerand place an additional destructor on that.To remove a destructor call talloc_set_destructor() with NULL for thedestructor.If your destructor attempts to talloc_free() the pointer that it isthe destructor for then talloc_free() will return -1 and the free willbe ignored. This would be a pointless operation anyway, as thedestructor is only called when the memory is just about to go away.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-int talloc_increase_ref_count(const void *ptr);The talloc_increase_ref_count(ptr) function is exactly equivalent to:  talloc_reference(NULL, ptr);You can use either syntax, depending on which you think is clearer inyour code.It returns 0 on success and -1 on failure.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-size_t talloc_reference_count(const void *ptr);Return the number of references to the pointer.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void talloc_set_name(const void *ptr, const char *fmt, ...);Each talloc pointer has a "name". The name is used principally fordebugging purposes, although it is also possible to set and get thename on a pointer in as a way of "marking" pointers in your code.The main use for names on pointer is for "talloc reports". Seetalloc_report() and talloc_report_full() for details. Also seetalloc_enable_leak_report() and talloc_enable_leak_report_full().The talloc_set_name() function allocates memory as a child of thepointer. It is logically equivalent to:  talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));Note that multiple calls to talloc_set_name() will allocate morememory without releasing the name. All of the memory is released whenthe ptr is freed using talloc_free().=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void talloc_set_name_const(const void *ptr, const char *name);The function talloc_set_name_const() is just like talloc_set_name(),but it takes a string constant, and is much faster. It is extensivelyused by the "auto naming" macros, such as talloc_p().This function does not allocate any memory. It just copies thesupplied pointer into the internal representation of the tallocptr. This means you must not pass a name pointer to memory that willdisappear before the ptr is freed with talloc_free().=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_named(const void *context, size_t size, const char *fmt, ...);The talloc_named() function creates a named talloc pointer. It isequivalent to:   ptr = talloc_size(context, size);   talloc_set_name(ptr, fmt, ....);=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_named_const(const void *context, size_t size, const char *name);This is equivalent to:   ptr = talloc_size(context, size);   talloc_set_name_const(ptr, name);=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-const char *talloc_get_name(const void *ptr);This returns the current name for the given talloc pointer. Seetalloc_set_name() for details.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_init(const char *fmt, ...);This function creates a zero length named talloc context as a toplevel context. It is equivalent to:  talloc_named(NULL, 0, fmt, ...);=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_new(void *ctx);This is a utility macro that creates a new memory context hangingoff an exiting context, automatically naming it "talloc_new: __location__"where __location__ is the source line it is called from. It isparticularly useful for creating a new temporary working context.=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-(type *)talloc_realloc(const void *context, void *ptr, type, count);The talloc_realloc() macro changes the size of a tallocpointer. The "count" argument is the number of elements of type "type"that you want the resulting pointer to hold. talloc_realloc() has the following equivalences:  talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);  talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);  talloc_realloc(context, ptr, type, 0)  ==> talloc_free(ptr);The "context" argument is only used if "ptr" is NULL, otherwise it isignored.talloc_realloc() returns the new pointer, or NULL on failure. The callwill fail either due to a lack of memory, or because the pointer hasmore than one parent (see talloc_reference()).=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-void *talloc_realloc_size(const void *context, void *ptr, size_t size);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -