1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: newdelete.cc,v 1.4 2008-10-28 08:46:23 Singular Exp $ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | #include <new> |
---|
6 | |
---|
7 | #include "assert.h" |
---|
8 | |
---|
9 | #ifdef USE_OLD_MEMMAN |
---|
10 | #include "memutil.h" |
---|
11 | #else |
---|
12 | #include "memman.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | // The C++ standard has ratified a change to the new operator. |
---|
16 | // |
---|
17 | // T *p = new T; |
---|
18 | // |
---|
19 | // Previously, if the call to new above failed, a null pointer would've been returned. |
---|
20 | // Under the ISO C++ Standard, an exception of type std::bad_alloc is thrown. |
---|
21 | // It is possible to suppress this behaviour in favour of the old style |
---|
22 | // by using the nothrow version. |
---|
23 | // |
---|
24 | // T *p = new (std::nothrow) T; |
---|
25 | // |
---|
26 | // So we have to overload this new also, just to be sure. |
---|
27 | // |
---|
28 | // A further interesting question is, if you don't have enough resources |
---|
29 | // to allocate a request for memory, |
---|
30 | // do you expect to have enough to be able to deal with it? |
---|
31 | // Most operating systems will have slowed to be unusable |
---|
32 | // long before the exception gets thrown. |
---|
33 | |
---|
34 | #ifdef USE_OLD_MEMMAN |
---|
35 | |
---|
36 | void * operator new ( size_t size ) |
---|
37 | { |
---|
38 | return getBlock( size ); |
---|
39 | } |
---|
40 | |
---|
41 | void operator delete ( void * block ) |
---|
42 | { |
---|
43 | freeBlock( block, 0 ); |
---|
44 | } |
---|
45 | |
---|
46 | void * operator new[] ( size_t size ) |
---|
47 | { |
---|
48 | return getBlock( size ); |
---|
49 | } |
---|
50 | |
---|
51 | void operator delete[] ( void * block ) |
---|
52 | { |
---|
53 | freeBlock( block, 0 ); |
---|
54 | } |
---|
55 | |
---|
56 | void * operator new(size_t size, std::nothrow_t) throw() |
---|
57 | { |
---|
58 | return getBlock( size ); |
---|
59 | } |
---|
60 | void * operator new[](size_t size, std::nothrow_t) throw() |
---|
61 | { |
---|
62 | return getBlock( size ); |
---|
63 | } |
---|
64 | |
---|
65 | #else |
---|
66 | |
---|
67 | void * operator new ( size_t size ) |
---|
68 | { |
---|
69 | return mmAlloc( size ); |
---|
70 | } |
---|
71 | |
---|
72 | void operator delete ( void * block ) |
---|
73 | { |
---|
74 | mmFree( block ); |
---|
75 | } |
---|
76 | |
---|
77 | void * operator new[] ( size_t size ) |
---|
78 | { |
---|
79 | return mmAlloc( size ); |
---|
80 | } |
---|
81 | |
---|
82 | void operator delete[] ( void * block ) |
---|
83 | { |
---|
84 | mmFree( block ); |
---|
85 | } |
---|
86 | |
---|
87 | void * operator new(size_t size, const std::nothrow_t&) throw() |
---|
88 | { |
---|
89 | return mmAlloc( size ); |
---|
90 | } |
---|
91 | void * operator new[](size_t size, const std::nothrow_t&) throw() |
---|
92 | { |
---|
93 | return mmAlloc( size ); |
---|
94 | } |
---|
95 | |
---|
96 | #endif /* USE_OLD_MEMMAN */ |
---|