summaryrefslogtreecommitdiff
path: root/xfa/fgas/crt/fgas_memory.cpp
blob: 4334789cb7dd99805610d448c906c945f4b2427f (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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

#include "xfa/fgas/crt/fgas_memory.h"

#include <algorithm>

namespace {

class CFX_DefStore : public IFX_MEMAllocator, public CFX_Target {
 public:
  CFX_DefStore() {}
  ~CFX_DefStore() {}
  virtual void Release() { delete this; }
  virtual void* Alloc(size_t size) { return FX_Alloc(uint8_t, size); }
  virtual void Free(void* pBlock) { FX_Free(pBlock); }
  virtual size_t GetBlockSize() const { return 0; }
  virtual size_t GetDefChunkSize() const { return 0; }
  virtual size_t SetDefChunkSize(size_t size) { return 0; }
  virtual size_t GetCurrentDataSize() const { return 0; }
};

#if _FX_OS_ != _FX_ANDROID_
#pragma pack(push, 1)
#endif
struct FX_STATICSTORECHUNK {
  FX_STATICSTORECHUNK* pNextChunk;
  size_t iChunkSize;
  size_t iFreeSize;
};
#if _FX_OS_ != _FX_ANDROID_
#pragma pack(pop)
#endif

class CFX_StaticStore : public IFX_MEMAllocator, public CFX_Target {
 public:
  CFX_StaticStore(size_t iDefChunkSize = 4096);
  ~CFX_StaticStore();
  virtual void Release() { delete this; }
  virtual void* Alloc(size_t size);
  virtual void Free(void* pBlock) {}
  virtual size_t GetBlockSize() const { return 0; }
  virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
  virtual size_t SetDefChunkSize(size_t size);
  virtual size_t GetCurrentDataSize() const { return m_iAllocatedSize; }

 protected:
  size_t m_iAllocatedSize;
  size_t m_iDefChunkSize;
  FX_STATICSTORECHUNK* m_pChunk;
  FX_STATICSTORECHUNK* m_pLastChunk;
  FX_STATICSTORECHUNK* AllocChunk(size_t size);
  FX_STATICSTORECHUNK* FindChunk(size_t size);
};

#if _FX_OS_ != _FX_ANDROID_
#pragma pack(push, 1)
#endif
struct FX_FIXEDSTORECHUNK {
  uint8_t* FirstFlag() { return reinterpret_cast<uint8_t*>(this + 1); }
  uint8_t* FirstBlock() { return FirstFlag() + iChunkSize; }

  FX_FIXEDSTORECHUNK* pNextChunk;
  size_t iChunkSize;
  size_t iFreeNum;
};
#if _FX_OS_ != _FX_ANDROID_
#pragma pack(pop)
#endif

class CFX_FixedStore : public IFX_MEMAllocator, public CFX_Target {
 public:
  CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk);
  virtual ~CFX_FixedStore();
  virtual void Release() { delete this; }
  virtual void* Alloc(size_t size);
  virtual void Free(void* pBlock);
  virtual size_t GetBlockSize() const { return m_iBlockSize; }
  virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
  virtual size_t SetDefChunkSize(size_t iChunkSize);
  virtual size_t GetCurrentDataSize() const { return 0; }

 protected:
  FX_FIXEDSTORECHUNK* AllocChunk();

  size_t m_iBlockSize;
  size_t m_iDefChunkSize;
  FX_FIXEDSTORECHUNK* m_pChunk;
};

#if _FX_OS_ != _FX_ANDROID_
#pragma pack(push, 1)
#endif
struct FX_DYNAMICSTOREBLOCK {
  uint8_t* Data() { return reinterpret_cast<uint8_t*>(this + 1); }
  FX_DYNAMICSTOREBLOCK* NextBlock() {
    return reinterpret_cast<FX_DYNAMICSTOREBLOCK*>(Data() + iBlockSize);
  }
  size_t iBlockSize;
  FX_BOOL bUsed;
};

struct FX_DYNAMICSTORECHUNK {
  FX_DYNAMICSTOREBLOCK* FirstBlock() {
    return reinterpret_cast<FX_DYNAMICSTOREBLOCK*>(this + 1);
  }
  FX_DYNAMICSTORECHUNK* pNextChunk;
  size_t iChunkSize;
  size_t iFreeSize;
};
#if _FX_OS_ != _FX_ANDROID_
#pragma pack(pop)
#endif

class CFX_DynamicStore : public IFX_MEMAllocator, public CFX_Target {
 public:
  CFX_DynamicStore(size_t iDefChunkSize = 4096);
  virtual ~CFX_DynamicStore();
  virtual void Release() { delete this; }
  virtual void* Alloc(size_t size);
  virtual void Free(void* pBlock);
  virtual size_t GetBlockSize() const { return 0; }
  virtual size_t GetDefChunkSize() const { return m_iDefChunkSize; }
  virtual size_t SetDefChunkSize(size_t size);
  virtual size_t GetCurrentDataSize() const { return 0; }

 protected:
  FX_DYNAMICSTORECHUNK* AllocChunk(size_t size);

  size_t m_iDefChunkSize;
  FX_DYNAMICSTORECHUNK* m_pChunk;
};

}  // namespace

#define FX_4BYTEALIGN(size) (((size) + 3) / 4 * 4)

IFX_MEMAllocator* FX_CreateAllocator(FX_ALLOCTYPE eType,
                                     size_t chunkSize,
                                     size_t blockSize) {
  switch (eType) {
    case FX_ALLOCTYPE_Dynamic:
      return new CFX_DynamicStore(chunkSize);
    case FX_ALLOCTYPE_Default:
      return new CFX_DefStore();
    case FX_ALLOCTYPE_Static:
      return new CFX_StaticStore(chunkSize);
    case FX_ALLOCTYPE_Fixed:
      return new CFX_FixedStore(blockSize, chunkSize);
    default:
      return NULL;
  }
}
CFX_StaticStore::CFX_StaticStore(size_t iDefChunkSize)
    : m_iAllocatedSize(0),
      m_iDefChunkSize(iDefChunkSize),
      m_pChunk(NULL),
      m_pLastChunk(NULL) {
  ASSERT(m_iDefChunkSize != 0);
}
CFX_StaticStore::~CFX_StaticStore() {
  FX_STATICSTORECHUNK* pChunk = m_pChunk;
  while (pChunk) {
    FX_STATICSTORECHUNK* pNext = pChunk->pNextChunk;
    FX_Free(pChunk);
    pChunk = pNext;
  }
}
FX_STATICSTORECHUNK* CFX_StaticStore::AllocChunk(size_t size) {
  ASSERT(size != 0);
  FX_STATICSTORECHUNK* pChunk = (FX_STATICSTORECHUNK*)FX_Alloc(
      uint8_t, sizeof(FX_STATICSTORECHUNK) + size);
  pChunk->iChunkSize = size;
  pChunk->iFreeSize = size;
  pChunk->pNextChunk = NULL;
  if (m_pLastChunk == NULL) {
    m_pChunk = pChunk;
  } else {
    m_pLastChunk->pNextChunk = pChunk;
  }
  m_pLastChunk = pChunk;
  return pChunk;
}
FX_STATICSTORECHUNK* CFX_StaticStore::FindChunk(size_t size) {
  ASSERT(size != 0);
  if (m_pLastChunk == NULL || m_pLastChunk->iFreeSize < size) {
    return AllocChunk(std::max(m_iDefChunkSize, size));
  }
  return m_pLastChunk;
}
void* CFX_StaticStore::Alloc(size_t size) {
  size = FX_4BYTEALIGN(size);
  ASSERT(size != 0);
  FX_STATICSTORECHUNK* pChunk = FindChunk(size);
  ASSERT(pChunk->iFreeSize >= size);
  uint8_t* p = (uint8_t*)pChunk;
  p += sizeof(FX_STATICSTORECHUNK) + pChunk->iChunkSize - pChunk->iFreeSize;
  pChunk->iFreeSize -= size;
  m_iAllocatedSize += size;
  return p;
}
size_t CFX_StaticStore::SetDefChunkSize(size_t size) {
  ASSERT(size != 0);
  size_t v = m_iDefChunkSize;
  m_iDefChunkSize = size;
  return v;
}
CFX_FixedStore::CFX_FixedStore(size_t iBlockSize, size_t iBlockNumsInChunk)
    : m_iBlockSize(FX_4BYTEALIGN(iBlockSize)),
      m_iDefChunkSize(FX_4BYTEALIGN(iBlockNumsInChunk)),
      m_pChunk(NULL) {
  ASSERT(m_iBlockSize != 0 && m_iDefChunkSize != 0);
}
CFX_FixedStore::~CFX_FixedStore() {
  FX_FIXEDSTORECHUNK* pChunk = m_pChunk;
  while (pChunk) {
    FX_FIXEDSTORECHUNK* pNext = pChunk->pNextChunk;
    FX_Free(pChunk);
    pChunk = pNext;
  }
}
FX_FIXEDSTORECHUNK* CFX_FixedStore::AllocChunk() {
  int32_t iTotalSize = sizeof(FX_FIXEDSTORECHUNK) + m_iDefChunkSize +
                       m_iBlockSize * m_iDefChunkSize;
  FX_FIXEDSTORECHUNK* pChunk =
      (FX_FIXEDSTORECHUNK*)FX_Alloc(uint8_t, iTotalSize);
  if (pChunk == NULL) {
    return NULL;
  }
  FXSYS_memset(pChunk->FirstFlag(), 0, m_iDefChunkSize);
  pChunk->pNextChunk = m_pChunk;
  pChunk->iChunkSize = m_iDefChunkSize;
  pChunk->iFreeNum = m_iDefChunkSize;
  m_pChunk = pChunk;
  return pChunk;
}
void* CFX_FixedStore::Alloc(size_t size) {
  if (size > m_iBlockSize) {
    return NULL;
  }
  FX_FIXEDSTORECHUNK* pChunk = m_pChunk;
  while (pChunk != NULL) {
    if (pChunk->iFreeNum > 0) {
      break;
    }
    pChunk = pChunk->pNextChunk;
  }
  if (pChunk == NULL) {
    pChunk = AllocChunk();
  }
  ASSERT(pChunk != NULL);
  uint8_t* pFlags = pChunk->FirstFlag();
  size_t i = 0;
  for (; i < pChunk->iChunkSize; i++)
    if (pFlags[i] == 0) {
      break;
    }
  ASSERT(i < pChunk->iChunkSize);
  pFlags[i] = 1;
  pChunk->iFreeNum--;
  return pChunk->FirstBlock() + i * m_iBlockSize;
}
void CFX_FixedStore::Free(void* pBlock) {
  ASSERT(pBlock != NULL);
  FX_FIXEDSTORECHUNK* pPrior = NULL;
  FX_FIXEDSTORECHUNK* pChunk = m_pChunk;
  uint8_t* pStart = NULL;
  uint8_t* pEnd;
  while (pChunk != NULL) {
    pStart = pChunk->FirstBlock();
    if (pBlock >= pStart) {
      pEnd = pStart + m_iBlockSize * pChunk->iChunkSize;
      if (pBlock < pEnd) {
        break;
      }
    }
    pPrior = pChunk, pChunk = pChunk->pNextChunk;
  }
  ASSERT(pChunk != NULL);
  size_t iPos = ((uint8_t*)pBlock - pStart) / m_iBlockSize;
  ASSERT(iPos < pChunk->iChunkSize);
  uint8_t* pFlags = pChunk->FirstFlag();
  if (pFlags[iPos] == 0) {
    return;
  }
  pFlags[iPos] = 0;
  pChunk->iFreeNum++;
  if (pChunk->iFreeNum == pChunk->iChunkSize) {
    if (pPrior == NULL) {
      m_pChunk = pChunk->pNextChunk;
    } else {
      pPrior->pNextChunk = pChunk->pNextChunk;
    }
    FX_Free(pChunk);
  }
}
size_t CFX_FixedStore::SetDefChunkSize(size_t iChunkSize) {
  ASSERT(iChunkSize != 0);
  size_t v = m_iDefChunkSize;
  m_iDefChunkSize = FX_4BYTEALIGN(iChunkSize);
  return v;
}
CFX_DynamicStore::CFX_DynamicStore(size_t iDefChunkSize)
    : m_iDefChunkSize(iDefChunkSize), m_pChunk(NULL) {
  ASSERT(m_iDefChunkSize != 0);
}
CFX_DynamicStore::~CFX_DynamicStore() {
  FX_DYNAMICSTORECHUNK* pChunk = m_pChunk;
  while (pChunk) {
    FX_DYNAMICSTORECHUNK* pNext = pChunk->pNextChunk;
    FX_Free(pChunk);
    pChunk = pNext;
  }
}
FX_DYNAMICSTORECHUNK* CFX_DynamicStore::AllocChunk(size_t size) {
  ASSERT(size != 0);
  FX_DYNAMICSTORECHUNK* pChunk = (FX_DYNAMICSTORECHUNK*)FX_Alloc(
      uint8_t,
      sizeof(FX_DYNAMICSTORECHUNK) + sizeof(FX_DYNAMICSTOREBLOCK) * 2 + size);
  if (pChunk == NULL) {
    return NULL;
  }
  pChunk->iChunkSize = size;
  pChunk->iFreeSize = size;
  FX_DYNAMICSTOREBLOCK* pBlock = pChunk->FirstBlock();
  pBlock->iBlockSize = size;
  pBlock->bUsed = FALSE;
  pBlock = pBlock->NextBlock();
  pBlock->iBlockSize = 0;
  pBlock->bUsed = TRUE;
  if (m_pChunk != NULL && size >= m_iDefChunkSize) {
    FX_DYNAMICSTORECHUNK* pLast = m_pChunk;
    while (pLast->pNextChunk != NULL) {
      pLast = pLast->pNextChunk;
    }
    pLast->pNextChunk = pChunk;
    pChunk->pNextChunk = NULL;
  } else {
    pChunk->pNextChunk = m_pChunk;
    m_pChunk = pChunk;
  }
  return pChunk;
}
void* CFX_DynamicStore::Alloc(size_t size) {
  size = FX_4BYTEALIGN(size);
  ASSERT(size != 0);
  FX_DYNAMICSTORECHUNK* pChunk = m_pChunk;
  FX_DYNAMICSTOREBLOCK* pBlock = NULL;
  while (pChunk != NULL) {
    if (pChunk->iFreeSize >= size) {
      pBlock = pChunk->FirstBlock();
      FX_BOOL bFind = FALSE;
      while (pBlock->iBlockSize != 0) {
        if (!pBlock->bUsed && pBlock->iBlockSize >= size) {
          bFind = TRUE;
          break;
        }
        pBlock = pBlock->NextBlock();
      }
      if (bFind) {
        break;
      }
    }
    pChunk = pChunk->pNextChunk;
  }
  if (pChunk == NULL) {
    pChunk = AllocChunk(std::max(m_iDefChunkSize, size));
    pBlock = pChunk->FirstBlock();
  }
  ASSERT(pChunk != NULL && pBlock != NULL);
  size_t m = size + sizeof(FX_DYNAMICSTOREBLOCK);
  pBlock->bUsed = TRUE;
  if (pBlock->iBlockSize > m) {
    size_t n = pBlock->iBlockSize;
    pBlock->iBlockSize = size;
    FX_DYNAMICSTOREBLOCK* pNextBlock = pBlock->NextBlock();
    pNextBlock->bUsed = FALSE;
    pNextBlock->iBlockSize = n - size - sizeof(FX_DYNAMICSTOREBLOCK);
    pChunk->iFreeSize -= size + sizeof(FX_DYNAMICSTOREBLOCK);
  } else {
    pChunk->iFreeSize -= pBlock->iBlockSize;
  }
  return pBlock->Data();
}
void CFX_DynamicStore::Free(void* pBlock) {
  ASSERT(pBlock != NULL);
  FX_DYNAMICSTORECHUNK* pPriorChunk = NULL;
  FX_DYNAMICSTORECHUNK* pChunk = m_pChunk;
  while (pChunk != NULL) {
    if (pBlock > pChunk &&
        pBlock <= ((uint8_t*)pChunk + sizeof(FX_DYNAMICSTORECHUNK) +
                   pChunk->iChunkSize)) {
      break;
    }
    pPriorChunk = pChunk, pChunk = pChunk->pNextChunk;
  }
  ASSERT(pChunk != NULL);
  FX_DYNAMICSTOREBLOCK* pPriorBlock = NULL;
  FX_DYNAMICSTOREBLOCK* pFindBlock = pChunk->FirstBlock();
  while (pFindBlock->iBlockSize != 0) {
    if (pBlock == (void*)pFindBlock->Data()) {
      break;
    }
    pPriorBlock = pFindBlock;
    pFindBlock = pFindBlock->NextBlock();
  }
  ASSERT(pFindBlock->iBlockSize != 0 && pFindBlock->bUsed &&
         pBlock == (void*)pFindBlock->Data());
  pFindBlock->bUsed = FALSE;
  pChunk->iFreeSize += pFindBlock->iBlockSize;
  if (pPriorBlock == NULL) {
    pPriorBlock = pChunk->FirstBlock();
  } else if (pPriorBlock->bUsed) {
    pPriorBlock = pFindBlock;
  }
  pFindBlock = pPriorBlock;
  size_t sizeFree = 0;
  size_t sizeBlock = 0;
  while (pFindBlock->iBlockSize != 0 && !pFindBlock->bUsed) {
    if (pFindBlock != pPriorBlock) {
      sizeFree += sizeof(FX_DYNAMICSTOREBLOCK);
      sizeBlock += sizeof(FX_DYNAMICSTOREBLOCK);
    }
    sizeBlock += pFindBlock->iBlockSize;
    pFindBlock = pFindBlock->NextBlock();
  }
  pPriorBlock->iBlockSize = sizeBlock;
  pChunk->iFreeSize += sizeFree;
  if (pChunk->iFreeSize == pChunk->iChunkSize) {
    if (pPriorChunk == NULL) {
      m_pChunk = pChunk->pNextChunk;
    } else {
      pPriorChunk->pNextChunk = pChunk->pNextChunk;
    }
    FX_Free(pChunk);
  }
}
size_t CFX_DynamicStore::SetDefChunkSize(size_t size) {
  ASSERT(size != 0);
  size_t v = m_iDefChunkSize;
  m_iDefChunkSize = size;
  return v;
}