~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/docs/ref/am_misc/struct.html

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!--$Id$-->
2
 
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
 
<!--All rights reserved.-->
4
 
<!--See the file LICENSE for redistribution information.-->
5
 
<html>
6
 
<head>
7
 
<title>Berkeley DB Reference Guide: Storing C/C++ structures/objects</title>
8
 
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
 
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,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><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
14
 
<td align=right><a href="../../ref/am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
15
 
</td></tr></table>
16
 
<p>
17
 
<h1 align=center>Storing C/C++ structures/objects</h1>
18
 
<p>Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean.
19
 
How you use this depends, to some extent, on the application language
20
 
you are using.  In the C/C++ languages, there are a couple of different
21
 
ways to store structures and objects.
22
 
<p>First, you can do some form of run-length encoding and copy your
23
 
structure into another piece of memory before storing it:
24
 
<p><blockquote><pre>struct {
25
 
        char *data1;
26
 
        u_int32_t data2;
27
 
        ...
28
 
} info;
29
 
size_t len;
30
 
u_int8_t *p, data_buffer[1024];
31
 
<p>
32
 
p = &data_buffer[0];
33
 
len = strlen(info.data1);
34
 
memcpy(p, &len, sizeof(len));
35
 
p += sizeof(len);
36
 
memcpy(p, info.data1, len);
37
 
p += len;
38
 
memcpy(p, &info.data2, sizeof(info.data2));
39
 
p += sizeof(info.data2);
40
 
...</pre></blockquote>
41
 
<p>and so on, until all the fields of the structure have been loaded into
42
 
the byte array.  If you want more examples, see the Berkeley DB logging
43
 
routines (for example, btree/btree_auto.c:__bam_split_log()).  This
44
 
technique is generally known as "marshalling".  If you use this
45
 
technique, you must then un-marshall the data when you read it back:
46
 
<p><blockquote><pre>struct {
47
 
        char *data1;
48
 
        u_int32_t data2;
49
 
        ...
50
 
} info;
51
 
size_t len;
52
 
u_int8_t *p;
53
 
<p>
54
 
p = &data_buffer[0];
55
 
memcpy(&len, p, sizeof(len));
56
 
p += sizeof(len);
57
 
info.data1 = malloc(len);
58
 
memcpy(info.data1, p, len);
59
 
p += len;
60
 
memcpy(&info.data2, p, sizeof(info.data2));
61
 
p += sizeof(info.data2);
62
 
...</pre></blockquote>
63
 
<p>and so on.
64
 
<p>The second way to solve this problem only works if you have just one
65
 
variable length field in the structure.  In that case, you can declare
66
 
the structure as follows:
67
 
<p><blockquote><pre>struct {
68
 
        int a, b, c;
69
 
        u_int8_t buf[1];
70
 
} info;</pre></blockquote>
71
 
<p>Then, let's say you have a string you want to store in this structure.
72
 
When you allocate the structure, you allocate it as:
73
 
<p><blockquote><pre>malloc(sizeof(struct info) + strlen(string));</pre></blockquote>
74
 
<p>Since the allocated memory is contiguous, you can the initialize the
75
 
structure as:
76
 
<p><blockquote><pre>info.a = 1;
77
 
info.b = 2;
78
 
info.c = 3;
79
 
memcpy(&info.buf[0], string, strlen(string));</pre></blockquote>
80
 
<p>and give it to Berkeley DB to store, with a length of:
81
 
<p><blockquote><pre>sizeof(struct info) + strlen(string);</pre></blockquote>
82
 
<p>In this case, the structure can be copied out of the database and used
83
 
without any additional work.
84
 
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
85
 
</td></tr></table>
86
 
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
87
 
</body>
88
 
</html>