source: git/omalloc/omallocClass.h

spielwiese
Last change on this file was 5ef600, checked in by Hans Schoenemann <hannes@…>, 2 years ago
fix: omallocClass only for omammloc, not for xalloc
  • Property mode set to 100644
File size: 1.9 KB
Line 
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/omConfig.h"
16#ifdef HAVE_OMALLOC
17#include "omalloc/omalloc.h"
18
19class omallocClass
20{
21public:
22/* We define those, so that our values of
23   OM_TRACK and OM_CHECK are used  */
24void* operator new ( size_t size )
25#ifndef __GNUC__
26throw (std::bad_alloc)
27#endif
28{
29  void* addr;
30  omTypeAlloc(void*, addr, size);
31  return addr;
32}
33
34void operator delete ( void* block )
35#ifndef __GNUC__
36throw ()
37#endif
38{
39  omFree( block );
40}
41
42
43void* operator new[] ( size_t size )
44#ifndef __GNUC__
45throw (std::bad_alloc)
46#endif
47{
48  void* addr;
49  if (size==(size_t)0) size = (size_t)1;
50  omTypeAlloc(void*, addr, size);
51  return addr;
52}
53
54void operator delete[] ( void* block )
55#ifndef __GNUC__
56throw ()
57#endif
58{
59  omfree( block );
60}
61
62
63// The C++ standard has ratified a change to the new operator.
64//
65//  T *p = new T;
66//
67// Previously, if the call to new above failed, a null pointer would've been returned.
68// Under the ISO C++ Standard, an exception of type std::bad_alloc is thrown.
69// It is possible to suppress this behaviour in favour of the old style
70// by using the nothrow version.
71//
72//  T *p = new (std::nothrow) T;
73//
74// So we have to overload this new also, just to be sure.
75//
76// A further interesting question is, if you don't have enough resources
77// to allocate a request for memory,
78// do you expect to have enough to be able to deal with it?
79// Most operating systems will have slowed to be unusable
80// long before the exception gets thrown.
81
82void * operator new(size_t size, const std::nothrow_t &) throw();
83
84void * operator new[](size_t size, const std::nothrow_t &) throw();
85};
86#endif
87#endif
88#endif
Note: See TracBrowser for help on using the repository browser.