Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: core/include/fxcrt/fx_memory.h

Issue 1226403008: Merge to M44: Abort on OOM by default in FX_Alloc(). (Closed) Base URL: https://pdfium.googlesource.com/pdfium@2403
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « BUILD.gn ('k') | core/include/fxcrt/fx_system.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 11
12 #ifdef __cplusplus 12 #ifdef __cplusplus
13 #include <new>
14 extern "C" { 13 extern "C" {
15 #endif 14 #endif
16 #define FX_Alloc(type, size)» » » » » » (type*)c alloc(size, sizeof(type)) 15 // For external C libraries to malloc through PDFium. These may return NULL.
17 #define FX_Realloc(type, ptr, size)» » » » » (type*)r ealloc(ptr, sizeof(type) * (size))
18 #define FX_AllocNL(type, size)» » » » » » FX_Alloc (type, size)
19 #define FX_ReallocNL(type, ptr, size)» » » » FX_Realloc(type, ptr, size)
20 #define FX_Free(ptr)» » » » » » » » free(ptr)
21 void* FXMEM_DefaultAlloc(size_t byte_size, int flags); 16 void* FXMEM_DefaultAlloc(size_t byte_size, int flags);
22 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags); 17 void* FXMEM_DefaultRealloc(void* pointer, size_t new_size, int flags);
23 void FXMEM_DefaultFree(void* pointer, int flags); 18 void FXMEM_DefaultFree(void* pointer, int flags);
24 #ifdef __cplusplus 19 #ifdef __cplusplus
20 } // extern "C"
21
22 #include <stdlib.h>
23 #include <limits>
24 #include <new>
25
26 NEVER_INLINE void FX_OutOfMemoryTerminate();
27
28 inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) {
29 if (num_members < std::numeric_limits<size_t>::max() / member_size) {
30 return realloc(ptr, num_members * member_size);
31 }
32 return nullptr;
33 }
34
35 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
36 // TODO(tsepez): See if we can avoid the implicit memset(0).
37 if (void* result = calloc(num_members, member_size)) {
38 return result;
39 }
40 FX_OutOfMemoryTerminate(); // Never returns.
41 return nullptr; // Suppress compiler warning.
25 } 42 }
26 43
27 #define FX_NEW_VECTOR(Pointer, Class, Count) (Pointer = new Class[Count]) 44 #define FX_NEW_VECTOR(Pointer, Class, Count) (Pointer = new Class[Count])
28 #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer 45 #define FX_DELETE_VECTOR(Pointer, Class, Count) delete[] Pointer
29 46
47 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
48 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
49 return result;
50 }
51 FX_OutOfMemoryTerminate(); // Never returns.
52 return nullptr; // Suppress compiler warning.
53 }
54
55 // Never returns NULL.
56 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type))
57 #define FX_Realloc(type, ptr, size) \
58 (type*)FX_ReallocOrDie(ptr, size, sizeof(type))
59
60 // May return NULL.
61 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type))
62 #define FX_TryRealloc(type, ptr, size) \
63 (type*)FX_SafeRealloc(ptr, size, sizeof(type))
64
65 #define FX_Free(ptr) free(ptr)
66
30 class CFX_DestructObject 67 class CFX_DestructObject
31 { 68 {
32 public: 69 public:
33 70
34 virtual ~CFX_DestructObject() {} 71 virtual ~CFX_DestructObject() {}
35 }; 72 };
36 class CFX_GrowOnlyPool 73 class CFX_GrowOnlyPool
37 { 74 {
38 public: 75 public:
39 76
(...skipping 25 matching lines...) Expand all
65 102
66 void Free(void*) {} 103 void Free(void*) {}
67 104
68 void FreeAll(); 105 void FreeAll();
69 private: 106 private:
70 107
71 size_t m_TrunkSize; 108 size_t m_TrunkSize;
72 109
73 void* m_pFirstTrunk; 110 void* m_pFirstTrunk;
74 }; 111 };
75 #endif 112 #endif // __cplusplust
76 #endif 113 #endif // _FX_MEMORY_H_
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | core/include/fxcrt/fx_system.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698