• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FXCRT_FX_MEMORY_H_
8 #define CORE_FXCRT_FX_MEMORY_H_
9 
10 #include <stddef.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 // For external C libraries to malloc through PDFium. These may return nullptr.
17 void* FXMEM_DefaultAlloc(size_t byte_size);
18 void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size);
19 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size);
20 void FXMEM_DefaultFree(void* pointer);
21 
22 #ifdef __cplusplus
23 }  // extern "C"
24 
25 #include "third_party/base/allocator/partition_allocator/partition_alloc.h"
26 
27 pdfium::base::PartitionAllocatorGeneric& GetArrayBufferPartitionAllocator();
28 pdfium::base::PartitionAllocatorGeneric& GetGeneralPartitionAllocator();
29 pdfium::base::PartitionAllocatorGeneric& GetStringPartitionAllocator();
30 
31 void FXMEM_InitializePartitionAlloc();
32 NOINLINE void FX_OutOfMemoryTerminate();
33 
34 // These never return nullptr, and must return cleared memory.
35 #define FX_Alloc(type, size) \
36   static_cast<type*>(FX_AllocOrDie(size, sizeof(type)))
37 #define FX_Alloc2D(type, w, h) \
38   static_cast<type*>(FX_AllocOrDie2D(w, h, sizeof(type)))
39 #define FX_Realloc(type, ptr, size) \
40   static_cast<type*>(FX_ReallocOrDie(ptr, size, sizeof(type)))
41 
42 // May return nullptr, but returns cleared memory otherwise.
43 #define FX_TryAlloc(type, size) \
44   static_cast<type*>(FX_SafeAlloc(size, sizeof(type)))
45 #define FX_TryRealloc(type, ptr, size) \
46   static_cast<type*>(FX_SafeRealloc(ptr, size, sizeof(type)))
47 
48 void* FX_SafeAlloc(size_t num_members, size_t member_size);
49 void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size);
50 void* FX_AllocOrDie(size_t num_members, size_t member_size);
51 void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size);
52 void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
53 void FX_Free(void* ptr);
54 
55 // The FX_ArraySize(arr) macro returns the # of elements in an array arr.
56 // The expression is a compile-time constant, and therefore can be
57 // used in defining new arrays, for example.  If you use FX_ArraySize on
58 // a pointer by mistake, you will get a compile-time error.
59 //
60 // One caveat is that FX_ArraySize() doesn't accept any array of an
61 // anonymous type or a type defined inside a function.
62 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array)))
63 
64 // This template function declaration is used in defining FX_ArraySize.
65 // Note that the function doesn't need an implementation, as we only
66 // use its type.
67 template <typename T, size_t N>
68 char (&ArraySizeHelper(T (&array)[N]))[N];
69 
70 // Round up to the power-of-two boundary N.
71 template <int N, typename T>
FxAlignToBoundary(T size)72 inline T FxAlignToBoundary(T size) {
73   static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
74   return (size + (N - 1)) & ~(N - 1);
75 }
76 
77 #endif  // __cplusplus
78 
79 #endif  // CORE_FXCRT_FX_MEMORY_H_
80