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 #ifndef CORE_INCLUDE_FXCRT_FX_MEMORY_H_
8 #define CORE_INCLUDE_FXCRT_FX_MEMORY_H_
9 
10 #include "fx_system.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 // For external C libraries to malloc through PDFium. These may return NULL.
16 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
17 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
18 void FXMEM_DefaultFree(void* pointer, int flags);
19 #ifdef __cplusplus
20 }  // extern "C"
21 
22 #include <stdlib.h>
23 #include <limits>
24 #include <new>
25 
26 NEVER_INLINE void FX_OutOfMemoryTerminate();
27 
FX_SafeRealloc(void * ptr,size_t num_members,size_t member_size)28 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
29   if (num_members < std::numeric_limits<size_t>::max() / member_size) {
30     return realloc(ptr, num_members * member_size);
31   }
32   return nullptr;
33 }
34 
FX_AllocOrDie(size_t num_members,size_t member_size)35 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
36   // TODO(tsepez): See if we can avoid the implicit memset(0).
37   if (void* result = calloc(num_members, member_size)) {
38     return result;
39   }
40   FX_OutOfMemoryTerminate();  // Never returns.
41   return nullptr;             // Suppress compiler warning.
42 }
43 
FX_AllocOrDie2D(size_t w,size_t h,size_t member_size)44 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
45   if (w < std::numeric_limits<size_t>::max() / h) {
46     return FX_AllocOrDie(w * h, member_size);
47   }
48   FX_OutOfMemoryTerminate();  // Never returns.
49   return nullptr;             // Suppress compiler warning.
50 }
51 
FX_ReallocOrDie(void * ptr,size_t num_members,size_t member_size)52 inline void* FX_ReallocOrDie(void* ptr,
53                              size_t num_members,
54                              size_t member_size) {
55   if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
56     return result;
57   }
58   FX_OutOfMemoryTerminate();  // Never returns.
59   return nullptr;             // Suppress compiler warning.
60 }
61 
62 // Never returns NULL.
63 #define FX_Alloc(type, size) (type*) FX_AllocOrDie(size, sizeof(type))
64 #define FX_Alloc2D(type, w, h) (type*) FX_AllocOrDie2D(w, h, sizeof(type))
65 #define FX_Realloc(type, ptr, size) \
66   (type*) FX_ReallocOrDie(ptr, size, sizeof(type))
67 
68 // May return NULL.
69 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type))
70 #define FX_TryRealloc(type, ptr, size) \
71   (type*) FX_SafeRealloc(ptr, size, sizeof(type))
72 
73 #define FX_Free(ptr) free(ptr)
74 
75 class CFX_DestructObject {
76  public:
~CFX_DestructObject()77   virtual ~CFX_DestructObject() {}
78 };
79 class CFX_GrowOnlyPool {
80  public:
81   CFX_GrowOnlyPool(size_t trunk_size = 16384);
82 
83   ~CFX_GrowOnlyPool();
84 
SetTrunkSize(size_t trunk_size)85   void SetTrunkSize(size_t trunk_size) { m_TrunkSize = trunk_size; }
86 
AllocDebug(size_t size,const FX_CHAR * file,int line)87   void* AllocDebug(size_t size, const FX_CHAR* file, int line) {
88     return Alloc(size);
89   }
90 
91   void* Alloc(size_t size);
92 
ReallocDebug(void * p,size_t new_size,const FX_CHAR * file,int line)93   void* ReallocDebug(void* p, size_t new_size, const FX_CHAR* file, int line) {
94     return NULL;
95   }
96 
Realloc(void * p,size_t new_size)97   void* Realloc(void* p, size_t new_size) { return NULL; }
98 
Free(void *)99   void Free(void*) {}
100 
101   void FreeAll();
102 
103  private:
104   size_t m_TrunkSize;
105 
106   void* m_pFirstTrunk;
107 };
108 #endif  // __cplusplus
109 
110 #endif  // CORE_INCLUDE_FXCRT_FX_MEMORY_H_
111