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