Home | History | Annotate | Download | only in fxcrt
      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 
     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 
     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 
     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 
     52 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
     53     if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
     54         return result;
     55     }
     56     FX_OutOfMemoryTerminate();  // Never returns.
     57     return nullptr;  // Suppress compiler warning.
     58 }
     59 
     60 // Never returns NULL.
     61 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type))
     62 #define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type))
     63 #define FX_Realloc(type, ptr, size) \
     64     (type*)FX_ReallocOrDie(ptr, size, sizeof(type))
     65 
     66 // May return NULL.
     67 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type))
     68 #define FX_TryRealloc(type, ptr, size) \
     69     (type*)FX_SafeRealloc(ptr, size, sizeof(type))
     70 
     71 #define FX_Free(ptr) free(ptr)
     72 
     73 class CFX_DestructObject
     74 {
     75 public:
     76 
     77     virtual ~CFX_DestructObject() {}
     78 };
     79 class CFX_GrowOnlyPool
     80 {
     81 public:
     82 
     83     CFX_GrowOnlyPool(size_t trunk_size = 16384);
     84 
     85     ~CFX_GrowOnlyPool();
     86 
     87     void	SetTrunkSize(size_t trunk_size)
     88     {
     89         m_TrunkSize = trunk_size;
     90     }
     91 
     92     void*	AllocDebug(size_t size, FX_LPCSTR file, int line)
     93     {
     94         return Alloc(size);
     95     }
     96 
     97     void*	Alloc(size_t size);
     98 
     99     void*	ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line)
    100     {
    101         return NULL;
    102     }
    103 
    104     void*	Realloc(void* p, size_t new_size)
    105     {
    106         return NULL;
    107     }
    108 
    109     void	Free(void*) {}
    110 
    111     void	FreeAll();
    112 private:
    113 
    114     size_t	m_TrunkSize;
    115 
    116     void*	m_pFirstTrunk;
    117 };
    118 #endif  // __cplusplus
    119 
    120 #endif  // CORE_INCLUDE_FXCRT_FX_MEMORY_H_
    121