aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2022-11-03 08:56:11 +0300
committerbabenko <babenko@yandex-team.com>2022-11-03 08:56:11 +0300
commit3910880160a4aaae4c26adeea4fe525e91856737 (patch)
treeb3c739692b49f1463cb62ddc7a0069b15fa0d516
parenta0d00502b1c5789adf896de18dd4c25b097a098d (diff)
downloadydb-3910880160a4aaae4c26adeea4fe525e91856737.tar.gz
Introduce aligned_malloc; drop more YTAlloc dependencies
-rw-r--r--library/cpp/yt/malloc/malloc.cpp18
-rw-r--r--library/cpp/yt/malloc/malloc.h3
-rw-r--r--library/cpp/yt/memory/CMakeLists.txt2
-rw-r--r--library/cpp/yt/memory/blob.cpp8
-rw-r--r--library/cpp/yt/memory/new-inl.h33
-rw-r--r--library/cpp/yt/memory/ref.cpp6
-rw-r--r--library/cpp/yt/memory/ref_counted.h2
-rw-r--r--library/cpp/yt/memory/serialize.h3
8 files changed, 37 insertions, 38 deletions
diff --git a/library/cpp/yt/malloc/malloc.cpp b/library/cpp/yt/malloc/malloc.cpp
index 4a35758cab..9a055eaaa0 100644
--- a/library/cpp/yt/malloc/malloc.cpp
+++ b/library/cpp/yt/malloc/malloc.cpp
@@ -1,9 +1,13 @@
#include "malloc.h"
#include <util/system/compiler.h>
+#include <util/system/platform.h>
+
+#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
+#ifndef _win_
Y_WEAK extern "C" size_t nallocx(size_t size, int /*flags*/) noexcept
{
return size;
@@ -13,5 +17,19 @@ Y_WEAK extern "C" size_t malloc_usable_size(void* /*ptr*/) noexcept
{
return 0;
}
+#endif
+
+void* aligned_malloc(size_t size, size_t alignment)
+{
+#if defined(_win_)
+ return _aligned_malloc(size, alignment);
+#elif defined(_darwin_) || defined(_linux_)
+ void* ptr = nullptr;
+ ::posix_memalign(&ptr, alignment, size);
+ return ptr;
+#else
+# error Unsupported platform
+#endif
+}
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/malloc/malloc.h b/library/cpp/yt/malloc/malloc.h
index b3c16d7849..2b3efbfd00 100644
--- a/library/cpp/yt/malloc/malloc.h
+++ b/library/cpp/yt/malloc/malloc.h
@@ -3,6 +3,9 @@
////////////////////////////////////////////////////////////////////////////////
extern "C" size_t malloc_usable_size(void* ptr) noexcept;
+
extern "C" size_t nallocx(size_t size, int flags) noexcept;
+void* aligned_malloc(size_t size, size_t alignment);
+
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/memory/CMakeLists.txt b/library/cpp/yt/memory/CMakeLists.txt
index a620c5c7ec..ca44ef58cb 100644
--- a/library/cpp/yt/memory/CMakeLists.txt
+++ b/library/cpp/yt/memory/CMakeLists.txt
@@ -13,7 +13,7 @@ target_link_libraries(cpp-yt-memory PUBLIC
yutil
cpp-yt-assert
cpp-yt-misc
- cpp-ytalloc-api
+ cpp-yt-malloc
)
target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/blob.cpp
diff --git a/library/cpp/yt/memory/blob.cpp b/library/cpp/yt/memory/blob.cpp
index ac2a8948bd..9c17cffad0 100644
--- a/library/cpp/yt/memory/blob.cpp
+++ b/library/cpp/yt/memory/blob.cpp
@@ -1,8 +1,8 @@
#include "blob.h"
-#include <library/cpp/yt/memory/ref.h>
+#include "ref.h"
-#include <library/cpp/ytalloc/api/ytalloc.h>
+#include <library/cpp/yt/malloc/malloc.h>
namespace NYT {
@@ -148,8 +148,8 @@ void TBlob::Reset()
char* TBlob::DoAllocate(size_t size)
{
return static_cast<char*>(PageAligned_
- ? NYTAlloc::AllocatePageAligned(size)
- : NYTAlloc::Allocate(size));
+ ? ::aligned_malloc(size, GetPageSize())
+ : ::malloc(size));
}
void TBlob::Allocate(size_t newCapacity)
diff --git a/library/cpp/yt/memory/new-inl.h b/library/cpp/yt/memory/new-inl.h
index 40bf49d3e4..ea2a9bca04 100644
--- a/library/cpp/yt/memory/new-inl.h
+++ b/library/cpp/yt/memory/new-inl.h
@@ -4,8 +4,6 @@
#include "new.h"
#endif
-#include <library/cpp/ytalloc/api/ytalloc.h>
-
#include <library/cpp/yt/malloc//malloc.h>
namespace NYT {
@@ -94,17 +92,6 @@ struct TRefCountedWrapperWithCookie final
namespace NDetail {
-Y_FORCE_INLINE void* AllignedMalloc(size_t size, size_t allignment)
-{
-#ifdef _win_
- return ::_aligned_malloc(size, allignment);
-#else
- void* ptr = nullptr;
- ::posix_memalign(&ptr, allignment, size);
- return ptr;
-#endif
-}
-
template <class... Args>
Y_FORCE_INLINE void CustomInitialize(Args... args)
{
@@ -188,15 +175,11 @@ Y_FORCE_INLINE TIntrusivePtr<T> SafeConstruct(void* ptr, As&&... args)
template <size_t Size, size_t Alignment>
void* AllocateConstSizeAligned()
{
-#ifdef _win_
- return AllignedMalloc(Size, Alignment);
-#else
- if (Alignment <= 16) {
- return NYTAlloc::AllocateConstSize<Size>();
+ if (Alignment <= sizeof(std::max_align_t)) {
+ return ::malloc(Size);
} else {
- return AllignedMalloc(Size, Alignment);
+ return ::aligned_malloc(Size, Alignment);
}
-#endif
}
} // namespace NDetail
@@ -236,15 +219,11 @@ Y_FORCE_INLINE TIntrusivePtr<T> NewWithExtraSpace(
auto totalSize = NYT::NDetail::TConstructHelper<T>::Size + extraSpaceSize;
void* ptr = nullptr;
-#ifdef _win_
- ptr = NYT::NDetail::AllignedMalloc(totalSize, NYT::NDetail::TConstructHelper<T>::Alignment);
-#else
- if (NYT::NDetail::TConstructHelper<T>::Alignment <= 16) {
- ptr = NYTAlloc::Allocate(totalSize);
+ if (NYT::NDetail::TConstructHelper<T>::Alignment <= sizeof(std::max_align_t)) {
+ ptr = ::malloc(totalSize);
} else {
- ptr = NYT::NDetail::AllignedMalloc(totalSize, NYT::NDetail::TConstructHelper<T>::Alignment);
+ ptr = ::aligned_malloc(totalSize, NYT::NDetail::TConstructHelper<T>::Alignment);
}
-#endif
return NYT::NDetail::SafeConstruct<T>(ptr, std::forward<As>(args)...);
}
diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp
index faa9e0613b..aff7baedd3 100644
--- a/library/cpp/yt/memory/ref.cpp
+++ b/library/cpp/yt/memory/ref.cpp
@@ -1,7 +1,7 @@
#include "ref.h"
#include "blob.h"
-#include <library/cpp/ytalloc/api/ytalloc.h>
+#include <library/cpp/yt/malloc/malloc.h>
#include <util/system/info.h>
#include <util/system/align.h>
@@ -140,14 +140,14 @@ class TPageAlignedAllocationHolder
{
public:
TPageAlignedAllocationHolder(size_t size, TSharedMutableRefAllocateOptions options, TRefCountedTypeCookie cookie)
- : Begin_(static_cast<char*>(NYTAlloc::AllocatePageAligned(size)))
+ : Begin_(static_cast<char*>(::aligned_malloc(size, GetPageSize())))
{
Initialize(size, options, cookie);
}
~TPageAlignedAllocationHolder()
{
- NYTAlloc::Free(Begin_);
+ ::free(Begin_);
}
char* GetBegin()
diff --git a/library/cpp/yt/memory/ref_counted.h b/library/cpp/yt/memory/ref_counted.h
index e48d601c84..e2ca9245be 100644
--- a/library/cpp/yt/memory/ref_counted.h
+++ b/library/cpp/yt/memory/ref_counted.h
@@ -4,8 +4,6 @@
#include <library/cpp/yt/assert/assert.h>
-#include <library/cpp/ytalloc/api/ytalloc.h>
-
#include <atomic>
namespace NYT {
diff --git a/library/cpp/yt/memory/serialize.h b/library/cpp/yt/memory/serialize.h
index c113e04961..1f7dec38c0 100644
--- a/library/cpp/yt/memory/serialize.h
+++ b/library/cpp/yt/memory/serialize.h
@@ -2,6 +2,8 @@
#include "intrusive_ptr.h"
+#include <util/ysaveload.h>
+
////////////////////////////////////////////////////////////////////////////////
template <class T>
@@ -9,7 +11,6 @@ class TSerializer<NYT::TIntrusivePtr<T>>
{
public:
static inline void Save(IOutputStream* output, const NYT::TIntrusivePtr<T>& ptr);
-
static inline void Load(IInputStream* input, NYT::TIntrusivePtr<T>& ptr);
};