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 _FX_MEMORY_H_
      8 #define _FX_MEMORY_H_
      9 #ifndef _FX_SYSTEM_H_
     10 #include "fx_system.h"
     11 #endif
     12 #define FXMEM_NONLEAVE			1
     13 #define FXMEM_MOVABLE			2
     14 #define FXMEM_DISCARDABLE		4
     15 #ifdef __cplusplus
     16 extern "C" {
     17 #endif
     18 typedef struct _FXMEM_SystemMgr {
     19 
     20     void* (*Alloc)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags);
     21 
     22     void* (*AllocDebug)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line);
     23 
     24     void* (*Realloc)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags);
     25 
     26     void* (*ReallocDebug)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line);
     27 
     28     void* (*Lock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
     29 
     30     void  (*Unlock)(struct _FXMEM_SystemMgr* pMgr, void* handle);
     31 
     32     void  (*Free)(struct _FXMEM_SystemMgr* pMgr, void* pointer, int flags);
     33 
     34     void  (*Purge)(struct _FXMEM_SystemMgr* pMgr);
     35 
     36     void  (*CollectAll)(struct _FXMEM_SystemMgr* pMgr);
     37 
     38 
     39     void* user;
     40 } FXMEM_SystemMgr;
     41 FX_DEFINEHANDLE(FXMEM_FoxitMgr)
     42 typedef struct _FXMEM_SystemMgr2 {
     43 
     44     FX_BOOL	(*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);
     45 
     46     void	(*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
     47 } FXMEM_SystemMgr2;
     48 FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible);
     49 void	FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr);
     50 void*	FXMEM_DefaultAlloc(size_t byte_size, int flags);
     51 void*	FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags);
     52 void*	FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
     53 void*	FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
     54 void	FXMEM_DefaultFree(void* pointer, int flags);
     55 #define FX_Alloc(type, size)			(type*)FXMEM_DefaultAlloc2(size, sizeof(type), 0)
     56 #define FX_Realloc(type, ptr, size)		(type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), 0)
     57 #define FX_AllocNL(type, size)			(type*)FXMEM_DefaultAlloc2(size, sizeof(type), FXMEM_NONLEAVE)
     58 #define FX_ReallocNL(type, ptr, size)	(type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), FXMEM_NONLEAVE)
     59 #define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0)
     60 #ifdef __cplusplus
     61 }
     62 #endif
     63 #ifdef __cplusplus
     64 #if defined(_DEBUG)
     65 #define FX_NEW new(__FILE__, __LINE__)
     66 #else
     67 
     68 #define FX_NEW new
     69 #endif
     70 class CFX_Object
     71 {
     72 public:
     73 
     74     void*			operator new (size_t size, FX_LPCSTR file, int line);
     75 
     76     void			operator delete (void* p, FX_LPCSTR file, int line);
     77 
     78     void*			operator new (size_t size);
     79 
     80     void			operator delete (void* p);
     81 
     82     void*			operator new[] (size_t size, FX_LPCSTR file, int line);
     83 
     84     void			operator delete[] (void* p, FX_LPCSTR file, int line);
     85 
     86     void*			operator new[] (size_t size);
     87 
     88     void			operator delete[] (void* p);
     89 
     90     void*			operator new (size_t, void* buf)
     91     {
     92         return buf;
     93     }
     94 
     95     void			operator delete (void*, void*) {}
     96 };
     97 #define FX_NEW_VECTOR(Pointer, Class, Count) \
     98     { \
     99         Pointer = FX_Alloc(Class, Count); \
    100         if (Pointer) { \
    101             for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
    102         } \
    103     }
    104 #define FX_DELETE_VECTOR(Pointer, Class, Count) \
    105     { \
    106         for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
    107         FX_Free(Pointer); \
    108     }
    109 class CFX_DestructObject : public CFX_Object
    110 {
    111 public:
    112 
    113     virtual ~CFX_DestructObject() {}
    114 };
    115 #ifdef __cplusplus
    116 extern "C" {
    117 #endif
    118 typedef struct _IFX_Allocator {
    119 
    120     void*	(*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);
    121 
    122     void*	(*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);
    123 
    124     void*	(*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);
    125 
    126     void*	(*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);
    127 
    128     void	(*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
    129 } IFX_Allocator;
    130 IFX_Allocator* FXMEM_GetDefAllocator();
    131 #ifdef __cplusplus
    132 }
    133 #endif
    134 #ifdef _DEBUG
    135 
    136 #define FX_Allocator_Alloc(fxAllocator, type, size) \
    137     ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))
    138 
    139 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
    140     ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
    141 #else
    142 
    143 #define FX_Allocator_Alloc(fxAllocator, type, size) \
    144     ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))
    145 
    146 #define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
    147     ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
    148 #endif
    149 #define FX_Allocator_Free(fxAllocator, ptr) \
    150     ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
    151 inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
    152 {
    153     return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
    154 }
    155 inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
    156 {
    157 }
    158 #define FX_NewAtAllocator(fxAllocator) \
    159     ::new(fxAllocator)
    160 #define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
    161     (pointer)->~__class__(); \
    162     FX_Allocator_Free(fxAllocator, pointer)
    163 class CFX_AllocObject
    164 {
    165 public:
    166 
    167     void*			operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
    168 #ifndef _FX_NO_EXCEPTION_
    169 
    170     void			operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
    171 #endif
    172 
    173     void*			operator new (size_t size, IFX_Allocator* pAllocator);
    174 
    175     void			operator delete (void* p);
    176 #ifndef _FX_NO_EXCEPTION_
    177 
    178     void			operator delete (void* p, IFX_Allocator* pAllocator);
    179 #endif
    180 
    181     void*			operator new (size_t, void* buf)
    182     {
    183         return buf;
    184     }
    185 #ifndef _FX_NO_EXCEPTION_
    186 
    187     void			operator delete (void*, void*) {}
    188 #endif
    189 
    190     IFX_Allocator*	GetAllocator() const
    191     {
    192         return m_pAllocator;
    193     }
    194 private:
    195 
    196     void*			operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line)
    197     {
    198         return operator new(size, pAllocator, file, line);
    199     }
    200 #ifndef _FX_NO_EXCEPTION_
    201 
    202     void			operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
    203 #endif
    204 
    205     void*			operator new[] (size_t size, IFX_Allocator* pAllocator)
    206     {
    207         return operator new(size, pAllocator);
    208     }
    209 
    210     void			operator delete[] (void* p) {}
    211 #ifndef _FX_NO_EXCEPTION_
    212 
    213     void			operator delete[] (void* p, IFX_Allocator* pAllocator) {}
    214 #endif
    215 protected:
    216 
    217     IFX_Allocator*	m_pAllocator;
    218 };
    219 #if defined(_DEBUG)
    220 #define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
    221 #else
    222 
    223 #define FX_NEWAT(pAllocator) new(pAllocator)
    224 #endif
    225 class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
    226 {
    227 public:
    228 
    229     CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);
    230 
    231     ~CFX_GrowOnlyPool();
    232 
    233     void	SetAllocator(IFX_Allocator* pAllocator);
    234 
    235     void	SetTrunkSize(size_t trunk_size)
    236     {
    237         m_TrunkSize = trunk_size;
    238     }
    239 
    240     void*	AllocDebug(size_t size, FX_LPCSTR file, int line)
    241     {
    242         return Alloc(size);
    243     }
    244 
    245     void*	Alloc(size_t size);
    246 
    247     void*	ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line)
    248     {
    249         return NULL;
    250     }
    251 
    252     void*	Realloc(void* p, size_t new_size)
    253     {
    254         return NULL;
    255     }
    256 
    257     void	Free(void*) {}
    258 
    259     void	FreeAll();
    260 private:
    261 
    262     size_t	m_TrunkSize;
    263 
    264     void*	m_pFirstTrunk;
    265 
    266     IFX_Allocator*	m_pAllocator;
    267 };
    268 #endif
    269 #ifdef __cplusplus
    270 extern "C" {
    271 #endif
    272 #define FX_FIXEDMEM_PAGESIZE		(4096 * 16)
    273 #define FX_FIXEDMEM_MIDBLOCKSIZE	(4096)
    274 typedef struct _FX_MEMCONFIG {
    275 
    276     size_t	nPageNum_Init8;
    277 
    278     size_t	nPageNum_Init16;
    279 
    280     size_t	nPageNum_Init32;
    281 
    282     size_t	nPageNum_More16;
    283 
    284     size_t	nPageNum_More32;
    285 
    286     size_t	nPageSize_Mid;
    287 
    288     size_t	nPageNum_InitMid;
    289 
    290     size_t	nPageNum_MoreMid;
    291 
    292     size_t	nPageSize_Large;
    293 
    294     size_t	nPageSize_Alone;
    295 } FX_MEMCONFIG;
    296 void	FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
    297 #ifdef __cplusplus
    298 }
    299 #endif
    300 #endif
    301