Skip to content

Commit

Permalink
[proxy](4/n) use customized placement-new
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Dec 21, 2024
1 parent f11ad47 commit dbe7e47
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/snmalloc/mem/corealloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pool.h"
#include "remotecache.h"
#include "sizeclasstable.h"
#include "snmalloc/proxy/new.h"
#include "ticker.h"

namespace snmalloc
Expand Down Expand Up @@ -1119,7 +1120,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);
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/mem/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "../ds/ds.h"
#include "entropy.h"
#include "snmalloc/proxy/new.h"

#include <cstdint>

Expand Down Expand Up @@ -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());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/snmalloc/mem/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../ds/ds.h"
#include "freelist.h"
#include "sizeclasstable.h"
#include "snmalloc/proxy/new.h"

namespace snmalloc
{
Expand Down Expand Up @@ -466,7 +467,7 @@ namespace snmalloc

large_ = false;

new (&client_meta_)
new (&client_meta_, placement_token)
typename ClientMeta::StorageType[get_client_storage_count(sizeclass)];
}

Expand All @@ -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();
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/snmalloc/mem/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "../ds/ds.h"
#include "pooled.h"

#include <new>

namespace snmalloc
{
/**
Expand Down
9 changes: 4 additions & 5 deletions src/snmalloc/mem/remoteallocator.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

#include "freelist_queue.h"

#include <new>
#include "snmalloc/proxy/new.h"

namespace snmalloc
{
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions src/snmalloc/proxy/new.h
Original file line number Diff line number Diff line change
@@ -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
{}

0 comments on commit dbe7e47

Please sign in to comment.