1 // This code is in the public domain -- castanyo@yahoo.es
2 
3 #ifndef NV_CORE_MEMORY_H
4 #define NV_CORE_MEMORY_H
5 
6 #include <nvcore/nvcore.h>
7 
8 #include <stdlib.h> // malloc(), realloc() and free()
9 #include <stddef.h>	// size_t
10 
11 #include <new>	// new and delete
12 
13 // Custom memory allocator
14 namespace nv
15 {
16 	namespace mem
17 	{
18 		NVCORE_API void * malloc(size_t size);
19 		NVCORE_API void * malloc(size_t size, const char * file, int line);
20 
21 		NVCORE_API void free(const void * ptr);
22 		NVCORE_API void * realloc(void * ptr, size_t size);
23 
24 	} // mem namespace
25 
26 } // nv namespace
27 
28 
29 // Override new/delete
30 
new(size_t size)31 inline void * operator new (size_t size) throw(std::bad_alloc)
32 {
33 	return nv::mem::malloc(size);
34 }
35 
delete(void * p)36 inline void operator delete (void *p) throw()
37 {
38 	nv::mem::free(p);
39 }
40 
throw(std::bad_alloc)41 inline void * operator new [] (size_t size) throw(std::bad_alloc)
42 {
43 	return nv::mem::malloc(size);
44 }
45 
throw()46 inline void operator delete [] (void * p) throw()
47 {
48 	nv::mem::free(p);
49 }
50 
51 /*
52 #ifdef _DEBUG
53 #define new new(__FILE__, __LINE__)
54 #define malloc(i) malloc(i, __FILE__, __LINE__)
55 #endif
56 */
57 
58 #if 0
59 /*
60     File:	main.cpp
61 
62     Version:	1.0
63 
64 	Abstract: Overrides the C++ 'operator new' and 'operator delete'.
65 
66     Disclaimer:	IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
67 		("Apple") in consideration of your agreement to the following terms, and your
68 		use, installation, modification or redistribution of this Apple software
69 		constitutes acceptance of these terms.  If you do not agree with these terms,
70 		please do not use, install, modify or redistribute this Apple software.
71 
72 		In consideration of your agreement to abide by the following terms, and subject
73 		to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
74 		copyrights in this original Apple software (the "Apple Software"), to use,
75 		reproduce, modify and redistribute the Apple Software, with or without
76 		modifications, in source and/or binary forms; provided that if you redistribute
77 		the Apple Software in its entirety and without modifications, you must retain
78 		this notice and the following text and disclaimers in all such redistributions of
79 		the Apple Software.  Neither the name, trademarks, service marks or logos of
80 		Apple Computer, Inc. may be used to endorse or promote products derived from the
81 		Apple Software without specific prior written permission from Apple.  Except as
82 		expressly stated in this notice, no other rights or licenses, express or implied,
83 		are granted by Apple herein, including but not limited to any patent rights that
84 		may be infringed by your derivative works or by other works in which the Apple
85 		Software may be incorporated.
86 
87 		The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
88 		WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
89 		WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90 		PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
91 		COMBINATION WITH YOUR PRODUCTS.
92 
93 		IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
94 		CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
95 		GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 		ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
97 		OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
98 		(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
99 		ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 
101 	Copyright © 2006 Apple Computer, Inc., All Rights Reserved
102 */
103 
104 /* This sample shows how to override the C++ global 'new' and 'delete' operators.  */
105 #include <new>
106 #include <iostream>
107 #include <cstdlib>
108 #include <stdexcept>
109 #include <locale>
110 
111 /* Some variables and code to make the example do something.  */
112 namespace {
113   unsigned long long gNewCounter; // number of times 'new' was called
114   unsigned long long gDeleteCounter;  // number of times 'delete' was called
115 
116   void printCounters()  // print the counters above
117   {
118 	std::cout << "new was called " << gNewCounter << " times and delete was called " << gDeleteCounter << " times\n";
119   }
120 }
121 
122 /* These are the overridden new and delete routines.
123    Most applications will want to override at least these four versions of new/delete if they override any of them.
124 
125    In Mac OS, it's not necessary to override the array versions of operator new and delete if all
126    they would do is call the non-array versions; the C++ standard library, as an extension
127    to the C++ standard, does this for you.
128 
129    Developers should consult the section [lib.support.dynamic] in the C++ standard to see the requirements
130    on the generic operators new and delete; the system may expect that your overridden operators meet all these
131    requirements.
132 
133    Your operators may be called by the system, even early in start-up before constructors have been executed.  */
134 void* operator new(std::size_t sz) throw (std::bad_alloc)
135 {
136 	void *result = std::malloc (sz == 0 ? 1 : sz);
137 	if (result == NULL)
138 		throw std::bad_alloc();
139 	gNewCounter++;
140 	return result;
141 }
142 void operator delete(void* p) throw()
143 {
144 	if (p == NULL)
145 		return;
146 	std::free (p);
147 	gDeleteCounter++;
148 }
149 
150 /* These are the 'nothrow' versions of the above operators.
151    The system version will try to call a std::new_handler if they
152    fail, but your overriding versions are not required to do this.  */
153 void* operator new(std::size_t sz, const std::nothrow_t&) throw()
154 {
155 	try {
156 		void * result = ::operator new (sz);  // calls our overridden operator new
157 		return result;
158 	} catch (std::bad_alloc &) {
159 	  return NULL;
160 	}
161 }
162 void operator delete(void* p, const std::nothrow_t&) throw()
163 {
164 	::operator delete (p);
165 }
166 
167 /* Bug 4067110 is that if your program has no weak symbols at all, the linker will not set the
168    WEAK_DEFINES bit in the Mach-O header and as a result the new and delete operators above won't
169    be seen by system libraries.  This is mostly a problem for test programs and small examples,
170    since almost all real C++ programs complicated enough to override new and delete will have at
171    least one weak symbol.  However, this is a small example, so:  */
172 void __attribute__((weak, visibility("default"))) workaroundFor4067110 () { }
173 
174 /* This is a simple test program that causes the runtime library to call new and delete.  */
175 int main()
176 {
177 	atexit (printCounters);
178 	try {
179 	  std::locale example("does_not_exist");
180 	} catch (std::runtime_error &x) {
181 	}
182 	return 0;
183 }
184 #endif // 0
185 
186 #endif // NV_CORE_MEMORY_H
187