NCBI C Toolkit Cross Reference

C/connect/ncbi_heapmgr.h


  1 #ifndef CONNECT___NCBI_HEAPMGR__H
  2 #define CONNECT___NCBI_HEAPMGR__H
  3 
  4 /*  $Id: ncbi_heapmgr.h,v 6.25 2006/11/20 20:18:56 lavr Exp $
  5  * ===========================================================================
  6  *
  7  *                            PUBLIC DOMAIN NOTICE
  8  *               National Center for Biotechnology Information
  9  *
 10  *  This software/database is a "United States Government Work" under the
 11  *  terms of the United States Copyright Act.  It was written as part of
 12  *  the author's official duties as a United States Government employee and
 13  *  thus cannot be copyrighted.  This software/database is freely available
 14  *  to the public for use. The National Library of Medicine and the U.S.
 15  *  Government have not placed any restriction on its use or reproduction.
 16  *
 17  *  Although all reasonable efforts have been taken to ensure the accuracy
 18  *  and reliability of the software and data, the NLM and the U.S.
 19  *  Government do not and cannot warrant the performance or results that
 20  *  may be obtained by using this software or data. The NLM and the U.S.
 21  *  Government disclaim all warranties, express or implied, including
 22  *  warranties of performance, merchantability or fitness for any particular
 23  *  purpose.
 24  *
 25  *  Please cite the author in any work or product based on this material.
 26  *
 27  * ===========================================================================
 28  *
 29  * Author:  Anton Lavrentiev, Denis Vakatov
 30  *
 31  * File Description:
 32  *   Simple heap manager with a primitive garbage collection
 33  *
 34  */
 35 
 36 #include <connect/ncbi_types.h>
 37 
 38 
 39 /** @addtogroup ServiceSupport
 40  *
 41  * @{
 42  */
 43 
 44 
 45 #ifdef __cplusplus
 46 extern "C" {
 47 #endif
 48 
 49 
 50 /* Heap handle
 51  */
 52 struct SHEAP_tag;
 53 typedef struct SHEAP_tag* HEAP;
 54 
 55 
 56 /* Header of a heap block
 57  */
 58 typedef struct {
 59     unsigned int flag;  /* (short)flag == 0 if block is vacant              */
 60     TNCBI_Size   size;  /* size of the block (including the block header)   */
 61 } SHEAP_Block;
 62 
 63 
 64 /* Callback to resize the heap (a la 'realloc').
 65  * NOTE: the returned address must be aligned with the 'double' boundary!
 66  *
 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  * (and must not change the original heap extent / content, if any).
 77  * When expected to return 0, this callback must always do so.
 78  */
 79 typedef void* (*FHEAP_Resize)
 80 (void*      old_base,  /* current base of the heap to be expanded           */
 81  TNCBI_Size new_size,  /* requested new heap size (zero to deallocate heap) */
 82  void*      arg        /* user-supplied argument, see HEAP_Create() below   */
 83  );
 84 
 85 
 86 /* Create new heap.
 87  * NOTE: the initial heap base must be aligned with a 'double' boundary!
 88  */
 89 extern NCBI_XCONNECT_EXPORT HEAP HEAP_Create
 90 (void*        base,        /* initial heap base (use "resize" if NULL) */
 91  TNCBI_Size   size,        /* initial heap size                        */
 92  TNCBI_Size   chunk_size,  /* minimal increment size                   */
 93  FHEAP_Resize resize,      /* NULL if not resizeable                   */
 94  void*        arg          /* user argument to pass to "resize"        */
 95  );
 96 
 97 
 98 /* Attach to an already existing heap (in read-only mode).
 99  */
100 extern NCBI_XCONNECT_EXPORT HEAP HEAP_Attach
101 (const void* base,         /* base of the heap to attach to */
102  int         serial        /* serial number to assign       */
103  );
104 
105 /* Expedited HEAP_Attach() that does not calculate heap size on its own */
106 extern NCBI_XCONNECT_EXPORT HEAP HEAP_AttachFast
107 (const void* base,         /* base of the heap to attach to                  */
108  TNCBI_Size  size,         /* heap extent -- must be non-0 for non-NULL base */
109  int         serial        /* serial number to assign                        */
110  );
111 
112 
113 /* Allocate a new block of memory in the heap.
114  */
115 extern NCBI_XCONNECT_EXPORT SHEAP_Block* HEAP_Alloc
116 (HEAP       heap,          /* heap handle                          */
117  TNCBI_Size size           /* data size of the block to accomodate */
118  );
119 
120 
121 /* Allocate a new block of memory in the heap
122  * (faster than HEAP_Alloc() but inverses the insertion order).
123  */
124 extern NCBI_XCONNECT_EXPORT SHEAP_Block* HEAP_AllocFast
125 (HEAP       heap,          /* heap handle                          */
126  TNCBI_Size size           /* data size of the block to accomodate */
127  );
128 
129 
130 /* Deallocate a block pointed to by "ptr".
131  */
132 extern NCBI_XCONNECT_EXPORT void HEAP_Free
133 (HEAP         heap,        /* heap handle         */
134  SHEAP_Block* ptr          /* block to deallocate */
135  );
136 
137 
138 /* Deallocate a block pointed to by "ptr" and having "prev" as its predecessor
139  * (NULL if "ptr" is the first on the heap) -- faster variant of HEAP_Free().
140  * NOTE:  Since the block pointed to by "ptr" may cause free blocks to
141  * coalesce, to use this call again while walking the following rule must
142  * be utilized:  If "prev" was free, "prev" must not get advanced;
143  * otherwise, "prev" must be updated with "ptr"'s value.
144  */
145 extern NCBI_XCONNECT_EXPORT void HEAP_FreeFast
146 (HEAP               heap,  /* heap handle         */
147  SHEAP_Block*       ptr,   /* block to deallocate */
148  const SHEAP_Block* prev   /* block's predecessor */
149  );
150 
151 
152 /* Iterate through the heap blocks.
153  * Return pointer to the block following block "prev_block".
154  * Return NULL if "prev_block" is the last block of the heap.
155  */
156 extern NCBI_XCONNECT_EXPORT SHEAP_Block* HEAP_Walk
157 (const HEAP         heap,  /* heap handle                                  */
158  const SHEAP_Block* prev   /* (if 0, then get the first block of the heap) */
159  );
160 
161 
162 /* Trim the heap, making garbage collection first.  Returned is
163  * the resultant heap, which has its last block (if any) trimmed to the
164  * size of heap chunk size as specified at the time of the heap creation.
165  * No change in size is made if the last block is not free or large
166  * enough to allow the trimming.  NULL gets returned on NULL or read-only
167  * heaps, or if a resize error has occurred.
168  * Note that trimming can cause the entire heap extent (of an empty heap)
169  * to deallocate (so that HEAP_Base() and HEAP_Size() will return 0).
170  */
171 extern NCBI_XCONNECT_EXPORT HEAP HEAP_Trim(HEAP heap);
172 
173 
174 /* Make a snapshot of a given heap.  Return a read-only heap
175  * (like the one after HEAP_Attach[Fast]), which must be freed by a call
176  * to either HEAP_Detach() or HEAP_Destroy() when no longer needed.
177  * A copy is created reference-counted (with the initial ref.count set to 1).
178  */
179 extern NCBI_XCONNECT_EXPORT HEAP HEAP_Copy
180 (const HEAP orig,          /* original heap to copy from               */
181  size_t     extra,         /* extra amount to add past the heap extent */
182  int        serial         /* serial number to assign                  */
183  );
184 
185 
186 /* Add reference counter to the given copy heap (no effect on
187  * a heap, which have been HEAP_Create()'d or HEAP_Attach[Fast]()'d).
188  * The heap handle then will be destroyed only when the internal
189  * reference counter reaches 0.  No internal locking is provided.
190  */
191 extern NCBI_XCONNECT_EXPORT void HEAP_AddRef(HEAP heap);
192 
193 
194 /* Detach heap (previously attached by HEAP_Attach[Fast]).
195  * For copy heap, it decrements an internal ref. counter by one, and
196  * destroys the heap handle if and only if the counter has reached 0.
197  * No internal locking of the reference counter is provided.
198  * For heaps that are results of HEAP_Copy() call,
199  * both HEAP_Detach() and HEAP_Destroy() can be used interchangeably.
200  */
201 extern NCBI_XCONNECT_EXPORT void HEAP_Detach(HEAP heap);
202 
203 
204 /* Destroy heap (previously created by HEAP_Create()).
205  * For copy heaps -- see comments for HEAP_Detach() above.
206  */
207 extern NCBI_XCONNECT_EXPORT void HEAP_Destroy(HEAP heap);
208 
209 
210 /* Get base address of the heap.
211  * Return NULL if heap is passed as NULL, or when the heap is completely empty.
212  */
213 extern NCBI_XCONNECT_EXPORT void* HEAP_Base(const HEAP heap);
214 
215 
216 /* Get the extent of the heap.
217  * Return 0 if heap is passed as NULL, or when the heap is completely empty.
218  */
219 extern NCBI_XCONNECT_EXPORT TNCBI_Size HEAP_Size(const HEAP heap);
220 
221 
222 /* Get non-zero serial number of the heap.
223  * Return 0 if heap is passed as NULL,
224  * or the heap is not a copy but the original.
225  */
226 extern NCBI_XCONNECT_EXPORT int HEAP_Serial(const HEAP heap);
227 
228 
229 /* Set heap access speed and check level while walking:
230  * fast == eOn  turns on fast heap operations (default);
231  * fast == eOff turns off fast heap operations (more checks, slower);
232  * fast == eDefault does not change current setting;
233  * newalk == eOn turns on new heap integrity checks while walking;
234  * newalk == eOff turns off new heap integrity checks (default);
235  * newalk == eDefault keeps current setting.
236  * This call is intended for internal uses; and default settings (fast ops
237  * w/o new structure integrity checks) should suffice for most users.
238  */
239 extern NCBI_XCONNECT_EXPORT void HEAP_Options(ESwitch fast, ESwitch newalk);
240 
241 
242 #ifdef __cplusplus
243 } /* extern "C" */
244 #endif
245 
246 
247 /* @} */
248 
249 
250 /*
251  * --------------------------------------------------------------------------
252  * $Log: ncbi_heapmgr.h,v $
253  * Revision 6.25  2006/11/20 20:18:56  lavr
254  * +HEAP_AllocFast() [and better documentation on HEAP_FreeFast()]
255  *
256  * Revision 6.24  2006/11/20 17:26:20  lavr
257  * HEAP_FreeFast() better documented (with all side effects)
258  *
259  * Revision 6.23  2006/11/20 17:01:52  lavr
260  * Added missing declaration of newly added HEAP_FreeFast()
261  *
262  * Revision 6.22  2006/11/20 16:38:15  lavr
263  * Faster heap with free blocks linked into a list.
264  * HEAP_AttachEx() -> HEAP_AttachFast()
265  * +HEAP_FreeFast(), +HEAP_Options()
266  *
267  * Revision 6.21  2006/03/05 17:32:35  lavr
268  * Revised API to allow to create ref-counted heap copies
269  *
270  * Revision 6.20  2004/07/08 14:11:11  lavr
271  * Fix few inline descriptions
272  *
273  * Revision 6.19  2003/09/23 21:00:53  lavr
274  * +HEAP_AttachEx()
275  *
276  * Revision 6.18  2003/09/02 20:45:45  lavr
277  * -<connect/connect_export.h> -- now included from <connect/ncbi_types.h>
278  *
279  * Revision 6.17  2003/08/28 21:09:37  lavr
280  * Accept (and allocate) additional heap extent in HEAP_CopySerial()
281  *
282  * Revision 6.16  2003/08/25 14:50:10  lavr
283  * Heap arena ptrs changed to be "void*";  expand routine to take user arg
284  *
285  * Revision 6.15  2003/07/31 17:53:43  lavr
286  * +HEAP_Trim()
287  *
288  * Revision 6.14  2003/04/09 17:58:51  siyan
289  * Added doxygen support
290  *
291  * Revision 6.13  2003/01/08 01:59:32  lavr
292  * DLL-ize CONNECT library for MSVC (add NCBI_XCONNECT_EXPORT)
293  *
294  * Revision 6.12  2002/09/25 20:08:43  lavr
295  * Added table to explain expand callback inputs and outputs
296  *
297  * Revision 6.11  2002/09/19 18:00:58  lavr
298  * Header file guard macro changed; log moved to the end
299  *
300  * Revision 6.10  2002/04/13 06:33:22  lavr
301  * +HEAP_Base(), +HEAP_Size(), +HEAP_Serial(), new HEAP_CopySerial()
302  *
303  * Revision 6.9  2001/07/03 20:23:46  lavr
304  * Added function: HEAP_Copy()
305  *
306  * Revision 6.8  2001/06/19 20:16:19  lavr
307  * Added #include <connect/ncbi_types.h>
308  *
309  * Revision 6.7  2001/06/19 19:09:35  lavr
310  * Type change: size_t -> TNCBI_Size; time_t -> TNCBI_Time
311  *
312  * Revision 6.6  2000/10/05 21:25:45  lavr
313  * ncbiconf.h removed
314  *
315  * Revision 6.5  2000/10/05 21:09:52  lavr
316  * ncbiconf.h included
317  *
318  * Revision 6.4  2000/05/23 21:41:05  lavr
319  * Alignment changed to 'double'
320  *
321  * Revision 6.3  2000/05/12 18:28:40  lavr
322  * First working revision
323  *
324  * ==========================================================================
325  */
326 
327 #endif /* CONNECT___NCBI_HEAPMGR__H */
328 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.