xref: /minix3/external/bsd/libc++/dist/libcxxrt/src/memory.cc (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc /*
2*4684ddb6SLionel Sambuc  * Copyright 2010-2011 PathScale, Inc. All rights reserved.
3*4684ddb6SLionel Sambuc  *
4*4684ddb6SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
5*4684ddb6SLionel Sambuc  * modification, are permitted provided that the following conditions are met:
6*4684ddb6SLionel Sambuc  *
7*4684ddb6SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright notice,
8*4684ddb6SLionel Sambuc  *    this list of conditions and the following disclaimer.
9*4684ddb6SLionel Sambuc  *
10*4684ddb6SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright notice,
11*4684ddb6SLionel Sambuc  *    this list of conditions and the following disclaimer in the documentation
12*4684ddb6SLionel Sambuc  *    and/or other materials provided with the distribution.
13*4684ddb6SLionel Sambuc  *
14*4684ddb6SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15*4684ddb6SLionel Sambuc  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16*4684ddb6SLionel Sambuc  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17*4684ddb6SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18*4684ddb6SLionel Sambuc  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19*4684ddb6SLionel Sambuc  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20*4684ddb6SLionel Sambuc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21*4684ddb6SLionel Sambuc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22*4684ddb6SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23*4684ddb6SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24*4684ddb6SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*4684ddb6SLionel Sambuc  */
26*4684ddb6SLionel Sambuc 
27*4684ddb6SLionel Sambuc /**
28*4684ddb6SLionel Sambuc  * memory.cc - Contains stub definition of C++ new/delete operators.
29*4684ddb6SLionel Sambuc  *
30*4684ddb6SLionel Sambuc  * These definitions are intended to be used for testing and are weak symbols
31*4684ddb6SLionel Sambuc  * to allow them to be replaced by definitions from a STL implementation.
32*4684ddb6SLionel Sambuc  * These versions simply wrap malloc() and free(), they do not provide a
33*4684ddb6SLionel Sambuc  * C++-specific allocator.
34*4684ddb6SLionel Sambuc  */
35*4684ddb6SLionel Sambuc 
36*4684ddb6SLionel Sambuc #include <stddef.h>
37*4684ddb6SLionel Sambuc #include <stdlib.h>
38*4684ddb6SLionel Sambuc #include "stdexcept.h"
39*4684ddb6SLionel Sambuc #include "atomic.h"
40*4684ddb6SLionel Sambuc 
41*4684ddb6SLionel Sambuc 
42*4684ddb6SLionel Sambuc namespace std
43*4684ddb6SLionel Sambuc {
44*4684ddb6SLionel Sambuc 	struct nothrow_t {};
45*4684ddb6SLionel Sambuc }
46*4684ddb6SLionel Sambuc 
47*4684ddb6SLionel Sambuc 
48*4684ddb6SLionel Sambuc /// The type of the function called when allocation fails.
49*4684ddb6SLionel Sambuc typedef void (*new_handler)();
50*4684ddb6SLionel Sambuc /**
51*4684ddb6SLionel Sambuc  * The function to call when allocation fails.  By default, there is no
52*4684ddb6SLionel Sambuc  * handler and a bad allocation exception is thrown if an allocation fails.
53*4684ddb6SLionel Sambuc  */
54*4684ddb6SLionel Sambuc static new_handler new_handl;
55*4684ddb6SLionel Sambuc 
56*4684ddb6SLionel Sambuc namespace std
57*4684ddb6SLionel Sambuc {
58*4684ddb6SLionel Sambuc 	/**
59*4684ddb6SLionel Sambuc 	 * Sets a function to be called when there is a failure in new.
60*4684ddb6SLionel Sambuc 	 */
61*4684ddb6SLionel Sambuc 	__attribute__((weak))
set_new_handler(new_handler handler)62*4684ddb6SLionel Sambuc 	new_handler set_new_handler(new_handler handler)
63*4684ddb6SLionel Sambuc 	{
64*4684ddb6SLionel Sambuc 		return ATOMIC_SWAP(&new_handl, handler);
65*4684ddb6SLionel Sambuc 	}
66*4684ddb6SLionel Sambuc 	__attribute__((weak))
get_new_handler(void)67*4684ddb6SLionel Sambuc 	new_handler get_new_handler(void)
68*4684ddb6SLionel Sambuc 	{
69*4684ddb6SLionel Sambuc 		return ATOMIC_LOAD(&new_handl);
70*4684ddb6SLionel Sambuc 	}
71*4684ddb6SLionel Sambuc }
72*4684ddb6SLionel Sambuc 
73*4684ddb6SLionel Sambuc 
74*4684ddb6SLionel Sambuc __attribute__((weak))
operator new(size_t size)75*4684ddb6SLionel Sambuc void* operator new(size_t size)
76*4684ddb6SLionel Sambuc {
77*4684ddb6SLionel Sambuc 	if (0 == size)
78*4684ddb6SLionel Sambuc 	{
79*4684ddb6SLionel Sambuc 		size = 1;
80*4684ddb6SLionel Sambuc 	}
81*4684ddb6SLionel Sambuc 	void * mem = malloc(size);
82*4684ddb6SLionel Sambuc 	while (0 == mem)
83*4684ddb6SLionel Sambuc 	{
84*4684ddb6SLionel Sambuc 		new_handler h = std::get_new_handler();
85*4684ddb6SLionel Sambuc 		if (0 != h)
86*4684ddb6SLionel Sambuc 		{
87*4684ddb6SLionel Sambuc 			h();
88*4684ddb6SLionel Sambuc 		}
89*4684ddb6SLionel Sambuc 		else
90*4684ddb6SLionel Sambuc 		{
91*4684ddb6SLionel Sambuc 			throw std::bad_alloc();
92*4684ddb6SLionel Sambuc 		}
93*4684ddb6SLionel Sambuc 		mem = malloc(size);
94*4684ddb6SLionel Sambuc 	}
95*4684ddb6SLionel Sambuc 
96*4684ddb6SLionel Sambuc 	return mem;
97*4684ddb6SLionel Sambuc }
98*4684ddb6SLionel Sambuc 
99*4684ddb6SLionel Sambuc __attribute__((weak))
operator new(size_t size,const std::nothrow_t &)100*4684ddb6SLionel Sambuc void* operator new(size_t size, const std::nothrow_t &) throw()
101*4684ddb6SLionel Sambuc {
102*4684ddb6SLionel Sambuc 	try {
103*4684ddb6SLionel Sambuc 		return :: operator new(size);
104*4684ddb6SLionel Sambuc 	} catch (...) {
105*4684ddb6SLionel Sambuc 		// nothrow operator new should return NULL in case of
106*4684ddb6SLionel Sambuc 		// std::bad_alloc exception in new handler
107*4684ddb6SLionel Sambuc 		return NULL;
108*4684ddb6SLionel Sambuc 	}
109*4684ddb6SLionel Sambuc }
110*4684ddb6SLionel Sambuc 
111*4684ddb6SLionel Sambuc 
112*4684ddb6SLionel Sambuc __attribute__((weak))
operator delete(void * ptr)113*4684ddb6SLionel Sambuc void operator delete(void * ptr)
114*4684ddb6SLionel Sambuc #if __cplusplus < 201000L
115*4684ddb6SLionel Sambuc throw()
116*4684ddb6SLionel Sambuc #endif
117*4684ddb6SLionel Sambuc {
118*4684ddb6SLionel Sambuc 	free(ptr);
119*4684ddb6SLionel Sambuc }
120*4684ddb6SLionel Sambuc 
121*4684ddb6SLionel Sambuc 
122*4684ddb6SLionel Sambuc __attribute__((weak))
operator new[](size_t size)123*4684ddb6SLionel Sambuc void * operator new[](size_t size)
124*4684ddb6SLionel Sambuc #if __cplusplus < 201000L
125*4684ddb6SLionel Sambuc throw(std::bad_alloc)
126*4684ddb6SLionel Sambuc #endif
127*4684ddb6SLionel Sambuc {
128*4684ddb6SLionel Sambuc 	return ::operator new(size);
129*4684ddb6SLionel Sambuc }
130*4684ddb6SLionel Sambuc 
131*4684ddb6SLionel Sambuc 
132*4684ddb6SLionel Sambuc __attribute__((weak))
operator new[](size_t size,const std::nothrow_t &)133*4684ddb6SLionel Sambuc void * operator new[](size_t size, const std::nothrow_t &) throw()
134*4684ddb6SLionel Sambuc {
135*4684ddb6SLionel Sambuc 	try {
136*4684ddb6SLionel Sambuc 		return ::operator new[](size);
137*4684ddb6SLionel Sambuc 	} catch (...) {
138*4684ddb6SLionel Sambuc 		// nothrow operator new should return NULL in case of
139*4684ddb6SLionel Sambuc 		// std::bad_alloc exception in new handler
140*4684ddb6SLionel Sambuc 		return NULL;
141*4684ddb6SLionel Sambuc 	}
142*4684ddb6SLionel Sambuc }
143*4684ddb6SLionel Sambuc 
144*4684ddb6SLionel Sambuc 
145*4684ddb6SLionel Sambuc __attribute__((weak))
operator delete[](void * ptr)146*4684ddb6SLionel Sambuc void operator delete[](void * ptr)
147*4684ddb6SLionel Sambuc #if __cplusplus < 201000L
148*4684ddb6SLionel Sambuc throw()
149*4684ddb6SLionel Sambuc #endif
150*4684ddb6SLionel Sambuc {
151*4684ddb6SLionel Sambuc 	::operator delete(ptr);
152*4684ddb6SLionel Sambuc }
153*4684ddb6SLionel Sambuc 
154*4684ddb6SLionel Sambuc 
155