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

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

Issue 1143663004: Add safe FX_Alloc2D() macro (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Drop one file, indent. Created 5 years, 7 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 | « core/include/fxcrt/fx_basic.h ('k') | core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp » ('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"
(...skipping 23 matching lines...) Expand all
34 34
35 inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { 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). 36 // TODO(tsepez): See if we can avoid the implicit memset(0).
37 if (void* result = calloc(num_members, member_size)) { 37 if (void* result = calloc(num_members, member_size)) {
38 return result; 38 return result;
39 } 39 }
40 FX_OutOfMemoryTerminate(); // Never returns. 40 FX_OutOfMemoryTerminate(); // Never returns.
41 return nullptr; // Suppress compiler warning. 41 return nullptr; // Suppress compiler warning.
42 } 42 }
43 43
44 inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
45 if (w < std::numeric_limits<size_t>::max() / h) {
46 return FX_AllocOrDie(w * h, member_size);
47 }
48 FX_OutOfMemoryTerminate(); // Never returns.
49 return nullptr; // Suppress compiler warning.
50 }
51
44 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) { 52 inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
45 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { 53 if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
46 return result; 54 return result;
47 } 55 }
48 FX_OutOfMemoryTerminate(); // Never returns. 56 FX_OutOfMemoryTerminate(); // Never returns.
49 return nullptr; // Suppress compiler warning. 57 return nullptr; // Suppress compiler warning.
50 } 58 }
51 59
52 // Never returns NULL. 60 // Never returns NULL.
53 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type)) 61 #define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type))
62 #define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type))
54 #define FX_Realloc(type, ptr, size) \ 63 #define FX_Realloc(type, ptr, size) \
55 (type*)FX_ReallocOrDie(ptr, size, sizeof(type)) 64 (type*)FX_ReallocOrDie(ptr, size, sizeof(type))
56 65
57 // May return NULL. 66 // May return NULL.
58 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type)) 67 #define FX_TryAlloc(type, size) (type*)calloc(size, sizeof(type))
59 #define FX_TryRealloc(type, ptr, size) \ 68 #define FX_TryRealloc(type, ptr, size) \
60 (type*)FX_SafeRealloc(ptr, size, sizeof(type)) 69 (type*)FX_SafeRealloc(ptr, size, sizeof(type))
61 70
62 #define FX_Free(ptr) free(ptr) 71 #define FX_Free(ptr) free(ptr)
63 72
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 110
102 void FreeAll(); 111 void FreeAll();
103 private: 112 private:
104 113
105 size_t m_TrunkSize; 114 size_t m_TrunkSize;
106 115
107 void* m_pFirstTrunk; 116 void* m_pFirstTrunk;
108 }; 117 };
109 #endif // __cplusplust 118 #endif // __cplusplust
110 #endif // _FX_MEMORY_H_ 119 #endif // _FX_MEMORY_H_
OLDNEW
« no previous file with comments | « core/include/fxcrt/fx_basic.h ('k') | core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698