blob: 037ddb37b6f10236df217cc247db69ca3ed3fde0 [file] [log] [blame]
<html lang="en">
<head>
<title>Allocation in an Obstack - 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="Preparing-for-Obstacks.html#Preparing-for-Obstacks" title="Preparing for Obstacks">
<link rel="next" href="Freeing-Obstack-Objects.html#Freeing-Obstack-Objects" title="Freeing Obstack Objects">
<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="Allocation-in-an-Obstack"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Freeing-Obstack-Objects.html#Freeing-Obstack-Objects">Freeing Obstack Objects</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Preparing-for-Obstacks.html#Preparing-for-Obstacks">Preparing for Obstacks</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Obstacks.html#Obstacks">Obstacks</a>
<hr>
</div>
<h5 class="subsubsection">3.2.4.3 Allocation in an Obstack</h5>
<p><a name="index-allocation-_0028obstacks_0029-307"></a>
The most direct way to allocate an object in an obstack is with
<code>obstack_alloc</code>, which is invoked almost like <code>malloc</code>.
<!-- obstack.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: void * <b>obstack_alloc</b> (<var>struct obstack *obstack-ptr, int size</var>)<var><a name="index-obstack_005falloc-308"></a></var><br>
<blockquote><p>This allocates an uninitialized block of <var>size</var> bytes in an obstack
and returns its address. Here <var>obstack-ptr</var> specifies which obstack
to allocate the block in; it is the address of the <code>struct obstack</code>
object which represents the obstack. Each obstack function or macro
requires you to specify an <var>obstack-ptr</var> as the first argument.
<p>This function calls the obstack's <code>obstack_chunk_alloc</code> function if
it needs to allocate a new chunk of memory; it calls
<code>obstack_alloc_failed_handler</code> if allocation of memory by
<code>obstack_chunk_alloc</code> failed.
</p></blockquote></div>
<p>For example, here is a function that allocates a copy of a string <var>str</var>
in a specific obstack, which is in the variable <code>string_obstack</code>:
<pre class="smallexample"> struct obstack string_obstack;
char *
copystring (char *string)
{
size_t len = strlen (string) + 1;
char *s = (char *) obstack_alloc (&amp;string_obstack, len);
memcpy (s, string, len);
return s;
}
</pre>
<p>To allocate a block with specified contents, use the function
<code>obstack_copy</code>, declared like this:
<!-- obstack.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: void * <b>obstack_copy</b> (<var>struct obstack *obstack-ptr, void *address, int size</var>)<var><a name="index-obstack_005fcopy-309"></a></var><br>
<blockquote><p>This allocates a block and initializes it by copying <var>size</var>
bytes of data starting at <var>address</var>. It calls
<code>obstack_alloc_failed_handler</code> if allocation of memory by
<code>obstack_chunk_alloc</code> failed.
</p></blockquote></div>
<!-- obstack.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: void * <b>obstack_copy0</b> (<var>struct obstack *obstack-ptr, void *address, int size</var>)<var><a name="index-obstack_005fcopy0-310"></a></var><br>
<blockquote><p>Like <code>obstack_copy</code>, but appends an extra byte containing a null
character. This extra byte is not counted in the argument <var>size</var>.
</p></blockquote></div>
<p>The <code>obstack_copy0</code> function is convenient for copying a sequence
of characters into an obstack as a null-terminated string. Here is an
example of its use:
<pre class="smallexample"> char *
obstack_savestring (char *addr, int size)
{
return obstack_copy0 (&amp;myobstack, addr, size);
}
</pre>
<p class="noindent">Contrast this with the previous example of <code>savestring</code> using
<code>malloc</code> (see <a href="Basic-Allocation.html#Basic-Allocation">Basic Allocation</a>).
</body></html>