31
31
* File Description:
32
32
* Simple heap manager with a primitive garbage collection
34
* --------------------------------------------------------------------------
35
* $Log: ncbi_heapmgr.h,v $
36
* Revision 6.9 2001/07/03 20:23:46 lavr
37
* Added function: HEAP_Copy()
39
* Revision 6.8 2001/06/19 20:16:19 lavr
40
* Added #include <connect/ncbi_types.h>
42
* Revision 6.7 2001/06/19 19:09:35 lavr
43
* Type change: size_t -> TNCBI_Size; time_t -> TNCBI_Time
45
* Revision 6.6 2000/10/05 21:25:45 lavr
48
* Revision 6.5 2000/10/05 21:09:52 lavr
51
* Revision 6.4 2000/05/23 21:41:05 lavr
52
* Alignment changed to 'double'
54
* Revision 6.3 2000/05/12 18:28:40 lavr
55
* First working revision
57
* ==========================================================================
60
36
#include <connect/ncbi_types.h>
39
/** @addtogroup ServiceSupport
81
64
/* Callback to expand the heap (a la 'realloc').
82
65
* NOTE: the returned address must be aligned on a 'double' boundary!
67
* old_base | new_size | Expected result
68
* ------------+------------+--------------------------------------------------
69
* non-NULL | 0 | Deallocate old_base and return 0
70
* non-NULL | non-zero | Reallocate to the requested size, return new base
71
* 0 | non-zero | Allocate (anew) and return base
72
* 0 | 0 | Do nothing, return 0
73
* ------------+------------+--------------------------------------------------
74
* Note that reallocation can request either to expand or to shrink the
75
* heap extent. When (re-)allocation fails, the callback should return 0.
76
* When expected to return 0, this callback has to always do so.
84
typedef char* (*FHEAP_Expand)
85
(char* old_base, /* current base of the heap to be expanded */
86
TNCBI_Size new_size /* requested new heap size (zero to deallocate heap) */
78
typedef void* (*FHEAP_Expand)
79
(void* old_base, /* current base of the heap to be expanded */
80
TNCBI_Size new_size, /* requested new heap size (zero to deallocate heap) */
81
void* arg /* user-supplied argument, see HEAP_Create() below */
90
85
/* Create new heap.
91
86
* NOTE: the initial heap base must be aligned on a 'double' boundary!
94
(char* base, /* initial heap base (use "expand" if NULL) */
88
extern NCBI_XCONNECT_EXPORT HEAP HEAP_Create
89
(void* base, /* initial heap base (use "expand" if NULL) */
95
90
TNCBI_Size size, /* initial heap size */
96
91
TNCBI_Size chunk_size, /* minimal increment size */
97
FHEAP_Expand expand /* NULL if not expandable */
92
FHEAP_Expand expand, /* NULL if not expandable */
93
void* arg /* arg to pass to expand (if any) */
101
97
/* Attach to an already existing heap (in read-only mode).
104
(char* base /* base of the heap to attach to */
99
extern NCBI_XCONNECT_EXPORT HEAP HEAP_Attach
100
(const void* base /* base of the heap to attach to */
103
extern NCBI_XCONNECT_EXPORT HEAP HEAP_AttachEx
108
109
/* Allocate a new block of memory in the heap.
110
SHEAP_Block* HEAP_Alloc
111
extern NCBI_XCONNECT_EXPORT SHEAP_Block* HEAP_Alloc
111
112
(HEAP heap, /* heap handle */
112
113
TNCBI_Size size /* data size of the block to contain */
125
126
* Return pointer to the block following block "prev_block".
126
127
* Return NULL if "prev_block" is the last block of the heap.
128
SHEAP_Block* HEAP_Walk
129
(HEAP heap, /* heap handle */
129
extern NCBI_XCONNECT_EXPORT SHEAP_Block* HEAP_Walk
130
(const HEAP heap, /* heap handle */
130
131
const SHEAP_Block* prev /* (if 0, then get the first block of the heap) */
134
/* Make a snapshot of a given heap. Returne a read-only heap
135
/* Trim the heap, making garbage collection first. Returned is
136
* the size of the resultant heap, which has its last block trimmed
137
* to the size of heap chunk size as specified at the time of the
138
* heap creation. No change in size is made if the last block is
139
* not free or large enough to allow the trimming. 0 returned on
140
* NULL or read-only heaps, or if an error has occurred.
142
extern NCBI_XCONNECT_EXPORT TNCBI_Size HEAP_Trim(HEAP heap);
145
/* Make a snapshot of a given heap. Return a read-only heap
135
146
* (like one after HEAP_Attach), which must be freed by a call to
136
147
* either HEAP_Detach or HEAP_Destroy when no longer needed.
148
* If a non-zero number provided (serial number) it is stored
149
* in the heap descriptor (zero serial is always turned into 1).
138
HEAP HEAP_Copy(HEAP orig);
151
extern NCBI_XCONNECT_EXPORT HEAP HEAP_CopySerial
152
(const HEAP orig, /* original heap to copy from */
153
size_t extra, /* extra amount to add past the heap extent */
154
int serial /* serial number to assign (default is 1) */
157
#define HEAP_Copy(orig) HEAP_CopySerial(orig, 0, 0)
141
160
/* Detach heap (previously attached by HEAP_Attach).
143
void HEAP_Detach(HEAP heap);
162
extern NCBI_XCONNECT_EXPORT void HEAP_Detach(HEAP heap);
146
165
/* Destroy heap (previously created by HEAP_Create).
148
void HEAP_Destroy(HEAP heap);
167
extern NCBI_XCONNECT_EXPORT void HEAP_Destroy(HEAP heap);
170
/* Get base address of the heap.
172
extern NCBI_XCONNECT_EXPORT void* HEAP_Base(const HEAP heap);
175
/* Get the extent of the heap.
177
extern NCBI_XCONNECT_EXPORT TNCBI_Size HEAP_Size(const HEAP heap);
180
/* Get non-zero serial number of the heap.
181
* Return 0 if HEAP is passed as 0, or the heap is not a copy but the original.
183
extern NCBI_XCONNECT_EXPORT int HEAP_Serial(const HEAP heap);
151
186
#ifdef __cplusplus
152
187
} /* extern "C" */
155
#endif /* NCBI_HEAPMGR__H */
195
* --------------------------------------------------------------------------
196
* $Log: ncbi_heapmgr.h,v $
197
* Revision 6.20 2004/07/08 14:11:11 lavr
198
* Fix few inline descriptions
200
* Revision 6.19 2003/09/23 21:00:53 lavr
203
* Revision 6.18 2003/09/02 20:45:45 lavr
204
* -<connect/connect_export.h> -- now included from <connect/ncbi_types.h>
206
* Revision 6.17 2003/08/28 21:09:37 lavr
207
* Accept (and allocate) additional heap extent in HEAP_CopySerial()
209
* Revision 6.16 2003/08/25 14:50:10 lavr
210
* Heap arena ptrs changed to be "void*"; expand routine to take user arg
212
* Revision 6.15 2003/07/31 17:53:43 lavr
215
* Revision 6.14 2003/04/09 17:58:51 siyan
216
* Added doxygen support
218
* Revision 6.13 2003/01/08 01:59:32 lavr
219
* DLL-ize CONNECT library for MSVC (add NCBI_XCONNECT_EXPORT)
221
* Revision 6.12 2002/09/25 20:08:43 lavr
222
* Added table to explain expand callback inputs and outputs
224
* Revision 6.11 2002/09/19 18:00:58 lavr
225
* Header file guard macro changed; log moved to the end
227
* Revision 6.10 2002/04/13 06:33:22 lavr
228
* +HEAP_Base(), +HEAP_Size(), +HEAP_Serial(), new HEAP_CopySerial()
230
* Revision 6.9 2001/07/03 20:23:46 lavr
231
* Added function: HEAP_Copy()
233
* Revision 6.8 2001/06/19 20:16:19 lavr
234
* Added #include <connect/ncbi_types.h>
236
* Revision 6.7 2001/06/19 19:09:35 lavr
237
* Type change: size_t -> TNCBI_Size; time_t -> TNCBI_Time
239
* Revision 6.6 2000/10/05 21:25:45 lavr
242
* Revision 6.5 2000/10/05 21:09:52 lavr
243
* ncbiconf.h included
245
* Revision 6.4 2000/05/23 21:41:05 lavr
246
* Alignment changed to 'double'
248
* Revision 6.3 2000/05/12 18:28:40 lavr
249
* First working revision
251
* ==========================================================================
254
#endif /* CONNECT___NCBI_HEAPMGR__H */