blob: 6a135ae0f87bdb7375627181d0578fe21b57c41b [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Xu35228762014-07-08 15:30:46 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclaira52ab742016-09-29 13:59:29 -07007#include "core/fxcrt/fx_memory.h"
Chris Palmer7726a582017-06-22 10:10:17 -07008#include "core/fxcrt/fx_safe_types.h"
Tom Sepezed099be2015-05-15 16:30:52 -07009
Tom Sepeza9deea92017-04-25 10:37:47 -070010#include <stdlib.h> // For abort().
11
Chris Palmere4b035b2017-03-26 15:48:34 -070012pdfium::base::PartitionAllocatorGeneric gArrayBufferPartitionAllocator;
Chris Palmer7726a582017-06-22 10:10:17 -070013pdfium::base::PartitionAllocatorGeneric gGeneralPartitionAllocator;
Chris Palmere4b035b2017-03-26 15:48:34 -070014pdfium::base::PartitionAllocatorGeneric gStringPartitionAllocator;
15
Dan Sinclairdbc3d3e2017-05-11 13:41:38 -040016void FXMEM_InitializePartitionAlloc() {
Chris Palmer2b797292017-04-06 14:45:39 -070017 static bool s_gPartitionAllocatorsInitialized = false;
18 if (!s_gPartitionAllocatorsInitialized) {
19 pdfium::base::PartitionAllocGlobalInit(FX_OutOfMemoryTerminate);
20 gArrayBufferPartitionAllocator.init();
Chris Palmer7726a582017-06-22 10:10:17 -070021 gGeneralPartitionAllocator.init();
Chris Palmer2b797292017-04-06 14:45:39 -070022 gStringPartitionAllocator.init();
23 s_gPartitionAllocatorsInitialized = true;
24 }
25}
26
Lei Zhangb810da22017-11-03 22:18:18 +000027void* FXMEM_DefaultAlloc(size_t byte_size) {
Nicolas Pena3e5ef462017-07-13 15:26:36 -040028 return pdfium::base::PartitionAllocGenericFlags(
29 gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull,
30 byte_size, "GeneralPartition");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
Nicolas Pena152bfe02017-04-18 15:36:29 -040032
33void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) {
Chris Palmer7726a582017-06-22 10:10:17 -070034 return FX_SafeAlloc(num_elems, byte_size);
Nicolas Pena152bfe02017-04-18 15:36:29 -040035}
36
Lei Zhangb810da22017-11-03 22:18:18 +000037void* FXMEM_DefaultRealloc(void* pointer, size_t new_size) {
Chris Palmer7726a582017-06-22 10:10:17 -070038 return pdfium::base::PartitionReallocGeneric(
39 gGeneralPartitionAllocator.root(), pointer, new_size, "GeneralPartition");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
Nicolas Pena152bfe02017-04-18 15:36:29 -040041
Lei Zhangb810da22017-11-03 22:18:18 +000042void FXMEM_DefaultFree(void* pointer) {
Chris Palmer7726a582017-06-22 10:10:17 -070043 pdfium::base::PartitionFree(pointer);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
Tom Sepezed099be2015-05-15 16:30:52 -070045
46NEVER_INLINE void FX_OutOfMemoryTerminate() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 // Termimate cleanly if we can, else crash at a specific address (0xbd).
48 abort();
Wei Lie91afba2016-03-22 19:19:01 -070049#ifndef _WIN32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 reinterpret_cast<void (*)()>(0xbd)();
Wei Lie91afba2016-03-22 19:19:01 -070051#endif
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052}