Skip to content

Commit 219769b

Browse files
authored
Reorganise allocators & improve tracking (#297)
1 parent 64e3a24 commit 219769b

26 files changed

Lines changed: 136 additions & 103 deletions

src/libs/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ add_subdirectory(thread)
88
add_subdirectory(banner)
99
add_subdirectory(commands)
1010
add_subdirectory(service)
11-
add_subdirectory(memory)
11+
add_subdirectory(allocators)
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

7-
set(LIBRARY_NAME memory)
7+
set(LIBRARY_NAME allocators)
88

99
add_library(${LIBRARY_NAME} STATIC
10-
include/memory/AsioAllocator.h
11-
include/memory/PoolAllocator.h
12-
include/memory/StaticAllocator.h
10+
include/allocators/AsioAllocator.h
11+
include/allocators/PoolAllocator.h
12+
include/allocators/StaticAllocator.h
13+
include/allocators/DefaultAllocator.h
14+
include/allocators/TLSBlockAllocator.h
15+
include/allocators/BlockAllocator.h
16+
include/allocators/Tracking.h
17+
include/allocators/AllocTrack.h
1318
Dummy.cpp
1419
)
1520

1621
target_include_directories(${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
17-
target_link_libraries(${LIBRARY_NAME} PUBLIC shared spark ${Boost_LIBRARIES})
18-
set_target_properties(${LIBRARY_NAME} PROPERTIES FOLDER "Common Libraries")
22+
target_link_libraries(${LIBRARY_NAME} PUBLIC shared ${Boost_LIBRARIES})
23+
set_target_properties(${LIBRARY_NAME} PROPERTIES FOLDER "Common Libraries")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2026 Ember
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#pragma once
10+
11+
#ifdef EMBER_DEBUG_ALLOCATOR_TRACKING
12+
#include <allocators/Tracking.h>
13+
#define ALLOC_TRACK(tag, type, ...) \
14+
ember::allocators::tracking::record(tag, type __VA_OPT__(,) __VA_ARGS__);
15+
#else
16+
#define ALLOC_TRACK(tag, type, ...)
17+
#endif

src/libs/core/memory/include/memory/AsioAllocator.h renamed to src/libs/core/allocators/include/allocators/AsioAllocator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
#include "PoolAllocator.h"
1212
#include "StaticAllocator.h"
13-
#include <memory/AllocReport.h>
13+
#include <allocators/AllocTrack.h>
1414
#include <string_view>
1515
#include <cstdlib>
1616

17-
namespace ember {
17+
namespace ember::allocators {
1818

1919
template<typename T, typename Allocator>
2020
class AsioAllocator {
@@ -62,4 +62,4 @@ class AsioAllocator {
6262
}
6363
};
6464

65-
} // ember
65+
} // allocators, ember

src/libs/spark/include/spark/buffers/allocators/BlockAllocator.h renamed to src/libs/core/allocators/include/allocators/BlockAllocator.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#pragma once
1010

11-
#include <spark/buffers/allocators/HugePages.h>
11+
#include <allocators/AllocTrack.h>
12+
#include <allocators/HugePages.h>
1213
#include <shared/utility/Utility.h>
1314
#include <array>
1415
#include <memory>
@@ -29,7 +30,7 @@
2930
#define EMBER_DEBUG_ALLOCATORS
3031
#endif
3132

32-
namespace ember::spark::io {
33+
namespace ember::allocators {
3334

3435
struct NoPageLock {};
3536
struct PageLock final : NoPageLock {};
@@ -163,6 +164,7 @@ class BlockAllocator {
163164
}
164165

165166
void initialise(std::size_t elements) {
167+
ALLOC_TRACK(tag_, mem_rep_create);
166168
allocate_storage(elements);
167169
page_lock_conditional();
168170
initialise_free_list();
@@ -199,12 +201,14 @@ class BlockAllocator {
199201
++storage_active_count;
200202
#endif
201203
block->meta.using_new = false;
204+
ALLOC_TRACK(tag_, mem_rep_alloc);
202205
} else {
203206
#ifdef EMBER_DEBUG_ALLOCATORS
204207
++new_active_count;
205208
#endif
206209
block = new Block;
207210
block->meta.using_new = true;
211+
ALLOC_TRACK(tag_, mem_rep_system_alloc);
208212
}
209213

210214
if constexpr(std::is_same_v<ValidatePolicy, ValidateDealloc>) {
@@ -215,6 +219,7 @@ class BlockAllocator {
215219
++total_allocs;
216220
++active_count;
217221
#endif
222+
ALLOC_TRACK(tag_, mem_rep_bytes_alloc, sizeof(Block));
218223
return new (&block->obj) _ty(std::forward<Args>(args)...);
219224
}
220225

@@ -233,18 +238,21 @@ class BlockAllocator {
233238
#endif
234239
t->~_ty();
235240
operator delete(block);
241+
ALLOC_TRACK(tag_, mem_rep_system_dealloc);
236242
} else {
237243
#ifdef EMBER_DEBUG_ALLOCATORS
238244
--storage_active_count;
239245
#endif
240246
t->~_ty();
241247
push(block);
248+
ALLOC_TRACK(tag_, mem_rep_dealloc);
242249
}
243250

244251
#ifdef EMBER_DEBUG_ALLOCATORS
245252
++total_deallocs;
246253
--active_count;
247254
#endif
255+
ALLOC_TRACK(tag_, mem_rep_bytes_dealloc, sizeof(Block));
248256
}
249257

250258
std::string_view tag() const {
@@ -258,7 +266,8 @@ class BlockAllocator {
258266
#ifdef EMBER_DEBUG_ALLOCATORS
259267
assert(active_count == 0);
260268
#endif
269+
ALLOC_TRACK(tag_, mem_rep_destroy);
261270
}
262271
};
263272

264-
} // io, spark, ember
273+
} // allocators, ember

src/libs/spark/include/spark/buffers/allocators/DefaultAllocator.h renamed to src/libs/core/allocators/include/allocators/DefaultAllocator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#pragma once
1010

11+
#include <allocators/AllocTrack.h>
1112
#include <string_view>
1213
#include <utility>
1314

14-
namespace ember::spark::io {
15+
namespace ember::allocators {
1516

1617
template<typename T>
1718
struct DefaultAllocator final {
@@ -49,4 +50,4 @@ struct DefaultAllocator final {
4950
}
5051
};
5152

52-
} // io, spark, ember
53+
} // allocators, ember

src/libs/spark/include/spark/buffers/allocators/HugePages.h renamed to src/libs/core/allocators/include/allocators/HugePages.h

File renamed without changes.

src/libs/core/memory/include/memory/PoolAllocator.h renamed to src/libs/core/allocators/include/allocators/PoolAllocator.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
#pragma once
1010

11-
#include <memory/AllocReport.h>
11+
#include <allocators/AllocTrack.h>
1212
#include <boost/pool/pool.hpp>
1313
#include <array>
1414
#include <cstddef>
1515

16-
namespace ember {
16+
namespace ember::allocators {
1717

1818
class PoolAllocator final {
1919
public:
@@ -31,6 +31,9 @@ class PoolAllocator final {
3131
};
3232

3333
private:
34+
static constexpr std::string_view default_tag { "asio_pool_allocator" };
35+
std::string_view tag;
36+
3437
constexpr static auto small_chunks = 8;
3538
constexpr static auto medium_chunks = 8;
3639
constexpr static auto large_chunks = 8;
@@ -72,14 +75,14 @@ class PoolAllocator final {
7275
}
7376

7477
public:
75-
static constexpr std::string_view tag { "asio_pool_allocator" };
76-
77-
PoolAllocator() {
78+
PoolAllocator(std::string_view tag = default_tag)
79+
: tag(tag) {
7880
ALLOC_TRACK(tag, mem_rep_create);
7981
}
8082

81-
PoolAllocator(const Config& conf)
82-
: pools(init_pools(conf)) {
83+
PoolAllocator(const Config& conf, std::string_view tag = default_tag)
84+
: tag(tag)
85+
, pools(init_pools(conf)) {
8386
ALLOC_TRACK(tag, mem_rep_create);
8487
}
8588

@@ -114,4 +117,4 @@ class PoolAllocator final {
114117
}
115118
};
116119

117-
} // ember
120+
} // allocators, ember

src/libs/core/memory/include/memory/StaticAllocator.h renamed to src/libs/core/allocators/include/allocators/StaticAllocator.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@
88

99
#pragma once
1010

11-
#include <spark/buffers/allocators/BlockAllocator.h>
12-
#include <memory/AllocReport.h>
11+
#include <allocators/AllocTrack.h>
12+
#include <allocators/BlockAllocator.h>
1313
#include <array>
1414
#include <string_view>
1515
#include <cstddef>
1616
#include <cstdlib>
1717

18-
namespace ember {
18+
namespace ember::allocators {
1919

2020
template<std::size_t _size, std::size_t _elements>
2121
class StaticAllocator final {
22-
static constexpr std::string_view tag { "asio_static_allocator" };
22+
static constexpr std::string_view tag { "asio_static" };
2323

2424
struct alignas(std::max_align_t) AlignedStorage {
2525
std::byte storage[_size];
2626
};
2727

28-
template<typename _alloc = spark::io::DefaultBlockAllocator>
29-
using BlockAllocator = spark::io::BlockAllocator<
30-
AlignedStorage, spark::io::NoPageLock, spark::io::NoValidateDealloc, _alloc
28+
template<typename _alloc = DefaultBlockAllocator>
29+
using BlockAllocator = BlockAllocator<
30+
AlignedStorage, NoPageLock, NoValidateDealloc, _alloc
3131
>;
3232

3333
class SlabAllocator {
34-
static constexpr std::string_view tag { "asio_static_slab_allocator" };
34+
static constexpr std::string_view tag { "asio_static_slab" };
3535
static constexpr std::size_t slab_size = BlockAllocator<>::block_size * _elements;
3636

3737
std::array<std::byte, slab_size> storage;
@@ -75,7 +75,13 @@ class StaticAllocator final {
7575
BlockAllocator<SlabAllocator> allocator;
7676

7777
public:
78-
StaticAllocator() : allocator(_elements, tag) {
78+
StaticAllocator()
79+
: allocator(_elements, tag) {
80+
ALLOC_TRACK(tag, mem_rep_create);
81+
}
82+
83+
StaticAllocator(std::string_view tag)
84+
: allocator(_elements, tag) {
7985
ALLOC_TRACK(tag, mem_rep_create);
8086
}
8187

@@ -108,4 +114,4 @@ class StaticAllocator final {
108114
}
109115
};
110116

111-
} // ember
117+
} // allocators, ember

0 commit comments

Comments
 (0)