Skip to content

Commit 21369f4

Browse files
committed
feat: custom allocator, add provider functions and context named
1 parent da314e4 commit 21369f4

31 files changed

+372
-57
lines changed

build_recipe.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ sources:
179179
outdir: include/ecsact/entt/detail
180180
- path: ./ecsact/entt/detail/globals.hh
181181
outdir: include/ecsact/entt/detail
182+
- path: ./ecsact/entt/detail/registry.hh
183+
outdir: include/ecsact/entt/detail
182184
- path: ./ecsact/entt/detail/internal_markers.hh
183185
outdir: include/ecsact/entt/detail
184186
- path: ./ecsact/entt/detail/system_execution_context.hh
@@ -206,6 +208,7 @@ sources:
206208
- ./runtime/ecsact_rt_entt_core.cc
207209
- ./runtime/ecsact_rt_entt_dynamic.cc
208210
- ./runtime/hash.cc
211+
- ./runtime/allocator.cc
209212

210213
exports:
211214
# core

ecsact/entt/detail/allocator.hh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <array>
5+
#include <memory>
6+
#include <atomic>
7+
#include <type_traits>
8+
#include <iostream>
9+
10+
namespace ecsact::entt::detail {
11+
12+
template<std::size_t Alignment, std::size_t Size>
13+
struct static_allocator_storage {
14+
using buffer_t = std::array<std::byte, Size>;
15+
16+
std::unique_ptr<buffer_t> buffer_ = std::make_unique<buffer_t>();
17+
std::atomic_size_t offset_ = 0;
18+
};
19+
20+
struct static_allocator_base {
21+
static constexpr std::size_t static_size = 1e+8;
22+
23+
static static_allocator_storage<1, static_size> storage1_;
24+
static static_allocator_storage<4, static_size> storage4_;
25+
static static_allocator_storage<8, static_size> storage8_;
26+
};
27+
28+
template<typename T>
29+
struct static_allocator : static_allocator_base {
30+
using value_type = T;
31+
32+
static_allocator() = default;
33+
static_allocator(const static_allocator& other) = default;
34+
35+
static_allocator(static_allocator&& other) = default;
36+
37+
template<typename U>
38+
static_allocator(const static_allocator<U>&) {
39+
}
40+
41+
template<typename U>
42+
static_allocator(static_allocator<U>&&) {
43+
}
44+
45+
[[nodiscard]] auto allocate(std::size_t n) noexcept -> T* {
46+
constexpr auto alignment = std::alignment_of_v<T>;
47+
48+
if constexpr(alignment == 1) {
49+
std::size_t alloc_start = storage1_.offset_.fetch_add(sizeof(T) * n);
50+
return std::assume_aligned<alignment>(
51+
reinterpret_cast<T*>(storage1_.buffer_->data() + alloc_start)
52+
);
53+
}
54+
55+
if constexpr(alignment == 4) {
56+
std::size_t alloc_start = storage4_.offset_.fetch_add(sizeof(T) * n);
57+
return std::assume_aligned<alignment>(
58+
reinterpret_cast<T*>(storage4_.buffer_->data() + alloc_start)
59+
);
60+
}
61+
62+
if constexpr(alignment == 8) {
63+
std::size_t alloc_start = storage8_.offset_.fetch_add(sizeof(T) * n);
64+
return std::assume_aligned<alignment>(
65+
reinterpret_cast<T*>(storage8_.buffer_->data() + alloc_start)
66+
);
67+
}
68+
69+
static_assert(
70+
alignment == 1 || alignment == 4 || alignment == 8,
71+
"Missing support for alignment"
72+
);
73+
}
74+
75+
auto deallocate(T* p, std::size_t n) noexcept -> void {
76+
}
77+
};
78+
79+
template<typename T, typename U>
80+
auto operator==(const static_allocator<T>&, const static_allocator<U>&)
81+
-> bool {
82+
return true;
83+
}
84+
85+
template<typename T, typename U>
86+
auto operator!=(const static_allocator<T>&, const static_allocator<U>&)
87+
-> bool {
88+
return false;
89+
}
90+
91+
} // namespace ecsact::entt::detail

ecsact/entt/detail/apply_pending.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace ecsact::entt::detail {
88

99
template<typename C>
10-
auto apply_pending_add(::entt::registry& registry) -> void {
10+
auto apply_pending_add(ecsact::entt::registry_t& registry) -> void {
1111
if constexpr(std::is_empty_v<C>) {
1212
registry.view<pending_add<C>>().each([&](auto entity) {
1313
registry.emplace<C>(entity);
@@ -26,7 +26,7 @@ auto apply_pending_add(::entt::registry& registry) -> void {
2626
}
2727

2828
template<typename C>
29-
auto apply_pending_remove(::entt::registry& registry) -> void {
29+
auto apply_pending_remove(ecsact::entt::registry_t& registry) -> void {
3030
registry.view<pending_remove<C>>().each([&](auto entity) {
3131
if constexpr(!std::is_empty_v<C>) {
3232
registry.erase<exec_beforechange_storage<C>>(entity);

ecsact/entt/detail/globals.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ecsact/runtime/core.h"
88
#include "ecsact/runtime/dynamic.h"
99
#include "ecsact/entt/detail/system_execution_context.hh"
10+
#include "ecsact/entt/detail/registry.hh"
1011

1112
/**
1213
* A small set of globals expected to be available the ecsact_rt_entt_codegen
@@ -19,7 +20,7 @@ namespace ecsact::entt::detail::globals {
1920
*/
2021
extern std::unordered_map< //
2122
ecsact_registry_id,
22-
::entt::registry>
23+
ecsact::entt::registry_t>
2324
registries;
2425

2526
/**

ecsact/entt/detail/internal_markers.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ constexpr bool system_markers_unimplemented_by_codegen = false;
8787

8888
template<typename C>
8989
auto add_system_markers_if_needed( //
90-
::entt::registry&,
90+
ecsact::entt::registry_t&,
9191
ecsact::entt::entity_id
9292
) -> void {
9393
static_assert(system_markers_unimplemented_by_codegen<C>, R"(
@@ -101,7 +101,7 @@ auto add_system_markers_if_needed( //
101101

102102
template<typename C>
103103
auto remove_system_markers_if_needed( //
104-
::entt::registry&,
104+
ecsact::entt::registry_t&,
105105
ecsact::entt::entity_id
106106
) -> void {
107107
static_assert(system_markers_unimplemented_by_codegen<C>, R"(
@@ -115,7 +115,7 @@ auto remove_system_markers_if_needed( //
115115

116116
template<typename C>
117117
auto add_exec_itr_beforechange_if_needed( //
118-
::entt::registry&,
118+
ecsact::entt::registry_t&,
119119
ecsact::entt::entity_id
120120
) -> void {
121121
static_assert(system_markers_unimplemented_by_codegen<C>, R"(

ecsact/entt/detail/registry.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include "entt/entity/registry.hpp"
4+
#include "ecsact/entt/detail/allocator.hh"
5+
6+
namespace ecsact::entt {
7+
8+
using registry_t = ::entt::
9+
basic_registry<::entt::entity, detail::static_allocator<::entt::entity>>;
10+
}
11+
12+
// using registry_t = ::entt::
13+
// basic_registry<::entt::entity, detail::static_allocator<::entt::entity>>;
14+
// }

ecsact/entt/detail/system_execution_context.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "ecsact/runtime/common.h"
1111
#include "ecsact/entt/event_markers.hh"
1212
#include "ecsact/entt/entity.hh"
13+
#include "ecsact/entt/detail/registry.hh"
1314

1415
namespace ecsact::entt {
1516
/**
@@ -30,10 +31,10 @@ constexpr auto underlying_assoc_index(assoc_index n) -> unsigned {
3031
} // namespace ecsact::entt
3132

3233
struct ecsact_system_execution_context {
33-
ecsact_system_like_id id;
34-
ecsact::entt::entity_id entity;
35-
::entt::registry* registry = nullptr;
36-
const void* action_data = nullptr;
34+
ecsact_system_like_id id;
35+
ecsact::entt::entity_id entity;
36+
ecsact::entt::registry_t* registry = nullptr;
37+
const void* action_data = nullptr;
3738

3839
// pass in the context to this class that's a pointer
3940
// context(ptr) = parent_ctx(ptr)

ecsact/entt/error_check.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ecsact::entt {
1919

2020
template<typename C>
2121
auto check_add_component_error( //
22-
::entt::registry&,
22+
ecsact::entt::registry_t&,
2323
::ecsact::entt::entity_id,
2424
const C&
2525
) -> ecsact_add_error {
@@ -34,7 +34,7 @@ auto check_add_component_error( //
3434

3535
template<typename C>
3636
auto check_update_component_error( //
37-
::entt::registry&,
37+
ecsact::entt::registry_t&,
3838
::ecsact::entt::entity_id,
3939
const C&
4040
) -> ecsact_update_error {
@@ -49,7 +49,7 @@ auto check_update_component_error( //
4949

5050
template<typename A>
5151
auto check_action_error( //
52-
::entt::registry&,
52+
ecsact::entt::registry_t&,
5353
const A&
5454
) -> ecsact_execute_systems_error {
5555
static_assert(detail::error_check_unimplemented_by_codegen<A>, R"(

ecsact/entt/execution.hh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct actions_map {
9393
*/
9494
template<typename System>
9595
auto execute_system( //
96-
::entt::registry& registry,
96+
ecsact::entt::registry_t& registry,
9797
ecsact_system_execution_context* parent,
9898
const ecsact::entt::actions_map& actions_map
9999
) -> void {
@@ -115,7 +115,7 @@ auto execute_system( //
115115
*/
116116
template<typename Action>
117117
auto execute_actions( //
118-
::entt::registry& registry,
118+
ecsact::entt::registry_t& registry,
119119
ecsact_system_execution_context* parent,
120120
const ecsact::entt::actions_map& actions_map
121121
) -> void {
@@ -129,7 +129,7 @@ auto execute_actions( //
129129
}
130130

131131
using execute_fn_t = void (*)(
132-
::entt::registry& registry,
132+
ecsact::entt::registry_t& registry,
133133
ecsact_system_execution_context* parent,
134134
const ecsact::entt::actions_map& actions_map
135135
);
@@ -140,7 +140,7 @@ using execute_fn_t = void (*)(
140140
* `ecsact_rt_entt_codegen`.
141141
*/
142142
template<typename SystemLike>
143-
auto prepare_system_like(::entt::registry&) -> void {
143+
auto prepare_system_like(ecsact::entt::registry_t&) -> void {
144144
static_assert(detail::unimplemented_by_codegen<SystemLike>, R"(
145145
-----------------------------------------------------------------------------
146146
| (!) CODEGEN ERROR |
@@ -155,7 +155,8 @@ auto prepare_system_like(::entt::registry&) -> void {
155155
* entity.
156156
*/
157157
template<typename SystemLike>
158-
auto entity_matches_system(::entt::registry&, ecsact::entt::entity_id) -> bool {
158+
auto entity_matches_system(ecsact::entt::registry_t&, ecsact::entt::entity_id)
159+
-> bool {
159160
static_assert(detail::unimplemented_by_codegen<SystemLike>, R"(
160161
-----------------------------------------------------------------------------
161162
| (!) CODEGEN ERROR |

ecsact/entt/registry_util.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
#include <cassert>
44
#include "entt/entity/registry.hpp"
55
#include "ecsact/entt/detail/globals.hh"
6+
#include "ecsact/entt/detail/registry.hh"
67

78
namespace ecsact::entt {
89

910
inline auto get_registry( //
1011
ecsact_registry_id id
11-
) -> ::entt::registry& {
12+
) -> ecsact::entt::registry_t& {
1213
using ecsact::entt::detail::globals::registries;
1314

1415
// Check to make sure we're not trying to get a registry that doesn't exist
@@ -18,7 +19,7 @@ inline auto get_registry( //
1819
}
1920

2021
inline auto create_registry()
21-
-> std::tuple<ecsact_registry_id, ::entt::registry&> {
22+
-> std::tuple<ecsact_registry_id, ecsact::entt::registry_t&> {
2223
using ecsact::entt::detail::globals::last_registry_id;
2324
using ecsact::entt::detail::globals::registries;
2425

ecsact/entt/wrapper/core.hh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,13 @@ inline auto prepare_component(ecsact_registry_id registry_id) -> void {
404404
reg.template storage<C>();
405405
reg.template storage<component_added<C>>();
406406
reg.template storage<component_removed<C>>();
407+
reg.template storage<detail::pending_add<C>>();
408+
reg.template storage<detail::pending_remove<C>>();
409+
reg.template storage<detail::beforeremove_storage<C>>();
407410

408411
if constexpr(!std::is_empty_v<C>) {
409-
reg.storage<detail::exec_beforechange_storage<C>>();
412+
reg.template storage<detail::exec_beforechange_storage<C>>();
413+
reg.template storage<detail::exec_itr_beforechange_storage<C>>();
410414
reg.template storage<component_updated<C>>();
411415
}
412416
}
@@ -418,6 +422,7 @@ inline auto prepare_system(ecsact_registry_id registry_id) -> void {
418422

419423
reg.template storage<system_sorted<S>>();
420424
reg.template storage<pending_lazy_execution<S>>();
425+
reg.template storage<run_system<S>>();
421426
}
422427

423428
template<typename C, typename V>
@@ -435,8 +440,10 @@ auto has_component_changed(entt::entity_id entity, V& view) -> bool {
435440
}
436441

437442
template<typename C>
438-
auto update_exec_itr_beforechange(entt::entity_id entity, ::entt::registry& reg)
439-
-> void {
443+
auto update_exec_itr_beforechange(
444+
entt::entity_id entity,
445+
ecsact::entt::registry_t& reg
446+
) -> void {
440447
auto comp = reg.get<C>(entity);
441448
auto& beforechange_comp =
442449
reg.get<detail::exec_itr_beforechange_storage<C>>(entity);

ecsact/entt/wrapper/dynamic.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ auto context_add(
4848

4949
template<typename C>
5050
auto component_add_trivial(
51-
::entt::registry& registry,
52-
ecsact::entt::entity_id entity_id
51+
ecsact::entt::registry_t& registry,
52+
ecsact::entt::entity_id entity_id
5353
) -> void {
5454
using ecsact::entt::component_added;
5555
using ecsact::entt::component_removed;
@@ -100,9 +100,9 @@ auto context_remove(
100100

101101
template<typename C>
102102
auto component_remove_trivial(
103-
::entt::registry& registry,
104-
ecsact::entt::entity_id entity_id,
105-
auto& view
103+
ecsact::entt::registry_t& registry,
104+
ecsact::entt::entity_id entity_id,
105+
auto& view
106106
) -> void {
107107
using ecsact::entt::component_removed;
108108
using ecsact::entt::component_updated;

rt_entt_codegen/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cc_ecsact_codegen_plugin(
1212
"//rt_entt_codegen/core",
1313
"//rt_entt_codegen/shared:ecsact_entt_details",
1414
"//rt_entt_codegen/shared:util",
15+
"@ecsact_lang_cpp//:cpp_codegen_plugin_util",
1516
"@ecsact_lang_cpp//:support",
1617
],
1718
)

0 commit comments

Comments
 (0)