| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
| 8 #ifdef __cplusplus | 8 |
| 9 extern "C" { | 9 extern "C" { |
| 10 #endif | 10 |
| 11 void* FXMEM_DefaultAlloc(size_t byte_size, int flags) | 11 void* FXMEM_DefaultAlloc(size_t byte_size, int flags) |
| 12 { | 12 { |
| 13 return (void*)malloc(byte_size); | 13 return malloc(byte_size); |
| 14 } | 14 } |
| 15 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags) | 15 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags) |
| 16 { | 16 { |
| 17 return realloc(pointer, new_size); | 17 return realloc(pointer, new_size); |
| 18 } | 18 } |
| 19 void FXMEM_DefaultFree(void* pointer, int flags) | 19 void FXMEM_DefaultFree(void* pointer, int flags) |
| 20 { | 20 { |
| 21 free(pointer); | 21 free(pointer); |
| 22 } | 22 } |
| 23 #ifdef __cplusplus | 23 |
| 24 } | 24 } // extern "C" |
| 25 #endif | |
| 26 CFX_GrowOnlyPool::CFX_GrowOnlyPool(size_t trunk_size) | |
| 27 { | |
| 28 m_TrunkSize = trunk_size; | |
| 29 m_pFirstTrunk = NULL; | |
| 30 } | |
| 31 CFX_GrowOnlyPool::~CFX_GrowOnlyPool() | |
| 32 { | |
| 33 FreeAll(); | |
| 34 } | |
| 35 struct _FX_GrowOnlyTrunk { | |
| 36 size_t» m_Size; | |
| 37 size_t» m_Allocated; | |
| 38 _FX_GrowOnlyTrunk*» m_pNext; | |
| 39 }; | |
| 40 void CFX_GrowOnlyPool::FreeAll() | |
| 41 { | |
| 42 _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; | |
| 43 while (pTrunk) { | |
| 44 _FX_GrowOnlyTrunk* pNext = pTrunk->m_pNext; | |
| 45 FX_Free(pTrunk); | |
| 46 pTrunk = pNext; | |
| 47 } | |
| 48 m_pFirstTrunk = NULL; | |
| 49 } | |
| 50 void* CFX_GrowOnlyPool::Alloc(size_t size) | |
| 51 { | |
| 52 size = (size + 3) / 4 * 4; | |
| 53 _FX_GrowOnlyTrunk* pTrunk = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; | |
| 54 while (pTrunk) { | |
| 55 if (pTrunk->m_Size - pTrunk->m_Allocated >= size) { | |
| 56 void* p = (FX_LPBYTE)(pTrunk + 1) + pTrunk->m_Allocated; | |
| 57 pTrunk->m_Allocated += size; | |
| 58 return p; | |
| 59 } | |
| 60 pTrunk = pTrunk->m_pNext; | |
| 61 } | |
| 62 size_t alloc_size = size > m_TrunkSize ? size : m_TrunkSize; | |
| 63 pTrunk = (_FX_GrowOnlyTrunk*)FX_Alloc(FX_BYTE, sizeof(_FX_GrowOnlyTrunk) + a
lloc_size); | |
| 64 pTrunk->m_Size = alloc_size; | |
| 65 pTrunk->m_Allocated = size; | |
| 66 pTrunk->m_pNext = (_FX_GrowOnlyTrunk*)m_pFirstTrunk; | |
| 67 m_pFirstTrunk = pTrunk; | |
| 68 return pTrunk + 1; | |
| 69 } | |
| OLD | NEW |