1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxcrt/fx_memory.h"
8 #include "core/fxcrt/fx_safe_types.h"
9
10 #include <stdlib.h> // For abort().
11
12 pdfium::base::PartitionAllocatorGeneric gArrayBufferPartitionAllocator;
13 pdfium::base::PartitionAllocatorGeneric gGeneralPartitionAllocator;
14 pdfium::base::PartitionAllocatorGeneric gStringPartitionAllocator;
15
FXMEM_InitializePartitionAlloc()16 void FXMEM_InitializePartitionAlloc() {
17 static bool s_gPartitionAllocatorsInitialized = false;
18 if (!s_gPartitionAllocatorsInitialized) {
19 pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
20 gArrayBufferPartitionAllocator.init();
21 gGeneralPartitionAllocator.init();
22 gStringPartitionAllocator.init();
23 s_gPartitionAllocatorsInitialized = true;
24 }
25 }
26
FXMEM_DefaultAlloc(size_t byte_size)27 void* FXMEM_DefaultAlloc(size_t byte_size) {
28 return pdfium::base::PartitionAllocGenericFlags(
29 gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
30 byte_size, "GeneralPartition");
31 }
32
FXMEM_DefaultCalloc(size_t num_elems,size_t byte_size)33 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
34 return FX_SafeAlloc(num_elems, byte_size);
35 }
36
FXMEM_DefaultRealloc(void * pointer,size_t new_size)37 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) {
38 return pdfium::base::PartitionReallocGeneric(
39 gGeneralPartitionAllocator.root(), pointer, new_size, "GeneralPartition");
40 }
41
FXMEM_DefaultFree(void * pointer)42 void FXMEM_DefaultFree(void* pointer) {
43 pdfium::base::PartitionFree(pointer);
44 }
45
FX_OutOfMemoryTerminate()46 NEVER_INLINE void FX_OutOfMemoryTerminate() {
47 // Termimate cleanly if we can, else crash at a specific address (0xbd).
48 abort();
49 #ifndef _WIN32
50 reinterpret_cast<void (*)()>(0xbd)();
51 #endif
52 }
53