summaryrefslogtreecommitdiff
path: root/core/include/fxcrt/fx_memory.h
blob: cf795fa869a5efc0631686303d66e5c88c243bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef _FX_MEMORY_H_
#define _FX_MEMORY_H_
#ifndef _FX_SYSTEM_H_
#include "fx_system.h"
#endif
#define FXMEM_NONLEAVE			1
#define FXMEM_MOVABLE			2
#define FXMEM_DISCARDABLE		4
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _FXMEM_SystemMgr {

    void* (*Alloc)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags);

    void* (*AllocDebug)(struct _FXMEM_SystemMgr* pMgr, size_t size, int flags, FX_LPCSTR file, int line);

    void* (*Realloc)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags);

    void* (*ReallocDebug)(struct _FXMEM_SystemMgr* pMgr, void* pointer, size_t size, int flags, FX_LPCSTR file, int line);

    void* (*Lock)(struct _FXMEM_SystemMgr* pMgr, void* handle);

    void  (*Unlock)(struct _FXMEM_SystemMgr* pMgr, void* handle);

    void  (*Free)(struct _FXMEM_SystemMgr* pMgr, void* pointer, int flags);

    void  (*Purge)(struct _FXMEM_SystemMgr* pMgr);

    void  (*CollectAll)(struct _FXMEM_SystemMgr* pMgr);


    void* user;
} FXMEM_SystemMgr;
FX_DEFINEHANDLE(FXMEM_FoxitMgr)
typedef struct _FXMEM_SystemMgr2 {

    FX_BOOL	(*More)(struct _FXMEM_SystemMgr2* pMgr, size_t alloc_size, void** new_memory, size_t* new_size);

    void	(*Free)(struct _FXMEM_SystemMgr2* pMgr, void* memory);
} FXMEM_SystemMgr2;
FXMEM_FoxitMgr* FXMEM_CreateMemoryMgr(size_t size, FX_BOOL extensible);
void	FXMEM_DestroyFoxitMgr(FXMEM_FoxitMgr* pFoxitMgr);
void*	FXMEM_DefaultAlloc(size_t byte_size, int flags);
void*	FXMEM_DefaultAlloc2(size_t units, size_t unit_size, int flags);
void*	FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
void*	FXMEM_DefaultRealloc2(void* pointer, size_t units, size_t unit_size, int flags);
void	FXMEM_DefaultFree(void* pointer, int flags);
#define FX_Alloc(type, size)			(type*)FXMEM_DefaultAlloc2(size, sizeof(type), 0)
#define FX_Realloc(type, ptr, size)		(type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), 0)
#define FX_AllocNL(type, size)			(type*)FXMEM_DefaultAlloc2(size, sizeof(type), FXMEM_NONLEAVE)
#define FX_ReallocNL(type, ptr, size)	(type*)FXMEM_DefaultRealloc2(ptr, size, sizeof(type), FXMEM_NONLEAVE)
#define FX_Free(pointer) FXMEM_DefaultFree(pointer, 0)
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
#if defined(_DEBUG)
#define FX_NEW new(__FILE__, __LINE__)
#else

#define FX_NEW new
#endif
class CFX_Object
{
public:

    void*			operator new (size_t size, FX_LPCSTR file, int line);

    void			operator delete (void* p, FX_LPCSTR file, int line);

    void*			operator new (size_t size);

    void			operator delete (void* p);

    void*			operator new[] (size_t size, FX_LPCSTR file, int line);

    void			operator delete[] (void* p, FX_LPCSTR file, int line);

    void*			operator new[] (size_t size);

    void			operator delete[] (void* p);

    void*			operator new (size_t, void* buf)
    {
        return buf;
    }

    void			operator delete (void*, void*) {}
};
#define FX_NEW_VECTOR(Pointer, Class, Count) \
    { \
        Pointer = FX_Alloc(Class, Count); \
        if (Pointer) { \
            for (int i = 0; i < (Count); i ++) new (Pointer + i) Class; \
        } \
    }
#define FX_DELETE_VECTOR(Pointer, Class, Count) \
    { \
        for (int i = 0; i < (Count); i ++) Pointer[i].~Class(); \
        FX_Free(Pointer); \
    }
class CFX_DestructObject : public CFX_Object
{
public:

    virtual ~CFX_DestructObject() {}
};
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _IFX_Allocator {

    void*	(*m_AllocDebug)(struct _IFX_Allocator* pAllocator, size_t size, FX_LPCSTR file, int line);

    void*	(*m_Alloc)(struct _IFX_Allocator* pAllocator, size_t size);

    void*	(*m_ReallocDebug)(struct _IFX_Allocator* pAllocator, void* p, size_t size, FX_LPCSTR file, int line);

    void*	(*m_Realloc)(struct _IFX_Allocator* pAllocator, void* p, size_t size);

    void	(*m_Free)(struct _IFX_Allocator* pAllocator, void* p);
} IFX_Allocator;
IFX_Allocator* FXMEM_GetDefAllocator();
#ifdef __cplusplus
}
#endif
#ifdef _DEBUG

#define FX_Allocator_Alloc(fxAllocator, type, size) \
    ((fxAllocator) ? (type*)(fxAllocator)->m_AllocDebug((fxAllocator), (size) * sizeof(type), __FILE__, __LINE__) : (FX_Alloc(type, size)))

#define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
    ((fxAllocator) ? (type*)(fxAllocator)->m_ReallocDebug((fxAllocator), (ptr), (new_size) * sizeof(type), __FILE__, __LINE__) : (FX_Realloc(type, ptr, new_size)))
#else

#define FX_Allocator_Alloc(fxAllocator, type, size) \
    ((fxAllocator) ? (type*)(fxAllocator)->m_Alloc((fxAllocator), (size) * sizeof(type)) : (FX_Alloc(type, size)))

#define FX_Allocator_Realloc(fxAllocator, type, ptr, new_size) \
    ((fxAllocator) ? (type*)(fxAllocator)->m_Realloc((fxAllocator), (ptr), (new_size) * sizeof(type)) : (FX_Realloc(type, ptr, new_size)))
#endif
#define FX_Allocator_Free(fxAllocator, ptr) \
    ((fxAllocator) ? (fxAllocator)->m_Free((fxAllocator), (ptr)) : (FX_Free(ptr)))
inline void* operator new(size_t size, IFX_Allocator* fxAllocator)
{
    return (void*)FX_Allocator_Alloc(fxAllocator, FX_BYTE, size);
}
inline void operator delete(void* ptr, IFX_Allocator* fxAllocator)
{
}
#define FX_NewAtAllocator(fxAllocator) \
    ::new(fxAllocator)
#define FX_DeleteAtAllocator(pointer, fxAllocator, __class__) \
    (pointer)->~__class__(); \
    FX_Allocator_Free(fxAllocator, pointer)
class CFX_AllocObject
{
public:

    void*			operator new (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
#ifndef _FX_NO_EXCEPTION_

    void			operator delete (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line);
#endif

    void*			operator new (size_t size, IFX_Allocator* pAllocator);

    void			operator delete (void* p);
#ifndef _FX_NO_EXCEPTION_

    void			operator delete (void* p, IFX_Allocator* pAllocator);
#endif

    void*			operator new (size_t, void* buf)
    {
        return buf;
    }
#ifndef _FX_NO_EXCEPTION_

    void			operator delete (void*, void*) {}
#endif

    IFX_Allocator*	GetAllocator() const
    {
        return m_pAllocator;
    }
private:

    void*			operator new[] (size_t size, IFX_Allocator* pAllocator, FX_LPCSTR file, int line)
    {
        return operator new(size, pAllocator, file, line);
    }
#ifndef _FX_NO_EXCEPTION_

    void			operator delete[] (void* p, IFX_Allocator* pAllocator, FX_LPCSTR file, int line) {}
#endif

    void*			operator new[] (size_t size, IFX_Allocator* pAllocator)
    {
        return operator new(size, pAllocator);
    }

    void			operator delete[] (void* p) {}
#ifndef _FX_NO_EXCEPTION_

    void			operator delete[] (void* p, IFX_Allocator* pAllocator) {}
#endif
protected:

    IFX_Allocator*	m_pAllocator;
};
#if defined(_DEBUG)
#define FX_NEWAT(pAllocator) new(pAllocator, __FILE__, __LINE__)
#else

#define FX_NEWAT(pAllocator) new(pAllocator)
#endif
class CFX_GrowOnlyPool : public IFX_Allocator, public CFX_Object
{
public:

    CFX_GrowOnlyPool(IFX_Allocator* pAllocator = NULL, size_t trunk_size = 16384);

    ~CFX_GrowOnlyPool();

    void	SetAllocator(IFX_Allocator* pAllocator);

    void	SetTrunkSize(size_t trunk_size)
    {
        m_TrunkSize = trunk_size;
    }

    void*	AllocDebug(size_t size, FX_LPCSTR file, int line)
    {
        return Alloc(size);
    }

    void*	Alloc(size_t size);

    void*	ReallocDebug(void* p, size_t new_size, FX_LPCSTR file, int line)
    {
        return NULL;
    }

    void*	Realloc(void* p, size_t new_size)
    {
        return NULL;
    }

    void	Free(void*) {}

    void	FreeAll();
private:

    size_t	m_TrunkSize;

    void*	m_pFirstTrunk;

    IFX_Allocator*	m_pAllocator;
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define FX_FIXEDMEM_PAGESIZE		(4096 * 16)
#define FX_FIXEDMEM_MIDBLOCKSIZE	(4096)
typedef struct _FX_MEMCONFIG {

    size_t	nPageNum_Init8;

    size_t	nPageNum_Init16;

    size_t	nPageNum_Init32;

    size_t	nPageNum_More16;

    size_t	nPageNum_More32;

    size_t	nPageSize_Mid;

    size_t	nPageNum_InitMid;

    size_t	nPageNum_MoreMid;

    size_t	nPageSize_Large;

    size_t	nPageSize_Alone;
} FX_MEMCONFIG;
void	FXMEM_SetConfig(const FX_MEMCONFIG* memConfig);
#ifdef __cplusplus
}
#endif
#endif