📄 talloc.3
字号:
talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);.fi.RE.PPThe\fIctx\fRargument is only used if\fIptr\fRis not NULL, otherwise it is ignored\..PPtalloc_realloc() returns the new pointer, or NULL on failure\. The call will fail either due to a lack of memory, or because the pointer has more than one parent (see\(lqtalloc_reference()\(rq)\..SS "void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);".PPthe talloc_realloc_size() function is useful when the type is not known so the type\-safe talloc_realloc() cannot be used\..SS "TYPE *talloc_steal(const void *\fInew_ctx\fR, const TYPE *\fIptr\fR);".PPThe talloc_steal() function changes the parent context of a talloc pointer\. It is typically used when the context that the pointer is currently a child of is going to be freed and you wish to keep the memory for a longer time\..PPThe talloc_steal() function returns the pointer that you pass it\. It does not have any failure modes\..PPNOTE: It is possible to produce loops in the parent/child relationship if you are not careful with talloc_steal()\. No guarantees are provided as to your sanity or the safety of your data if you do this\..SS "TYPE *talloc_move(const void *\fInew_ctx\fR, TYPE **\fIptr\fR);".PPThe talloc_move() function is a wrapper around talloc_steal() which zeros the source pointer after the move\. This avoids a potential source of bugs where a programmer leaves a pointer in two structures, and uses the pointer from the old structure after it has been moved to a new one\..SS "size_t talloc_total_size(const void *\fIptr\fR);".PPThe talloc_total_size() function returns the total size in bytes used by this pointer and all child pointers\. Mostly useful for debugging\..PPPassing NULL is allowed, but it will only give a meaningful result if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called\..SS "size_t talloc_total_blocks(const void *\fIptr\fR);".PPThe talloc_total_blocks() function returns the total memory block count used by this pointer and all child pointers\. Mostly useful for debugging\..PPPassing NULL is allowed, but it will only give a meaningful result if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called\..SS "void talloc_report(const void *ptr, FILE *f);".PPThe talloc_report() function prints a summary report of all memory used by\fIptr\fR\. One line of report is printed for each immediate child of ptr, showing the total memory and number of blocks used by that child\..PPYou can pass NULL for the pointer, in which case a report is printed for the top level memory context, but only if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called\..SS "void talloc_report_full(const void *\fIptr\fR, FILE *\fIf\fR);".PPThis provides a more detailed report than talloc_report()\. It will recursively print the entire tree of memory referenced by the pointer\. References in the tree are shown by giving the name of the pointer that is referenced\..PPYou can pass NULL for the pointer, in which case a report is printed for the top level memory context, but only if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called\..SS "".HP 28.BI "void talloc_report_depth_cb(" "const\ void\ *ptr" ", " "int\ depth" ", " "int\ max_depth" ", " "void\ (*callback)(const\ void\ *ptr,\ int\ depth,\ int\ max_depth,\ int\ is_ref,\ void\ *priv)" ", " "void\ *priv" ");".PPThis provides a more flexible reports than talloc_report()\. It will recursively call the callback for the entire tree of memory referenced by the pointer\. References in the tree are passed with\fIis_ref = 1\fRand the pointer that is referenced\..PPYou can pass NULL for the pointer, in which case a report is printed for the top level memory context, but only if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called\..PPThe recursion is stopped when depth >= max_depth\. max_depth = \-1 means only stop at leaf nodes\..SS "".HP 30.BI "void talloc_report_depth_file(" "const\ void\ *ptr" ", " "int\ depth" ", " "int\ max_depth" ", " "FILE\ *f" ");".PPThis provides a more flexible reports than talloc_report()\. It will let you specify the depth and max_depth\..SS "void talloc_enable_leak_report(void);".PPThis enables calling of talloc_report(NULL, stderr) when the program exits\. In Samba4 this is enabled by using the \-\-leak\-report command line option\..PPFor it to be useful, this function must be called before any other talloc function as it establishes a "null context" that acts as the top of the tree\. If you don\'t call this function first then passing NULL to talloc_report() or talloc_report_full() won\'t give you the full tree printout\..PPHere is a typical talloc report:.sp.RS 4.nftalloc report on \'null_context\' (total 267 bytes in 15 blocks)libcli/auth/spnego_parse\.c:55 contains 31 bytes in 2 blockslibcli/auth/spnego_parse\.c:55 contains 31 bytes in 2 blocksiconv(UTF8,CP850) contains 42 bytes in 2 blockslibcli/auth/spnego_parse\.c:55 contains 31 bytes in 2 blocksiconv(CP850,UTF8) contains 42 bytes in 2 blocksiconv(UTF8,UTF\-16LE) contains 45 bytes in 2 blocksiconv(UTF\-16LE,UTF8) contains 45 bytes in 2 blocks .fi.RE.SS "void talloc_enable_leak_report_full(void);".PPThis enables calling of talloc_report_full(NULL, stderr) when the program exits\. In Samba4 this is enabled by using the \-\-leak\-report\-full command line option\..PPFor it to be useful, this function must be called before any other talloc function as it establishes a "null context" that acts as the top of the tree\. If you don\'t call this function first then passing NULL to talloc_report() or talloc_report_full() won\'t give you the full tree printout\..PPHere is a typical full report:.sp.RS 4.nffull talloc report on \'root\' (total 18 bytes in 8 blocks)p1 contains 18 bytes in 7 blocks (ref 0) r1 contains 13 bytes in 2 blocks (ref 0) reference to: p2 p2 contains 1 bytes in 1 blocks (ref 1) x3 contains 1 bytes in 1 blocks (ref 0) x2 contains 1 bytes in 1 blocks (ref 0) x1 contains 1 bytes in 1 blocks (ref 0) .fi.RE.SS "(\fItype\fR *)talloc_zero(const void *\fIctx\fR, \fItype\fR);".PPThe talloc_zero() macro is equivalent to:.sp.RS 4.nfptr = talloc(ctx, type);if (ptr) memset(ptr, 0, sizeof(type));.fi.RE.SS "void *talloc_zero_size(const void *\fIctx\fR, size_t \fIsize\fR)".PPThe talloc_zero_size() function is useful when you don\'t have a known type\..SS "void *talloc_memdup(const void *\fIctx\fR, const void *\fIp\fR, size_t size);".PPThe talloc_memdup() function is equivalent to:.sp.RS 4.nfptr = talloc_size(ctx, size);if (ptr) memcpy(ptr, p, size);.fi.RE.SS "char *talloc_strdup(const void *\fIctx\fR, const char *\fIp\fR);".PPThe talloc_strdup() function is equivalent to:.sp.RS 4.nfptr = talloc_size(ctx, strlen(p)+1);if (ptr) memcpy(ptr, p, strlen(p)+1);.fi.RE.PPThis function sets the name of the new pointer to the passed string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "char *talloc_strndup(const void *\fIt\fR, const char *\fIp\fR, size_t \fIn\fR);".PPThe talloc_strndup() function is the talloc equivalent of the C library function strndup(3)\..PPThis function sets the name of the new pointer to the passed string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "char *talloc_append_string(const void *\fIt\fR, char *\fIorig\fR, const char *\fIappend\fR);".PPThe talloc_append_string() function appends the given formatted string to the given string\..PPThis function sets the name of the new pointer to the new string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "char *talloc_vasprintf(const void *\fIt\fR, const char *\fIfmt\fR, va_list \fIap\fR);".PPThe talloc_vasprintf() function is the talloc equivalent of the C library function vasprintf(3)\..PPThis function sets the name of the new pointer to the new string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "char *talloc_asprintf(const void *\fIt\fR, const char *\fIfmt\fR, \.\.\.);".PPThe talloc_asprintf() function is the talloc equivalent of the C library function asprintf(3)\..PPThis function sets the name of the new pointer to the passed string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "char *talloc_asprintf_append(char *s, const char *fmt, \.\.\.);".PPThe talloc_asprintf_append() function appends the given formatted string to the given string\..PPThis function sets the name of the new pointer to the new string\. This is equivalent to:.sp.RS 4.nftalloc_set_name_const(ptr, ptr).fi.RE.SS "(type *)talloc_array(const void *ctx, type, uint_t count);".PPThe talloc_array() macro is equivalent to:.sp.RS 4.nf(type *)talloc_size(ctx, sizeof(type) * count);.fi.RE.PPexcept that it provides integer overflow protection for the multiply, returning NULL if the multiply overflows\..SS "void *talloc_array_size(const void *ctx, size_t size, uint_t count);".PPThe talloc_array_size() function is useful when the type is not known\. It operates in the same way as talloc_array(), but takes a size instead of a type\..SS "(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);".PPThe talloc_ptrtype() macro should be used when you have a pointer to an array and want to allocate memory of an array to point at with this pointer\. When compiling with gcc >= 3 it is typesafe\. Note this is a wrapper of talloc_array_size() and talloc_get_name() will return the current location in the source file\. and not the type\..SS "void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)".PPThis is a non\-macro version of talloc_realloc(), which is useful as libraries sometimes want a realloc function pointer\. A realloc(3) implementation encapsulates the functionality of malloc(3), free(3) and realloc(3) in one call, which is why it is useful to be able to pass around a single function pointer\..SS "void *talloc_autofree_context(void);".PPThis is a handy utility function that returns a talloc context which will be automatically freed on program exit\. This can be used to reduce the noise in memory leak reports\..SS "void *talloc_check_name(const void *ptr, const char *name);".PPThis function checks if a pointer has the specified\fIname\fR\. If it does then the pointer is returned\. It it doesn\'t then NULL is returned\..SS "(type *)talloc_get_type(const void *ptr, type);".PPThis macro allows you to do type checking on talloc pointers\. It is particularly useful for void* private pointers\. It is equivalent to this:.sp.RS 4.nf(type *)talloc_check_name(ptr, #type).fi.RE.SS "talloc_set_type(const void *ptr, type);".PPThis macro allows you to force the name of a pointer to be a particular\fItype\fR\. This can be used in conjunction with talloc_get_type() to do type checking on void* pointers\..PPIt is equivalent to this:.sp.RS 4.nftalloc_set_name_const(ptr, #type).fi.RE.SH "PERFORMANCE".PPAll the additional features of talloc(3) over malloc(3) do come at a price\. We have a simple performance test in Samba4 that measures talloc() versus malloc() performance, and it seems that talloc() is about 10% slower than malloc() on my x86 Debian Linux box\. For Samba, the great reduction in code complexity that we get by using talloc makes this worthwhile, especially as the total overhead of talloc/malloc in Samba is already quite small\..SH "SEE ALSO".PPmalloc(3), strndup(3), vasprintf(3), asprintf(3),\fI\%http://talloc.samba.org/\fR.SH "COPYRIGHT/LICENSE".PPCopyright (C) Andrew Tridgell 2004.PPThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version\..PPThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\. See the GNU General Public License for more details\..PPYou should have received a copy of the GNU General Public License along with this program; if not, see http://www\.gnu\.org/licenses/\.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -