Skip to content

Commit 10a71dd

Browse files
author
babenko
committed
YT-25312: Fix TWithExtraSpace::GetExtraSpace to handle zero returned by malloc_usable_size
commit_hash:d4310bf35afeb00d408d28e96cf82ebafd82886b
1 parent 7a0ac03 commit 10a71dd

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

library/cpp/yt/memory/new-inl.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,17 @@ void* TWithExtraSpace<T>::GetExtraSpacePtr()
340340
}
341341

342342
template <class T>
343-
size_t TWithExtraSpace<T>::GetUsableSpaceSize() const
343+
std::optional<size_t> TWithExtraSpace<T>::GetUsableSpaceSize() const
344344
{
345345
#ifdef _win_
346346
return 0;
347347
#else
348-
return malloc_usable_size(const_cast<T*>(static_cast<const T*>(this))) - sizeof(T);
348+
size_t usableSize = malloc_usable_size(const_cast<T*>(static_cast<const T*>(this)));
349+
if (usableSize == 0) {
350+
return std::nullopt;
351+
}
352+
YT_ASSERT(usableSize >= sizeof(T));
353+
return usableSize - sizeof(T);
349354
#endif
350355
}
351356

library/cpp/yt/memory/new.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,14 @@ template <class T>
130130
class TWithExtraSpace
131131
{
132132
protected:
133+
//! Returns the pointer to the extra space associated with this instance.
133134
const void* GetExtraSpacePtr() const;
134135
void* GetExtraSpacePtr();
135-
size_t GetUsableSpaceSize() const;
136+
137+
//! Returns the size of the extra space associated with this instance.
138+
//! This is determined via the call to |malloc_usable_size| and may be
139+
//! null if the allocator support is unavailable.
140+
std::optional<size_t> GetUsableSpaceSize() const;
136141
};
137142

138143
////////////////////////////////////////////////////////////////////////////////

library/cpp/yt/memory/ref.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class TDefaultAllocationHolder
150150
TRefCountedTypeCookie cookie)
151151
{
152152
if (options.ExtendToUsableSize) {
153-
if (auto usableSize = GetUsableSpaceSize(); usableSize != 0) {
154-
size = usableSize;
153+
if (auto usableSize = GetUsableSpaceSize()) {
154+
size = *usableSize;
155155
}
156156
}
157157
Initialize(size, options, cookie);

0 commit comments

Comments
 (0)