source: git/kernel/mmalloc.cc @ 6a9f2e

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