CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
OdAllocOp.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, Open Design Alliance (the "Alliance").
3// All rights reserved.
4//
5// This software and its documentation and related materials are owned by
6// the Alliance. The software may only be incorporated into application
7// programs owned by members of the Alliance, subject to a signed
8// Membership Agreement and Supplemental Software License Agreement with the
9// Alliance. The structure and organization of this software are the valuable
10// trade secrets of the Alliance and its suppliers. The software is also
11// protected by copyright law and international treaty provisions. Application
12// programs incorporating this software must include the following statement
13// with their copyright notices:
14//
15// This application incorporates Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 by Open Design Alliance.
18// All rights reserved.
19//
20// By use of this software, its documentation or related materials, you
21// acknowledge and accept the above terms.
23#ifndef __ODAALLOCOP_H__
24#define __ODAALLOCOP_H__
25#include <stdlib.h>
26#include "OdAllocExport.h"
27#include "OdAlloc.h"
28#include <new>
29
30// Duplicate symbol link errors result using PocketPC if we define our own new & delete
31#if !defined(_WIN32_WCE)
32
33#ifdef _MSC_VER
34#pragma warning(disable: 4290) // C++ exception spec. ignored
35#endif
36
37#if (defined(__GNUC__) && __GNUC__ >= 5 && __cpp_sized_deallocation >= 201309) || (defined(_MSC_VER) && _MSC_VER >= 1800) //VS2015 and newer or GCC 5 and newer (support sized deallocation)
38#define ODRX_SIZED_DEALLOC_OPERATORS() \
39 void operator delete(void* p, std::size_t) \
40 { \
41 if (!p) return; \
42 ::odrxFree(p); \
43 } \
44 void operator delete[](void* p, std::size_t) \
45 { \
46 if (!p) return; \
47 ::odrxFree(p); \
48 }\
49 void operator delete(void* ptr, std::size_t s, const std::nothrow_t&) \
50 { \
51 operator delete(ptr, s); \
52 } \
53 void operator delete[](void* ptr, std::size_t s, const std::nothrow_t&) \
54 { \
55 operator delete[](ptr, s); \
56 } \
57
58#else
59 #define ODRX_SIZED_DEALLOC_OPERATORS()
60#endif
61
62#define ODRX_NOTHROW_ALLOC_OPERATORS() \
63void *operator new (size_t size, const std::nothrow_t&) throw() { return operator new(size); } \
64void * operator new[] (::size_t count, const std::nothrow_t&) throw() { return operator new[](count); }\
65void operator delete[](void *ptr, const std::nothrow_t&) throw() { operator delete[](ptr); } \
66void operator delete (void *ptr, const std::nothrow_t&) throw() { operator delete(ptr); } \
67
68#ifdef __GNUC__
69 #define ODRX_ALLOC_OPERATORS() \
70 void* operator new (size_t size) \
71 { \
72 void* p = ::odrxAlloc(size); \
73 if (!p && size) NEW_HANDLER() \
74 return p; \
75 } \
76 void operator delete (void *p) throw() \
77 { \
78 if (!p) return; \
79 ::odrxFree(p); \
80 } \
81 void* operator new[](size_t size) \
82 { \
83 void* p = ::odrxAlloc(size); \
84 if (!p && size) NEW_HANDLER() \
85 return p; \
86 } \
87 void operator delete[](void *p) throw() \
88 { \
89 if (!p) return; \
90 ::odrxFree(p); \
91 } \
92 ODRX_NOTHROW_ALLOC_OPERATORS() \
93 ODRX_SIZED_DEALLOC_OPERATORS() \
94
95#else //__GNUC__
96 #define ODRX_ALLOC_OPERATORS() \
97 void* operator new (size_t size) \
98 { \
99 void* p = ::odrxAlloc(size); \
100 /*Under GCC std::allocator allocates zero sizes via operator new (by default)*/ \
101 if (!p && size) NEW_HANDLER() \
102 return p; \
103 } \
104 void operator delete (void *p) throw() \
105 { \
106 if (!p) return; \
107 ::odrxFree(p); \
108 } \
109 void* operator new[](size_t size) \
110 { \
111 void* p = ::odrxAlloc(size); \
112 if (!p && size) NEW_HANDLER() \
113 return p; \
114 } \
115 void operator delete[](void *p) throw() \
116 { \
117 if (!p) return; \
118 ::odrxFree(p); \
119 } \
120 ODRX_NOTHROW_ALLOC_OPERATORS() \
121 ODRX_SIZED_DEALLOC_OPERATORS() \
122
123#endif //__GNUC__
124#else
125
126#define ODRX_ALLOC_OPERATORS()
127#define ODRX_CRTDBG_MAP_ALLOC_OPERATORS()
128
129#endif
130
132// Support _CRTDBG_MAP_ALLOC memory allocation
133#if (_MSC_VER >= 1200) && defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
134
135#define ODRX_CRTDBG_MAP_ALLOC_OPERATORS() \
136extern "C" { extern _CRTIMP void * __cdecl _malloc_dbg(size_t, int, const char *, int); } \
137void* __cdecl operator new(size_t size, int type, const char* fname, int line) { return _malloc_dbg(size, type, fname, line); } \
138void* __cdecl operator new[](size_t size, int type, const char* fname, int line) { return operator new(size, type, fname, line); } \
139
140#else //_CRTDBG_MAP_ALLOC
141
142#ifdef _MSC_VER
143#define TD_CDECL __cdecl
144#else
145#define TD_CDECL
146#endif
147
148#if !defined(_WIN32_WCE)
149#define ODRX_CRTDBG_MAP_ALLOC_OPERATORS() \
150void* TD_CDECL operator new(size_t size, int, const char*,int) { return ::operator new(size); } \
151void* TD_CDECL operator new[](size_t size, int, const char*, int) { return ::operator new[](size); }
152
153#endif
154
155#endif //_CRTDBG_MAP_ALLOC
156#endif //__ODAALLOCOP_H__