blob: 6d18cdc821ad93ef56bc7a8c40ab86a229cdb3d9 [file] [log] [blame]
<html lang="en">
<head>
<title>Obstack Chunks - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Obstacks.html#Obstacks" title="Obstacks">
<link rel="prev" href="Obstacks-Data-Alignment.html#Obstacks-Data-Alignment" title="Obstacks Data Alignment">
<link rel="next" href="Summary-of-Obstacks.html#Summary-of-Obstacks" title="Summary of Obstacks">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Obstack-Chunks"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Summary-of-Obstacks.html#Summary-of-Obstacks">Summary of Obstacks</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Obstacks-Data-Alignment.html#Obstacks-Data-Alignment">Obstacks Data Alignment</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Obstacks.html#Obstacks">Obstacks</a>
<hr>
</div>
<h5 class="subsubsection">3.2.4.10 Obstack Chunks</h5>
<p><a name="index-efficiency-of-chunks-338"></a><a name="index-chunks-339"></a>
Obstacks work by allocating space for themselves in large chunks, and
then parceling out space in the chunks to satisfy your requests. Chunks
are normally 4096 bytes long unless you specify a different chunk size.
The chunk size includes 8 bytes of overhead that are not actually used
for storing objects. Regardless of the specified size, longer chunks
will be allocated when necessary for long objects.
<p>The obstack library allocates chunks by calling the function
<code>obstack_chunk_alloc</code>, which you must define. When a chunk is no
longer needed because you have freed all the objects in it, the obstack
library frees the chunk by calling <code>obstack_chunk_free</code>, which you
must also define.
<p>These two must be defined (as macros) or declared (as functions) in each
source file that uses <code>obstack_init</code> (see <a href="Creating-Obstacks.html#Creating-Obstacks">Creating Obstacks</a>).
Most often they are defined as macros like this:
<pre class="smallexample"> #define obstack_chunk_alloc malloc
#define obstack_chunk_free free
</pre>
<p>Note that these are simple macros (no arguments). Macro definitions with
arguments will not work! It is necessary that <code>obstack_chunk_alloc</code>
or <code>obstack_chunk_free</code>, alone, expand into a function name if it is
not itself a function name.
<p>If you allocate chunks with <code>malloc</code>, the chunk size should be a
power of 2. The default chunk size, 4096, was chosen because it is long
enough to satisfy many typical requests on the obstack yet short enough
not to waste too much memory in the portion of the last chunk not yet used.
<!-- obstack.h -->
<!-- GNU -->
<div class="defun">
&mdash; Macro: int <b>obstack_chunk_size</b> (<var>struct obstack *obstack-ptr</var>)<var><a name="index-obstack_005fchunk_005fsize-340"></a></var><br>
<blockquote><p>This returns the chunk size of the given obstack.
</p></blockquote></div>
<p>Since this macro expands to an lvalue, you can specify a new chunk size by
assigning it a new value. Doing so does not affect the chunks already
allocated, but will change the size of chunks allocated for that particular
obstack in the future. It is unlikely to be useful to make the chunk size
smaller, but making it larger might improve efficiency if you are
allocating many objects whose size is comparable to the chunk size. Here
is how to do so cleanly:
<pre class="smallexample"> if (obstack_chunk_size (obstack_ptr) &lt; <var>new-chunk-size</var>)
obstack_chunk_size (obstack_ptr) = <var>new-chunk-size</var>;
</pre>
</body></html>