1/* nedalloc, an alternative malloc implementation for multiple threads without 2lock contention based on dlmalloc v2.8.3. (C) 2005 Niall Douglas 3 4Boost Software License - Version 1.0 - August 17th, 2003 5 6Permission is hereby granted, free of charge, to any person or organization 7obtaining a copy of the software and accompanying documentation covered by 8this license (the "Software") to use, reproduce, display, distribute, 9execute, and transmit the Software, and to prepare derivative works of the 10Software, and to permit third-parties to whom the Software is furnished to 11do so, all subject to the following: 12 13The copyright notices in the Software and this entire statement, including 14the above license grant, this restriction and the following disclaimer, 15must be included in all copies of the Software, in whole or in part, and 16all derivative works of the Software, unless such copies or derivative 17works are solely in the form of machine-executable object code generated by 18a source language processor. 19 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26DEALINGS IN THE SOFTWARE. 27*/ 28 29#ifndef NEDMALLOC_H 30#define NEDMALLOC_H 31 32 33/* See malloc.c.h for what each function does. 34 35REPLACE_SYSTEM_ALLOCATOR causes nedalloc's functions to be called malloc, 36free etc. instead of nedmalloc, nedfree etc. You may or may not want this. 37 38NO_NED_NAMESPACE prevents the functions from being defined in the nedalloc 39namespace when in C++ (uses the global namespace instead). 40 41EXTSPEC can be defined to be __declspec(dllexport) or 42__attribute__ ((visibility("default"))) or whatever you like. It defaults 43to extern. 44 45USE_LOCKS can be 2 if you want to define your own MLOCK_T, INITIAL_LOCK, 46ACQUIRE_LOCK, RELEASE_LOCK, TRY_LOCK, IS_LOCKED and NULL_LOCK_INITIALIZER. 47 48*/ 49 50#include <stddef.h>/* for size_t */ 51 52#ifndef EXTSPEC 53#define EXTSPEC extern 54#endif 55 56#if defined(_MSC_VER) && _MSC_VER>=1400 57#define MALLOCATTR __declspec(restrict) 58#endif 59#ifdef __GNUC__ 60#define MALLOCATTR __attribute__ ((malloc)) 61#endif 62#ifndef MALLOCATTR 63#define MALLOCATTR 64#endif 65 66#ifdef REPLACE_SYSTEM_ALLOCATOR 67#define nedmalloc malloc 68#define nedcalloc calloc 69#define nedrealloc realloc 70#define nedfree free 71#define nedmemalign memalign 72#define nedmallinfo mallinfo 73#define nedmallopt mallopt 74#define nedmalloc_trim malloc_trim 75#define nedmalloc_stats malloc_stats 76#define nedmalloc_footprint malloc_footprint 77#define nedindependent_calloc independent_calloc 78#define nedindependent_comalloc independent_comalloc 79#ifdef _MSC_VER 80#define nedblksize _msize 81#endif 82#endif 83 84#ifndef NO_MALLINFO 85#define NO_MALLINFO 0 86#endif 87 88#if !NO_MALLINFO 89struct mallinfo; 90#endif 91 92#if defined(__cplusplus) 93#if !defined(NO_NED_NAMESPACE) 94namespace nedalloc { 95#else 96extern"C"{ 97#endif 98#define THROWSPEC throw() 99#else 100#define THROWSPEC 101#endif 102 103/* These are the global functions */ 104 105/* Gets the usable size of an allocated block. Note this will always be bigger than what was 106asked for due to rounding etc. 107*/ 108EXTSPEC size_tnedblksize(void*mem) THROWSPEC; 109 110EXTSPEC voidnedsetvalue(void*v) THROWSPEC; 111 112EXTSPEC MALLOCATTR void*nedmalloc(size_t size) THROWSPEC; 113EXTSPEC MALLOCATTR void*nedcalloc(size_t no,size_t size) THROWSPEC; 114EXTSPEC MALLOCATTR void*nedrealloc(void*mem,size_t size) THROWSPEC; 115EXTSPEC voidnedfree(void*mem) THROWSPEC; 116EXTSPEC MALLOCATTR void*nedmemalign(size_t alignment,size_t bytes) THROWSPEC; 117#if !NO_MALLINFO 118EXTSPEC struct mallinfo nedmallinfo(void) THROWSPEC; 119#endif 120EXTSPEC intnedmallopt(int parno,int value) THROWSPEC; 121EXTSPEC intnedmalloc_trim(size_t pad) THROWSPEC; 122EXTSPEC voidnedmalloc_stats(void) THROWSPEC; 123EXTSPEC size_tnedmalloc_footprint(void) THROWSPEC; 124EXTSPEC MALLOCATTR void**nedindependent_calloc(size_t elemsno,size_t elemsize,void**chunks) THROWSPEC; 125EXTSPEC MALLOCATTR void**nedindependent_comalloc(size_t elems,size_t*sizes,void**chunks) THROWSPEC; 126 127/* These are the pool functions */ 128struct nedpool_t; 129typedefstruct nedpool_t nedpool; 130 131/* Creates a memory pool for use with the nedp* functions below. 132Capacity is how much to allocate immediately (if you know you'll be allocating a lot 133of memory very soon) which you can leave at zero. Threads specifies how many threads 134will *normally* be accessing the pool concurrently. Setting this to zero means it 135extends on demand, but be careful of this as it can rapidly consume system resources 136where bursts of concurrent threads use a pool at once. 137*/ 138EXTSPEC MALLOCATTR nedpool *nedcreatepool(size_t capacity,int threads) THROWSPEC; 139 140/* Destroys a memory pool previously created by nedcreatepool(). 141*/ 142EXTSPEC voidneddestroypool(nedpool *p) THROWSPEC; 143 144/* Sets a value to be associated with a pool. You can retrieve this value by passing 145any memory block allocated from that pool. 146*/ 147EXTSPEC voidnedpsetvalue(nedpool *p,void*v) THROWSPEC; 148/* Gets a previously set value using nedpsetvalue() or zero if memory is unknown. 149Optionally can also retrieve pool. 150*/ 151EXTSPEC void*nedgetvalue(nedpool **p,void*mem) THROWSPEC; 152 153/* Disables the thread cache for the calling thread, returning any existing cache 154data to the central pool. 155*/ 156EXTSPEC voidneddisablethreadcache(nedpool *p) THROWSPEC; 157 158EXTSPEC MALLOCATTR void*nedpmalloc(nedpool *p,size_t size) THROWSPEC; 159EXTSPEC MALLOCATTR void*nedpcalloc(nedpool *p,size_t no,size_t size) THROWSPEC; 160EXTSPEC MALLOCATTR void*nedprealloc(nedpool *p,void*mem,size_t size) THROWSPEC; 161EXTSPEC voidnedpfree(nedpool *p,void*mem) THROWSPEC; 162EXTSPEC MALLOCATTR void*nedpmemalign(nedpool *p,size_t alignment,size_t bytes) THROWSPEC; 163#if !NO_MALLINFO 164EXTSPEC struct mallinfo nedpmallinfo(nedpool *p) THROWSPEC; 165#endif 166EXTSPEC intnedpmallopt(nedpool *p,int parno,int value) THROWSPEC; 167EXTSPEC intnedpmalloc_trim(nedpool *p,size_t pad) THROWSPEC; 168EXTSPEC voidnedpmalloc_stats(nedpool *p) THROWSPEC; 169EXTSPEC size_tnedpmalloc_footprint(nedpool *p) THROWSPEC; 170EXTSPEC MALLOCATTR void**nedpindependent_calloc(nedpool *p,size_t elemsno,size_t elemsize,void**chunks) THROWSPEC; 171EXTSPEC MALLOCATTR void**nedpindependent_comalloc(nedpool *p,size_t elems,size_t*sizes,void**chunks) THROWSPEC; 172 173#if defined(__cplusplus) 174} 175#endif 176 177#undef MALLOCATTR 178#undef EXTSPEC 179 180#endif