aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PSVita/PSVitaExtras/user_new.cpp
blob: 1f71f6d2123d4ddbc3c58f5513ade7b0c7313ad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* SCE CONFIDENTIAL
 PlayStation(R)Vita Programmer Tool Runtime Library Release 03.000.061
 * Copyright (C) 2012 Sony Computer Entertainment Inc.
 * All Rights Reserved.
 */

#include <new>
#include <cstdlib>
//#include <libdbg.h>

void *user_new(std::size_t size) throw(std::bad_alloc);
void *user_new(std::size_t size, const std::nothrow_t& x) throw();
void *user_new_array(std::size_t size) throw(std::bad_alloc);
void *user_new_array(std::size_t size, const std::nothrow_t& x) throw();
void user_delete(void *ptr) throw();
void user_delete(void *ptr, const std::nothrow_t& x) throw();
void user_delete_array(void *ptr) throw();
void user_delete_array(void *ptr, const std::nothrow_t& x) throw();

/**E Replace operator new. */
/**J operator new と置き換わる */
void *user_new(std::size_t size) throw(std::bad_alloc)
{
	void *ptr;

//	SCE_DBG_LOG_TRACE("Called operator new(%u)", size);

	if (size == 0)
		size = 1;

	while ((ptr = (void *)std::malloc(size)) == NULL) {
		/**E Obtain new_handler */
		/**J new_handler を取得する */
		std::new_handler handler = std::_get_new_handler();

		/**E When new_handler is a NULL pointer, bad_alloc is send. If not, new_handler is called. */
		/**J new_handler が NULL ポインタの場合、bad_alloc を送出する、そうでない場合、new_handler を呼び出す */
		if (!handler)
			throw std::bad_alloc();
		else
			(*handler)();
	}
	return ptr;
}

/**E Replace operator new(std::nothrow). */
/**J operator(std::nothrow) と置き換わる */
void *user_new(std::size_t size, const std::nothrow_t& x) throw()
{
	void *ptr;

	(void)x;

//	SCE_DBG_LOG_TRACE("Called operator new(nothrow)(%u)", size);

	if (size == 0)
		size = 1;

	while ((ptr = (void *)std::malloc(size)) == NULL) {
		/**E Obtain new_handler */
		/**J new_handler を取得する */
		std::new_handler handler = std::_get_new_handler();

		/**E When new_handler is a NULL pointer, NULL is returned. */
		/**J new_handler が NULL ポインタの場合、NULL を返す */
		if (!handler)
			return NULL;

		/**E Call new_handler. If new_handler sends bad_alloc, NULL is returned. */
		/**J new_handler を呼び出す、new_handler が bad_alloc を送出した場合、NULL を返す */
		try {
			(*handler)();
		} catch (std::bad_alloc) {
			return NULL;
		}
	}
	return ptr;
}

/**E Replace operator new[]. */
/**J operator new[] と置き換わる */
void *user_new_array(std::size_t size) throw(std::bad_alloc)
{
//	SCE_DBG_LOG_TRACE("Called operator new[](%u)", size);
	return user_new(size);
}

/**E Replace operator new[](std::nothrow). */
/**J operator new[](std::nothrow) と置き換わる */
void *user_new_array(std::size_t size, const std::nothrow_t& x) throw()
{
//	SCE_DBG_LOG_TRACE("Called operator new(nothrow)[](%u)", size);
	return user_new(size, x);
}

/**E Replace operator delete. */
/**J operator delete と置き換わる */
void user_delete(void *ptr) throw()
{
//	SCE_DBG_LOG_TRACE("Called operator delete(%p)", ptr);
	/**E In the case of the NULL pointer, no action will be taken. */
	/**J NULL ポインタの場合、何も行わない */
	if (ptr != NULL)
		std::free(ptr);
}

/**E Replace operator delete(std::nothrow). */
/**J operator delete(std::nothrow) と置き換わる */
void user_delete(void *ptr, const std::nothrow_t& x) throw()
{
	(void)x;

//	SCE_DBG_LOG_TRACE("Called operator delete(nothrow)(%p)", ptr);
	user_delete(ptr);
}

/**E Replace operator delete[]. */
/**J operator delete[] と置き換わる */
void user_delete_array(void *ptr) throw()
{
//	SCE_DBG_LOG_TRACE("Called operator delete[](%p)", ptr);
	user_delete(ptr);
}

/**E Replace operator delete[](std::nothrow). */
/**J operator delete[](std::nothrow) と置き換わる */
void user_delete_array(void *ptr, const std::nothrow_t& x) throw()
{
//	SCE_DBG_LOG_TRACE("Called operator delete(nothrow)[](%p)", ptr);
	user_delete(ptr, x);
}