嵌入式技术
int x; int z = 5; void func() { static int y; } int main() { return 0; }
struct A { int i; char c1; char c2; }; int main() { cout << sizeof(A) << endl; // 有效对齐值为4, output : 8 return 0; }
struct { #if PLATFORM_LITTLE_ENDIAN uint16 Mantissa : 10; uint16 Exponent : 5; uint16 Sign : 1; #else uint16 Sign : 1; uint16 Exponent : 5; uint16 Mantissa : 10; #endif } Components;
::(optional) new (placement_params)(optional) ( type ) initializer(optional)
p_var = new type(initializer); // p_var = new type{initializer};
p_var = new type[size]; // 分配 delete[] p_var; // 释放
auto p = new double[2][2]; auto p = new double[2][2]{ {1.0,2.0},{3.0,4.0} };
new (nothrow) Type (optional-initializer-expression-list)
auto p = new auto('c');
class A { private: int data; public: A(int indata) : data(indata) { } void print() { cout << data << endl; } }; int main() { const int size = 10; char buf[size * sizeof(A)]; // 内存分配 for (size_t i = 0; i < size; i++) { new (buf + i * sizeof(A)) A(i); // 对象构造 } A* arr = (A*)buf; for (size_t i = 0; i < size; i++) { arr[i].print(); arr[i].~A(); // 对象析构 } // 栈上预分配的内存自动释放 return 0; }
auto p = new(std::align_val_t{ 32 }) A;
// Default placement versions of operator new. inline void* operator new(std::size_t, void* __p) throw() { return __p; } inline void* operator new[](std::size_t, void* __p) throw() { return __p; } // Default placement versions of operator delete. inline void operator delete (void*, void*) throw() { } inline void operator delete[](void*, void*) throw() { }
void* operator new(std::size_t, const std::nothrow_t&) throw(); void* operator new[](std::size_t, const std::nothrow_t&) throw(); void operator delete(void*, const std::nothrow_t&) throw(); void operator delete[](void*, const std::nothrow_t&) throw();
#pragma pack(n)
struct alignas(8) S { // ... }; struct alignas(1) U { S s; };
struct alignas(2) S { int n; };
struct alignas(3) S { };
// every object of type sse_t will be aligned to 16-byte boundary struct alignas(16) sse_t { float sse_data[4]; }; // the array "cacheline" will be aligned to 128-byte boundary alignas(128) char cacheline[128];
struct Foo { int i; float f; char c; }; struct Empty { }; struct alignas(64) Empty64 { }; int main() { std::cout << "Alignment of" " " "- char :" << alignof(char) << " " // 1 "- pointer :" << alignof(int*) << " " // 8 "- class Foo :" << alignof(Foo) << " " // 4 "- empty class :" << alignof(Empty) << " " // 1 "- alignas(64) Empty:" << alignof(Empty64) << " "; // 64 }
template < class T, class Alloc = allocator
allocator<string> alloc; // 构造allocator对象 auto const p = alloc.allocate(n); // 分配n个未初始化的string
auto q = p; alloc.construct(q++, "hello"); // 在分配的内存处创建对象 alloc.construct(q++, 10, 'c');
while(q != p) alloc.destroy(--q);
alloc.deallocate(p, n);
uninitialized_copy(b, e, b2) // 从迭代器b, e 中的元素拷贝到b2指定的未构造的原始内存中; uninitialized_copy(b, n, b2) // 从迭代器b指向的元素开始,拷贝n个元素到b2开始的内存中; uninitialized_fill(b, e, t) // 从迭代器b和e指定的原始内存范围中创建对象,对象的值均为t的拷贝; uninitialized_fill_n(b, n, t) // 从迭代器b指向的内存地址开始创建n个对象;
shared_ptr<int> p = make_shared<int>(42);
auto p = make_shared<int>(42); auto r = make_shared<int>(42); r = q; // 递增q指向的对象,递减r指向的对象
shared_ptr<int> p1 = new int(1024); // err shared_ptr<int> p2(new int(1024)); // ok
p.reset(new int(1024));
unique_ptr<string> p = make_unique<string>("test");
unique_ptr<string> p1(new string("test")); unique_ptr<string> p2(p1); // err unique_ptr<string> p3; p3 = p2; // err
unique_ptr<string> p2(p1.release()); unique_ptr<string> p3(new string("test")); p2.reset(p3.release());
p2.release(); // err auto p = p2.release(); // ok, but remember to delete(p)
unique_ptr<int> clone(int p) { return unique_ptr<int>(new int(p)); }
auto p = make_shared<int>(42); weak_ptr<int> wp(p); if(shared_ptr<int> np = wp.lock()) { // ... }
template <typename T> FORCEINLINE constexpr T Align(T Val, uint64 Alignment) { static_assert(TIsIntegral
/** The global memory allocator's interface. */ class CORE_API FMalloc : public FUseSystemMallocForNew, public FExec { public: virtual void* Malloc( SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT ) = 0; virtual void* TryMalloc( SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT ); virtual void* Realloc( void* Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT ) = 0; virtual void* TryRealloc(void* Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT); virtual void Free( void* Original ) = 0; // ... };
/** Which allocator is being used */ enum EMemoryAllocatorToUse { Ansi, // Default C allocator Stomp, // Allocator to check for memory stomping TBB, // Thread Building Blocks malloc Jemalloc, // Linux/FreeBSD malloc Binned, // Older binned malloc Binned2, // Newer binned malloc Binned3, // Newer VM-based binned malloc, 64 bit only Platform, // Custom platform specific allocator Mimalloc, // mimalloc };
struct CORE_API FMemory { /** @name Memory functions (wrapper for FPlatformMemory) */ static FORCEINLINE void* Memmove( void* Dest, const void* Src, SIZE_T Count ) { return FPlatformMemory::Memmove( Dest, Src, Count ); } static FORCEINLINE int32 Memcmp( const void* Buf1, const void* Buf2, SIZE_T Count ) { return FPlatformMemory::Memcmp( Buf1, Buf2, Count ); } // ... static void* Malloc(SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT); static void* Realloc(void* Original, SIZE_T Count, uint32 Alignment = DEFAULT_ALIGNMENT); static void Free(void* Original); static SIZE_T GetAllocSize(void* Original); // ... };
template<typename InElementType, typename InAllocator> class TArray { // ... };
template <int IndexSize> class TSizedHeapAllocator { ... }; // Default Allocator using FHeapAllocator = TSizedHeapAllocator<32>;
template
template
template
template
template<typename InElementAllocator = FDefaultAllocator,typename InBitArrayAllocator = FDefaultBitArrayAllocator> class TSparseArrayAllocator { public: typedef InElementAllocator ElementAllocator; typedef InBitArrayAllocator BitArrayAllocator; };
template< typename InSparseArrayAllocator = TSparseArrayAllocator<>, typename InHashAllocator = TInlineAllocator<1,FDefaultAllocator>, uint32 AverageNumberOfElementsPerHashBucket = DEFAULT_NUMBER_OF_ELEMENTS_PER_HASH_BUCKET, uint32 BaseNumberOfHashBuckets = DEFAULT_BASE_NUMBER_OF_HASH_BUCKETS, uint32 MinNumberOfHashedElements = DEFAULT_MIN_NUMBER_OF_HASHED_ELEMENTS > class TSetAllocator { public: static FORCEINLINE uint32 GetNumberOfHashBuckets(uint32 NumHashedElements) { //... } typedef InSparseArrayAllocator SparseArrayAllocator; typedef InHashAllocator HashAllocator; };
template< class ObjectType, ESPMode Mode > class TSharedPtr { // ... private: ObjectType* Object; SharedPointerInternals::FSharedReferencer< Mode > SharedReferenceCount; };
template< class ObjectType, ESPMode Mode > class TSharedRef { // ... private: ObjectType* Object; SharedPointerInternals::FSharedReferencer< Mode > SharedReferenceCount; };
template<typename ReferencedType> class TRefCountPtr { // ... private: ReferencedType* Reference; };
class FRefCountBase { public: // ... private: mutable int32 NumRefs = 0; };
template< class ObjectType, ESPMode Mode > class TWeakPtr { };
template<class T, class TWeakObjectPtrBase> struct TWeakObjectPtr : private TWeakObjectPtrBase { // ... }; struct FWeakObjectPtr { // ... private: int32 ObjectIndex; int32 ObjectSerialNumber; };
typedef __m128 VectorRegister; FORCEINLINE VectorRegister VectorLoad( const void* Ptr ) { return _mm_loadu_ps((float*)(Ptr)); } FORCEINLINE VectorRegister VectorAdd( const VectorRegister& Vec1, const VectorRegister& Vec2 ) { return _mm_add_ps(Vec1, Vec2); } FORCEINLINE VectorRegister VectorSubtract( const VectorRegister& Vec1, const VectorRegister& Vec2 ) { return _mm_sub_ps(Vec1, Vec2); } FORCEINLINE VectorRegister VectorMultiply( const VectorRegister& Vec1, const VectorRegister& Vec2 ) { return _mm_mul_ps(Vec1, Vec2); }
/** Addition operator. */ friend FORCEINLINE TSHVector operator+(const TSHVector& A,const TSHVector& B) { TSHVector Result; for(int32 BasisIndex = 0;BasisIndex < NumSIMDVectors;BasisIndex++) { VectorRegister AddResult = VectorAdd( VectorLoadAligned(&A.V[BasisIndex * NumComponentsPerSIMDVector]), VectorLoadAligned(&B.V[BasisIndex * NumComponentsPerSIMDVector]) ); VectorStoreAligned(AddResult, &Result.V[BasisIndex * NumComponentsPerSIMDVector]); } return Result;
全部0条评论
快来发表一下你的评论吧 !