1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: standard version of C++-memory management alloc func |
---|
7 | */ |
---|
8 | #include <stdlib.h> |
---|
9 | |
---|
10 | #include <kernel/mod2.h> |
---|
11 | |
---|
12 | #include <omalloc/omalloc.h> |
---|
13 | #include <new> |
---|
14 | |
---|
15 | /* We define those, so that our values of |
---|
16 | OM_TRACK and OM_CHECK are used */ |
---|
17 | void* operator new ( size_t size ) |
---|
18 | #ifndef __GNUC__ |
---|
19 | throw (std::bad_alloc) |
---|
20 | #endif |
---|
21 | { |
---|
22 | void* addr; |
---|
23 | if (size==(size_t)0) size = 1; |
---|
24 | omTypeAlloc(void*, addr, size); |
---|
25 | return addr; |
---|
26 | } |
---|
27 | |
---|
28 | void operator delete ( void* block ) |
---|
29 | #ifndef __GNUC__ |
---|
30 | throw () |
---|
31 | #endif |
---|
32 | { |
---|
33 | omfree( block ); |
---|
34 | } |
---|
35 | |
---|
36 | void* operator new[] ( size_t size ) |
---|
37 | #ifndef __GNUC__ |
---|
38 | throw (std::bad_alloc) |
---|
39 | #endif |
---|
40 | { |
---|
41 | void* addr; |
---|
42 | if (size==(size_t)0) size = (size_t)1; |
---|
43 | omTypeAlloc(void*, addr, size); |
---|
44 | return addr; |
---|
45 | } |
---|
46 | |
---|
47 | void operator delete[] ( void* block ) |
---|
48 | #ifndef __GNUC__ |
---|
49 | throw () |
---|
50 | #endif |
---|
51 | { |
---|
52 | omfree( block ); |
---|
53 | } |
---|
54 | |
---|
55 | // The C++ standard has ratified a change to the new operator. |
---|
56 | // |
---|
57 | // T *p = new T; |
---|
58 | // |
---|
59 | // Previously, if the call to new above failed, a null pointer would've been returned. |
---|
60 | // Under the ISO C++ Standard, an exception of type std::bad_alloc is thrown. |
---|
61 | // It is possible to suppress this behaviour in favour of the old style |
---|
62 | // by using the nothrow version. |
---|
63 | // |
---|
64 | // T *p = new (std::nothrow) T; |
---|
65 | // |
---|
66 | // So we have to overload this new also, just to be sure. |
---|
67 | // |
---|
68 | // A further interesting question is, if you don't have enough resources |
---|
69 | // to allocate a request for memory, |
---|
70 | // do you expect to have enough to be able to deal with it? |
---|
71 | // Most operating systems will have slowed to be unusable |
---|
72 | // long before the exception gets thrown. |
---|
73 | |
---|
74 | void * operator new(size_t size, const std::nothrow_t &) throw() |
---|
75 | { |
---|
76 | void* addr; |
---|
77 | if (size==(size_t)0) size = (size_t)1; |
---|
78 | omTypeAlloc(void*, addr, size); |
---|
79 | return addr; |
---|
80 | } |
---|
81 | |
---|
82 | void * operator new[](size_t size, const std::nothrow_t &) throw() |
---|
83 | { |
---|
84 | void* addr; |
---|
85 | if (size==(size_t)0) size = (size_t)1; |
---|
86 | omTypeAlloc(void*, addr, size); |
---|
87 | return addr; |
---|
88 | } |
---|