Skip to content

Commit

Permalink
[kernel] fix: change how services are allocated
Browse files Browse the repository at this point in the history
Use a MemoryScope instead.
  • Loading branch information
jd28 committed Oct 11, 2024
1 parent 90da3e3 commit c603ea2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 8 additions & 4 deletions lib/nw/kernel/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ void Service::set_scope(MemoryScope* scope) noexcept
}

Services::Services()
: kernel_arena_(MB(16))
, kernel_scope_(&kernel_arena_)
, service_scope_(nullptr)
{
// The ordering here is important. Pretty much everything depends on strings and resman
add<Strings>();
Expand All @@ -40,6 +43,8 @@ Services::Services()
add<TilesetRegistry>();
add<FactionSystem>();

service_scope_ = kernel_scope_.alloc_obj<MemoryScope>(&kernel_arena_);

for (auto& entry : services_) {
if (!entry.service) { break; }
entry.service->set_scope(service_scope_);
Expand All @@ -50,7 +55,7 @@ void Services::start()
{
if (config().version() == GameVersion::vEE
|| config().version() == GameVersion::v1_69) {
profile_ = std::make_unique<nwn1::Profile>();
profile_ = kernel_scope_.alloc_obj<nwn1::Profile>();
} else {
std::runtime_error("currently selected game version is unsupported");
}
Expand All @@ -63,16 +68,15 @@ void Services::start()

GameProfile* Services::profile() const
{
return profile_.get();
return profile_;
}

void Services::shutdown()
{
for (size_t i = services().services_count_; i > 0; --i) {
services().services_[i - 1].service->clear();
}

profile_.reset();
if (service_scope_) { service_scope_->reset(); }
}

Config& config()
Expand Down
14 changes: 10 additions & 4 deletions lib/nw/kernel/Kernel.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../config.hpp"
#include "../util/memory.hpp"
#include "../util/templates.hpp"
#include "Config.hpp"

Expand Down Expand Up @@ -92,18 +93,20 @@ struct Services {
friend void unload_module();

private:
GameProfile* profile_ = nullptr;
std::unique_ptr<GameProfile> profile_;
std::array<ServiceEntry, 32> services_;
size_t services_count_ = 0;
GameProfile* profile_ = nullptr;
MemoryArena kernel_arena_;
MemoryScope kernel_scope_;
MemoryScope* service_scope_ = nullptr;
};

template <typename T>
T* Services::add()
{
T* service = get_mut<T>();
if (!service) {
service = new T;
service = kernel_scope_.alloc_obj<T>();
CHECK_F(services_count_ < 32, "Only 32 total services are allowed");
services_[services_count_] = ServiceEntry{T::type_index, service};
++services_count_;
Expand Down Expand Up @@ -142,12 +145,15 @@ Config& config();
Services& services();

/// Loads a module
/// If instantiate is false, no areas are loaded and service init at `` module_post_instantiation``
/// If instantiate is false, no areas are loaded and Service::initialize at ``module_post_instantiation``
/// is not called.
Module* load_module(const std::filesystem::path& path, bool instantiate = true);

/// Unloads currently active module
void unload_module();

static thread_local MemoryArena tsl_arena_(MB(1));
static thread_local MemoryScope tsl_scope(&tsl_arena_);

} // namespace kernel
} // namespace nw

0 comments on commit c603ea2

Please sign in to comment.