Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate locking from static range #540

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 4 additions & 31 deletions src/snmalloc/backend_helpers/globalrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../ds/ds.h"
#include "empty_range.h"
#include "lockrange.h"
#include "staticrange.h"

namespace snmalloc
{
Expand All @@ -12,36 +14,7 @@ namespace snmalloc
struct GlobalRange
{
template<typename ParentRange = EmptyRange<>>
class Type : public StaticParent<ParentRange>
{
using StaticParent<ParentRange>::parent;

/**
* This is infrequently used code, a spin lock simplifies the code
* considerably, and should never be on the fast path.
*/
SNMALLOC_REQUIRE_CONSTINIT static inline FlagWord spin_lock{};

public:
static constexpr bool Aligned = ParentRange::Aligned;

static constexpr bool ConcurrencySafe = true;

using ChunkBounds = typename ParentRange::ChunkBounds;

constexpr Type() = default;

CapPtr<void, ChunkBounds> alloc_range(size_t size)
{
FlagLock lock(spin_lock);
return parent.alloc_range(size);
}

void dealloc_range(CapPtr<void, ChunkBounds> base, size_t size)
{
FlagLock lock(spin_lock);
parent.dealloc_range(base, size);
}
};
class Type : public Pipe<ParentRange, LockRange, StaticRange>
{};
};
} // namespace snmalloc
46 changes: 46 additions & 0 deletions src/snmalloc/backend_helpers/lockrange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "../ds/ds.h"
#include "empty_range.h"

namespace snmalloc
{
/**
* Protect the ParentRange with a spin lock.
nwf-msr marked this conversation as resolved.
Show resolved Hide resolved
*/
struct LockRange
{
template<typename ParentRange = EmptyRange<>>
class Type : public ContainsParent<ParentRange>
{
using ContainsParent<ParentRange>::parent;

/**
* This is infrequently used code, a spin lock simplifies the code
* considerably, and should never be on the fast path.
*/
FlagWord spin_lock{};

public:
static constexpr bool Aligned = ParentRange::Aligned;

using ChunkBounds = typename ParentRange::ChunkBounds;

static constexpr bool ConcurrencySafe = true;

constexpr Type() = default;

CapPtr<void, ChunkBounds> alloc_range(size_t size)
{
FlagLock lock(spin_lock);
return parent.alloc_range(size);
}

void dealloc_range(CapPtr<void, ChunkBounds> base, size_t size)
{
FlagLock lock(spin_lock);
parent.dealloc_range(base, size);
}
};
};
} // namespace snmalloc
42 changes: 42 additions & 0 deletions src/snmalloc/backend_helpers/staticrange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "../ds/ds.h"
#include "empty_range.h"

namespace snmalloc
{
/**
* Makes the supplied ParentRange into a global variable.
*/
struct StaticRange
{
template<typename ParentRange = EmptyRange<>>
class Type : public StaticParent<ParentRange>
{
using StaticParent<ParentRange>::parent;

public:
static constexpr bool Aligned = ParentRange::Aligned;

static_assert(
ParentRange::ConcurrencySafe,
"StaticRange requires a concurrency safe parent.");

static constexpr bool ConcurrencySafe = true;

using ChunkBounds = typename ParentRange::ChunkBounds;

constexpr Type() = default;

CapPtr<void, ChunkBounds> alloc_range(size_t size)
{
return parent.alloc_range(size);
}

void dealloc_range(CapPtr<void, ChunkBounds> base, size_t size)
{
parent.dealloc_range(base, size);
}
};
};
} // namespace snmalloc