51 #define MM_DEBUG if(0) 60 cerr <<
"MM_register(): Only 1 instance of MM_register should exist.\n";
76 cerr <<
"MM_register(): Only 1 instance of MM_register should exist.\n";
88 size_t availMB = (remaining >> 20);
90 cout <<
"available memory: " << availMB <<
"MB " 91 <<
"(" << remaining <<
"B)" 94 cout <<
"available memory: " << remaining <<
"B, exceeding: " 95 << used - user_limit <<
"B" 106 if (used > new_limit) {
108 switch (register_new) {
110 cerr <<
" MM_register::set_memory_limit to " << new_limit
111 <<
", used " << used <<
". allocation exceeds new limit.\n";
118 cerr <<
" MM_register::set_memory_limit to " << new_limit
119 <<
", used " << used <<
". allocation exceeds new limit.\n";
125 user_limit = new_limit;
130 assert(used <= new_limit);
132 if (new_limit < user_limit) {
133 remaining -= user_limit - new_limit;
135 remaining += new_limit - user_limit;
137 user_limit = new_limit;
155 if (used > user_limit) {
156 cerr <<
" MM_register::enforce_memory_limit: limit=" << user_limit
157 <<
", used=" << used <<
". allocation exceeds limit.\n";
180 cout <<
"Memory manager registering memory in ";
181 switch (register_new) {
183 cout <<
"MM_ABORT_ON_MEMORY_EXCEEDED";
186 cout <<
"MM_WARN_ON_MEMORY_EXCEEDED";
189 cout <<
"MM_IGNORE_MEMORY_EXCEEDED";
192 cout <<
" mode." << endl;
224 static const size_t SIZE_SPACE=(
sizeof(size_t) > 8 ?
sizeof(
size_t) : 8);
240 if (request > remaining) {
247 remaining -= request;
262 remaining = user_limit;
267 if (used < user_limit) {
268 remaining = user_limit - used;
284 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER 285 void* MM_register::operator
new[] (
size_t sz)
throw (std::bad_alloc) {
287 void* MM_register::operator
new[] (
size_t sz) {
291 MM_DEBUG cout <<
"new: sz=" << sz <<
", register " 292 << sz+SIZE_SPACE <<
"B ,";
300 <<
"allocating " << sz <<
"B. " 301 <<
"limit exceeded by " 310 <<
"allocating " << sz <<
"B. " 311 <<
" limit exceeded by " 321 p =
malloc(sz + SIZE_SPACE);
324 cerr <<
"new: out of memory while allocating " << sz <<
"B" << endl;
329 *((
size_t *) p) = sz;
331 MM_DEBUG cout <<
"ptr=" << (
void*) (((
char *) p) + SIZE_SPACE) << endl;
333 return ((
char *) p) + SIZE_SPACE;
339 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER 340 void* MM_register::operator
new (
size_t sz)
throw (std::bad_alloc) {
342 void* MM_register::operator
new (
size_t sz) {
346 MM_DEBUG cout <<
"new: sz=" << sz <<
", register " 347 << sz+SIZE_SPACE <<
"B ,";
355 <<
"allocating " << sz <<
"B. " 356 <<
"limit exceeded by " 365 <<
"allocating " << sz <<
"B. " 366 <<
" limit exceeded by " 376 p =
malloc(sz + SIZE_SPACE);
379 cerr <<
"new: out of memory while allocating " << sz <<
"B" << endl;
384 *((
size_t *) p) = sz;
386 MM_DEBUG cout <<
"ptr=" << (
void*) (((
char *) p) + SIZE_SPACE) << endl;
388 return ((
char *) p) + SIZE_SPACE;
395 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER 396 void MM_register::operator
delete (
void *ptr)
throw() {
398 void MM_register::operator
delete (
void *ptr) noexcept {
403 MM_DEBUG cout <<
"delete: ptr=" << ptr <<
",";
406 cerr <<
"MM warning: operator delete was given a NULL pointer\n";
422 p = ((
char *)ptr) - SIZE_SPACE;
425 MM_DEBUG cout <<
"size=" << sz <<
", free " << p <<
"B and deallocate " 426 << sz + SIZE_SPACE << endl;
430 cerr <<
"delete: MM_manager.register_deallocation failed\n";
442 #ifdef GRASS_MM_USE_EXCEPTION_SPECIFIER 443 void MM_register::operator
delete[] (
void *ptr)
throw() {
445 void MM_register::operator
delete[] (
void *ptr) noexcept {
450 MM_DEBUG cout <<
"delete[]: ptr=" << ptr <<
",";
454 cerr <<
"MM warning: operator delete [] was given a NULL pointer\n";
464 p = ((
char *)ptr) - SIZE_SPACE;
467 MM_DEBUG cout <<
"size=" << sz <<
", free " << p <<
"B and deallocate " 468 << sz + SIZE_SPACE << endl;
472 cerr <<
"delete[]: MM_manager.register_deallocation failed\n";
488 int MM_register::instances = 0;
501 unsigned int mm_register_init::count;
#define MM_DEFAULT_MM_SIZE
MM_err register_deallocation(size_t sz)
MM_err register_allocation(size_t sz)
#define assert(condition)
MM_err set_memory_limit(size_t sz)
void enforce_memory_limit()
void ignore_memory_limit()
size_t memory_available()