-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathatom.h
179 lines (152 loc) · 5.02 KB
/
atom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* atom.h -- indexed data block storage */
/**
* \file atom.h
*
* Indexed data block store.
*
* Atoms are indices assigned to blocks of stored data. Identical data blocks
* are assigned the same atom. Atoms belonging to the same set can be
* directly compared avoiding the need to memcmp, strcmp, or otherwise
* linearly compare the contents of the respective data blocks.
*
* Data blocks can be retrieved by quoting an atom to atom_get. This returns
* a pointer to the data block along with its length.
*
* To avoid heap overhead, atoms are stored in a series of fixed-size pools
* of memory. The size of the pools may be specified when the set is first
* created.
*/
#ifndef DATASTRUCT_ATOM_H
#define DATASTRUCT_ATOM_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
#include "base/result.h"
/* ----------------------------------------------------------------------- */
#define result_ATOM_SET_EMPTY (result_BASE_ATOM + 0)
#define result_ATOM_NAME_EXISTS (result_BASE_ATOM + 1)
#define result_ATOM_OUT_OF_RANGE (result_BASE_ATOM + 2)
/* ----------------------------------------------------------------------- */
/**
* An 'atom set' holds a collection of data blocks.
*/
typedef struct atom_set atom_set_t;
/* ----------------------------------------------------------------------- */
/**
* Create a new atom set.
*
* This chooses default values for maximum data block size, etc.
*
* \return New atom set, or NULL if out of memory.
*/
atom_set_t *atom_create(void);
/**
* Create a new atom set using the specified data sizes.
*
* \param locpoolsz Size of a location pool, or zero for the default.
* Set this to the number of entries you typically expect to
* store.
* \param blkpoolsz Size of a block pool, or zero for the default.
* No inserted data block may be larger than this.
* Increasing this value will use fewer individual block
* pools, reducing heap overhead, at the expense of
* potentially greater wasted space should the block pool
* remain not fully allocated.
*
* \return New atom set, or NULL if out of memory.
*/
atom_set_t *atom_create_tuned(size_t locpoolsz, size_t blkpoolsz);
/**
* Destroy an existing atom set.
*
* \param doomed Atom set to destroy.
*/
void atom_destroy(atom_set_t *doomed);
/* ----------------------------------------------------------------------- */
/**
* An atom - an index assigned to a stored block of memory.
*/
typedef int atom_t;
#define atom_NOT_FOUND ((atom_t) -1)
/* ----------------------------------------------------------------------- */
/**
* Create a new atom from the specified data block.
*
* \param set Atom set.
* \param block Data block to insert.
* \param length Length of data block, in bytes.
* \param[out] atom New atom.
*
* \return Error indication.
*/
result_t atom_new(atom_set_t *set,
const unsigned char *block,
size_t length,
atom_t *atom);
/**
* Delete an existing atom.
*
* \param set Atom set.
* \param atom Atom to delete.
*/
void atom_delete(atom_set_t *set, atom_t atom);
/**
* Retrieve an existing data block and its length.
*
* \param set Atom set.
* \param atom Atom to retrieve.
* \param[out] length Length of data block, in bytes.
* NULL if length is not required.
*
* \return Data block.
*/
const unsigned char *atom_get(atom_set_t *set,
atom_t atom,
size_t *length);
/**
* Set an atom to hold new data.
*
* \param set Atom set.
* \param atom Atom to set.
* \param block New data block.
* \param length Length of data block, in bytes.
*
* \return Error indication.
*/
result_t atom_set(atom_set_t *set,
atom_t atom,
const unsigned char *block,
size_t length);
/**
* Retrieve the atom matching the specified data block.
*
* \param set Atom set.
* \param block Data block to search for.
* \param length Length of data block, in bytes.
*
* \return Existing atom or atom_NOT_FOUND.
*/
atom_t atom_for_block(atom_set_t *set,
const unsigned char *block,
size_t length);
/* ----------------------------------------------------------------------- */
/**
* Delete an existing atom specified by data block.
*
* This is a convenience function equivalent to:
* atom_delete(set, atom_for_block(set, block, length)).
*
* \param set Atom set.
* \param block Data block to retrieve.
* \param length Length of data block, in bytes.
*/
void atom_delete_block(atom_set_t *set,
const unsigned char *block,
size_t length);
/* ----------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
#endif /* DATASTRUCT_ATOM_H */