nedmalloc.h

Go to the documentation of this file.
00001 /* nedalloc, an alternative malloc implementation for multiple threads without
00002 lock contention based on dlmalloc v2.8.3. (C) 2005 Niall Douglas
00003 
00004 Boost Software License - Version 1.0 - August 17th, 2003
00005 
00006 Permission is hereby granted, free of charge, to any person or organization
00007 obtaining a copy of the software and accompanying documentation covered by
00008 this license (the "Software") to use, reproduce, display, distribute,
00009 execute, and transmit the Software, and to prepare derivative works of the
00010 Software, and to permit third-parties to whom the Software is furnished to
00011 do so, all subject to the following:
00012 
00013 The copyright notices in the Software and this entire statement, including
00014 the above license grant, this restriction and the following disclaimer,
00015 must be included in all copies of the Software, in whole or in part, and
00016 all derivative works of the Software, unless such copies or derivative
00017 works are solely in the form of machine-executable object code generated by
00018 a source language processor.
00019 
00020 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00023 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00024 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00025 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00026 DEALINGS IN THE SOFTWARE.
00027 */
00028 
00029 #ifndef NEDMALLOC_H
00030 #define NEDMALLOC_H
00031 
00032 
00033 
00034 #define NO_NED_NAMESPACE
00035 
00036 
00037 
00038 /* See malloc.c.h for what each function does.
00039 
00040 REPLACE_SYSTEM_ALLOCATOR causes nedalloc's functions to be called malloc,
00041 free etc. instead of nedmalloc, nedfree etc. You may or may not want this.
00042 
00043 NO_NED_NAMESPACE prevents the functions from being defined in the nedalloc
00044 namespace when in C++ (uses the global namespace instead).
00045 
00046 EXTSPEC can be defined to be __declspec(dllexport) or
00047 __attribute__ ((visibility("default"))) or whatever you like. It defaults
00048 to extern.
00049 
00050 USE_LOCKS can be 2 if you want to define your own MLOCK_T, INITIAL_LOCK,
00051 ACQUIRE_LOCK, RELEASE_LOCK, TRY_LOCK, IS_LOCKED and NULL_LOCK_INITIALIZER.
00052 
00053 */
00054 
00055 #include <stddef.h>   /* for size_t */
00056 
00057 #ifndef EXTSPEC
00058  #define EXTSPEC extern
00059 #endif
00060 
00061 #if defined(_MSC_VER) && _MSC_VER>=1400
00062  #define MALLOCATTR __declspec(restrict)
00063 #endif
00064 #ifdef __GNUC__
00065  #define MALLOCATTR __attribute__ ((malloc))
00066 #endif
00067 #ifndef MALLOCATTR
00068  #define MALLOCATTR
00069 #endif
00070 
00071 #ifdef REPLACE_SYSTEM_ALLOCATOR
00072  #define nedmalloc               malloc
00073  #define nedcalloc               calloc
00074  #define nedrealloc              realloc
00075  #define nedfree                 free
00076  #define nedmemalign             memalign
00077  #define nedmallinfo             mallinfo
00078  #define nedmallopt              mallopt
00079  #define nedmalloc_trim          malloc_trim
00080  #define nedmalloc_stats         malloc_stats
00081  #define nedmalloc_footprint     malloc_footprint
00082  #define nedindependent_calloc   independent_calloc
00083  #define nedindependent_comalloc independent_comalloc
00084  #ifdef _MSC_VER
00085   #define nedblksize              _msize
00086  #endif
00087 #endif
00088 
00089 #ifndef NO_MALLINFO
00090 #define NO_MALLINFO 0
00091 #endif
00092 
00093 #if !NO_MALLINFO
00094 struct mallinfo;
00095 #endif
00096 
00097 #if defined(__cplusplus)
00098  #if !defined(NO_NED_NAMESPACE)
00099 namespace nedalloc {
00100  #else
00101 extern "C" {
00102  #endif
00103  #define THROWSPEC throw()
00104 #else
00105  #define THROWSPEC
00106 #endif
00107 
00108 /* These are the global functions */
00109 
00110 /* Gets the usable size of an allocated block. Note this will always be bigger than what was
00111 asked for due to rounding etc.
00112 */
00113 EXTSPEC size_t nedblksize(void *mem) THROWSPEC;
00114 
00115 EXTSPEC void nedsetvalue(void *v) THROWSPEC;
00116 
00117 EXTSPEC MALLOCATTR void * nedmalloc(size_t size) THROWSPEC;
00118 EXTSPEC MALLOCATTR void * nedcalloc(size_t no, size_t size) THROWSPEC;
00119 EXTSPEC MALLOCATTR void * nedrealloc(void *mem, size_t size) THROWSPEC;
00120 EXTSPEC void   nedfree(void *mem) THROWSPEC;
00121 EXTSPEC MALLOCATTR void * nedmemalign(size_t alignment, size_t bytes) THROWSPEC;
00122 #if !NO_MALLINFO
00123 EXTSPEC struct mallinfo nedmallinfo(void) THROWSPEC;
00124 #endif
00125 EXTSPEC int    nedmallopt(int parno, int value) THROWSPEC;
00126 EXTSPEC int    nedmalloc_trim(size_t pad) THROWSPEC;
00127 EXTSPEC void   nedmalloc_stats(void) THROWSPEC;
00128 EXTSPEC size_t nedmalloc_footprint(void) THROWSPEC;
00129 EXTSPEC MALLOCATTR void **nedindependent_calloc(size_t elemsno, size_t elemsize, void **chunks) THROWSPEC;
00130 EXTSPEC MALLOCATTR void **nedindependent_comalloc(size_t elems, size_t *sizes, void **chunks) THROWSPEC;
00131 
00132 /* These are the pool functions */
00133 struct nedpool_t;
00134 typedef struct nedpool_t nedpool;
00135 
00136 /* Creates a memory pool for use with the nedp* functions below.
00137 Capacity is how much to allocate immediately (if you know you'll be allocating a lot
00138 of memory very soon) which you can leave at zero. Threads specifies how many threads
00139 will *normally* be accessing the pool concurrently. Setting this to zero means it
00140 extends on demand, but be careful of this as it can rapidly consume system resources
00141 where bursts of concurrent threads use a pool at once.
00142 */
00143 EXTSPEC MALLOCATTR nedpool *nedcreatepool(size_t capacity, int threads) THROWSPEC;
00144 
00145 /* Destroys a memory pool previously created by nedcreatepool().
00146 */
00147 EXTSPEC void neddestroypool(nedpool *p) THROWSPEC;
00148 
00149 /* Sets a value to be associated with a pool. You can retrieve this value by passing
00150 any memory block allocated from that pool.
00151 */
00152 EXTSPEC void nedpsetvalue(nedpool *p, void *v) THROWSPEC;
00153 /* Gets a previously set value using nedpsetvalue() or zero if memory is unknown.
00154 Optionally can also retrieve pool.
00155 */
00156 EXTSPEC void *nedgetvalue(nedpool **p, void *mem) THROWSPEC;
00157 
00158 /* Disables the thread cache for the calling thread, returning any existing cache
00159 data to the central pool.
00160 */
00161 EXTSPEC void neddisablethreadcache(nedpool *p) THROWSPEC;
00162 
00163 EXTSPEC MALLOCATTR void * nedpmalloc(nedpool *p, size_t size) THROWSPEC;
00164 EXTSPEC MALLOCATTR void * nedpcalloc(nedpool *p, size_t no, size_t size) THROWSPEC;
00165 EXTSPEC MALLOCATTR void * nedprealloc(nedpool *p, void *mem, size_t size) THROWSPEC;
00166 EXTSPEC void   nedpfree(nedpool *p, void *mem) THROWSPEC;
00167 EXTSPEC MALLOCATTR void * nedpmemalign(nedpool *p, size_t alignment, size_t bytes) THROWSPEC;
00168 #if !NO_MALLINFO
00169 EXTSPEC struct mallinfo nedpmallinfo(nedpool *p) THROWSPEC;
00170 #endif
00171 EXTSPEC int    nedpmallopt(nedpool *p, int parno, int value) THROWSPEC;
00172 EXTSPEC int    nedpmalloc_trim(nedpool *p, size_t pad) THROWSPEC;
00173 EXTSPEC void   nedpmalloc_stats(nedpool *p) THROWSPEC;
00174 EXTSPEC size_t nedpmalloc_footprint(nedpool *p) THROWSPEC;
00175 EXTSPEC MALLOCATTR void **nedpindependent_calloc(nedpool *p, size_t elemsno, size_t elemsize, void **chunks) THROWSPEC;
00176 EXTSPEC MALLOCATTR void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **chunks) THROWSPEC;
00177 
00178 #if defined(__cplusplus)
00179 }
00180 #endif
00181 
00182 #undef MALLOCATTR
00183 #undef EXTSPEC
00184 
00185 #endif

Generated on Wed May 23 06:00:13 2012 for cpp by  doxygen 1.5.6