16#if !defined(OPENNURBS_MEMORY_INC_)
17#define OPENNURBS_MEMORY_INC_
19#if defined (cplusplus) || defined(_cplusplus) || defined(__cplusplus)
32// ALL memory managment in the openNURBS toolkit is done through calls to
33// onmalloc(), oncalloc(), onrealloc(), onfree(),
34// onmsize(), onmemdup(), onstrdup(), ..., and the
35// new and delete operators
36// The on*() functions are all declared in opennurbs_memory.h and defined
37// in opennurbs_memory.c. The new and delete operators that use onmalloc()
38// and onfree() are defined in opennurbs_memory_new.cpp.
40// You may OPTIONALLY provide your own memory managment functions. See
41// opennurbs_memory.c for details.
47// void* onmalloc( site_t sz );
49// If sz is zero, NULL is returned.
50// If sz is positive and there is not enough memory to satify
51// the allocation request, the ON_memory_error_handler(0) is called
52// and NULL is returned.
54// If you have implemented multiple memory pools in a custom manager,
55// the request is sent to the current pool.
58// void* oncalloc( size_t num, size_t sz );
60// If sz or num is zero, NULL is returned.
61// If sz and num are positive and there is not enough memory to satify
62// the allocation request, the ON_memory_error_handler(0) is called
63// and NULL is returned.
65// If you have implemented multiple memory pools in a custom manager,
66// the request is sent to the current pool.
69// void* onrealloc( void* p, size_t sz );
71// If p is NULL, then onmalloc(sz) is called.
72// If sz is zero, then onfree(p) is called and
74// If p is not NULL, sz is positive, and there is not
75// enough memory to to satify the allocation request,
76// the ON_memory_error_handler(0) is called and NULL is returned.
77// If p is not NULL and is known to be invalid, then
78// ON_memory_error_handler(1) is called.
80// If you have implemented multiple memory pools in a custom manager,
81// the request is sent to the current pool.
83// NOTE WELL: Microsoft's VC 6.0 realloc() contains a bug that can cause
84// crashes and should be avoided. See MSDN Knowledge Base
85// article ID Q225099 for more information.
88// void onfree( void* p );
90// NULL p is tolerated but considered poor style.
91// If p is not NULL and is known to be invalid, then
92// ON_memory_error_handler(2) is called.
95// size_t onmsize( void* p );
97// If p is NULL, then zero is returned. Otherwise the
98// the size in bytes of the memory block allocated by onmalloc(),
99// oncalloc(), or onrealloc() is returned.
102// void* onmemdup( const void* src, size_t sz );
104// If src is not NULL and sz is positive, then onmalloc( sz )
105// is called to get memory, sz bytes of src are copied into this
106// memory, and the pointer to this memory is returned.
107// If onmalloc() returns NULL, then NULL is returned.
108// If src is NULL or sz is zero, then NULL is returned.
111// char* onstrdup( const char* src );
113// If src is not NULL, then onmemdup( sc, (strlen(src)+1)*sizeof(src[0]) )
114// is called. If onmemdup() returns NULL, then NULL is returned.
115// If src is NULL, then NULL is returned.
118// ON_register_memory_error_handler( ON_memory_error_handler my_handler );
120// Use this function to specify the error handler function that will be called
121// if an error occures. See the description of ON_memory_error_handler() for the
122// error handler function specification.
124// int ON_memory_error_handler( int error_number );
126// This function is called when an error occurs. The values of error_number
129// 0: There is not enough memory to satisfy an onmalloc() or onrealloc()
132// If ON_memory_error_handler() returns 0, then onmalloc() or onrealloc()
133// returns NULL. If ON_memory_error_handler() returns 1, the allocation
134// is attempted again.
136// 1: An invalid pointer was passed to onrealloc().
138// If ON_memory_error_handler() returns 0, then onrealloc() returns NULL.
139// If ON_memory_error_handler() returns 1, the reallocation is attempted
142// 2: An invalid pointer was passed to onfree().
144// If ON_memory_error_handler() returns 0, then free() returns.
145// If ON_memory_error_handler() returns 1, the deallocation is
148// 3: An query to onmsize() returned 0 or 0xFFFFFFFF.
150// If ON_memory_error_handler() returns 0, then onmsize() returns 0.
151// If ON_memory_error_handler() returns 1, the query is
154// These functions allow you to direct a memory request to a specific pool. If
155// you have not implemented custom memory pool management, these behave exactly
156// like onmalloc(), oncalloc(), and onfree().
158// void* onmalloc_from_pool( ON_MEMORY_POOL*, site_t sz );
159// void* oncalloc_from_pool( ON_MEMORY_POOL*, size_t num, size_t sz );
160// void* onrealloc_from_pool( ON_MEMORY_POOL*, void* p, size_t sz );
168// Memory error handler - the default handler does nothing.
171// See opennurbs_memory.c for instructions on using
172// custom memory managers.
174// ON_memory_error_register_handler() returns a pointer to
175// the handler you are replacing. If you pass a NULL,
176// the default memory error handler is used. The default
177// memory error handler returns 0 for all errors.
179// The input to ON_memory_error_handler(int) is:
181// 0: malloc/calloc/realloc request returned NULL
182// 1: invalid pointer passed to realloc()
183// 2: invalid pointer passed to free()
184// 3: msize query returned zero or 0xFFFFFFFF
186// If ON_memory_error_handler(int) returns
188// 0: The malloc()/calloc()/realloc()/msize() request returns 0.
189// 1: The call to malloc()/calloc()/realloc()/msize() is repeated.
200// Memory pool managment - by default these do nothing
201// See opennurbs_memory.c for instructions on using
202// custom memory managers
325#if defined(_WCHAR_T_DEFINED)
327wchar_t* onwcsdup(
const wchar_t* );
335 size_t* malloc_count,
336 size_t* realloc_count,
344#define ontcsdup onwcsdup
346#define ontcsdup onmbsdup
348#define ontcsdup onstrdup
351#if defined (cplusplus) || defined(_cplusplus) || defined(__cplusplus)
#define ON_DECL
Definition opennurbs_defines.h:92
ON_DECL char * onstrdup(const char *)
ON_DECL void onfree(void *)
ON_DECL void * onrealloc(void *, size_t)
ON_DECL ON_MEMORY_POOL * ON_MainMemoryPool(void)
ON_DECL void ON_SetWorkerMemoryPool(ON_MEMORY_POOL *)
ON_DECL ON_MEMORY_POOL * ON_WorkerMemoryPool(void)
ON_DECL unsigned char * onmbsdup(const unsigned char *)
ON_DECL void ON_MemoryManagerEnd(void)
ON_DECL void * oncalloc_from_pool(ON_MEMORY_POOL *, size_t, size_t)
ON_DECL ON_MEMORY_POOL * ON_CreateMemoryPool(void)
ON_DECL void * oncalloc(size_t, size_t)
ON_DECL void * onrealloc_from_pool(ON_MEMORY_POOL *, void *, size_t)
ON_DECL size_t onmemoryusecount(size_t *malloc_count, size_t *realloc_count, size_t *free_count, size_t *pool_count)
ON_DECL void ON_DestroyMemoryPool(ON_MEMORY_POOL *)
ON_DECL void * onmalloc(size_t)
ON_DECL size_t onmsize(const void *)
ON_DECL void * onmalloc_from_pool(ON_MEMORY_POOL *, size_t)
int(* ON_memory_error_handler)(int)
Definition opennurbs_memory.h:193
ON_DECL void ON_MemoryManagerBegin(void)
ON_DECL ON_memory_error_handler ON_memory_error_register_handler(ON_memory_error_handler)
ON_DECL void ON_CompactMemoryPool(ON_MEMORY_POOL *)
ON_DECL void * onmemdup(const void *, size_t)
Definition opennurbs_memory.h:207
unsigned long m_disabled
Definition opennurbs_memory.h:238
int m_j
Definition opennurbs_memory.h:263
int m_pool_id
Definition opennurbs_memory.h:256
int m_k
Definition opennurbs_memory.h:269
void(* m_EndThreadFunction)(ON_MEMORY_POOL *pThisPool)
Definition opennurbs_memory.h:224
char m__s[16]
Definition opennurbs_memory.h:208
int m_n
Definition opennurbs_memory.h:274
unsigned long m_thread_id
Definition opennurbs_memory.h:230
void * m_heap
Definition opennurbs_memory.h:222