Skip to content

Commit a082fb9

Browse files
committed
Don't allocate script maps on heap
1 parent 5107f47 commit a082fb9

File tree

8 files changed

+34
-34
lines changed

8 files changed

+34
-34
lines changed

src/celestia/celestiacore.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ CelestiaCore::CelestiaCore() :
191191
#ifdef CELX
192192
m_luaPlugin(std::make_unique<LuaScriptPlugin>(this)),
193193
#endif
194-
m_scriptMaps(new ScriptMaps()),
195194
oldFOV(stdFOV),
196195
console(new Console(*renderer, 200, 120)),
197196
m_tee(std::cout, std::cerr)

src/celestia/celestiacore.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class CelestiaCore // : public Watchable<CelestiaCore>
355355
void setTypedText(const char *);
356356

357357
void setScriptHook(std::unique_ptr<celestia::scripts::IScriptHook> &&hook) { m_scriptHook = std::move(hook); }
358-
const std::shared_ptr<celestia::scripts::ScriptMaps>& scriptMaps() const { return m_scriptMaps; }
358+
celestia::scripts::ScriptMaps& scriptMaps() { return m_scriptMaps; }
359359

360360
void getCaptureInfo(std::array<int, 4>& viewport, celestia::engine::PixelFormat& format) const;
361361
bool captureImage(std::uint8_t* buffer, const std::array<int, 4>& viewport, celestia::engine::PixelFormat format) const;
@@ -428,7 +428,8 @@ class CelestiaCore // : public Watchable<CelestiaCore>
428428
#ifdef CELX
429429
std::unique_ptr<celestia::scripts::LuaScriptPlugin> m_luaPlugin;
430430
#endif
431-
std::shared_ptr<celestia::scripts::ScriptMaps> m_scriptMaps;
431+
432+
celestia::scripts::ScriptMaps m_scriptMaps;
432433

433434
enum ScriptState
434435
{

src/celscript/legacy/cmdparser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ using ParseCommandPtr = ParseResult (*)(const Hash&, const ScriptMaps&);
995995
} // end unnamed namespace
996996

997997

998-
CommandParser::CommandParser(std::istream& in, const std::shared_ptr<ScriptMaps> &sm) :
998+
CommandParser::CommandParser(std::istream& in, const ScriptMaps &sm) :
999999
tokenizer(std::make_unique<Tokenizer>(&in)),
10001000
scriptMaps(sm)
10011001
{
@@ -1085,7 +1085,7 @@ std::unique_ptr<Command> CommandParser::parseCommand()
10851085
}
10861086

10871087
return std::visit(ParseResultVisitor([this](std::string&& s) { error(std::move(s)); }),
1088-
ptr->parser(*paramList, *scriptMaps));
1088+
ptr->parser(*paramList, scriptMaps));
10891089
}
10901090

10911091
} // end namespace celestia::scripts

src/celscript/legacy/cmdparser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ class ScriptMaps;
2929

3030
class CommandParser
3131
{
32-
public:
33-
CommandParser(std::istream&, const std::shared_ptr<celestia::scripts::ScriptMaps> &sm);
32+
public:
33+
CommandParser(std::istream&, const celestia::scripts::ScriptMaps &sm);
3434
~CommandParser();
3535

3636
CommandSequence parse();
3737
celestia::util::array_view<std::string> getErrors() const;
3838

39-
private:
39+
private:
4040
std::unique_ptr<Command> parseCommand();
4141
void error(std::string&&);
4242

4343
std::unique_ptr<Parser> parser;
4444
std::unique_ptr<Tokenizer> tokenizer;
4545
std::vector<std::string> errorList;
46-
std::shared_ptr<celestia::scripts::ScriptMaps> scriptMaps;
46+
const celestia::scripts::ScriptMaps &scriptMaps;
4747
};
4848

4949
} // end namespace celestia::scripts

src/celscript/legacy/command.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,14 +823,14 @@ CommandSetLineColor::CommandSetLineColor(std::string _item, const Color& _color)
823823

824824
void CommandSetLineColor::processInstantaneous(ExecutionEnvironment& env)
825825
{
826-
auto &LineColorMap = env.getCelestiaCore()->scriptMaps()->LineColorMap;
826+
auto &LineColorMap = env.getCelestiaCore()->scriptMaps().LineColorMap;
827827
if (LineColorMap.count(item) == 0)
828828
{
829829
GetLogger()->warn("Unknown line style: {}\n", item);
830830
}
831831
else
832832
{
833-
*LineColorMap[item] = color;
833+
*(LineColorMap[item]) = color;
834834
}
835835
}
836836

@@ -846,14 +846,14 @@ CommandSetLabelColor::CommandSetLabelColor(std::string _item, const Color& _colo
846846

847847
void CommandSetLabelColor::processInstantaneous(ExecutionEnvironment& env)
848848
{
849-
auto &LabelColorMap = env.getCelestiaCore()->scriptMaps()->LabelColorMap;
849+
auto &LabelColorMap = env.getCelestiaCore()->scriptMaps().LabelColorMap;
850850
if (LabelColorMap.count(item) == 0)
851851
{
852852
GetLogger()->error("Unknown label style: {}\n", item);
853853
}
854854
else
855855
{
856-
*LabelColorMap[item] = color;
856+
*(LabelColorMap[item]) = color;
857857
}
858858
}
859859

src/celscript/lua/celx_celestia.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static int celestia_show(lua_State* l)
224224

225225
int argc = lua_gettop(l);
226226
uint64_t flags = 0;
227-
auto &RenderFlagMap = appCore->scriptMaps()->RenderFlagMap;
227+
auto &RenderFlagMap = appCore->scriptMaps().RenderFlagMap;
228228
for (int i = 2; i <= argc; i++)
229229
{
230230
string renderFlag = Celx_SafeGetString(l, i, AllErrors, "Arguments to celestia:show() must be strings");
@@ -248,7 +248,7 @@ static int celestia_hide(lua_State* l)
248248

249249
int argc = lua_gettop(l);
250250
uint64_t flags = 0;
251-
auto &RenderFlagMap = appCore->scriptMaps()->RenderFlagMap;
251+
auto &RenderFlagMap = appCore->scriptMaps().RenderFlagMap;
252252
for (int i = 2; i <= argc; i++)
253253
{
254254
string renderFlag = Celx_SafeGetString(l, i, AllErrors, "Arguments to celestia:hide() must be strings");
@@ -303,9 +303,9 @@ static int celestia_setrenderflags(lua_State* l)
303303
{
304304
appCore->setLightDelayActive(value);
305305
}
306-
else if (appCore->scriptMaps()->RenderFlagMap.count(key) > 0)
306+
else if (appCore->scriptMaps().RenderFlagMap.count(key) > 0)
307307
{
308-
uint64_t flag = appCore->scriptMaps()->RenderFlagMap[key];
308+
uint64_t flag = appCore->scriptMaps().RenderFlagMap[key];
309309
if (value)
310310
renderFlags |= flag;
311311
else
@@ -331,7 +331,7 @@ static int celestia_getrenderflags(lua_State* l)
331331
const uint64_t renderFlags = appCore->getRenderer()->getRenderFlags();
332332
std::string rfmString;
333333
rfmString.reserve(FlagMapNameLength);
334-
for (const auto& rfm : appCore->scriptMaps()->RenderFlagMap)
334+
for (const auto& rfm : appCore->scriptMaps().RenderFlagMap)
335335
{
336336
rfmString.clear();
337337
rfmString.append(rfm.first);
@@ -440,7 +440,7 @@ static int celestia_showlabel(lua_State* l)
440440

441441
int argc = lua_gettop(l);
442442
int flags = 0;
443-
auto &LabelFlagMap = appCore->scriptMaps()->LabelFlagMap;
443+
auto &LabelFlagMap = appCore->scriptMaps().LabelFlagMap;
444444
for (int i = 2; i <= argc; i++)
445445
{
446446
string labelFlag = Celx_SafeGetString(l, i, AllErrors, "Arguments to celestia:showlabel() must be strings");
@@ -462,7 +462,7 @@ static int celestia_hidelabel(lua_State* l)
462462

463463
int argc = lua_gettop(l);
464464
int flags = 0;
465-
auto &LabelFlagMap = appCore->scriptMaps()->LabelFlagMap;
465+
auto &LabelFlagMap = appCore->scriptMaps().LabelFlagMap;
466466
for (int i = 2; i <= argc; i++)
467467
{
468468
string labelFlag = Celx_SafeGetString(l, i, AllErrors, "Arguments to celestia:hidelabel() must be strings");
@@ -488,7 +488,7 @@ static int celestia_setlabelflags(lua_State* l)
488488
}
489489

490490
int labelFlags = appCore->getRenderer()->getLabelMode();
491-
auto &LabelFlagMap = appCore->scriptMaps()->LabelFlagMap;
491+
auto &LabelFlagMap = appCore->scriptMaps().LabelFlagMap;
492492
lua_pushnil(l);
493493
while (lua_next(l, -2) != 0)
494494
{
@@ -540,7 +540,7 @@ static int celestia_getlabelflags(lua_State* l)
540540
const int labelFlags = appCore->getRenderer()->getLabelMode();
541541
std::string lfmString;
542542
lfmString.reserve(FlagMapNameLength);
543-
for (const auto& lfm : appCore->scriptMaps()->LabelFlagMap)
543+
for (const auto& lfm : appCore->scriptMaps().LabelFlagMap)
544544
{
545545
lfmString.clear();
546546
lfmString.append(lfm.first);
@@ -563,7 +563,7 @@ static int celestia_setorbitflags(lua_State* l)
563563

564564
int orbitFlags = appCore->getRenderer()->getOrbitMask();
565565
lua_pushnil(l);
566-
auto &BodyTypeMap = appCore->scriptMaps()->BodyTypeMap;
566+
auto &BodyTypeMap = appCore->scriptMaps().BodyTypeMap;
567567
while (lua_next(l, -2) != 0)
568568
{
569569
string key;
@@ -612,7 +612,7 @@ static int celestia_getorbitflags(lua_State* l)
612612
const int orbitFlags = appCore->getRenderer()->getOrbitMask();
613613
std::string btmString;
614614
btmString.reserve(FlagMapNameLength);
615-
for (const auto& btm : appCore->scriptMaps()->BodyTypeMap)
615+
for (const auto& btm : appCore->scriptMaps().BodyTypeMap)
616616
{
617617
btmString.clear();
618618
btmString.append(btm.first);
@@ -772,7 +772,7 @@ static int celestia_setoverlayelements(lua_State* l)
772772

773773
auto overlayElements = appCore->getOverlayElements();
774774
lua_pushnil(l);
775-
const auto &OverlayElementMap = appCore->scriptMaps()->OverlayElementMap;
775+
const auto &OverlayElementMap = appCore->scriptMaps().OverlayElementMap;
776776
while (lua_next(l, -2) != 0)
777777
{
778778
string key;
@@ -821,7 +821,7 @@ static int celestia_getoverlayelements(lua_State* l)
821821
const auto overlayElements = appCore->getOverlayElements();
822822
std::string oemString;
823823
oemString.reserve(FlagMapNameLength);
824-
for (const auto& oem : appCore->scriptMaps()->OverlayElementMap)
824+
for (const auto& oem : appCore->scriptMaps().OverlayElementMap)
825825
{
826826
oemString.clear();
827827
oemString.append(oem.first);
@@ -878,7 +878,7 @@ static int celestia_setlabelcolor(lua_State* l)
878878
Color* color = nullptr;
879879
string key;
880880
key = lua_tostring(l, 2);
881-
auto &LabelColorMap = this_celestia(l)->scriptMaps()->LabelColorMap;
881+
auto &LabelColorMap = this_celestia(l)->scriptMaps().LabelColorMap;
882882
if (LabelColorMap.count(key) == 0)
883883
{
884884
GetLogger()->warn("Unknown label style: {}\n", key);
@@ -910,7 +910,7 @@ static int celestia_getlabelcolor(lua_State* l)
910910
string key = Celx_SafeGetString(l, 2, AllErrors, "Argument to celestia:getlabelcolor() must be a string");
911911

912912
Color* labelColor = nullptr;
913-
auto &LabelColorMap = this_celestia(l)->scriptMaps()->LabelColorMap;
913+
auto &LabelColorMap = this_celestia(l)->scriptMaps().LabelColorMap;
914914
if (LabelColorMap.count(key) == 0)
915915
{
916916
GetLogger()->error("Unknown label style: {}\n", key);
@@ -938,7 +938,7 @@ static int celestia_setlinecolor(lua_State* l)
938938
Color* color = nullptr;
939939
string key;
940940
key = lua_tostring(l, 2);
941-
auto &LineColorMap = this_celestia(l)->scriptMaps()->LineColorMap;
941+
auto &LineColorMap = this_celestia(l)->scriptMaps().LineColorMap;
942942
if (LineColorMap.count(key) == 0)
943943
{
944944
GetLogger()->warn("Unknown line style: {}\n", key);
@@ -969,7 +969,7 @@ static int celestia_getlinecolor(lua_State* l)
969969
Celx_CheckArgs(l, 2, 2, "One argument expected for celestia:getlinecolor()");
970970
string key = Celx_SafeGetString(l, 2, AllErrors, "Argument to celestia:getlinecolor() must be a string");
971971

972-
auto &LineColorMap = this_celestia(l)->scriptMaps()->LineColorMap;
972+
auto &LineColorMap = this_celestia(l)->scriptMaps().LineColorMap;
973973
if (LineColorMap.count(key) == 0)
974974
{
975975
GetLogger()->error("Unknown line style: {}\n", key);

src/celscript/lua/celx_object.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static int object_setorbitvisibility(lua_State* l)
284284
string key;
285285
key = lua_tostring(l, 2);
286286

287-
auto &OrbitVisibilityMap = celx.appCore(AllErrors)->scriptMaps()->OrbitVisibilityMap;
287+
auto &OrbitVisibilityMap = celx.appCore(AllErrors)->scriptMaps().OrbitVisibilityMap;
288288
if (OrbitVisibilityMap.count(key) == 0)
289289
{
290290
GetLogger()->warn("Unknown visibility policy: {}\n", key);
@@ -729,7 +729,7 @@ static int object_getinfo(lua_State* l)
729729
celx.setTable("infoURL", location->getInfoURL().c_str());
730730

731731
auto featureType = location->getFeatureType();
732-
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps()->LocationFlagMap;
732+
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps().LocationFlagMap;
733733
auto iter = std::find_if(LocationFlagMap.begin(),
734734
LocationFlagMap.end(),
735735
[&featureType](auto& it){ return it.second == featureType; });

src/celscript/lua/celx_observer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static int observer_setlocationflags(lua_State* l)
899899
celx.doError("Values in table-argument to observer:setlocationflags() must be boolean");
900900
return 0;
901901
}
902-
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps()->LocationFlagMap;
902+
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps().LocationFlagMap;
903903
if (LocationFlagMap.count(key) == 0)
904904
{
905905
GetLogger()->warn("Unknown key: {}\n", key);
@@ -929,7 +929,7 @@ static int observer_getlocationflags(lua_State* l)
929929
Observer* obs = this_observer(l);
930930
lua_newtable(l);
931931
const auto locationFlags = obs->getLocationFilter();
932-
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps()->LocationFlagMap;
932+
auto &LocationFlagMap = celx.appCore(AllErrors)->scriptMaps().LocationFlagMap;
933933
std::string itString;
934934
itString.reserve(celestia::scripts::FlagMapNameLength);
935935
for (const auto& it : LocationFlagMap)

0 commit comments

Comments
 (0)