Symphony Of Empires
heap_ext.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // heap_ext.cpp
20 //
21 // Abstract:
22 // Overrides default new/delete operators.
23 // ----------------------------------------------------------------------------
24 
25 #ifdef E3D_MANAGED_HEAP
26 #include <cstdlib>
27 #include <new>
28 
29 #include "eng3d/heap_ext.hpp"
30 #include "eng3d/utils.hpp"
31 
32 void* operator new(std::size_t size) {
33 #ifdef E3D_DEBUG
34  std::printf("Alloc size = %zu\n", size);
35 #endif
36  if(size == 0) size++;
37  void* p = std::malloc(size);
38  if(p) return p;
39  CXX_THROW(std::bad_alloc);
40 }
41 
42 void operator delete(void* ptr) noexcept {
43  std::free(ptr);
44 }
45 
46 void* operator new[](std::size_t size) {
47 #ifdef E3D_DEBUG
48  std::printf("Alloc[] size = %zu\n", size);
49 #endif
50  if(size == 0) size++;
51  void* p = std::malloc(size);
52  if(p) return p;
53  CXX_THROW(std::bad_alloc);
54 }
55 
56 void operator delete[](void* ptr) noexcept {
57  std::free(ptr);
58 }
59 #endif
#define CXX_THROW(class,...)
Definition: utils.hpp:98