1<!--$Id: db_set_alloc.so,v 10.3 2002/08/18 21:15:51 bostic Exp $-->
2<!--$Id: env_set_alloc.so,v 1.24 2008/01/19 14:12:57 bostic Exp $-->
3<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
4<!--See the file LICENSE for redistribution information.-->
5<html>
6<head>
7<title>Berkeley DB: DB-&gt;set_alloc</title>
8<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
10</head>
11<body bgcolor=white>
12<table width="100%"><tr valign=top>
13<td>
14<b>DB-&gt;set_alloc</b>
15</td>
16<td align=right>
17<a href="/api_c/api_core.html"><img src="/images/api.gif" alt="API"></a>
18<a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a></td>
19</tr></table>
20<hr size=1 noshade>
21<tt>
22<b><pre>
23#include &lt;db.h&gt;
24<p>
25int
26DB-&gt;set_alloc(DB *db,
27    void *(*app_malloc)(size_t),
28    void *(*app_realloc)(void *, size_t),
29    void (*app_free)(void *));
30</pre></b>
31<hr size=1 noshade>
32<b>Description: DB-&gt;set_alloc</b>
33<p>Set the allocation functions used by the <a href="/api_c/env_class.html">DB_ENV</a> and <a href="/api_c/db_class.html">DB</a>
34methods to allocate or free memory owned by the application.</p>
35<p>There are a number of interfaces in Berkeley DB where memory is allocated by
36the library and then given to the application.  For example, the
37<a href="/api_c/dbt_class.html#DB_DBT_MALLOC">DB_DBT_MALLOC</a> flag, when specified in the <a href="/api_c/dbt_class.html">DBT</a> object,
38will cause the <a href="/api_c/db_class.html">DB</a> methods to allocate and reallocate memory
39which then becomes the responsibility of the calling application.  (See
40<a href="/api_c/dbt_class.html">DBT</a> for more information.)  Other examples are the Berkeley DB
41interfaces which return statistical information to the application:
42<a href="/api_c/db_stat.html">DB-&gt;stat</a>, <a href="/api_c/lock_stat.html">DB_ENV-&gt;lock_stat</a>, <a href="/api_c/log_archive.html">DB_ENV-&gt;log_archive</a>,
43<a href="/api_c/log_stat.html">DB_ENV-&gt;log_stat</a>, <a href="/api_c/memp_stat.html">DB_ENV-&gt;memp_stat</a>, and <a href="/api_c/txn_stat.html">DB_ENV-&gt;txn_stat</a>.  There is one
44method in Berkeley DB where memory is allocated by the application and then
45given to the library: <a href="/api_c/db_associate.html">DB-&gt;associate</a>.</p>
46<p>On systems in which there may be multiple library versions of the
47standard allocation routines (notably Windows NT), transferring memory
48between the library and the application will fail because the Berkeley DB
49library allocates memory from a different heap than the application uses
50to free it.  To avoid this problem, the <a href="/api_c/env_set_alloc.html">DB_ENV-&gt;set_alloc</a> and
51DB-&gt;set_alloc methods can be used to pass Berkeley DB references to the
52application's allocation routines.</p>
53<p>It is not an error to specify only one or two of the possible allocation
54function parameters to these interfaces; however, in that case the
55specified interfaces must be compatible with the standard library
56interfaces, as they will be used together.  The functions specified must
57match the calling conventions of the ANSI C X3.159-1989 (ANSI C) library routines of
58the same name.</p>
59<p>Because databases opened within Berkeley DB environments use the allocation
60interfaces specified to the environment, it is an error to attempt to
61set those interfaces in a database created within an environment.</p>
62<p>The DB-&gt;set_alloc method may not be called after the <a href="/api_c/db_open.html">DB-&gt;open</a> method is called.
63</p>
64<p>The DB-&gt;set_alloc method
65returns a non-zero error value on failure
66and 0 on success.
67</p>
68<br><b>Errors</b>
69<p>The DB-&gt;set_alloc method
70may fail and return one of the following non-zero errors:</p>
71<br>
72<b>EINVAL</b><ul compact><li>If called in a database environment, or called after <a href="/api_c/db_open.html">DB-&gt;open</a> was
73called; or if an
74invalid flag value or parameter was specified.</ul>
75<br>
76<hr size=1 noshade>
77<br><b>Class</b>
78<a href="/api_c/db_class.html">DB</a>
79<br><b>See Also</b>
80<a href="/api_c/db_list.html">Databases and Related Methods</a>
81</tt>
82<table width="100%"><tr><td><br></td><td align=right>
83<a href="/api_c/api_core.html"><img src="/images/api.gif" alt="API"></a><a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a>
84</td></tr></table>
85<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
86</body>
87</html>
88