Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp41 committed Feb 21, 2025
1 parent 0fd6768 commit 41392d1
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 726 deletions.
2 changes: 2 additions & 0 deletions docs/AddressSpace.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ We give here some notes on the internal orchestration.
Consider a first, "small" allocation (typically less than a platform page); such allocations showcase more of the machinery.
For simplicity, we assume that

TODO CoreAllocator rewrite here:

- this is not an `OPEN_ENCLAVE` build,
- the `BackendAllocator` has not been told to use a `fixed_range`,
- this is not a `SNMALLOC_CHECK_CLIENT` build, and
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/backend/fixedglobalconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace snmalloc
public:
using LocalState = StandardLocalState<PAL, Pagemap>;

using GlobalPoolState = PoolState<CoreAllocator<FixedRangeConfig>>;
using GlobalPoolState = PoolState<Allocator<FixedRangeConfig>>;

using Backend =
BackendAllocator<PAL, PagemapEntry, Pagemap, Authmap, LocalState>;
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/backend/globalconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace snmalloc
template<typename ClientMetaDataProvider = NoClientMetaDataProvider>
class StandardConfigClientMeta final : public CommonConfig
{
using GlobalPoolState = PoolState<
CoreAllocator<StandardConfigClientMeta<ClientMetaDataProvider>>>;
using GlobalPoolState =
PoolState<Allocator<StandardConfigClientMeta<ClientMetaDataProvider>>>;

public:
using Pal = DefaultPal;
Expand Down
10 changes: 5 additions & 5 deletions src/snmalloc/ds_core/concept.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* use concept-qualified parameters should use this to remain compatible across
* C++ versions: "template<SNMALLOC_CONCEPT(FooConcept) Foo>"
*/
#ifdef __cpp_concepts
# define SNMALLOC_CONCEPT(c) c
#else
# define SNMALLOC_CONCEPT(c) typename
#endif
// #ifdef __cpp_concepts
// # define SNMALLOC_CONCEPT(c) c
// #else
#define SNMALLOC_CONCEPT(c) typename
// #endif

#ifdef __cpp_concepts
namespace snmalloc
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/global/globalalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ namespace snmalloc

SNMALLOC_FAST_PATH_INLINE void debug_teardown()
{
return ThreadAlloc::get().teardown();
return ThreadAlloc::teardown();
}

template<SNMALLOC_CONCEPT(IsConfig) Config = Config>
Expand Down
10 changes: 6 additions & 4 deletions src/snmalloc/global/scopedalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ namespace snmalloc
/**
* The allocator that this wrapper will use.
*/
SAlloc alloc;
SAlloc* alloc;

/**
* Constructor. Claims an allocator from the global pool
*/
ScopedAllocator()
{
alloc.init();
alloc = AllocPool<typename SAlloc::Config>::acquire();
};

/**
Expand Down Expand Up @@ -60,7 +60,9 @@ namespace snmalloc
*/
~ScopedAllocator()
{
alloc.flush();
alloc->flush();
AllocPool<typename SAlloc::Config>::release(alloc);
alloc = nullptr;
}

/**
Expand All @@ -69,7 +71,7 @@ namespace snmalloc
*/
SAlloc* operator->()
{
return &alloc;
return alloc;
}
};

Expand Down
54 changes: 33 additions & 21 deletions src/snmalloc/global/threadalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ namespace snmalloc
return ThreadAllocExternal::get();
}

static void teardown()
{
Alloc* alloc = &get();
alloc->flush();
AllocPool<Config>::release(alloc);
}

// This will always call the success path as the client is responsible
// handling the initialisation.
using CheckInit = CheckInitDefault;
Expand All @@ -63,7 +70,10 @@ namespace snmalloc
*/
class ThreadAlloc
{
SNMALLOC_REQUIRE_CONSTINIT static inline thread_local Alloc alloc{};
SNMALLOC_REQUIRE_CONSTINIT static const inline Alloc default_alloc{true};

SNMALLOC_REQUIRE_CONSTINIT static inline thread_local Alloc* alloc{
const_cast<Alloc*>(&default_alloc)};

// As allocation and deallocation can occur during thread teardown
// we need to record if we are already in that state as we will not
Expand All @@ -81,7 +91,19 @@ namespace snmalloc
*/
static SNMALLOC_FAST_PATH Alloc& get()
{
return alloc;
return *alloc;
}

static void teardown()
{
// No work required for teardown.
if (alloc == &default_alloc)
return;

teardown_called = true;
alloc->flush();
AllocPool<Config>::release(alloc);
alloc = const_cast<Alloc*>(&default_alloc);
}

template<typename Subclass>
Expand All @@ -104,11 +126,7 @@ namespace snmalloc
}
else
{
// Initialise the thread local allocator
if constexpr (Config::Options.CoreAllocOwnsLocalState)
{
alloc.init();
}
alloc = AllocPool<Config>::acquire();

// register_clean_up must be called after init. register clean up
// may be implemented with allocation, so need to ensure we have a
Expand All @@ -121,7 +139,7 @@ namespace snmalloc
Subclass::register_clean_up();

// Perform underlying operation
return r(args...);
return r(alloc, args...);
}

OnDestruct od([]() {
Expand All @@ -130,11 +148,11 @@ namespace snmalloc
# endif
// We didn't have an allocator because the thread is being torndown.
// We need to return any local state, so we don't leak it.
alloc.teardown();
ThreadAlloc::teardown();
});

// Perform underlying operation
return r(args...);
return r(alloc, args...);
}
}

Expand All @@ -143,19 +161,13 @@ namespace snmalloc
SNMALLOC_FAST_PATH static auto
check_init(Success s, Restart r, Args... args)
{
if (alloc.is_init())
if (alloc != &default_alloc)
{
return s();
}

return check_init_slow(r, args...);
}

static void teardown()
{
teardown_called = true;
alloc.teardown();
}
};
# ifdef SNMALLOC_USE_PTHREAD_DESTRUCTORS
using CheckInit = CheckInitPthread;
Expand All @@ -175,15 +187,15 @@ namespace snmalloc
*/
static void pthread_cleanup(void*)
{
teardown();
ThreadAlloc::teardown();
}

/**
* Used to give correct signature to teardown required by atexit.
*/
static void pthread_cleanup_main_thread()
{
teardown();
ThreadAlloc::teardown();
}

/**
Expand Down Expand Up @@ -234,7 +246,7 @@ namespace snmalloc
*/
static void register_clean_up()
{
static thread_local OnDestruct dummy([]() { teardown(); });
static thread_local OnDestruct dummy([]() { ThreadAlloc::teardown(); });
UNUSED(dummy);
# ifdef SNMALLOC_TRACING
message<1024>("Using C++ destructor clean up");
Expand All @@ -253,6 +265,6 @@ namespace snmalloc
SNMALLOC_USED_FUNCTION
inline void _malloc_thread_cleanup()
{
snmalloc::ThreadAlloc::get().teardown();
ThreadAlloc::teardown();
}
#endif
Loading

0 comments on commit 41392d1

Please sign in to comment.