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