ReactOS 0.4.16-dev-853-g88d9285
new_handler.cpp
Go to the documentation of this file.
1//
2// new_handler.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Implementation of the new handler and related functions..
7//
8#include <corecrt_internal.h>
9#include <new.h>
10
11// The global new handler. This pointer should only be accessed via the
12// functions defined in this source file.
13static __crt_state_management::dual_state_global<_PNH> __acrt_new_handler;
14
15
16
17// Initializes the new handler to the encoded nullptr value.
18extern "C" void __cdecl __acrt_initialize_new_handler(void* const encoded_null)
19{
20 __acrt_new_handler.initialize(reinterpret_cast<_PNH>(encoded_null));
21}
22
23// Sets the new handler to the new new handler and returns the old handler.
24_PNH __cdecl _set_new_handler(_PNH new_handler)
25{
26 _PNH result = nullptr;
27
29 __try
30 {
31 result = __crt_fast_decode_pointer(__acrt_new_handler.value());
32 __acrt_new_handler.value() = __crt_fast_encode_pointer(new_handler);
33 }
35 {
37 }
39
40 return result;
41}
42
43// Sets the new handler to nullptr and returns the old handler. The argument
44// must be zero. This overload is used to enable a caller to pass nullptr.
45_PNH __cdecl _set_new_handler(int const new_handler)
46{
47 // This is referenced only in the Debug CRT build
48 UNREFERENCED_PARAMETER(new_handler);
49
50 _ASSERTE(new_handler == 0);
51
52 return _set_new_handler(__crt_fast_encode_pointer(nullptr));
53}
54
55// Gets the current new handler.
57{
58 _PNH result = nullptr;
59
61 __try
62 {
63 result = __crt_fast_decode_pointer(__acrt_new_handler.value());
64 }
66 {
68 }
70
71 return result;
72}
73
74// Calls the currently registered new handler with the provided size. If the
75// new handler succeeds, this function returns 1. This function returns 0 on
76// failure. The new handler may throw std::bad_alloc.
77extern "C" int __cdecl _callnewh(size_t const size)
78{
79 _PNH const pnh = _query_new_handler();
80
81 if (pnh == nullptr || (*pnh)(size) == 0)
82 return 0;
83
84 return 1;
85}
#define __cdecl
Definition: accygwin.h:79
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
@ __acrt_heap_lock
#define _ASSERTE(expr)
Definition: crtdbg.h:114
__acrt_lock(__acrt_heap_lock)
GLsizeiptr size
Definition: glext.h:5919
GLuint64EXT * result
Definition: glext.h:11304
_PNH __cdecl _set_new_handler(_PNH new_handler)
Definition: new_handler.cpp:24
int __cdecl _callnewh(size_t const size)
Definition: new_handler.cpp:77
static __crt_state_management::dual_state_global< _PNH > __acrt_new_handler
Definition: new_handler.cpp:13
_PNH __cdecl _query_new_handler()
Definition: new_handler.cpp:56
void __cdecl __acrt_initialize_new_handler(void *const encoded_null)
Definition: new_handler.cpp:18
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174