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