49 #define MM_DEBUG if (0)
57 cerr <<
"MM_register(): Only 1 instance of MM_register should exist.\n";
72 cerr <<
"MM_register(): Only 1 instance of MM_register should exist.\n";
84 size_t availMB = (remaining >> 20);
86 cout <<
"available memory: " << availMB <<
"MB "
87 <<
"(" << remaining <<
"B)" << endl;
90 cout <<
"available memory: " << remaining
91 <<
"B, exceeding: " << used - user_limit <<
"B" << endl;
101 if (used > new_limit) {
103 switch (register_new) {
105 cerr <<
" MM_register::set_memory_limit to " << new_limit
106 <<
", used " << used <<
". allocation exceeds new limit.\n";
113 cerr <<
" MM_register::set_memory_limit to " << new_limit
114 <<
", used " << used <<
". allocation exceeds new limit.\n";
120 user_limit = new_limit;
125 assert(used <= new_limit);
127 if (new_limit < user_limit) {
128 remaining -= user_limit - new_limit;
131 remaining += new_limit - user_limit;
133 user_limit = new_limit;
150 if (used > user_limit) {
151 cerr <<
" MM_register::enforce_memory_limit: limit=" << user_limit
152 <<
", used=" << used <<
". allocation exceeds limit.\n";
176 cout <<
"Memory manager registering memory in ";
177 switch (register_new) {
179 cout <<
"MM_ABORT_ON_MEMORY_EXCEEDED";
182 cout <<
"MM_WARN_ON_MEMORY_EXCEEDED";
185 cout <<
"MM_IGNORE_MEMORY_EXCEEDED";
188 cout <<
" mode." << endl;
218 static const size_t SIZE_SPACE = (
sizeof(size_t) > 8 ?
sizeof(
size_t) : 8);
231 if (request > remaining) {
238 remaining -= request;
252 remaining = user_limit;
258 if (used < user_limit) {
259 remaining = user_limit - used;
274 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
275 void *MM_register::operator
new[](
size_t sz)
throw(std::bad_alloc)
278 void *MM_register::operator
new[](
size_t sz)
283 MM_DEBUG cout <<
"new: sz=" << sz <<
", register " << sz + SIZE_SPACE
292 <<
"allocating " << sz <<
"B. "
293 <<
"limit exceeded by "
302 <<
"allocating " << sz <<
"B. "
303 <<
" limit exceeded by "
313 p =
malloc(sz + SIZE_SPACE);
316 cerr <<
"new: out of memory while allocating " << sz <<
"B" << endl;
323 MM_DEBUG cout <<
"ptr=" << (
void *)(((
char *)p) + SIZE_SPACE) << endl;
325 return ((
char *)p) + SIZE_SPACE;
329 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
330 void *MM_register::operator
new(
size_t sz)
throw(std::bad_alloc)
333 void *MM_register::operator
new(
size_t sz)
338 MM_DEBUG cout <<
"new: sz=" << sz <<
", register " << sz + SIZE_SPACE
347 <<
"allocating " << sz <<
"B. "
348 <<
"limit exceeded by "
357 <<
"allocating " << sz <<
"B. "
358 <<
" limit exceeded by "
368 p =
malloc(sz + SIZE_SPACE);
371 cerr <<
"new: out of memory while allocating " << sz <<
"B" << endl;
378 MM_DEBUG cout <<
"ptr=" << (
void *)(((
char *)p) + SIZE_SPACE) << endl;
380 return ((
char *)p) + SIZE_SPACE;
384 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
385 void MM_register::operator
delete(
void *ptr)
throw()
388 void MM_register::operator
delete(
void *ptr) noexcept
394 MM_DEBUG cout <<
"delete: ptr=" << ptr <<
",";
397 cerr <<
"MM warning: operator delete was given a NULL pointer\n";
413 p = ((
char *)ptr) - SIZE_SPACE;
416 MM_DEBUG cout <<
"size=" << sz <<
", free " << p <<
"B and deallocate "
417 << sz + SIZE_SPACE << endl;
422 cerr <<
"delete: MM_manager.register_deallocation failed\n";
431 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER
432 void MM_register::operator
delete[](
void *ptr)
throw()
435 void MM_register::operator
delete[](
void *ptr) noexcept
441 MM_DEBUG cout <<
"delete[]: ptr=" << ptr <<
",";
445 cerr <<
"MM warning: operator delete [] was given a NULL pointer\n";
455 p = ((
char *)ptr) - SIZE_SPACE;
458 MM_DEBUG cout <<
"size=" << sz <<
", free " << p <<
"B and deallocate "
459 << sz + SIZE_SPACE << endl;
464 cerr <<
"delete[]: MM_manager.register_deallocation failed\n";
476 int MM_register::instances = 0;
484 unsigned int mm_register_init::count;
MM_err register_allocation(size_t sz)
void enforce_memory_limit()
void ignore_memory_limit()
size_t memory_available()
MM_err register_deallocation(size_t sz)
MM_err set_memory_limit(size_t sz)
#define assert(condition)
@ MM_ERROR_INSUFFICIENT_SPACE
#define MM_DEFAULT_MM_SIZE
@ MM_ABORT_ON_MEMORY_EXCEEDED
@ MM_WARN_ON_MEMORY_EXCEEDED
@ MM_IGNORE_MEMORY_EXCEEDED