Skip to content

Commit 3ae29aa

Browse files
HouseOfHufflepuffarvidn
authored andcommitted
remove malloc_usable_size() and friends from buffer
malloc_usable_size(), _msize() and malloc_size() are meant for diagnostics, not for relying on in release builds. Arch Linux already patches this out downstream for -D_FORTIFY_SOURCE=3 compatibility, which treats the call as a fatal memory violation. buffer already fell back to the requested (8-byte aligned) size on platforms without one of these functions, so this just makes that the only behavior, and drops the now-unused platform headers. Fixes #8051
1 parent 05c44cc commit 3ae29aa

1 file changed

Lines changed: 2 additions & 26 deletions

File tree

include/libtorrent/aux_/buffer.hpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,11 @@ see LICENSE file.
2424
#include "libtorrent/span.hpp"
2525
#include "libtorrent/aux_/throw.hpp"
2626

27-
#if defined __GLIBC__
28-
#include <malloc.h>
29-
#elif defined _MSC_VER
30-
#include <malloc.h>
31-
#elif defined __FreeBSD__
32-
#include <malloc_np.h>
33-
#elif defined __APPLE__
34-
#include <malloc/malloc.h>
35-
#endif
36-
3727
namespace libtorrent {
3828
namespace aux {
3929

4030
// the buffer is allocated once and cannot be resized. The size() may be
41-
// larger than requested, in case the underlying allocator over allocated. In
31+
// larger than requested, since allocations are rounded up to 8 bytes. In
4232
// order to "grow" an allocation, create a new buffer and initialize it by
4333
// the range of bytes from the existing, and move-assign the new over the
4434
// old.
@@ -55,27 +45,13 @@ class buffer
5545

5646
if (size <= 0) return;
5747

58-
// this rounds up the size to be 8 bytes aligned
59-
// it mostly makes sense for platforms without support
60-
// for a variation of "malloc_size()"
48+
// round up the size to be 8 bytes aligned
6149
size = (size + 7) & (~difference_type(0x7));
6250

63-
// we have to use malloc here, to be compatible with the fancy query
64-
// functions below
6551
m_begin = static_cast<char*>(std::malloc(static_cast<std::size_t>(size)));
6652
if (m_begin == nullptr) aux::throw_ex<std::bad_alloc>();
6753

68-
// the actual allocation may be larger than we requested. If so, let the
69-
// user take advantage of every single byte
70-
#if (defined __GLIBC__ && !defined __UCLIBC__) || defined __FreeBSD__
71-
m_size = static_cast<difference_type>(::malloc_usable_size(m_begin));
72-
#elif defined _MSC_VER
73-
m_size = static_cast<difference_type>(::_msize(m_begin));
74-
#elif defined __APPLE__
75-
m_size = static_cast<difference_type>(::malloc_size(m_begin));
76-
#else
7754
m_size = size;
78-
#endif
7955
}
8056

8157
// allocate an uninitialized buffer of the specified size

0 commit comments

Comments
 (0)