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 "third_party/base/compiler_specific.h"
26
28NOINLINE void FX_OutOfMemoryTerminate(size_t size);
29
30// General Partition Allocators.
31
32// These never return nullptr, and must return cleared memory.
33#define FX_Alloc(type, size)
34 static_cast<type*>(pdfium::internal::CallocOrDie(size, sizeof(type)))
35#define FX_Alloc2D(type, w, h)
36 static_cast<type*>(pdfium::internal::CallocOrDie2D(w, h, sizeof(type)))
37#define FX_Realloc(type, ptr, size)
38 static_cast<type*>(pdfium::internal::ReallocOrDie(ptr, size, sizeof(type)))
39
40// May return nullptr, but returns cleared memory otherwise.
41#define FX_TryAlloc(type, size)
42 static_cast<type*>(pdfium::internal::Calloc(size, sizeof(type)))
43#define FX_TryRealloc(type, ptr, size)
44 static_cast<type*>(pdfium::internal::Realloc(ptr, size, sizeof(type)))
45
46// These never return nullptr, but return uninitialized memory.
47// TODO(thestig): Add FX_TryAllocUninit() if there is a use case.
48#define FX_AllocUninit(type, size)
49 static_cast<type*>(pdfium::internal::AllocOrDie(size, sizeof(type)))
50#define FX_AllocUninit2D(type, w, h)
51 static_cast<type*>(pdfium::internal::AllocOrDie2D(w, h, sizeof(type)))
52
53// FX_Free frees memory from the above.
54#define FX_Free(ptr) pdfium::internal::Dealloc(ptr)
55
56// String Partition Allocators.
57
58// This never returns nullptr, but returns uninitialized memory.
59#define FX_StringAlloc(type, size)
60 static_cast<type*>(pdfium::internal::StringAllocOrDie(size, sizeof(type)))
61
62// FX_StringFree frees memory from FX_StringAlloc.
63#define FX_StringFree(ptr) pdfium::internal::StringDealloc(ptr)
64
65#ifndef V8_ENABLE_SANDBOX
66// V8 Array Buffer Partition Allocators.
67
68// This never returns nullptr, and returns zeroed memory.
69void* FX_ArrayBufferAllocate(size_t length);
70
71// This never returns nullptr, but returns uninitialized memory.
72void* FX_ArrayBufferAllocateUninitialized(size_t length);
73
74// FX_ArrayBufferFree accepts memory from both of the above.
75void FX_ArrayBufferFree(void* data);
76#endif // V8_ENABLE_SANDBOX
77
78namespace pdfium {
79namespace internal {
80
81// General partition.
82void* Alloc(size_t num_members, size_t member_size);
83void* AllocOrDie(size_t num_members, size_t member_size);
84void* AllocOrDie2D(size_t w, size_t h, size_t member_size);
85void* Calloc(size_t num_members, size_t member_size);
86void* Realloc(void* ptr, size_t num_members, size_t member_size);
87void* CallocOrDie(size_t num_members, size_t member_size);
88void* CallocOrDie2D(size_t w, size_t h, size_t member_size);
89void* ReallocOrDie(void* ptr, size_t num_members, size_t member_size);
90void Dealloc(void* ptr);
91
92// String partition.
93void* StringAlloc(size_t num_members, size_t member_size);
94void* StringAllocOrDie(size_t num_members, size_t member_size);
95void StringDealloc(void* ptr);
96
97} // namespace internal
98} // namespace pdfium
99
100// Force stack allocation of a class. Classes that do complex work in a
101// destructor, such as the flushing of buffers, should be declared as
102// stack-allocated as possible, since future memory allocation schemes
103// may not run destructors in a predictable manner if an instance is
104// heap-allocated.
105#define FX_STACK_ALLOCATED()
106 void* operator new(size_t) = delete;
107 void* operator new(size_t, void*) = delete
108
109// Round up to the power-of-two boundary N.
110template <int N, typename T>
111inline T FxAlignToBoundary(T size) {
112 static_assert(N > 0 && (N & (N - 1)) == 0, "Not non-zero power of two");
113 return (size + (N - 1)) & ~(N - 1);
114}
115
116#endif // __cplusplus
117
118#endif // CORE_FXCRT_FX_MEMORY_H_
uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetLoweredA(ByteStringView str)
uint32_t FX_HashCode_GetAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetA(ByteStringView str)
CPDF_Creator(CPDF_Document *pDoc, RetainPtr< IFX_RetainableWriteStream > archive)
bool SetFileVersion(int32_t fileVersion)
void RemoveSecurity()
bool Create(uint32_t flags)
virtual FX_FILESIZE CurrentOffset() const =0
static RetainPtr< IFX_SeekableReadStream > CreateFromFilename(const char *filename)
Definition fx_stream.cpp:68
virtual size_t ReadBlock(pdfium::span< uint8_t > buffer)
Definition fx_stream.cpp:88
virtual FX_FILESIZE GetPosition()
Definition fx_stream.cpp:84
virtual bool ReadBlockAtOffset(pdfium::span< uint8_t > buffer, FX_FILESIZE offset)=0
virtual bool IsEOF()
Definition fx_stream.cpp:80
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:92
virtual bool WriteBlockAtOffset(pdfium::span< const uint8_t > data, FX_FILESIZE offset)=0
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:76
virtual bool Flush()=0
virtual FX_FILESIZE GetSize()=0
bool WriteFilesize(FX_FILESIZE size)
Definition fx_stream.cpp:61
bool WriteByte(uint8_t byte)
Definition fx_stream.cpp:51
bool WriteString(ByteStringView str)
Definition fx_stream.cpp:47
virtual ~IFX_WriteStream()=default
bool WriteDWord(uint32_t i)
Definition fx_stream.cpp:55
virtual bool WriteBlock(pdfium::span< const uint8_t > data)=0
void TrimLeft(ByteStringView targets)
bool IsValidLength(size_t length) const
Definition bytestring.h:121
bool Contains(ByteStringView lpszSub, size_t start=0) const
Definition bytestring.h:187
const_iterator begin() const
Definition bytestring.h:102
bool IsValidIndex(size_t index) const
Definition bytestring.h:120
static ByteString FormatFloat(float f)
ByteStringView AsStringView() const
Definition bytestring.h:87
uint32_t GetID() const
Definition bytestring.h:213
void Reserve(size_t len)
ByteString(const char *ptr)
bool EqualNoCase(ByteStringView str) const
static ByteString Format(const char *pFormat,...)
ByteString & operator+=(const ByteString &str)
bool operator!=(ByteStringView str) const
Definition bytestring.h:131
size_t GetLength() const
Definition bytestring.h:115
ByteString Substr(size_t first, size_t count) const
size_t GetStringLength() const
Definition bytestring.h:116
size_t InsertAtBack(char ch)
Definition bytestring.h:162
bool operator==(ByteStringView str) const
pdfium::span< const uint8_t > raw_span() const
Definition bytestring.h:97
void AllocCopy(ByteString &dest, size_t nCopyLen, size_t nCopyIndex) const
void AssignCopy(const char *pSrcData, size_t nSrcLen)
bool Contains(char ch, size_t start=0) const
Definition bytestring.h:191
absl::optional< size_t > ReverseFind(char ch) const
intptr_t ReferenceCountForTesting() const
size_t Delete(size_t index, size_t count=1)
const_reverse_iterator rend() const
Definition bytestring.h:111
ByteString & operator+=(char ch)
const uint8_t * raw_str() const
Definition bytestring.h:80
void ReallocBeforeWrite(size_t nNewLen)
ByteString(ByteStringView bstrc)
RetainPtr< StringData > m_pData
Definition bytestring.h:225
static ByteString FormatInteger(int i)
pdfium::span< char > GetBuffer(size_t nMinBufLength)
void Trim(char target)
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
const_iterator end() const
Definition bytestring.h:103
ByteString(const ByteString &other)
size_t Remove(char ch)
ByteString(const char *pStr, size_t len)
bool operator<(ByteStringView str) const
ByteString(wchar_t)=delete
ByteString & operator+=(const char *str)
ByteString & operator+=(ByteStringView str)
ByteString(const uint8_t *pStr, size_t len)
size_t Replace(ByteStringView pOld, ByteStringView pNew)
absl::optional< size_t > Find(ByteStringView subStr, size_t start=0) const
bool operator!=(const ByteString &other) const
Definition bytestring.h:132
void SetAt(size_t index, char c)
ByteString & operator=(ByteStringView str)
ByteString(ByteStringView str1, ByteStringView str2)
void Trim(ByteStringView targets)
ByteString & operator=(const char *str)
static ByteString FormatV(const char *pFormat, va_list argList)
void TrimRight(char target)
const char * c_str() const
Definition bytestring.h:76
ByteString Substr(size_t offset) const
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
void TrimRight(ByteStringView targets)
bool operator<(const ByteString &other) const
ByteString(const std::initializer_list< ByteStringView > &list)
CharType operator[](const size_t index) const
Definition bytestring.h:150
const_reverse_iterator rbegin() const
Definition bytestring.h:108
CharType Back() const
Definition bytestring.h:156
CharType Front() const
Definition bytestring.h:155
bool operator!=(const char *ptr) const
Definition bytestring.h:130
int Compare(ByteStringView str) const
size_t Insert(size_t index, char ch)
void ReleaseBuffer(size_t nNewLength)
ByteString(const fxcrt::ostringstream &outStream)
void TrimLeft(char target)
size_t InsertAtFront(char ch)
Definition bytestring.h:161
ByteString First(size_t count) const
void AllocBeforeWrite(size_t nNewLen)
bool operator<(const char *ptr) const
void Concat(const char *pSrcData, size_t nSrcLen)
absl::optional< size_t > Find(char ch, size_t start=0) const
ByteString(ByteString &&other) noexcept
ByteString Last(size_t count) const
#define FPDFCREATE_INCREMENTAL
#define FPDFCREATE_NO_ORIGINAL
NOINLINE void FX_OutOfMemoryTerminate(size_t size)
Definition fx_memory.cpp:37
void FXMEM_DefaultFree(void *pointer)
Definition fx_memory.cpp:33
void * FXMEM_DefaultAlloc(size_t byte_size)
Definition fx_memory.cpp:21
void * FXMEM_DefaultCalloc(size_t num_elems, size_t byte_size)
Definition fx_memory.cpp:25
void * FXMEM_DefaultRealloc(void *pointer, size_t new_size)
Definition fx_memory.cpp:29
void * FX_ArrayBufferAllocate(size_t length)
void FX_InitializeMemoryAllocators()
void FX_ArrayBufferFree(void *data)
void * FX_ArrayBufferAllocateUninitialized(size_t length)
void * FX_Random_MT_Start(uint32_t dwSeed)
Definition fx_random.cpp:85
uint32_t FX_Random_MT_Generate(void *pContext)
Definition fx_random.cpp:96
void FX_Random_MT_Close(void *pContext)
#define FX_FILESIZE
Definition fx_types.h:19
bool operator==(const char *lhs, const ByteString &rhs)
Definition bytestring.h:233
bool operator<(const ByteStringView &lhs, const char *rhs)
Definition bytestring.h:251
ByteString operator+(const ByteString &str1, const ByteString &str2)
Definition bytestring.h:270
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:258
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:248
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:273
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:279
void PrintTo(const ByteString &str, std::ostream *os)
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:276
ByteString operator+(const ByteString &str1, ByteStringView str2)
Definition bytestring.h:285
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:282
bool operator!=(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:242
bool operator!=(const char *lhs, const ByteString &rhs)
Definition bytestring.h:239
ByteString operator+(ByteStringView str1, ByteStringView str2)
Definition bytestring.h:255
ByteString operator+(ByteStringView str1, char ch)
Definition bytestring.h:264
bool operator<(const char *lhs, const ByteString &rhs)
Definition bytestring.h:245
ByteString operator+(char ch, ByteStringView str2)
Definition bytestring.h:267
ByteString operator+(ByteStringView str1, const ByteString &str2)
Definition bytestring.h:288
ByteString operator+(const char *str1, ByteStringView str2)
Definition bytestring.h:261
bool operator==(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:236
void * CallocOrDie2D(size_t w, size_t h, size_t member_size)
Definition fx_memory.cpp:79
void * AllocOrDie2D(size_t w, size_t h, size_t member_size)
Definition fx_memory.cpp:65
void * StringAllocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:94
void * Alloc(size_t num_members, size_t member_size)
void * StringAlloc(size_t num_members, size_t member_size)
void * ReallocOrDie(void *ptr, size_t num_members, size_t member_size)
Definition fx_memory.cpp:86
void * Realloc(void *ptr, size_t num_members, size_t member_size)
void StringDealloc(void *ptr)
void * AllocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:57
void * Calloc(size_t num_members, size_t member_size)
void * CallocOrDie(size_t num_members, size_t member_size)
Definition fx_memory.cpp:71
void Dealloc(void *ptr)
#define CHECK(cvref)
void operator()(void *ptr) const
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
size_t operator()(const ByteString &str) const
Definition bytestring.h:315