|
|
|
@ -31,11 +31,23 @@ |
|
|
|
|
#include "src/core/lib/gpr/alloc.h" |
|
|
|
|
#include "src/core/lib/gprpp/memory.h" |
|
|
|
|
|
|
|
|
|
template <size_t alignment> |
|
|
|
|
static void* gpr_arena_malloc(size_t size) { |
|
|
|
|
return gpr_malloc_aligned(size, alignment); |
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
void* ArenaStorage(size_t initial_size) { |
|
|
|
|
static constexpr size_t base_size = |
|
|
|
|
GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_core::Arena)); |
|
|
|
|
initial_size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(initial_size); |
|
|
|
|
size_t alloc_size = base_size + initial_size; |
|
|
|
|
static constexpr size_t alignment = |
|
|
|
|
(GPR_CACHELINE_SIZE > GPR_MAX_ALIGNMENT && |
|
|
|
|
GPR_CACHELINE_SIZE % GPR_MAX_ALIGNMENT == 0) |
|
|
|
|
? GPR_CACHELINE_SIZE |
|
|
|
|
: GPR_MAX_ALIGNMENT; |
|
|
|
|
return gpr_malloc_aligned(alloc_size, alignment); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
namespace grpc_core { |
|
|
|
|
|
|
|
|
|
Arena::~Arena() { |
|
|
|
@ -49,16 +61,17 @@ Arena::~Arena() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Arena* Arena::Create(size_t initial_size) { |
|
|
|
|
return new (ArenaStorage(initial_size)) Arena(initial_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Pair<Arena*, void*> Arena::CreateWithAlloc(size_t initial_size, |
|
|
|
|
size_t alloc_size) { |
|
|
|
|
static constexpr size_t base_size = |
|
|
|
|
GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena)); |
|
|
|
|
initial_size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(initial_size); |
|
|
|
|
size_t alloc_size = base_size + initial_size; |
|
|
|
|
static constexpr size_t alignment = |
|
|
|
|
(GPR_CACHELINE_SIZE > GPR_MAX_ALIGNMENT && |
|
|
|
|
GPR_CACHELINE_SIZE % GPR_MAX_ALIGNMENT == 0) |
|
|
|
|
? GPR_CACHELINE_SIZE |
|
|
|
|
: GPR_MAX_ALIGNMENT; |
|
|
|
|
return new (gpr_arena_malloc<alignment>(alloc_size)) Arena(initial_size); |
|
|
|
|
auto* new_arena = |
|
|
|
|
new (ArenaStorage(initial_size)) Arena(initial_size, alloc_size); |
|
|
|
|
void* first_alloc = reinterpret_cast<char*>(new_arena) + base_size; |
|
|
|
|
return MakePair(new_arena, first_alloc); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size_t Arena::Destroy() { |
|
|
|
@ -77,7 +90,7 @@ void* Arena::AllocZone(size_t size) { |
|
|
|
|
static constexpr size_t zone_base_size = |
|
|
|
|
GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Zone)); |
|
|
|
|
size_t alloc_size = zone_base_size + size; |
|
|
|
|
Zone* z = new (gpr_arena_malloc<GPR_MAX_ALIGNMENT>(alloc_size)) Zone(); |
|
|
|
|
Zone* z = new (gpr_malloc_aligned(alloc_size, GPR_MAX_ALIGNMENT)) Zone(); |
|
|
|
|
{ |
|
|
|
|
gpr_spinlock_lock(&arena_growth_spinlock_); |
|
|
|
|
z->prev = last_zone_; |
|
|
|
|