diff options
-rw-r--r-- | core/fxcrt/fx_memory.cpp | 5 | ||||
-rw-r--r-- | core/fxcrt/fx_memory.h | 32 |
2 files changed, 19 insertions, 18 deletions
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp index 589a4cf508..6a592a12d2 100644 --- a/core/fxcrt/fx_memory.cpp +++ b/core/fxcrt/fx_memory.cpp @@ -26,8 +26,9 @@ void FXMEM_InitializePartitionAlloc() { // TODO(palmer): Remove the |flags| argument. void* FXMEM_DefaultAlloc(size_t byte_size, int flags) { - return pdfium::base::PartitionAllocGeneric(gGeneralPartitionAllocator.root(), - byte_size, "GeneralPartition"); + return pdfium::base::PartitionAllocGenericFlags( + gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull, + byte_size, "GeneralPartition"); } void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size) { diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h index fdf64db190..65cf19c980 100644 --- a/core/fxcrt/fx_memory.h +++ b/core/fxcrt/fx_memory.h @@ -40,22 +40,23 @@ NEVER_INLINE void FX_OutOfMemoryTerminate(); inline void* FX_SafeAlloc(size_t num_members, size_t member_size) { FX_SAFE_SIZE_T total = member_size; total *= num_members; - if (!total.IsValid()) { + if (!total.IsValid()) return nullptr; - } - void* result = pdfium::base::PartitionAllocGeneric( - gGeneralPartitionAllocator.root(), total.ValueOrDie(), - "GeneralPartition"); - memset(result, 0, total.ValueOrDie()); + + void* result = pdfium::base::PartitionAllocGenericFlags( + gGeneralPartitionAllocator.root(), pdfium::base::PartitionAllocReturnNull, + total.ValueOrDie(), "GeneralPartition"); + if (result) + memset(result, 0, total.ValueOrDie()); return result; } inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { FX_SAFE_SIZE_T size = num_members; size *= member_size; - if (!size.IsValid()) { + if (!size.IsValid()) return nullptr; - } + return pdfium::base::PartitionReallocGeneric( gGeneralPartitionAllocator.root(), ptr, size.ValueOrDie(), "GeneralPartition"); @@ -63,17 +64,17 @@ inline void* FX_SafeRealloc(void* ptr, size_t num_members, size_t member_size) { inline void* FX_AllocOrDie(size_t num_members, size_t member_size) { // TODO(tsepez): See if we can avoid the implicit memset(0). - if (void* result = FX_SafeAlloc(num_members, member_size)) { + if (void* result = FX_SafeAlloc(num_members, member_size)) return result; - } + FX_OutOfMemoryTerminate(); // Never returns. return nullptr; // Suppress compiler warning. } inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) { - if (w < std::numeric_limits<size_t>::max() / h) { + if (w < std::numeric_limits<size_t>::max() / h) return FX_AllocOrDie(w * h, member_size); - } + FX_OutOfMemoryTerminate(); // Never returns. return nullptr; // Suppress compiler warning. } @@ -81,9 +82,9 @@ inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) { inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) { - if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) { + if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) return result; - } + FX_OutOfMemoryTerminate(); // Never returns. return nullptr; // Suppress compiler warning. } @@ -111,9 +112,8 @@ inline void FX_Free(void* ptr) { // // So this check is hiding (what I consider to be) bugs, and we should try to // fix them. https://bugs.chromium.org/p/pdfium/issues/detail?id=690 - if (ptr) { + if (ptr) pdfium::base::PartitionFree(ptr); - } } // The FX_ArraySize(arr) macro returns the # of elements in an array arr. |