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

📄 memory.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@item void obstack_blank (struct obstack *@var{obstack-ptr}, int @var{size})Add @var{size} uninitialized bytes to a growing object.@xref{Growing Objects}.@item void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})Add @var{size} bytes, copied from @var{address}, to a growing object.@xref{Growing Objects}.@item void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})Add @var{size} bytes, copied from @var{address}, to a growing object,and then add another byte containing a null character.  @xref{GrowingObjects}.@item void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{data-char})Add one byte containing @var{data-char} to a growing object.@xref{Growing Objects}.@item void *obstack_finish (struct obstack *@var{obstack-ptr})Finalize the object that is growing and return its permanent address.@xref{Growing Objects}.@item int obstack_object_size (struct obstack *@var{obstack-ptr})Get the current size of the currently growing object.  @xref{GrowingObjects}.@item void obstack_blank_fast (struct obstack *@var{obstack-ptr}, int @var{size})Add @var{size} uninitialized bytes to a growing object without checkingthat there is enough room.  @xref{Extra Fast Growing}.@item void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{data-char})Add one byte containing @var{data-char} to a growing object withoutchecking that there is enough room.  @xref{Extra Fast Growing}.@item int obstack_room (struct obstack *@var{obstack-ptr})Get the amount of room now available for growing the current object.@xref{Extra Fast Growing}.@item int obstack_alignment_mask (struct obstack *@var{obstack-ptr})The mask used for aligning the beginning of an object.  This is anlvalue.  @xref{Obstacks Data Alignment}.@item int obstack_chunk_size (struct obstack *@var{obstack-ptr})The size for allocating chunks.  This is an lvalue.  @xref{Obstack Chunks}.@item void *obstack_base (struct obstack *@var{obstack-ptr})Tentative starting address of the currently growing object.@xref{Status of an Obstack}.@item void *obstack_next_free (struct obstack *@var{obstack-ptr})Address just after the end of the currently growing object.@xref{Status of an Obstack}.@end table@node Variable Size Automatic@section Automatic Storage with Variable Size@cindex automatic freeing@cindex @code{alloca} function@cindex automatic storage with variable sizeThe function @code{alloca} supports a kind of half-dynamic allocation inwhich blocks are allocated dynamically but freed automatically.Allocating a block with @code{alloca} is an explicit action; you canallocate as many blocks as you wish, and compute the size at run time.  Butall the blocks are freed when you exit the function that @code{alloca} wascalled from, just as if they were automatic variables declared in thatfunction.  There is no way to free the space explicitly.The prototype for @code{alloca} is in @file{stdlib.h}.  This function isa BSD extension.@pindex stdlib.h@comment stdlib.h@comment GNU, BSD@deftypefun {void *} alloca (size_t @var{size});The return value of @code{alloca} is the address of a block of @var{size}bytes of storage, allocated in the stack frame of the calling function.@end deftypefunDo not use @code{alloca} inside the arguments of a function call---youwill get unpredictable results, because the stack space for the@code{alloca} would appear on the stack in the middle of the space forthe function arguments.  An example of what to avoid is @code{foo (x,alloca (4), y)}.@c This might get fixed in future versions of GCC, but that won't make@c it safe with compilers generally.@menu* Alloca Example::              Example of using @code{alloca}.* Advantages of Alloca::        Reasons to use @code{alloca}.* Disadvantages of Alloca::     Reasons to avoid @code{alloca}.* GNU C Variable-Size Arrays::  Only in GNU C, here is an alternative				 method of allocating dynamically and				 freeing automatically.@end menu@node Alloca Example@subsection @code{alloca} ExampleAs an example of use of @code{alloca}, here is a function that opens a filename made from concatenating two argument strings, and returns a filedescriptor or minus one signifying failure:@smallexampleintopen2 (char *str1, char *str2, int flags, int mode)@{  char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);  strcpy (name, str1);  strcat (name, str2);  return open (name, flags, mode);@}@end smallexample@noindentHere is how you would get the same results with @code{malloc} and@code{free}:@smallexampleintopen2 (char *str1, char *str2, int flags, int mode)@{  char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);  int desc;  if (name == 0)    fatal ("virtual memory exceeded");  strcpy (name, str1);  strcat (name, str2);  desc = open (name, flags, mode);  free (name);  return desc;@}@end smallexampleAs you can see, it is simpler with @code{alloca}.  But @code{alloca} hasother, more important advantages, and some disadvantages.@node Advantages of Alloca@subsection Advantages of @code{alloca}Here are the reasons why @code{alloca} may be preferable to @code{malloc}:@itemize @bullet@itemUsing @code{alloca} wastes very little space and is very fast.  (It isopen-coded by the GNU C compiler.)@itemSince @code{alloca} does not have separate pools for different sizes ofblock, space used for any size block can be reused for any other size.@code{alloca} does not cause storage fragmentation.@item@cindex longjmpNonlocal exits done with @code{longjmp} (@pxref{Non-Local Exits})automatically free the space allocated with @code{alloca} when they exitthrough the function that called @code{alloca}.  This is the mostimportant reason to use @code{alloca}.To illustrate this, suppose you have a function@code{open_or_report_error} which returns a descriptor, like@code{open}, if it succeeds, but does not return to its caller if itfails.  If the file cannot be opened, it prints an error message andjumps out to the command level of your program using @code{longjmp}.Let's change @code{open2} (@pxref{Alloca Example}) to use thissubroutine:@refill@smallexampleintopen2 (char *str1, char *str2, int flags, int mode)@{  char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);  strcpy (name, str1);  strcat (name, str2);  return open_or_report_error (name, flags, mode);@}@end smallexample@noindentBecause of the way @code{alloca} works, the storage it allocates isfreed even when an error occurs, with no special effort required.By contrast, the previous definition of @code{open2} (which uses@code{malloc} and @code{free}) would develop a storage leak if it werechanged in this way.  Even if you are willing to make more changes tofix it, there is no easy way to do so.@end itemize@node Disadvantages of Alloca@subsection Disadvantages of @code{alloca}@cindex @code{alloca} disadvantages@cindex disadvantages of @code{alloca}These are the disadvantages of @code{alloca} in comparison with@code{malloc}:@itemize @bullet@itemIf you try to allocate more storage than the machine can provide, youdon't get a clean error message.  Instead you get a fatal signal likethe one you would get from an infinite recursion; probably asegmentation violation (@pxref{Program Error Signals}).@itemSome non-GNU systems fail to support @code{alloca}, so it is lessportable.  However, a slower emulation of @code{alloca} written in Cis available for use on systems with this deficiency.@end itemize@node GNU C Variable-Size Arrays@subsection GNU C Variable-Size Arrays@cindex variable-sized arraysIn GNU C, you can replace most uses of @code{alloca} with an array ofvariable size.  Here is how @code{open2} would look then:@smallexampleint open2 (char *str1, char *str2, int flags, int mode)@{  char name[strlen (str1) + strlen (str2) + 1];  strcpy (name, str1);  strcat (name, str2);  return open (name, flags, mode);@}@end smallexampleBut @code{alloca} is not always equivalent to a variable-sized array, forseveral reasons:@itemize @bullet@itemA variable size array's space is freed at the end of the scope of thename of the array.  The space allocated with @code{alloca}remains until the end of the function.@itemIt is possible to use @code{alloca} within a loop, allocating anadditional block on each iteration.  This is impossible withvariable-sized arrays.@end itemize@strong{Note:} If you mix use of @code{alloca} and variable-sized arrayswithin one function, exiting a scope in which a variable-sized array wasdeclared frees all blocks allocated with @code{alloca} during theexecution of that scope.@node Relocating Allocator@section Relocating Allocator@cindex relocating memory allocatorAny system of dynamic memory allocation has overhead: the amount ofspace it uses is more than the amount the program asks for.  The@dfn{relocating memory allocator} achieves very low overhead by movingblocks in memory as necessary, on its own initiative.@menu* Relocator Concepts::		How to understand relocating allocation.* Using Relocator::		Functions for relocating allocation.@end menu@node Relocator Concepts@subsection Concepts of Relocating Allocation@ifinfoThe @dfn{relocating memory allocator} achieves very low overhead bymoving blocks in memory as necessary, on its own initiative.@end ifinfoWhen you allocate a block with @code{malloc}, the address of the blocknever changes unless you use @code{realloc} to change its size.  Thus,you can safely store the address in various places, temporarily orpermanently, as you like.  This is not safe when you use the relocatingmemory allocator, because any and all relocatable blocks can movewhenever you allocate memory in any fashion.  Even calling @code{malloc}or @code{realloc} can move the relocatable blocks.@cindex handleFor each relocatable block, you must make a @dfn{handle}---a pointerobject in memory, designated to store the address of that block.  Therelocating allocator knows where each block's handle is, and updates theaddress stored there whenever it moves the block, so that the handlealways points to the block.  Each time you access the contents of theblock, you should fetch its address anew from the handle.To call any of the relocating allocator functions from a signal handleris almost certainly incorrect, because the signal could happen at anytime and relocate all the blocks.  The only way to make this safe is toblock the signal around any access to the contents of any relocatableblock---not a convenient mode of operation.  @xref{Nonreentrancy}.@node Using Relocator@subsection Allocating and Freeing Relocatable Blocks@pindex malloc.hIn the descriptions below, @var{handleptr} designates the address of thehandle.  All the functions are declared in @file{malloc.h}; all are GNUextensions.@comment malloc.h@comment GNU@deftypefun {void *} r_alloc (void **@var{handleptr}, size_t @var{size})This function allocates a relocatable block of size @var{size}.  Itstores the block's address in @code{*@var{handleptr}} and returnsa non-null pointer to indicate success.If @code{r_alloc} can't get the space needed, it stores a null pointerin @code{*@var{handleptr}}, and returns a null pointer.@end deftypefun@comment malloc.h@comment GNU@deftypefun void r_alloc_free (void **@var{handleptr})This function is the way to free a relocatable block.  It frees theblock that @code{*@var{handleptr}} points to, and stores a null pointerin @code{*@var{handleptr}} to show it doesn't point to an allocatedblock any more.@end deftypefun@comment malloc.h@comment GNU@deftypefun {void *} r_re_alloc (void **@var{handleptr}, size_t @var{size})The function @code{r_re_alloc} adjusts the size of the block that@code{*@var{handleptr}} points to, making it @var{size} bytes long.  Itstores the address of the resized block in @code{*@var{handleptr}} andreturns a non-null pointer to indicate success.If enough memory is not available, this function returns a null pointerand does not modify @code{*@var{handleptr}}.@end deftypefun@node Memory Warnings@section Memory Usage Warnings@cindex memory usage warnings@cindex warnings of memory almost full@pindex malloc.cYou can ask for warnings as the program approaches running out of memoryspace, by calling @code{memory_warnings}.  This tells @code{malloc} tocheck memory usage every time it asks for more memory from the operatingsystem.  This is a GNU extension declared in @file{malloc.h}.@comment malloc.h@comment GNU@deftypefun void memory

⌨️ 快捷键说明

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