QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_memory.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2007 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(OPENNURBS_MEMORY_INC_)
17#define OPENNURBS_MEMORY_INC_
18
19#if defined (cplusplus) || defined(_cplusplus) || defined(__cplusplus)
20extern "C" {
21#endif
22
25
28
29/*
31//
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.
39//
40// You may OPTIONALLY provide your own memory managment functions. See
41// opennurbs_memory.c for details.
42//
44//
45// Details:
46//
47// void* onmalloc( site_t sz );
48//
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.
53//
54// If you have implemented multiple memory pools in a custom manager,
55// the request is sent to the current pool.
56//
57//
58// void* oncalloc( size_t num, size_t sz );
59//
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.
64//
65// If you have implemented multiple memory pools in a custom manager,
66// the request is sent to the current pool.
67//
68//
69// void* onrealloc( void* p, size_t sz );
70//
71// If p is NULL, then onmalloc(sz) is called.
72// If sz is zero, then onfree(p) is called and
73// NULL is returned.
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.
79//
80// If you have implemented multiple memory pools in a custom manager,
81// the request is sent to the current pool.
82//
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.
86//
87//
88// void onfree( void* p );
89//
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.
93//
94//
95// size_t onmsize( void* p );
96//
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.
100//
101//
102// void* onmemdup( const void* src, size_t sz );
103//
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.
109//
110//
111// char* onstrdup( const char* src );
112//
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.
116//
117//
118// ON_register_memory_error_handler( ON_memory_error_handler my_handler );
119//
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.
123//
124// int ON_memory_error_handler( int error_number );
125//
126// This function is called when an error occurs. The values of error_number
127// are:
128//
129// 0: There is not enough memory to satisfy an onmalloc() or onrealloc()
130// request.
131//
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.
135//
136// 1: An invalid pointer was passed to onrealloc().
137//
138// If ON_memory_error_handler() returns 0, then onrealloc() returns NULL.
139// If ON_memory_error_handler() returns 1, the reallocation is attempted
140// again;
141//
142// 2: An invalid pointer was passed to onfree().
143//
144// If ON_memory_error_handler() returns 0, then free() returns.
145// If ON_memory_error_handler() returns 1, the deallocation is
146// attempted again;
147//
148// 3: An query to onmsize() returned 0 or 0xFFFFFFFF.
149//
150// If ON_memory_error_handler() returns 0, then onmsize() returns 0.
151// If ON_memory_error_handler() returns 1, the query is
152// attempted again;
153//
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().
157//
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 );
161*/
162
163/* ^^^ see comments above for details ^^^ */
164
165/*
167//
168// Memory error handler - the default handler does nothing.
169//
170//
171// See opennurbs_memory.c for instructions on using
172// custom memory managers.
173//
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.
178//
179// The input to ON_memory_error_handler(int) is:
180//
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
185//
186// If ON_memory_error_handler(int) returns
187//
188// 0: The malloc()/calloc()/realloc()/msize() request returns 0.
189// 1: The call to malloc()/calloc()/realloc()/msize() is repeated.
190//
191*/
192/* error handling */
193typedef int (*ON_memory_error_handler)(int);
196
197/*
199//
200// Memory pool managment - by default these do nothing
201// See opennurbs_memory.c for instructions on using
202// custom memory managers
203*/
204
207{
208 char m__s[16]; // used to store the string "ON_MEMORY_POOL" so
209 // we know what this chunk of memory contains
210 // when debugging complex multi-pool memory leaks.
211 /*
212 // The default memory managment doesn't use pools.
213 // If you provide custom memory managment, you can use these fields.
214 //
215 // The comments below indicate how Rhino uses these fields.
216 */
217
218 /*
219 // If nonzero, points to a heap. If zero, pool uses CRT
220 // malloc/calloc/realloc/free.
221 */
222 void* m_heap;
223
225
226 /*
227 // If nonzero, this pool is associated with an active
228 // worker thead and m_thread_id is the thread's id.
229 */
230 unsigned long m_thread_id;
231
232 /*
233 // If nonzero, this pool is temporarily disabled and the
234 // main pool should be used for allocation requests that
235 // are sent to this pool.
236 // onmalloc(), oncalloc(), and onrealloc(0,sz).
237 */
238 unsigned long m_disabled;
239
240 /*
241 // Pool ID.
242 // = 0: invalid value.
243 // = -1: invalid value.
244 // = 1: main application pool
245 // > 1: pool is or was a worker thread pool.
246 // If m_thread_id is nonzero, then the worker thread
247 // is still active. If m_thread_id is zero, the
248 // worker thread successfully completed its task
249 // and created objects that use memory from this pool.
250 // < -1: pool was a worker thread pool and the pool is no longer
251 // in use. If This can happen because the worker thread was
252 // canceled or the worker thread finished and didn't leave
253 // any memory allocated in the pool. The value is the
254 // negative of the original pool id.
255 */
257
258 /*
259 // Worker thread flag.
260 // If m_h is nonzero, m_i > 1, and m_j is nonzero, then
261 // then a worker thread is running and will be canceled.
262 */
263 int m_j;
264
265 /*
266 // If pool is a worker thread pool, this value is passed
267 // to ExitThread().
268 */
269 int m_k;
270
271 /*
272 // Reserved for future use.
273 */
274 int m_n;
275};
276
279
282
285
288
291
294
296void* onmalloc( size_t );
297
300
302void* oncalloc( size_t, size_t );
303
305void* oncalloc_from_pool( ON_MEMORY_POOL*, size_t, size_t );
306
308void onfree( void* );
309
311void* onrealloc( void*, size_t );
312
314void* onrealloc_from_pool( ON_MEMORY_POOL*, void*, size_t );
315
317size_t onmsize( const void* );
318
320void* onmemdup( const void*, size_t );
321
323char* onstrdup( const char* );
324
325#if defined(_WCHAR_T_DEFINED)
327wchar_t* onwcsdup( const wchar_t* );
328#endif
329
331unsigned char* onmbsdup( const unsigned char* );
332
335 size_t* malloc_count,
336 size_t* realloc_count,
337 size_t* free_count,
338 size_t* pool_count
339 );
340
341
342/* define to handle _TCHAR* ontcsdup( const _TCHAR* ) */
343#if defined(_UNICODE)
344#define ontcsdup onwcsdup
345#elif defined(_MBCS)
346#define ontcsdup onmbsdup
347#else
348#define ontcsdup onstrdup
349#endif
350
351#if defined (cplusplus) || defined(_cplusplus) || defined(__cplusplus)
352}
353#endif
354
355#endif
#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