ReactOS 0.4.16-dev-889-g9563c07
malloc_base.cpp
Go to the documentation of this file.
1//
2// malloc_base.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Implementation of _malloc_base(). This is defined in a different source file
7// from the malloc() function to allow malloc() to be replaced by the user.
8//
9#include <corecrt_internal.h>
10#include <malloc.h>
11#include <new.h>
12
13
14
15// This function implements the logic of malloc(). It is called directly by the
16// malloc() function in the Release CRT and is called by the debug heap in the
17// Debug CRT.
18//
19// This function must be marked noinline, otherwise malloc and
20// _malloc_base will have identical COMDATs, and the linker will fold
21// them when calling one from the CRT. This is necessary because malloc
22// needs to support users patching in custom implementations.
23extern "C" __declspec(noinline) _CRTRESTRICT void* __cdecl _malloc_base(size_t const size)
24{
25 // Ensure that the requested size is not too large:
27
28 // Ensure we request an allocation of at least one byte:
29 size_t const actual_size = size == 0 ? 1 : size;
30
31 for (;;)
32 {
33 void* const block = HeapAlloc(__acrt_heap, 0, actual_size);
34 if (block)
35 return block;
36
37 // Otherwise, see if we need to call the new handler, and if so call it.
38 // If the new handler fails, just return nullptr:
39 if (_query_new_mode() == 0 || !_callnewh(actual_size))
40 {
41 errno = ENOMEM;
42 return nullptr;
43 }
44
45 // The new handler was successful; try to allocate again...
46 }
47}
#define ENOMEM
Definition: acclib.h:84
#define __cdecl
Definition: accygwin.h:79
_Check_return_ _ACRTIMP int __cdecl _callnewh(_In_ size_t _Size)
#define _CRTRESTRICT
Definition: corecrt.h:17
#define _HEAP_MAXREQ
Definition: malloc.h:24
#define HeapAlloc
Definition: compat.h:733
#define noinline
Definition: types.h:64
void __declspec(noinline) __cdecl _free_base(void *const block)
Definition: free_base.cpp:98
GLsizeiptr size
Definition: glext.h:5919
HANDLE __acrt_heap
Definition: heap_handle.cpp:15
#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr)
#define errno
Definition: errno.h:18
_ACRTIMP int __cdecl _query_new_mode(void)
Definition: heap.c:217
static unsigned int block
Definition: xmlmemory.c:101