Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_memory_malloc.cpp
Go to the documentation of this file.
1// Copyright 2022 The PDFium Authors
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#include "core/fxcrt/fx_memory.h"
8
9#include <stdlib.h>
10
11#include <limits>
12
13#include "build/build_config.h"
14#include "core/fxcrt/fx_safe_types.h"
15
16#if defined(PDF_USE_PARTITION_ALLOC)
17#error "File compiled under wrong build option."
18#endif
19
20namespace pdfium {
21namespace internal {
22
23// Slightly less than 2GB, typically.
24constexpr size_t kMallocSizeLimit = std::numeric_limits<int>::max() - (1 << 12);
25
26void* Alloc(size_t num_members, size_t member_size) {
27 FX_SAFE_SIZE_T total = member_size;
28 total *= num_members;
29 if (!total.IsValid() || total.ValueOrDie() >= kMallocSizeLimit)
30 return nullptr;
31 return malloc(total.ValueOrDie());
32}
33
34void* Calloc(size_t num_members, size_t member_size) {
35 FX_SAFE_SIZE_T total = member_size;
36 total *= num_members;
37 if (!total.IsValid() || total.ValueOrDie() >= kMallocSizeLimit)
38 return nullptr;
39 return calloc(num_members, member_size);
40}
41
42void* Realloc(void* ptr, size_t num_members, size_t member_size) {
43 FX_SAFE_SIZE_T total = num_members;
44 total *= member_size;
45 if (!total.IsValid() || total.ValueOrDie() >= kMallocSizeLimit)
46 return nullptr;
47 return realloc(ptr, total.ValueOrDie());
48}
49
50void Dealloc(void* ptr) {
51 free(ptr);
52}
53
54void* StringAlloc(size_t num_members, size_t member_size) {
55 FX_SAFE_SIZE_T total = member_size;
56 total *= num_members;
57 if (!total.IsValid())
58 return nullptr;
59 return malloc(total.ValueOrDie());
60}
61
62void StringDealloc(void* ptr) {
63 free(ptr);
64}
65
66} // namespace internal
67} // namespace pdfium
68
70
71void* FX_ArrayBufferAllocate(size_t length) {
72 void* result = calloc(length, 1);
73 if (!result)
74 FX_OutOfMemoryTerminate(length);
75 return result;
76}
77
79 void* result = malloc(length);
80 if (!result)
81 FX_OutOfMemoryTerminate(length);
82 return result;
83}
84
85void FX_ArrayBufferFree(void* data) {
86 free(data);
87}
void * FX_ArrayBufferAllocate(size_t length)
void FX_InitializeMemoryAllocators()
void FX_ArrayBufferFree(void *data)
void * FX_ArrayBufferAllocateUninitialized(size_t length)
void * Alloc(size_t num_members, size_t member_size)
void * StringAlloc(size_t num_members, size_t member_size)
void * Realloc(void *ptr, size_t num_members, size_t member_size)
void StringDealloc(void *ptr)
constexpr size_t kMallocSizeLimit
void * Calloc(size_t num_members, size_t member_size)
void Dealloc(void *ptr)