diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/balloc | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/balloc')
-rw-r--r-- | library/cpp/balloc/balloc.cpp | 256 | ||||
-rw-r--r-- | library/cpp/balloc/malloc-info.cpp | 26 |
2 files changed, 141 insertions, 141 deletions
diff --git a/library/cpp/balloc/balloc.cpp b/library/cpp/balloc/balloc.cpp index fab489db4c..a81f73c65a 100644 --- a/library/cpp/balloc/balloc.cpp +++ b/library/cpp/balloc/balloc.cpp @@ -3,40 +3,40 @@ namespace NBalloc { - static constexpr size_t ALIVE_SIGNATURE = 0xaULL << 56; - static constexpr size_t DISABLED_SIGNATURE = 0xbULL << 56; - static constexpr size_t SIGNATURE_MASK = 0xfULL << 56; + static constexpr size_t ALIVE_SIGNATURE = 0xaULL << 56; + static constexpr size_t DISABLED_SIGNATURE = 0xbULL << 56; + static constexpr size_t SIGNATURE_MASK = 0xfULL << 56; static constexpr size_t MINIMAL_ALIGNMENT = sizeof(NBalloc::TAllocHeader); static_assert(((MINIMAL_ALIGNMENT - 1) & MINIMAL_ALIGNMENT) == 0, "invalid BALLOC_MINIMAL_ALIGNMENT"); - static Y_FORCE_INLINE void* Malloc(size_t size) { - TLS& ltls = tls; + static Y_FORCE_INLINE void* Malloc(size_t size) { + TLS& ltls = tls; size = Align(size, sizeof(TAllocHeader)); - if (Y_UNLIKELY(ltls.Mode == Empty || ltls.Mode == ToBeEnabled)) { + if (Y_UNLIKELY(ltls.Mode == Empty || ltls.Mode == ToBeEnabled)) { Init(ltls); - } - if (Y_LIKELY(ltls.Mode != Disabled)) { + } + if (Y_LIKELY(ltls.Mode != Disabled)) { TAllocHeader* allocHeader = AllocateRaw(size, ALIVE_SIGNATURE); return allocHeader + 1; - } else { - // ltls.Mode == Disabled - const size_t extsize = size + sizeof(TAllocHeader); + } else { + // ltls.Mode == Disabled + const size_t extsize = size + sizeof(TAllocHeader); TAllocHeader* allocHeader = (TAllocHeader*)LibcMalloc(extsize); allocHeader->Encode(allocHeader, size, DISABLED_SIGNATURE); return allocHeader + 1; } - } + } - static void Y_FORCE_INLINE Free(void* ptr) { + static void Y_FORCE_INLINE Free(void* ptr) { if (ptr == nullptr) { - return; + return; } TAllocHeader* allocHeader = ((TAllocHeader*)ptr) - 1; size_t size = allocHeader->AllocSize; - const size_t signature = size & SIGNATURE_MASK; - if (Y_LIKELY(signature == ALIVE_SIGNATURE)) { + const size_t signature = size & SIGNATURE_MASK; + if (Y_LIKELY(signature == ALIVE_SIGNATURE)) { allocHeader->AllocSize = 0; // abort later on double free #ifdef DBG_FILL_MEMORY memset(ptr, 0xde, size - signature); @@ -45,62 +45,62 @@ namespace NBalloc { if (NAllocStats::IsEnabled()) { NAllocStats::DecThreadAllocStats(size - signature); } - } else if (signature == DISABLED_SIGNATURE) { + } else if (signature == DISABLED_SIGNATURE) { LibcFree(allocHeader->Block); - } else { - NMalloc::AbortFromCorruptedAllocator(); - } + } else { + NMalloc::AbortFromCorruptedAllocator(); + } } - static bool Y_FORCE_INLINE IsOwnedByBalloc(void* ptr) { - TAllocHeader* allocHeader = ((TAllocHeader*)ptr) - 1; - size_t size = allocHeader->AllocSize; - const size_t signature = size & SIGNATURE_MASK; - if (signature == ALIVE_SIGNATURE) { - return true; - } else if (signature == DISABLED_SIGNATURE) { - return false; - } + static bool Y_FORCE_INLINE IsOwnedByBalloc(void* ptr) { + TAllocHeader* allocHeader = ((TAllocHeader*)ptr) - 1; + size_t size = allocHeader->AllocSize; + const size_t signature = size & SIGNATURE_MASK; + if (signature == ALIVE_SIGNATURE) { + return true; + } else if (signature == DISABLED_SIGNATURE) { + return false; + } NMalloc::AbortFromCorruptedAllocator(); - Y_UNREACHABLE(); + Y_UNREACHABLE(); } - static void Y_FORCE_INLINE Disable() { + static void Y_FORCE_INLINE Disable() { #if defined(_musl_) - // just skip it + // just skip it #else - tls.Mode = Disabled; + tls.Mode = Disabled; #endif - } + } - static void Y_FORCE_INLINE Enable() { + static void Y_FORCE_INLINE Enable() { tls.Mode = ToBeEnabled; - } + } - static bool Y_FORCE_INLINE IsDisabled() { - return tls.Mode == Disabled; - } + static bool Y_FORCE_INLINE IsDisabled() { + return tls.Mode == Disabled; + } }; -#if defined(Y_COVER_PTR) -void* CoverPtr(void* ptr, size_t len) noexcept; -void* UncoverPtr(void* ptr) noexcept; -#endif - -extern "C" void* malloc(size_t size) { -#if defined(Y_COVER_PTR) - return CoverPtr(NBalloc::Malloc(size + 32), size); -#else +#if defined(Y_COVER_PTR) +void* CoverPtr(void* ptr, size_t len) noexcept; +void* UncoverPtr(void* ptr) noexcept; +#endif + +extern "C" void* malloc(size_t size) { +#if defined(Y_COVER_PTR) + return CoverPtr(NBalloc::Malloc(size + 32), size); +#else return NBalloc::Malloc(size); -#endif +#endif } -extern "C" void free(void* data) { -#if defined(Y_COVER_PTR) - NBalloc::Free(UncoverPtr(data)); -#else +extern "C" void free(void* data) { +#if defined(Y_COVER_PTR) + NBalloc::Free(UncoverPtr(data)); +#else NBalloc::Free(data); -#endif +#endif } #if defined(USE_INTELCC) || defined(_darwin_) || defined(_freebsd_) || defined(_STLPORT_VERSION) @@ -110,19 +110,19 @@ extern "C" void free(void* data) { #endif void* operator new(size_t size) { -#if defined(Y_COVER_PTR) - return malloc(size); -#else +#if defined(Y_COVER_PTR) + return malloc(size); +#else return NBalloc::Malloc(size); -#endif +#endif } int posix_memalign(void** memptr, const size_t alignment, const size_t size) { -#if defined(Y_COVER_PTR) - (void)alignment; - *memptr = malloc(size); - return 0; -#else +#if defined(Y_COVER_PTR) + (void)alignment; + *memptr = malloc(size); + return 0; +#else if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void*)) { return EINVAL; } @@ -141,98 +141,98 @@ int posix_memalign(void** memptr, const size_t alignment, const size_t size) { } *memptr = alignedPtr; return 0; -#endif +#endif } void* operator new(size_t size, const std::nothrow_t&) OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - return malloc(size); -#else +#if defined(Y_COVER_PTR) + return malloc(size); +#else return NBalloc::Malloc(size); -#endif +#endif } void operator delete(void* p)OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - free(p); -#else +#if defined(Y_COVER_PTR) + free(p); +#else NBalloc::Free(p); -#endif +#endif } void operator delete(void* p, const std::nothrow_t&)OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - free(p); -#else +#if defined(Y_COVER_PTR) + free(p); +#else NBalloc::Free(p); -#endif +#endif } void* operator new[](size_t size) { -#if defined(Y_COVER_PTR) - return malloc(size); -#else +#if defined(Y_COVER_PTR) + return malloc(size); +#else return NBalloc::Malloc(size); -#endif +#endif } void* operator new[](size_t size, const std::nothrow_t&) OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - return malloc(size); -#else +#if defined(Y_COVER_PTR) + return malloc(size); +#else return NBalloc::Malloc(size); -#endif +#endif } void operator delete[](void* p) OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - free(p); -#else +#if defined(Y_COVER_PTR) + free(p); +#else NBalloc::Free(p); -#endif +#endif } void operator delete[](void* p, const std::nothrow_t&) OP_THROWNOTHING { -#if defined(Y_COVER_PTR) - free(p); -#else +#if defined(Y_COVER_PTR) + free(p); +#else NBalloc::Free(p); -#endif +#endif } extern "C" void* calloc(size_t n, size_t elemSize) { const size_t size = n * elemSize; - + if (elemSize != 0 && size / elemSize != n) { return nullptr; } - -#if defined(Y_COVER_PTR) - void* result = malloc(size); -#else + +#if defined(Y_COVER_PTR) + void* result = malloc(size); +#else void* result = NBalloc::Malloc(size); -#endif - +#endif + if (result) { memset(result, 0, size); } - + return result; } extern "C" void cfree(void* ptr) { -#if defined(Y_COVER_PTR) - free(ptr); -#else +#if defined(Y_COVER_PTR) + free(ptr); +#else NBalloc::Free(ptr); -#endif +#endif } -#if defined(Y_COVER_PTR) -static inline void* DoRealloc(void* oldPtr, size_t newSize) { -#else +#if defined(Y_COVER_PTR) +static inline void* DoRealloc(void* oldPtr, size_t newSize) { +#else extern "C" void* realloc(void* oldPtr, size_t newSize) { -#endif +#endif if (!oldPtr) { void* result = NBalloc::Malloc(newSize); return result; @@ -241,11 +241,11 @@ extern "C" void* realloc(void* oldPtr, size_t newSize) { NBalloc::Free(oldPtr); return nullptr; } - void* newPtr = NBalloc::Malloc(newSize); + void* newPtr = NBalloc::Malloc(newSize); if (!newPtr) { return nullptr; } - NBalloc::TAllocHeader* header = (NBalloc::TAllocHeader*)oldPtr - 1; + NBalloc::TAllocHeader* header = (NBalloc::TAllocHeader*)oldPtr - 1; const size_t oldSize = header->AllocSize & ~NBalloc::SIGNATURE_MASK; const size_t signature = header->AllocSize & NBalloc::SIGNATURE_MASK; if (Y_LIKELY((signature == NBalloc::ALIVE_SIGNATURE) || (signature == NBalloc::DISABLED_SIGNATURE))) { @@ -257,24 +257,24 @@ extern "C" void* realloc(void* oldPtr, size_t newSize) { return nullptr; } -#if defined(Y_COVER_PTR) -extern "C" void* realloc(void* oldPtr, size_t newSize) { - if (!oldPtr) { - return malloc(newSize); - } - - if (!newSize) { - free(oldPtr); - - return nullptr; - } - - return CoverPtr(DoRealloc(UncoverPtr(oldPtr), newSize + 32), newSize); -} -#endif - +#if defined(Y_COVER_PTR) +extern "C" void* realloc(void* oldPtr, size_t newSize) { + if (!oldPtr) { + return malloc(newSize); + } + + if (!newSize) { + free(oldPtr); + + return nullptr; + } + + return CoverPtr(DoRealloc(UncoverPtr(oldPtr), newSize + 32), newSize); +} +#endif + // Only for testing purposes. Never use in production. -extern "C" bool IsOwnedByBalloc(void* ptr) { +extern "C" bool IsOwnedByBalloc(void* ptr) { return NBalloc::IsOwnedByBalloc(ptr); } @@ -300,7 +300,7 @@ extern "C" void* valloc(size_t size) { return memalign(NBalloc::PAGE_ELEM, size); } -#if !defined(_MSC_VER) && !defined(_freebsd_) +#if !defined(_MSC_VER) && !defined(_freebsd_) // Workaround for pthread_create bug in linux. extern "C" void* __libc_memalign(size_t alignment, size_t size) { return memalign(alignment, size); diff --git a/library/cpp/balloc/malloc-info.cpp b/library/cpp/balloc/malloc-info.cpp index 604b1fb145..008606e0a8 100644 --- a/library/cpp/balloc/malloc-info.cpp +++ b/library/cpp/balloc/malloc-info.cpp @@ -1,26 +1,26 @@ #include <library/cpp/malloc/api/malloc.h> -#include <string.h> +#include <string.h> using namespace NMalloc; -extern "C" void DisableBalloc(); +extern "C" void DisableBalloc(); extern "C" void EnableBalloc(); extern "C" bool BallocDisabled(); -namespace { +namespace { bool SetAllocParam(const char* name, const char* value) { - if (strcmp(name, "disable") == 0) { + if (strcmp(name, "disable") == 0) { if (value == nullptr || strcmp(value, "false") != 0) { // all values other than "false" are considred to be "true" for compatibility DisableBalloc(); } else { EnableBalloc(); } - return true; - } - return false; - } + return true; + } + return false; + } bool CheckAllocParam(const char* name, bool defaultValue) { if (strcmp(name, "disable") == 0) { @@ -28,14 +28,14 @@ namespace { } return defaultValue; } -} - +} + TMallocInfo NMalloc::MallocInfo() { TMallocInfo r; - + r.Name = "balloc"; - r.SetParam = SetAllocParam; + r.SetParam = SetAllocParam; r.CheckParam = CheckAllocParam; - + return r; } |