[proxy](4/n) use customised placement-new (#719)

* [proxy](4/n) use customized placement-new

* fix
This commit is contained in:
Schrodinger ZHU Yifan
2025-01-08 14:38:23 +08:00
committed by GitHub
parent 046c5ac766
commit d1c65c2852
6 changed files with 47 additions and 11 deletions

View File

@@ -6,6 +6,7 @@
#include "pool.h"
#include "remotecache.h"
#include "sizeclasstable.h"
#include "snmalloc/stl/new.h"
#include "ticker.h"
namespace snmalloc
@@ -1128,7 +1129,8 @@ namespace snmalloc
capptr::Alloc<void> spare_start = pointer_offset(raw, round_sizeof);
Range<capptr::bounds::Alloc> r{spare_start, spare};
auto p = capptr::Alloc<CA>::unsafe_from(new (raw.unsafe_ptr()) CA(r));
auto p = capptr::Alloc<CA>::unsafe_from(
new (raw.unsafe_ptr(), placement_token) CA(r));
// Remove excess from the bounds.
p = Aal::capptr_bound<CA, capptr::bounds::Alloc>(p, round_sizeof);

View File

@@ -35,6 +35,7 @@
#include "../ds/ds.h"
#include "entropy.h"
#include "snmalloc/stl/new.h"
#include <cstdint>
@@ -265,7 +266,7 @@ namespace snmalloc
static BHeadPtr<BView, BQueue> make(CapPtr<void, BView> p)
{
return CapPtr<Object::T<BQueue>, BView>::unsafe_from(
new (p.unsafe_ptr()) Object::T());
new (p.unsafe_ptr(), placement_token) Object::T());
}
/**

View File

@@ -3,6 +3,7 @@
#include "../ds/ds.h"
#include "freelist.h"
#include "sizeclasstable.h"
#include "snmalloc/stl/new.h"
namespace snmalloc
{
@@ -466,7 +467,7 @@ namespace snmalloc
large_ = false;
new (&client_meta_)
new (&client_meta_, placement_token)
typename ClientMeta::StorageType[get_client_storage_count(sizeclass)];
}
@@ -486,7 +487,7 @@ namespace snmalloc
// Jump to slow path on first deallocation.
needed() = 1;
new (&client_meta_) typename ClientMeta::StorageType();
new (&client_meta_, placement_token) typename ClientMeta::StorageType();
}
/**

View File

@@ -3,8 +3,6 @@
#include "../ds/ds.h"
#include "pooled.h"
#include <new>
namespace snmalloc
{
/**

View File

@@ -1,8 +1,7 @@
#pragma once
#include "freelist_queue.h"
#include <new>
#include "snmalloc/stl/new.h"
namespace snmalloc
{
@@ -43,7 +42,7 @@ namespace snmalloc
static auto emplace_in_alloc(capptr::Alloc<void> alloc)
{
return CapPtr<BatchedRemoteMessage, capptr::bounds::Alloc>::unsafe_from(
new (alloc.unsafe_ptr()) BatchedRemoteMessage());
new (alloc.unsafe_ptr(), placement_token) BatchedRemoteMessage());
}
static auto mk_from_freelist_builder(
@@ -67,7 +66,7 @@ namespace snmalloc
auto last_prev = last->prev;
auto self =
CapPtr<BatchedRemoteMessage, capptr::bounds::Alloc>::unsafe_from(
new (last.unsafe_ptr()) BatchedRemoteMessage());
new (last.unsafe_ptr(), placement_token) BatchedRemoteMessage());
self->free_ring.prev = last_prev;
// XXX On CHERI, we could do a fair bit better if we had a primitive for
@@ -222,7 +221,7 @@ namespace snmalloc
static auto emplace_in_alloc(capptr::Alloc<void> alloc)
{
return CapPtr<SingletonRemoteMessage, capptr::bounds::Alloc>::unsafe_from(
new (alloc.unsafe_ptr()) SingletonRemoteMessage());
new (alloc.unsafe_ptr(), placement_token) SingletonRemoteMessage());
}
static freelist::HeadPtr

35
src/snmalloc/stl/new.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "snmalloc/ds_core/defines.h"
namespace snmalloc
{
// This is used in both vendored and non-vendored mode.
struct PlacementToken
{};
inline constexpr PlacementToken placement_token{};
} // namespace snmalloc
SNMALLOC_FAST_PATH_INLINE void*
operator new(size_t, void* ptr, snmalloc::PlacementToken) noexcept
{
return ptr;
}
SNMALLOC_FAST_PATH_INLINE void*
operator new[](size_t, void* ptr, snmalloc::PlacementToken) noexcept
{
return ptr;
}
// The following is not really needed, but windows expects that new/delete
// definitions are paired.
SNMALLOC_FAST_PATH_INLINE void
operator delete(void*, void*, snmalloc::PlacementToken) noexcept
{}
SNMALLOC_FAST_PATH_INLINE void
operator delete[](void*, void*, snmalloc::PlacementToken) noexcept
{}