Skip to content

Commit

Permalink
start the process of passing down an allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
jd28 committed Oct 13, 2024
1 parent 6e366f4 commit 4262a3e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
29 changes: 13 additions & 16 deletions lib/nw/kernel/Rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ const std::type_index Rules::type_index{typeid(Rules)};

Rules::Rules(MemoryResource* scope)
: Service(scope)
, baseitems{allocator()}
, classes{allocator()}
, feats{allocator()}
, races{allocator()}
, spells{allocator()}
, spellschools{allocator()}
, skills{allocator()}
, master_feats{allocator()}
, modifiers{allocator()}
, phenotypes{allocator()}
, appearances{allocator()}
, placeables{allocator()}
, traps{allocator()}
{
}

Expand All @@ -20,22 +33,6 @@ Rules::~Rules()
clear();
}

void Rules::clear()
{
modifiers.clear();
baseitems.clear();
classes.clear();
feats.clear();
races.clear();
spells.clear();
skills.clear();
master_feats.clear();
appearances.clear();
phenotypes.clear();
traps.clear();
placeables.clear();
}

void Rules::initialize(ServiceInitTime time)
{
if (time != ServiceInitTime::kernel_start && time != ServiceInitTime::module_post_load) {
Expand Down
3 changes: 0 additions & 3 deletions lib/nw/kernel/Rules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ struct Rules : public Service {
/// Initializes rules system
virtual void initialize(ServiceInitTime time) override;

/// Clears rules system of all rules and cached 2da files
virtual void clear() override;

/// Gets attack functions
[[nodiscard]] const AttackFuncs& attack_functions() const noexcept;

Expand Down
11 changes: 9 additions & 2 deletions lib/nw/rules/Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ struct ClassArray {
InternedString,
Class,
InternedStringHash,
InternedStringEq>;
InternedStringEq,
Allocator<std::pair<const InternedString, const ClassInfo>>>;

ClassArray(MemoryResource* allocator = kernel::global_allocator())
: entries(allocator)
, constant_to_index(allocator)
{
}

const ClassInfo* get(Class class_) const noexcept;
bool is_valid(Class class_) const noexcept;
Expand Down Expand Up @@ -120,7 +127,7 @@ struct ClassArray {
std::set<Vector<int>> attack_tables;
Vector<int> stat_gain_tables;

Vector<ClassInfo> entries;
PVector<ClassInfo> entries;
map_type constant_to_index;
};

Expand Down
5 changes: 5 additions & 0 deletions lib/nw/rules/feats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ FeatInfo::FeatInfo(const TwoDARowView& tda)
// -- Master Feats ------------------------------------------------------------
// ----------------------------------------------------------------------------

MasterFeatRegistry::MasterFeatRegistry(MemoryResource* allocator)
: allocator_(allocator)
{
}

const ModifierVariant& MasterFeatRegistry::get_bonus(MasterFeat mfeat) const
{
return bonuses_[*mfeat];
Expand Down
3 changes: 3 additions & 0 deletions lib/nw/rules/feats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ inline bool operator==(const MasterFeatEntry& lhs, const MasterFeatEntry& rhs)
}

struct MasterFeatRegistry {
MasterFeatRegistry(MemoryResource* allocator = nw::kernel::global_allocator());

template <typename T>
void add(T type, MasterFeat mfeat, Feat feat);
void clear() noexcept;
Expand All @@ -100,6 +102,7 @@ struct MasterFeatRegistry {
private:
Vector<MasterFeatEntry> entries_;
Vector<ModifierVariant> bonuses_;
MemoryResource* allocator_ = nullptr;
};

template <typename T>
Expand Down
12 changes: 10 additions & 2 deletions lib/nw/rules/rule_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../util/InternedString.hpp"

#include "../kernel/Kernel.hpp"
#include <absl/container/flat_hash_map.h>
#include <nlohmann/json_fwd.hpp>

Expand Down Expand Up @@ -155,7 +156,14 @@ struct RuleTypeArray {
InternedString,
RuleType,
InternedStringHash,
InternedStringEq>;
InternedStringEq,
Allocator<std::pair<const InternedString, const RuleType>>>;

RuleTypeArray(MemoryResource* allocator = kernel::global_allocator())
: entries(allocator)
, constant_to_index(allocator)
{
}

const RuleTypeInfo* get(RuleType type) const noexcept
{
Expand Down Expand Up @@ -187,7 +195,7 @@ struct RuleTypeArray {
}
}

Vector<RuleTypeInfo> entries;
PVector<RuleTypeInfo> entries;
map_type constant_to_index;
};

Expand Down
5 changes: 5 additions & 0 deletions lib/nw/rules/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ nw::Modifier make_modifier(nw::ModifierType type, nw::ModifierVariant value,
};
}

ModifierRegistry::ModifierRegistry(MemoryResource* allocator)
: allocator_(allocator)
{
}

void ModifierRegistry::add(Modifier mod)
{
auto it = std::lower_bound(std::begin(entries_), std::end(entries_), mod);
Expand Down
11 changes: 5 additions & 6 deletions lib/nw/rules/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
#include "rule_type.hpp"
#include "system.hpp"

#include <absl/container/fixed_array.h>
#include <absl/container/inlined_vector.h>

#include <limits>
#include <optional>
#include <string>
#include <variant>

namespace nw {

struct Class;
Expand Down Expand Up @@ -91,7 +87,7 @@ enum struct ModifierSource {
};

struct ModifierResult {
ModifierResult() { };
ModifierResult() {};
ModifierResult(int value);
ModifierResult(float value);
ModifierResult(DamageRoll value);
Expand Down Expand Up @@ -187,6 +183,8 @@ struct ModifierRegistry {
using iterator = Storage::iterator;
using const_iterator = Storage::const_iterator;

ModifierRegistry(MemoryResource* allocator = nw::kernel::global_allocator());

/// Adds a modifier to the system
void add(Modifier mod);

Expand Down Expand Up @@ -232,6 +230,7 @@ struct ModifierRegistry {

private:
Storage entries_;
MemoryResource* allocator_ = nullptr;
};

} // namespace nw

0 comments on commit 4262a3e

Please sign in to comment.