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

📄 library_3.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
Often you do not know for certain how big a block you will ultimately needat the time you must begin to use the block.  For example, the block mightbe a buffer that you use to hold a line being read from a file; no matterhow long you make the buffer initially, you may encounter a line that islonger.<P>You can make the block longer by calling <CODE>realloc</CODE>.  This functionis declared in <TT>`stdlib.h'</TT>.<A NAME="IDX147"></A><P><A NAME="IDX148"></A><U>Function:</U> void * <B>realloc</B> <I>(void *<VAR>ptr</VAR>, size_t <VAR>newsize</VAR>)</I><P>The <CODE>realloc</CODE> function changes the size of the block whose address is<VAR>ptr</VAR> to be <VAR>newsize</VAR>.<P>Since the space after the end of the block may be in use, <CODE>realloc</CODE>may find it necessary to copy the block to a new address where more freespace is available.  The value of <CODE>realloc</CODE> is the new address of theblock.  If the block needs to be moved, <CODE>realloc</CODE> copies the oldcontents.<P>Like <CODE>malloc</CODE>, <CODE>realloc</CODE> may return a null pointer if nomemory space is available to make the block bigger.  When this happens,the original block is untouched; it has not been modified or relocated.<P>In most cases it makes no difference what happens to the original blockwhen <CODE>realloc</CODE> fails, because the application program cannot continuewhen it is out of memory, and the only thing to do is to give a fatal errormessage.  Often it is convenient to write and use a subroutine,conventionally called <CODE>xrealloc</CODE>, that takes care of the error messageas <CODE>xmalloc</CODE> does for <CODE>malloc</CODE>:<P><PRE>void *xrealloc (void *ptr, size_t size){  register void *value = realloc (ptr, size);  if (value == 0)    fatal ("Virtual memory exhausted");  return value;}</PRE><P>You can also use <CODE>realloc</CODE> to make a block smaller.  The reason youwould do this is to avoid tying up a lot of memory space when only a littleis needed.  Making a block smaller sometimes necessitates copying it, so itcan fail if no other space is available.<P>If the new size you specify is the same as the old size, <CODE>realloc</CODE>is guaranteed to change nothing and return the same address that you gave.<P><H3><A NAME="SEC26" HREF="library_toc.html#SEC26" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC26">Allocating Cleared Space</A></H3><P>The function <CODE>calloc</CODE> allocates memory and clears it to zero.  Itis declared in <TT>`stdlib.h'</TT>.<A NAME="IDX149"></A><P><A NAME="IDX150"></A><U>Function:</U> void * <B>calloc</B> <I>(size_t <VAR>count</VAR>, size_t <VAR>eltsize</VAR>)</I><P>This function allocates a block long enough to contain a vector of<VAR>count</VAR> elements, each of size <VAR>eltsize</VAR>.  Its contents arecleared to zero before <CODE>calloc</CODE> returns.<P>You could define <CODE>calloc</CODE> as follows:<P><PRE>void *calloc (size_t count, size_t eltsize){  size_t size = count * eltsize;  void *value = malloc (size);  if (value != 0)    memset (value, 0, size);  return value;}</PRE><P>We rarely use <CODE>calloc</CODE> today, because it is equivalent to such asimple combination of other features that are more often used.  It is ahistorical holdover that is not quite obsolete.<P><A NAME="IDX151"></A><H3><A NAME="SEC27" HREF="library_toc.html#SEC27" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC27">Efficiency Considerations for <CODE>malloc</CODE></A></H3><P>To make the best use of <CODE>malloc</CODE>, it helps to know that the GNUversion of <CODE>malloc</CODE> always dispenses small amounts of memory inblocks whose sizes are powers of two.  It keeps separate pools for eachpower of two.  This holds for sizes up to a page size.  Therefore, ifyou are free to choose the size of a small block in order to make<CODE>malloc</CODE> more efficient, make it a power of two.<P>Once a page is split up for a particular block size, it can't be reusedfor another size unless all the blocks in it are freed.  In manyprograms, this is unlikely to happen.  Thus, you can sometimes make aprogram use memory more efficiently by using blocks of the same size formany different purposes.<P>When you ask for memory blocks of a page or larger, <CODE>malloc</CODE> uses adifferent strategy; it rounds the size up to a multiple of a page, andit can coalesce and split blocks as needed.<P>The reason for the two strategies is that it is important to allocateand free small blocks as fast as possible, but speed is less importantfor a large block since the program normally spends a fair amount oftime using it.  Also, large blocks are normally fewer in number.Therefore, for large blocks, it makes sense to use a method which takesmore time to minimize the wasted space.<P><H3><A NAME="SEC28" HREF="library_toc.html#SEC28" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC28">Allocating Aligned Memory Blocks</A></H3><A NAME="IDX152"></A><A NAME="IDX153"></A><A NAME="IDX154"></A><P>The address of a block returned by <CODE>malloc</CODE> or <CODE>realloc</CODE> inthe GNU system is always a multiple of eight.  If you need a block whoseaddress is a multiple of a higher power of two than that, use<CODE>memalign</CODE> or <CODE>valloc</CODE>.  These functions are declared in<TT>`stdlib.h'</TT>.<P>With the GNU library, you can use <CODE>free</CODE> to free the blocks that<CODE>memalign</CODE> and <CODE>valloc</CODE> return.  That does not work in BSD,however--BSD does not provide any way to free such blocks.<P><A NAME="IDX155"></A><U>Function:</U> void * <B>memalign</B> <I>(size_t <VAR>size</VAR>, int <VAR>boundary</VAR>)</I><P>The <CODE>memalign</CODE> function allocates a block of <VAR>size</VAR> bytes whoseaddress is a multiple of <VAR>boundary</VAR>.  The <VAR>boundary</VAR> must be apower of two!  The function <CODE>memalign</CODE> works by calling<CODE>malloc</CODE> to allocate a somewhat larger block, and then returning anaddress within the block that is on the specified boundary.<P><A NAME="IDX156"></A><U>Function:</U> void * <B>valloc</B> <I>(size_t <VAR>size</VAR>)</I><P>Using <CODE>valloc</CODE> is like using <CODE>memalign</CODE> and passing the page sizeas the value of the second argument.<P><H3><A NAME="SEC29" HREF="library_toc.html#SEC29" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC29">Heap Consistency Checking</A></H3><A NAME="IDX157"></A><A NAME="IDX158"></A><P>You can ask <CODE>malloc</CODE> to check the consistency of dynamic storage byusing the <CODE>mcheck</CODE> function.  This function is a GNU extension,declared in <TT>`malloc.h'</TT>.<A NAME="IDX159"></A><P><A NAME="IDX160"></A><U>Function:</U> void <B>mcheck</B> <I>(void (*<VAR>abortfn</VAR>) (void))</I><P>Calling <CODE>mcheck</CODE> tells <CODE>malloc</CODE> to perform occasionalconsistency checks.  These will catch things such as writingpast the end of a block that was allocated with <CODE>malloc</CODE>.<P>The <VAR>abortfn</VAR> argument is the function to call when an inconsistencyis found.  If you supply a null pointer, the <CODE>abort</CODE> function isused.<P>It is too late to begin allocation checking once you have allocatedanything with <CODE>malloc</CODE>.  So <CODE>mcheck</CODE> does nothing in thatcase.  The function returns <CODE>-1</CODE> if you call it too late, and<CODE>0</CODE> otherwise (when it is successful).<P>The easiest way to arrange to call <CODE>mcheck</CODE> early enough is to usethe option <SAMP>`-lmcheck'</SAMP> when you link your program.<P><A NAME="IDX161"></A><H3><A NAME="SEC30" HREF="library_toc.html#SEC30" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC30">Storage Allocation Hooks</A></H3><P>The GNU C library lets you modify the behavior of <CODE>malloc</CODE>,<CODE>realloc</CODE>, and <CODE>free</CODE> by specifying appropriate hookfunctions.  You can use these hooks to help you debug programs that usedynamic storage allocation, for example.<P>The hook variables are declared in <TT>`malloc.h'</TT>.<A NAME="IDX162"></A><P><A NAME="IDX163"></A><U>Variable:</U> <B>__malloc_hook</B><P>The value of this variable is a pointer to function that <CODE>malloc</CODE>uses whenever it is called.  You should define this function to looklike <CODE>malloc</CODE>; that is, like:<P><PRE>void *<VAR>function</VAR> (size_t <VAR>size</VAR>)</PRE><P><A NAME="IDX164"></A><U>Variable:</U> <B>__realloc_hook</B><P>The value of this variable is a pointer to function that <CODE>realloc</CODE>uses whenever it is called.  You should define this function to looklike <CODE>realloc</CODE>; that is, like:<P><PRE>void *<VAR>function</VAR> (void *<VAR>ptr</VAR>, size_t <VAR>size</VAR>)</PRE><P><A NAME="IDX165"></A><U>Variable:</U> <B>__free_hook</B><P>The value of this variable is a pointer to function that <CODE>free</CODE>uses whenever it is called.  You should define this function to looklike <CODE>free</CODE>; that is, like:<P><PRE>void <VAR>function</VAR> (void *<VAR>ptr</VAR>)</PRE><P>You must make sure that the function you install as a hook for one ofthese functions does not call that function recursively without restoringthe old value of the hook first!  Otherwise, your program will get stuckin an infinite recursion.<P>Here is an example showing how to use <CODE>__malloc_hook</CODE> properly.  Itinstalls a function that prints out information every time <CODE>malloc</CODE>is called.<P><PRE>static void *(*old_malloc_hook) (size_t);static void *my_malloc_hook (size_t size){  void *result;  __malloc_hook = old_malloc_hook;  result = malloc (size);  __malloc_hook = my_malloc_hook;  printf ("malloc (%u) returns %p\n", (unsigned int) size, result);  return result;}main (){  ...  old_malloc_hook = __malloc_hook;  __malloc_hook = my_malloc_hook;  ...}</PRE><P>The <CODE>mcheck</CODE> function (see section <A HREF="library_3.html#SEC29" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_3.html#SEC29">Heap Consistency Checking</A>) works byinstalling such hooks.<P><H3><A NAME="SEC31" HREF="library_toc.html#SEC31" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC31">Statistics for Storage Allocation with <CODE>malloc</CODE></A></H3><A NAME="IDX166"></A><P>You can get information about dynamic storage allocation by calling the<CODE>mstats</CODE> function.  This function and its associated data type aredeclared in <TT>`malloc.h'</TT>; they are a GNU extension.<A NAME="IDX167"></A><P><A NAME="IDX168"></A><U>Data Type:</U> <B>struct mstats</B><P>This structure type is used to return information about the dynamicstorage allocator.  It contains the following members:<P><DL COMPACT><DT><CODE>size_t bytes_total</CODE><DD>This is the total size of memory managed by malloc, in bytes.<P><DT><CODE>size_t chunks_used</CODE><DD>This is the number of chunks in use.  (The storage allocator internallygets chunks of memory from the operating system, and then carves them upto satisfy individual <CODE>malloc</CODE> requests; see section <A HREF="library_3.html#SEC27" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_3.html#SEC27">Efficiency Considerations for <CODE>malloc</CODE></A>.)<P><DT><CODE>size_t bytes_used</CODE><DD>This is the number of bytes in use.<P><DT><CODE>size_t chunks_free</CODE>

⌨️ 快捷键说明

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