John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Bo Xu | 3522876 | 2014-07-08 15:30:46 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 7 | #include "core/fxcrt/fx_memory.h" |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 8 | #include "core/fxcrt/fx_safe_types.h" |
Tom Sepez | ed099be | 2015-05-15 16:30:52 -0700 | [diff] [blame] | 9 | |
Tom Sepez | a9deea9 | 2017-04-25 10:37:47 -0700 | [diff] [blame] | 10 | #include <stdlib.h> // For abort(). |
| 11 | |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 12 | pdfium::base::PartitionAllocatorGeneric gArrayBufferPartitionAllocator; |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 13 | pdfium::base::PartitionAllocatorGeneric gGeneralPartitionAllocator; |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 14 | pdfium::base::PartitionAllocatorGeneric gStringPartitionAllocator; |
| 15 | |
Dan Sinclair | dbc3d3e | 2017-05-11 13:41:38 -0400 | [diff] [blame] | 16 | void FXMEM_InitializePartitionAlloc() { |
Chris Palmer | 2b79729 | 2017-04-06 14:45:39 -0700 | [diff] [blame] | 17 | static bool s_gPartitionAllocatorsInitialized = false; |
| 18 | if (!s_gPartitionAllocatorsInitialized) { |
| 19 | pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate); |
| 20 | gArrayBufferPartitionAllocator.init(); |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 21 | gGeneralPartitionAllocator.init(); |
Chris Palmer | 2b79729 | 2017-04-06 14:45:39 -0700 | [diff] [blame] | 22 | gStringPartitionAllocator.init(); |
| 23 | s_gPartitionAllocatorsInitialized = true; |
| 24 | } |
| 25 | } |
| 26 | |
Lei Zhang | b810da2 | 2017-11-03 22:18:18 +0000 | [diff] [blame] | 27 | void* FXMEM_DefaultAlloc(size_t byte_size) { |
Nicolas Pena | 3e5ef46 | 2017-07-13 15:26:36 -0400 | [diff] [blame] | 28 | return pdfium::base::PartitionAllocGenericFlags( |
| 29 | gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull, |
| 30 | byte_size, "GeneralPartition"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | } |
Nicolas Pena | 152bfe0 | 2017-04-18 15:36:29 -0400 | [diff] [blame] | 32 | |
| 33 | void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) { |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 34 | return FX_SafeAlloc(num_elems, byte_size); |
Nicolas Pena | 152bfe0 | 2017-04-18 15:36:29 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Lei Zhang | b810da2 | 2017-11-03 22:18:18 +0000 | [diff] [blame] | 37 | void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) { |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 38 | return pdfium::base::PartitionReallocGeneric( |
| 39 | gGeneralPartitionAllocator.root(), pointer, new_size, "GeneralPartition"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | } |
Nicolas Pena | 152bfe0 | 2017-04-18 15:36:29 -0400 | [diff] [blame] | 41 | |
Lei Zhang | b810da2 | 2017-11-03 22:18:18 +0000 | [diff] [blame] | 42 | void FXMEM_DefaultFree(void* pointer) { |
Chris Palmer | 7726a58 | 2017-06-22 10:10:17 -0700 | [diff] [blame] | 43 | pdfium::base::PartitionFree(pointer); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | } |
Tom Sepez | ed099be | 2015-05-15 16:30:52 -0700 | [diff] [blame] | 45 | |
| 46 | NEVER_INLINE void FX_OutOfMemoryTerminate() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | // Termimate cleanly if we can, else crash at a specific address (0xbd). |
| 48 | abort(); |
Wei Li | e91afba | 2016-03-22 19:19:01 -0700 | [diff] [blame] | 49 | #ifndef _WIN32 |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | reinterpret_cast<void (*)()>(0xbd)(); |
Wei Li | e91afba | 2016-03-22 19:19:01 -0700 | [diff] [blame] | 51 | #endif |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | } |