• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/db-4.8.30/docs/programmer_reference/
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>Storing C/C++ structures/objects</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9    <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
10    <link rel="up" href="am_misc.html" title="Chapter��4.�� Access Method Wrapup" />
11    <link rel="prev" href="am_misc_partial.html" title="Partial record storage and retrieval" />
12    <link rel="next" href="am_misc_perm.html" title="Retrieved key/data permanence for C/C++" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Storing C/C++ structures/objects</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="am_misc_partial.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��4.��
23		Access Method Wrapup
24        </th>
25          <td width="20%" align="right">��<a accesskey="n" href="am_misc_perm.html">Next</a></td>
26        </tr>
27      </table>
28      <hr />
29    </div>
30    <div class="sect1" lang="en" xml:lang="en">
31      <div class="titlepage">
32        <div>
33          <div>
34            <h2 class="title" style="clear: both"><a id="am_misc_struct"></a>Storing C/C++ structures/objects</h2>
35          </div>
36        </div>
37      </div>
38      <p>Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean.
39How you use this depends, to some extent, on the application language
40you are using.  In the C/C++ languages, there are a couple of different
41ways to store structures and objects.</p>
42      <p>First, you can do some form of run-length encoding and copy your
43structure into another piece of memory before storing it:</p>
44      <pre class="programlisting">struct {
45	char *data1;
46	u_int32_t data2;
47	...
48} info;
49size_t len;
50u_int8_t *p, data_buffer[1024];
51
52p = &amp;data_buffer[0];
53len = strlen(info.data1);
54memcpy(p, &amp;len, sizeof(len));
55p += sizeof(len);
56memcpy(p, info.data1, len);
57p += len;
58memcpy(p, &amp;info.data2, sizeof(info.data2));
59p += sizeof(info.data2);
60...</pre>
61      <p>and so on, until all the fields of the structure have been loaded into
62the byte array.  If you want more examples, see the Berkeley DB logging
63routines (for example, btree/btree_auto.c:__bam_split_log()).  This
64technique is generally known as "marshalling".  If you use this
65technique, you must then un-marshall the data when you read it back:</p>
66      <pre class="programlisting">struct {
67	char *data1;
68	u_int32_t data2;
69	...
70} info;
71size_t len;
72u_int8_t *p;
73
74p = &amp;data_buffer[0];
75memcpy(&amp;len, p, sizeof(len));
76p += sizeof(len);
77info.data1 = malloc(len);
78memcpy(info.data1, p, len);
79p += len;
80memcpy(&amp;info.data2, p, sizeof(info.data2));
81p += sizeof(info.data2);
82...</pre>
83      <p>and so on.</p>
84      <p>The second way to solve this problem only works if you have just one
85variable length field in the structure.  In that case, you can declare
86the structure as follows:</p>
87      <pre class="programlisting">struct {
88	int a, b, c;
89	u_int8_t buf[1];
90} info;</pre>
91      <p>Then, let's say you have a string you want to store in this structure.
92When you allocate the structure, you allocate it as:</p>
93      <pre class="programlisting">malloc(sizeof(struct info) + strlen(string));</pre>
94      <p>Since the allocated memory is contiguous, you can the initialize the
95structure as:</p>
96      <pre class="programlisting">info.a = 1;
97info.b = 2;
98info.c = 3;
99memcpy(&amp;info.buf[0], string, strlen(string) + 1);</pre>
100      <p>and give it to Berkeley DB to store, with a length of:</p>
101      <pre class="programlisting">sizeof(struct info) + strlen(string);</pre>
102      <p>In this case, the structure can be copied out of the database and used
103without any additional work.</p>
104    </div>
105    <div class="navfooter">
106      <hr />
107      <table width="100%" summary="Navigation footer">
108        <tr>
109          <td width="40%" align="left"><a accesskey="p" href="am_misc_partial.html">Prev</a>��</td>
110          <td width="20%" align="center">
111            <a accesskey="u" href="am_misc.html">Up</a>
112          </td>
113          <td width="40%" align="right">��<a accesskey="n" href="am_misc_perm.html">Next</a></td>
114        </tr>
115        <tr>
116          <td width="40%" align="left" valign="top">Partial record storage and retrieval��</td>
117          <td width="20%" align="center">
118            <a accesskey="h" href="index.html">Home</a>
119          </td>
120          <td width="40%" align="right" valign="top">��Retrieved key/data permanence for C/C++</td>
121        </tr>
122      </table>
123    </div>
124  </body>
125</html>
126