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