📄 talloc.3
字号:
.\" Title: talloc.\" Author: .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>.\" Date: 06/03/2008.\" Manual: .\" Source: .\".TH "TALLOC" "3" "06/03/2008" "" "".\" disable hyphenation.nh.\" disable justification (adjust text to left margin only).ad l.SH "NAME"talloc - hierarchical reference counted memory pool system with destructors.SH "SYNOPSIS".sp.RS 4.nf#include <talloc/talloc\.h>.fi.RE.SH "DESCRIPTION".PPIf you are used to talloc from Samba3 then please read this carefully, as talloc has changed a lot\..PPThe new talloc is a hierarchical, reference counted memory pool system with destructors\. Quite a mouthful really, but not too bad once you get used to it\..PPPerhaps the biggest change from Samba3 is that there is no distinction between a "talloc context" and a "talloc pointer"\. Any pointer returned from talloc() is itself a valid talloc context\. This means you can do this:.sp.RS 4.nf struct foo *X = talloc(mem_ctx, struct foo); X\->name = talloc_strdup(X, "foo"); .fi.RE.PPand the pointerX\->namewould be a "child" of the talloc contextXwhich is itself a child ofmem_ctx\. So if you dotalloc_free(mem_ctx)then it is all destroyed, whereas if you dotalloc_free(X)then justXandX\->nameare destroyed, and if you dotalloc_free(X\->name)then just the name element ofXis destroyed\..PPIf you think about this, then what this effectively gives you is an n\-ary tree, where you can free any part of the tree with talloc_free()\..PPIf you find this confusing, then I suggest you run thetestsuiteprogram to watch talloc in action\. You may also like to add your own tests totestsuite\.cto clarify how some particular situation is handled\..SH "TALLOC API".PPThe following is a complete guide to the talloc API\. Read it all at least twice\..SS "(type *)talloc(const void *ctx, type);".PPThe talloc() macro is the core of the talloc library\. It takes a memory\fIctx\fRand a\fItype\fR, and returns a pointer to a new area of memory of the given\fItype\fR\..PPThe returned pointer is itself a talloc context, so you can use it as the\fIctx\fRargument to more calls to talloc() if you wish\..PPThe returned pointer is a "child" of the supplied context\. This means that if you talloc_free() the\fIctx\fRthen the new child disappears as well\. Alternatively you can free just the child\..PPThe\fIctx\fRargument to talloc() can be NULL, in which case a new top level context is created\..SS "void *talloc_size(const void *ctx, size_t size);".PPThe function talloc_size() should be used when you don\'t have a convenient type to pass to talloc()\. Unlike talloc(), it is not type safe (as it returns a void *), so you are on your own for type checking\..SS "(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);".PPThe talloc_ptrtype() macro should be used when you have a pointer and want to allocate memory to point at with this pointer\. When compiling with 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\..SS "int talloc_free(void *ptr);".PPThe talloc_free() function frees a piece of talloc memory, and all its children\. You can call talloc_free() on any pointer returned by talloc()\..PPThe return value of talloc_free() indicates success or failure, with 0 returned for success and \-1 for failure\. The only possible failure condition is if\fIptr\fRhad a destructor attached to it and the destructor returned \-1\. See\(lqtalloc_set_destructor()\(rqfor details on destructors\..PPIf this pointer has an additional parent when talloc_free() is called then the memory is not actually released, but instead the most recently established parent is destroyed\. See\(lqtalloc_reference()\(rqfor details on establishing additional parents\..PPFor more control on which parent is removed, see\(lqtalloc_unlink()\(rq\..PPtalloc_free() operates recursively on its children\..SS "void *talloc_reference(const void *ctx, const void *ptr);".PPThe talloc_reference() function makes\fIctx\fRan additional parent of\fIptr\fR\..PPThe return value of talloc_reference() is always the original pointer\fIptr\fR, unless talloc ran out of memory in creating the reference in which case it will return NULL (each additional reference consumes around 48 bytes of memory on intel x86 platforms)\..PPIf\fIptr\fRis NULL, then the function is a no\-op, and simply returns NULL\..PPAfter creating a reference you can free it in one of the following ways:.PP.sp.RS 4\h'-04'\(bu\h'+03'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\..RE.sp.RS 4\h'-04'\(bu\h'+03'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\..RE.sp.RE.PPFor more control on which parent to remove, see\(lqtalloc_unlink()\(rq\..SS "int talloc_unlink(const void *ctx, const void *ptr);".PPThe talloc_unlink() function removes a specific parent from\fIptr\fR\. The\fIctx\fRpassed must either be a context used in talloc_reference() with this pointer, or must be a direct parent of ptr\..PPNote that if the parent has already been removed using talloc_free() then this function will fail and will return \-1\. Likewise, if\fIptr\fRis NULL, then the function will make no modifications and return \-1\..PPUsually you can just use talloc_free() instead of talloc_unlink(), but sometimes it is useful to have the additional control on which parent is removed\..SS "void talloc_set_destructor(const void *ptr, int (*destructor)(void *));".PPThe function talloc_set_destructor() sets the\fIdestructor\fRfor the pointer\fIptr\fR\. A\fIdestructor\fRis a function that is called when the memory used by a pointer is about to be released\. The destructor receives\fIptr\fRas an argument, and should return 0 for success and \-1 for failure\..PPThe\fIdestructor\fRcan do anything it wants to, including freeing other pieces of memory\. A common use for destructors is to clean up operating system resources (such as open file descriptors) contained in the structure the destructor is placed on\..PPYou can only place one destructor on a pointer\. If you need more than one destructor then you can create a zero\-length child of the pointer and place an additional destructor on that\..PPTo remove a destructor call talloc_set_destructor() with NULL for the destructor\..PPIf your destructor attempts to talloc_free() the pointer that it is the destructor for then talloc_free() will return \-1 and the free will be ignored\. This would be a pointless operation anyway, as the destructor is only called when the memory is just about to go away\..SS "int talloc_increase_ref_count(const void *\fIptr\fR);".PPThe talloc_increase_ref_count(\fIptr\fR) function is exactly equivalent to:.sp.RS 4.nftalloc_reference(NULL, ptr);.fi.RE.PPYou can use either syntax, depending on which you think is clearer in your code\..PPIt returns 0 on success and \-1 on failure\..SS "size_t talloc_reference_count(const void *\fIptr\fR);".PPReturn the number of references to the pointer\..SS "void talloc_set_name(const void *ptr, const char *fmt, \.\.\.);".PPEach talloc pointer has a "name"\. The name is used principally for debugging purposes, although it is also possible to set and get the name on a pointer in as a way of "marking" pointers in your code\..PPThe main use for names on pointer is for "talloc reports"\. See\(lqtalloc_report_depth_cb()\(rq,\(lqtalloc_report_depth_file()\(rq,\(lqtalloc_report()\(rq\(lqtalloc_report()\(rqand\(lqtalloc_report_full()\(rqfor details\. Also see\(lqtalloc_enable_leak_report()\(rqand\(lqtalloc_enable_leak_report_full()\(rq\..PPThe talloc_set_name() function allocates memory as a child of the pointer\. It is logically equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, \.\.\.));.fi.RE.PPNote that multiple calls to talloc_set_name() will allocate more memory without releasing the name\. All of the memory is released when the ptr is freed using talloc_free()\..SS "void talloc_set_name_const(const void *\fIptr\fR, const char *\fIname\fR);".PPThe function talloc_set_name_const() is just like talloc_set_name(), but it takes a string constant, and is much faster\. It is extensively used by the "auto naming" macros, such as talloc_p()\..PPThis function does not allocate any memory\. It just copies the supplied pointer into the internal representation of the talloc ptr\. This means you must not pass a\fIname\fRpointer to memory that will disappear before\fIptr\fRis freed with talloc_free()\..SS "void *talloc_named(const void *\fIctx\fR, size_t \fIsize\fR, const char *\fIfmt\fR, \.\.\.);".PPThe talloc_named() function creates a named talloc pointer\. It is equivalent to:.sp.RS 4.nfptr = talloc_size(ctx, size);talloc_set_name(ptr, fmt, \.\.\.\.);.fi.RE.SS "void *talloc_named_const(const void *\fIctx\fR, size_t \fIsize\fR, const char *\fIname\fR);".PPThis is equivalent to:.sp.RS 4.nfptr = talloc_size(ctx, size);talloc_set_name_const(ptr, name);.fi.RE.SS "const char *talloc_get_name(const void *\fIptr\fR);".PPThis returns the current name for the given talloc pointer,\fIptr\fR\. See\(lqtalloc_set_name()\(rqfor details\..SS "void *talloc_init(const char *\fIfmt\fR, \.\.\.);".PPThis function creates a zero length named talloc context as a top level context\. It is equivalent to:.sp.RS 4.nftalloc_named(NULL, 0, fmt, \.\.\.);.fi.RE.SS "void *talloc_new(void *\fIctx\fR);".PPThis is a utility macro that creates a new memory context hanging off an exiting context, automatically naming it "talloc_new: __location__" where __location__ is the source line it is called from\. It is particularly useful for creating a new temporary working context\..SS "(\fItype\fR *)talloc_realloc(const void *\fIctx\fR, void *\fIptr\fR, \fItype\fR, \fIcount\fR);".PPThe talloc_realloc() macro changes the size of a talloc pointer\. It has the following equivalences:.sp.RS 4.nftalloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -