-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.cc
63 lines (53 loc) · 1.31 KB
/
new.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdlib>
#include <iostream>
#include <new>
#include <cmath>
// #include <mem_test_user>
using std::cerr;
using std::endl;
using std::set_new_handler;
static size_t dynamic_memory=0;
void my_new_handler()
{
cerr << endl << "Memory limits exceeded" << endl;
exit(1);
}
void (*old_new_handler)()=set_new_handler(&my_new_handler);
#ifdef __DECCXX_LIBCXX_RH70
void *operator new(size_t size, const std::nothrow_t&) _RWSTD_THROW_SPEC_NULL
#else
#define _RWSTD_THROW_SPEC_NULL
void *operator new(size_t size)
#endif
{
void *mem=malloc(size);
if(size && !mem) my_new_handler();
dynamic_memory += size;
return mem;
}
void *operator new(size_t size, int len)
{
void *mem=calloc(len,size);
if(len && !mem) (my_new_handler)();
dynamic_memory += len*size;
return mem;
}
void operator delete(void *ptr) throw ()//_RWSTD_THROW_SPEC_NULL
{
free(ptr);
}
// provide a C++ interface to vector-resize via realloc
// Warning: this does not initialize virtual member functions.
void *operator new(size_t size, void *ptr, int new_len)
{
size_t new_size=new_len*size;
dynamic_memory += new_size;
void *mem=realloc(ptr, new_size);
if(new_size && !mem) (my_new_handler)();
return mem;
}
// Total amount of dynamically allocated (including subsequently freed) memory.
size_t memory()
{
return dynamic_memory;
}