OLD | NEW |
| (Empty) |
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 "core/include/fxcrt/fx_system.h" | |
11 | |
12 #ifdef __cplusplus | |
13 extern "C" { | |
14 #endif | |
15 | |
16 // For external C libraries to malloc through PDFium. These may return NULL. | |
17 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); | |
18 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); | |
19 void FXMEM_DefaultFree(void* pointer, int flags); | |
20 | |
21 #ifdef __cplusplus | |
22 } // extern "C" | |
23 | |
24 #include <stdlib.h> | |
25 #include <limits> | |
26 #include <new> | |
27 | |
28 NEVER_INLINE void FX_OutOfMemoryTerminate(); | |
29 | |
30 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { | |
31 if (num_members < std::numeric_limits<size_t>::max() / member_size) { | |
32 return realloc(ptr, num_members * member_size); | |
33 } | |
34 return nullptr; | |
35 } | |
36 | |
37 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { | |
38 // TODO(tsepez): See if we can avoid the implicit memset(0). | |
39 if (void* result = calloc(num_members, member_size)) { | |
40 return result; | |
41 } | |
42 FX_OutOfMemoryTerminate(); // Never returns. | |
43 return nullptr; // Suppress compiler warning. | |
44 } | |
45 | |
46 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) { | |
47 if (w < std::numeric_limits<size_t>::max() / h) { | |
48 return FX_AllocOrDie(w * h, member_size); | |
49 } | |
50 FX_OutOfMemoryTerminate(); // Never returns. | |
51 return nullptr; // Suppress compiler warning. | |
52 } | |
53 | |
54 inline void* FX_ReallocOrDie(void* ptr, | |
55 size_t num_members, | |
56 size_t member_size) { | |
57 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { | |
58 return result; | |
59 } | |
60 FX_OutOfMemoryTerminate(); // Never returns. | |
61 return nullptr; // Suppress compiler warning. | |
62 } | |
63 | |
64 // Never returns NULL. | |
65 #define FX_Alloc(type, size) (type*) FX_AllocOrDie(size, sizeof(type)) | |
66 #define FX_Alloc2D(type, w, h) (type*) FX_AllocOrDie2D(w, h, sizeof(type)) | |
67 #define FX_Realloc(type, ptr, size) \ | |
68 (type*) FX_ReallocOrDie(ptr, size, sizeof(type)) | |
69 | |
70 // May return NULL. | |
71 #define FX_TryAlloc(type, size) (type*) calloc(size, sizeof(type)) | |
72 #define FX_TryRealloc(type, ptr, size) \ | |
73 (type*) FX_SafeRealloc(ptr, size, sizeof(type)) | |
74 | |
75 #define FX_Free(ptr) free(ptr) | |
76 | |
77 class CFX_DestructObject { | |
78 public: | |
79 virtual ~CFX_DestructObject() {} | |
80 }; | |
81 | |
82 #endif // __cplusplus | |
83 | |
84 #endif // CORE_INCLUDE_FXCRT_FX_MEMORY_H_ | |
OLD | NEW |