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.h
Go to the documentation of this file.
1// Copyright 2014 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#ifndef CORE_FXCRT_FX_MEMORY_H_
8#define CORE_FXCRT_FX_MEMORY_H_
9
10#include <stddef.h>
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16// For external C libraries to malloc through PDFium. These may return nullptr.
17void* FXMEM_DefaultAlloc(size_t byte_size);
18void* FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size);
19void* FXMEM_DefaultRealloc(void* pointer, size_t new_size);
20void FXMEM_DefaultFree(void* pointer);
21
22#ifdef __cplusplus
23} // extern "C"
24
25#include "core/fxcrt/compiler_specific.h"
26
27#if defined(COMPILER_MSVC)
28#include <malloc.h>
29#else
30#include <stdlib.h>
31#endif
32
35NOINLINE void FX_OutOfMemoryTerminate(size_t size);
36
37// General Partition Allocators.
38
39// These never return nullptr, and must return cleared memory.
40#define FX_Alloc(type, size)
41 static_cast<type*>(pdfium::internal::CallocOrDie(size, sizeof(type)))
42#define FX_Alloc2D(type, w, h)
43 static_cast<type*>(pdfium::internal::CallocOrDie2D(w, h, sizeof(type)))
44#define FX_Realloc(type, ptr, size)
45 static_cast<type*>(pdfium::internal::ReallocOrDie(ptr, size, sizeof(type)))
46
47// May return nullptr, but returns cleared memory otherwise.
48#define FX_TryAlloc(type, size)
49 static_cast<type*>(pdfium::internal::Calloc(size, sizeof(type)))
50#define FX_TryRealloc(type, ptr, size)
51 static_cast<type*>(pdfium::internal::Realloc(ptr, size, sizeof(type)))
52
53// These never return nullptr, but return uninitialized memory.
54#define FX_AllocUninit(type, size)
55 static_cast<type*>(pdfium::internal::AllocOrDie(size, sizeof(type)))
56#define FX_AllocUninit2D(type, w, h)
57 static_cast<type*>(pdfium::internal::AllocOrDie2D(w, h, sizeof(type)))
58
59// May return nullptr, but returns uninitialized memory otherwise.
60#define FX_TryAllocUninit(type, size)
61 static_cast<type*>(pdfium::internal::Alloc(size, sizeof(type)))
62#define FX_TryAllocUninit2D(type, w, h)
63 static_cast<type*>(pdfium::internal::Alloc2D(w, h, sizeof(type)))
64
65// FX_Free frees memory from the above.
66#define FX_Free(ptr) pdfium::internal::Dealloc(ptr)
67
68// String Partition Allocators.
69
70// This never returns nullptr, but returns uninitialized memory.
71#define FX_StringAlloc(type, size)
72 static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
73
74// FX_StringFree frees memory from FX_StringAlloc.
75#define FX_StringFree(ptr) pdfium::internal::StringDealloc(ptr)
76
77#ifndef V8_ENABLE_SANDBOX
78// V8 Array Buffer Partition Allocators.
79
80// This never returns nullptr, and returns zeroed memory.
81void* FX_ArrayBufferAllocate(size_t length);
82
83// This never returns nullptr, but returns uninitialized memory.
84void* FX_ArrayBufferAllocateUninitialized(size_t length);
85
86// FX_ArrayBufferFree accepts memory from both of the above.
87void FX_ArrayBufferFree(void* data);
88#endif // V8_ENABLE_SANDBOX
89
90// Aligned allocators.
91
92// This can be replaced with std::aligned_alloc when we have C++17.
93// Caveat: std::aligned_alloc requires the size parameter be an integral
94// multiple of alignment.
95void* FX_AlignedAlloc(size_t size, size_t alignment);
96
97inline void FX_AlignedFree(void* ptr) {
98#if defined(COMPILER_MSVC)
99 _aligned_free(ptr);
100#else
101 free(ptr);
102#endif
103}
104
105namespace pdfium {
106namespace internal {
107
108// General partition.
109void* Alloc(size_t num_members, size_t member_size);
110void* Alloc2D(size_t w, size_t h, size_t member_size);
111void* AllocOrDie(size_t num_members, size_t member_size);
112void* AllocOrDie2D(size_t w, size_t h, size_t member_size);
113void* Calloc(size_t num_members, size_t member_size);
114void* Realloc(void* ptr, size_t num_members, size_t member_size);
115void* CallocOrDie(size_t num_members, size_t member_size);
116void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
117void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
118void Dealloc(void* ptr);
119
120// String partition.
121void* StringAlloc(size_t num_members, size_t member_size);
122void* StringAllocOrDie(size_t num_members, size_t member_size);
123void StringDealloc(void* ptr);
124
125} // namespace internal
126} // namespace pdfium
127
128// Force stack allocation of a class. Classes that do complex work in a
129// destructor, such as the flushing of buffers, should be declared as
130// stack-allocated as possible, since future memory allocation schemes
131// may not run destructors in a predictable manner if an instance is
132// heap-allocated.
133#define FX_STACK_ALLOCATED()
134 void* operator new(size_t) = delete;
135 void* operator new(size_t, void*) = delete
136
137// Round up to the power-of-two boundary N.
138template <int N, typename T>
139inline T FxAlignToBoundary(T size) {
140 static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
141 return (size + (N - 1)) & ~(N - 1);
142}
143
144#endif // __cplusplus
145
146#endif // CORE_FXCRT_FX_MEMORY_H_
T & reference
Definition span.h:195
constexpr span(T(&array)[N]) noexcept
Definition span.h:211
constexpr reverse_iterator rend() const noexcept
Definition span.h:348
span last() const
Definition span.h:283
constexpr reverse_iterator rbegin() const noexcept
Definition span.h:345
T * iterator
Definition span.h:196
constexpr const_reverse_iterator crend() const noexcept
Definition span.h:355
span first() const
Definition span.h:269
std::reverse_iterator< iterator > reverse_iterator
Definition span.h:198
const span first(size_t count) const
Definition span.h:276
typename std::remove_cv< T >::type value_type
Definition span.h:193
span & operator=(const span &other) noexcept
Definition span.h:258
constexpr const_iterator cbegin() const noexcept
Definition span.h:342
constexpr const_iterator cend() const noexcept
Definition span.h:343
constexpr const_reverse_iterator crbegin() const noexcept
Definition span.h:352
const span last(size_t count) const
Definition span.h:290
constexpr bool empty() const noexcept
Definition span.h:316
constexpr size_t size() const noexcept
Definition span.h:314
constexpr span(Container &container)
Definition span.h:238
friend constexpr span< U > make_span(U *data, size_t size) noexcept
span subspan() const
Definition span.h:297
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition span.h:199
const span subspan(size_t pos, size_t count=dynamic_extent) const
Definition span.h:305
constexpr iterator begin() const noexcept
Definition span.h:337
constexpr size_t size_bytes() const noexcept
Definition span.h:315
constexpr span() noexcept=default
span(const Container &container)
Definition span.h:245
constexpr span(std::array< T, N > &array) noexcept
Definition span.h:216
T * pointer
Definition span.h:194
constexpr T & back() const noexcept
Definition span.h:329
constexpr span(const std::array< std::remove_cv_t< T >, N > &array) noexcept
Definition span.h:221
constexpr T * data() const noexcept
Definition span.h:334
constexpr T & front() const noexcept
Definition span.h:324
constexpr span(const span &other) noexcept=default
UNSAFE_BUFFER_USAGE constexpr span(T *data, size_t size) noexcept
Definition span.h:204
constexpr span(const span< U, M, R > &other)
Definition span.h:255
T & operator[](size_t index) const noexcept
Definition span.h:319
constexpr iterator end() const noexcept
Definition span.h:338
const T * const_iterator
Definition span.h:197
~span() noexcept=default
#define NOINLINE
#define UNSAFE_BUFFERS(...)
#define GSL_POINTER
#define TRIVIAL_ABI
#define UNLIKELY(x)
#define HAS_ATTRIBUTE(x)
#define UNSAFE_BUFFER_USAGE
void CRYPT_MD5Finish(CRYPT_md5_context *context, pdfium::span< uint8_t, 16 > digest)
Definition fx_crypt.cpp:203
#define P(a, b, c, d, k, s, t)
void CRYPT_MD5Generate(pdfium::span< const uint8_t > data, pdfium::span< uint8_t, 16 > digest)
Definition fx_crypt.cpp:219
#define S(x, n)
void CRYPT_ArcFourCryptBlock(pdfium::span< uint8_t > data, pdfium::span< const uint8_t > key)
Definition fx_crypt.cpp:157
void CRYPT_ArcFourSetup(CRYPT_rc4_context *context, pdfium::span< const uint8_t > key)
Definition fx_crypt.cpp:131
void CRYPT_MD5Update(CRYPT_md5_context *context, pdfium::span< const uint8_t > data)
Definition fx_crypt.cpp:175
void CRYPT_ArcFourCrypt(CRYPT_rc4_context *context, pdfium::span< uint8_t > data)
Definition fx_crypt.cpp:146
CRYPT_md5_context CRYPT_MD5Start()
Definition fx_crypt.cpp:164
void CRYPT_AESEncrypt(CRYPT_aes_context *ctx, pdfium::span< uint8_t > dest, pdfium::span< const uint8_t > src)
void CRYPT_AESSetIV(CRYPT_aes_context *ctx, const uint8_t *iv)
void CRYPT_AESDecrypt(CRYPT_aes_context *ctx, uint8_t *dest, const uint8_t *src, uint32_t size)
void CRYPT_AESSetKey(CRYPT_aes_context *ctx, const uint8_t *key, uint32_t keylen)
void CRYPT_SHA512Update(CRYPT_sha2_context *context, pdfium::span< const uint8_t > data)
void CRYPT_SHA384Update(CRYPT_sha2_context *context, pdfium::span< const uint8_t > data)
DataVector< uint8_t > CRYPT_SHA256Generate(pdfium::span< const uint8_t > data)
void CRYPT_SHA1Finish(CRYPT_sha1_context *context, pdfium::span< uint8_t, 20 > digest)
void CRYPT_SHA1Update(CRYPT_sha1_context *context, pdfium::span< const uint8_t > data)
void CRYPT_SHA512Finish(CRYPT_sha2_context *context, pdfium::span< uint8_t, 64 > digest)
DataVector< uint8_t > CRYPT_SHA384Generate(pdfium::span< const uint8_t > data)
void CRYPT_SHA384Start(CRYPT_sha2_context *context)
void CRYPT_SHA384Finish(CRYPT_sha2_context *context, pdfium::span< uint8_t, 48 > digest)
void CRYPT_SHA512Start(CRYPT_sha2_context *context)
DataVector< uint8_t > CRYPT_SHA1Generate(pdfium::span< const uint8_t > data)
void CRYPT_SHA256Start(CRYPT_sha2_context *context)
DataVector< uint8_t > CRYPT_SHA512Generate(pdfium::span< const uint8_t > data)
void CRYPT_SHA1Start(CRYPT_sha1_context *context)
void CRYPT_SHA256Finish(CRYPT_sha2_context *context, pdfium::span< uint8_t, 32 > digest)
void CRYPT_SHA256Update(CRYPT_sha2_context *context, pdfium::span< const uint8_t > data)
NOINLINE void FX_OutOfMemoryTerminate(size_t size)
Definition fx_memory.cpp:82
void * FX_AlignedAlloc(size_t size, size_t alignment)
void FXMEM_DefaultFree(void *pointer)
Definition fx_memory.cpp:78
void * FXMEM_DefaultAlloc(size_t byte_size)
Definition fx_memory.cpp:66
void * FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size)
Definition fx_memory.cpp:70
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size)
Definition fx_memory.cpp:74
void * FX_ArrayBufferAllocate(size_t length)
void FX_DestroyMemoryAllocators()
void FX_InitializeMemoryAllocators()
void FX_ArrayBufferFree(void *data)
void * FX_ArrayBufferAllocateUninitialized(size_t length)
#define IMMEDIATE_CRASH_ALWAYS_INLINE
#define TRAP_SEQUENCE_()
void * CallocOrDie2D(size_t w, size_t h, size_t member_size)
void * AllocOrDie2D(size_t w, size_t h, size_t member_size)
std::is_integral< decltype(std::declval< Container >().size())> ContainerHasIntegralSize
Definition span.h:63
void * StringAllocOrDie(size_t num_members, size_t member_size)
void * Alloc(size_t num_members, size_t member_size)
IsSpanImpl< typename std::decay< T >::type > IsSpan
Definition span.h:43
void * StringAlloc(size_t num_members, size_t member_size)
void * ReallocOrDie(void *ptr, size_t num_members, size_t member_size)
std::is_convertible< From *, To * > IsLegalSpanConversion
Definition span.h:55
void * Realloc(void *ptr, size_t num_members, size_t member_size)
IsStdArrayImpl< typename std::decay< T >::type > IsStdArray
Definition span.h:52
void StringDealloc(void *ptr)
void * AllocOrDie(size_t num_members, size_t member_size)
void * Alloc2D(size_t w, size_t h, size_t member_size)
void * Calloc(size_t num_members, size_t member_size)
void * CallocOrDie(size_t num_members, size_t member_size)
void Dealloc(void *ptr)
constexpr span< T > make_span(T(&array)[N]) noexcept
Definition span.h:374
span< char > as_writable_chars(span< T, N, P > s) noexcept
Definition span.h:427
span< const uint8_t > as_bytes(span< T, N, P > s) noexcept
Definition span.h:400
constexpr size_t dynamic_extent
Definition span.h:24
static constexpr span< T > span_from_ref(T &single_object) noexcept
Definition span.h:437
static constexpr span< const uint8_t > byte_span_from_ref(const T &single_object) noexcept
Definition span.h:446
UNSAFE_BUFFER_USAGE constexpr span< T > make_span(T *data, size_t size) noexcept
Definition span.h:369
span< const uint8_t > as_byte_span(T &&arg)
Definition span.h:465
span< const char > as_chars(span< T, N, P > s) noexcept
Definition span.h:417
span< const uint8_t > as_byte_span(const T &arg)
Definition span.h:461
UNOWNED_PTR_EXCLUSION T * DefaultSpanInternalPtr
Definition span.h:27
span< uint8_t > as_writable_bytes(span< T, N, P > s) noexcept
Definition span.h:410
constexpr span< T > make_span(std::array< T, N > &array) noexcept
Definition span.h:379
constexpr span< T > make_span(Container &container)
Definition span.h:386
constexpr span< uint8_t > as_writable_byte_span(T &&arg)
Definition span.h:475
IMMEDIATE_CRASH_ALWAYS_INLINE void ImmediateCrash()
static constexpr span< uint8_t > byte_span_from_ref(T &single_object) noexcept
Definition span.h:451
constexpr span< T > make_span(const Container &container)
Definition span.h:394
#define CHECK(cvref)
#define assert
#define __has_attribute(x)
std::array< uint32_t, kMaxNb > iv
std::array< uint32_t, kSchedSize > invkeysched
static constexpr int kMaxNr
static constexpr int kSchedSize
static constexpr int kMaxNb
std::array< uint32_t, kSchedSize > keysched
uint8_t buffer[64]
Definition fx_crypt.h:28
std::array< uint32_t, 4 > state
Definition fx_crypt.h:27
std::array< uint32_t, 2 > total
Definition fx_crypt.h:26
std::array< int32_t, kPermutationLength > m
Definition fx_crypt.h:22
static constexpr int32_t kPermutationLength
Definition fx_crypt.h:18
std::array< uint32_t, 5 > h
std::array< uint8_t, 64 > block
std::array< uint64_t, 8 > state
uint8_t buffer[128]
void operator()(void *ptr) const
FxPartitionAllocAllocator< U, Alloc, Free > other
pointer allocate(size_type n, const void *hint=0)
void deallocate(pointer p, size_type n)
const_pointer address(const_reference x) const noexcept
FxPartitionAllocAllocator() noexcept=default
size_type max_size() const noexcept
~FxPartitionAllocAllocator()=default
pointer address(reference x) const noexcept
FxPartitionAllocAllocator(const FxPartitionAllocAllocator< U, Alloc, Free > &other) noexcept
bool operator!=(const FxPartitionAllocAllocator &that)
void construct(U *p, Args &&... args)
bool operator==(const FxPartitionAllocAllocator &that)
FxPartitionAllocAllocator(const FxPartitionAllocAllocator &other) noexcept=default
#define UNOWNED_PTR_EXCLUSION