source: git/omalloc/omallocClass.h @ bccae1

fieker-DuValspielwiese
Last change on this file since bccae1 was 3e056d, checked in by Hans Schoenemann <hannes@…>, 8 years ago
xalloc: how to use and fixes
  • Property mode set to 100644
File size: 1.8 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/omalloc.h>
16
17class omallocClass
18{
19public:
20/* We define those, so that our values of
21   OM_TRACK and OM_CHECK are used  */
22void* operator new ( size_t size )
23#ifndef __GNUC__
24throw (std::bad_alloc)
25#endif
26{
27  void* addr;
28  omTypeAlloc(void*, addr, size);
29  return addr;
30}
31
32void operator delete ( void* block )
33#ifndef __GNUC__
34throw ()
35#endif
36{
37  omFree( block );
38}
39
40
41void* operator new[] ( size_t size )
42#ifndef __GNUC__
43throw (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
52void operator delete[] ( void* block )
53#ifndef __GNUC__
54throw ()
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
80void * operator new(size_t size, const std::nothrow_t &) throw();
81
82void * operator new[](size_t size, const std::nothrow_t &) throw();
83};
84#endif
85#endif
Note: See TracBrowser for help on using the repository browser.