OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef _FX_MEMORY_H_ | 7 #ifndef _FX_MEMORY_H_ |
8 #define _FX_MEMORY_H_ | 8 #define _FX_MEMORY_H_ |
9 | 9 |
10 #include "fx_system.h" | 10 #include "fx_system.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { | 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). | 38 // TODO(tsepez): See if we can avoid the implicit memset(0). |
39 if (void* result = calloc(num_members, member_size)) { | 39 if (void* result = calloc(num_members, member_size)) { |
40 return result; | 40 return result; |
41 } | 41 } |
42 FX_OutOfMemoryTerminate(); // Never returns. | 42 FX_OutOfMemoryTerminate(); // Never returns. |
43 return nullptr; // Suppress compiler warning. | 43 return nullptr; // Suppress compiler warning. |
44 } | 44 } |
45 | 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 |
46 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
{ | 54 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
{ |
47 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { | 55 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { |
48 return result; | 56 return result; |
49 } | 57 } |
50 FX_OutOfMemoryTerminate(); // Never returns. | 58 FX_OutOfMemoryTerminate(); // Never returns. |
51 return nullptr; // Suppress compiler warning. | 59 return nullptr; // Suppress compiler warning. |
52 } | 60 } |
53 | 61 |
54 // Never returns NULL. | 62 // Never returns NULL. |
55 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) | 63 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) |
| 64 #define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type)) |
56 #define FX_Realloc(type, ptr, size) \ | 65 #define FX_Realloc(type, ptr, size) \ |
57 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) | 66 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) |
58 | 67 |
59 // May return NULL. | 68 // May return NULL. |
60 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) | 69 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) |
61 #define FX_TryRealloc(type, ptr, size) \ | 70 #define FX_TryRealloc(type, ptr, size) \ |
62 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) | 71 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) |
63 | 72 |
64 #define FX_Free(ptr) free(ptr) | 73 #define FX_Free(ptr) free(ptr) |
65 | 74 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 112 |
104 void FreeAll(); | 113 void FreeAll(); |
105 private: | 114 private: |
106 | 115 |
107 size_t m_TrunkSize; | 116 size_t m_TrunkSize; |
108 | 117 |
109 void* m_pFirstTrunk; | 118 void* m_pFirstTrunk; |
110 }; | 119 }; |
111 #endif // __cplusplust | 120 #endif // __cplusplust |
112 #endif // _FX_MEMORY_H_ | 121 #endif // _FX_MEMORY_H_ |
OLD | NEW |