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#include "atomic.h"
40
41
42namespace std
43{
44	struct nothrow_t {};
45}
46
47
48/// The type of the function called when allocation fails.
49typedef void (*new_handler)();
50/**
51 * The function to call when allocation fails.  By default, there is no
52 * handler and a bad allocation exception is thrown if an allocation fails.
53 */
54static new_handler new_handl;
55
56namespace std
57{
58	/**
59	 * Sets a function to be called when there is a failure in new.
60	 */
61	__attribute__((weak))
62	new_handler set_new_handler(new_handler handler)
63	{
64		return ATOMIC_SWAP(&new_handl, handler);
65	}
66	__attribute__((weak))
67	new_handler get_new_handler(void)
68	{
69		return ATOMIC_LOAD(&new_handl);
70	}
71}
72
73
74__attribute__((weak))
75void* operator new(size_t size)
76{
77	if (0 == size)
78	{
79		size = 1;
80	}
81	void * mem = malloc(size);
82	while (0 == mem)
83	{
84		new_handler h = std::get_new_handler();
85		if (0 != h)
86		{
87			h();
88		}
89		else
90		{
91			throw std::bad_alloc();
92		}
93		mem = malloc(size);
94	}
95
96	return mem;
97}
98
99__attribute__((weak))
100void* operator new(size_t size, const std::nothrow_t &) throw()
101{
102	try {
103		return :: operator new(size);
104	} catch (...) {
105		// nothrow operator new should return NULL in case of
106		// std::bad_alloc exception in new handler
107		return NULL;
108	}
109}
110
111
112__attribute__((weak))
113void operator delete(void * ptr)
114#if __cplusplus < 201000L
115throw()
116#endif
117{
118	free(ptr);
119}
120
121
122__attribute__((weak))
123void * operator new[](size_t size)
124#if __cplusplus < 201000L
125throw(std::bad_alloc)
126#endif
127{
128	return ::operator new(size);
129}
130
131
132__attribute__((weak))
133void * operator new[](size_t size, const std::nothrow_t &) throw()
134{
135	try {
136		return ::operator new[](size);
137	} catch (...) {
138		// nothrow operator new should return NULL in case of
139		// std::bad_alloc exception in new handler
140		return NULL;
141	}
142}
143
144
145__attribute__((weak))
146void operator delete[](void * ptr)
147#if __cplusplus < 201000L
148throw()
149#endif
150{
151	::operator delete(ptr);
152}
153
154
155