Skip to content

Commit fbd3167

Browse files
committed
Switched out alloca calls for file buffers to heap allocate
1 parent b7fcc94 commit fbd3167

File tree

8 files changed

+29
-28
lines changed

8 files changed

+29
-28
lines changed

engine/core/entity/Entity.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,21 @@ class Entity
3333

3434
/**
3535
* Zero-param constructor for Entity, initialises both
36-
* position and rotation to zero, and name to "Entity"
36+
* position and rotation to zero, and type to Entity
3737
*/
3838
Entity() : Entity(TOKEN_Entity) {};
3939

4040
/**
4141
* Single-param constructor for Entity that simply defines
4242
* a custom name for the Entity
43-
* @param typeName - a const reference to the name of the
44-
* entity as a string
43+
* @param type - a token for the entity to use as its type
4544
*/
4645
Entity(Token type) : Entity(type, {Vec3::Zero(), 0.f}) {};
4746

4847
/**
4948
* Delegate constructor for Entity, initialises
5049
* generational index to zero
51-
* @param name - a const reference to the name of the
52-
* entity as a string
50+
* @param type - a token for the entity to use as its type
5351
* @param transform - the initial transition of the entity
5452
* @param zIndex - the initial z-index of the entity,
5553
* defaults to zero

engine/utils/FileSystem.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@
1313
#include <cstdio>
1414
#include <fstream>
1515

16+
#include "Defer.h"
1617
#include "Logging.h"
1718

1819
namespace Siege::FileSystem
1920
{
2021
String Read(const String& filename)
2122
{
22-
// Try open the file for reading
2323
FILE* file = fopen(filename, "rb");
2424
if (!file) return "";
2525

26-
// Determine file size
2726
fseek(file, 0, SEEK_END);
2827
size_t size = ftell(file);
2928
rewind(file);
3029

31-
// Copy the content
32-
char* content = static_cast<char*>(alloca(size + 1));
30+
char* content = static_cast<char*>(malloc(size + 1));
31+
defer([&content] { free(content); });
32+
3333
fread(content, sizeof(char), size, file);
3434
content[size] = '\0';
3535

36-
// Close the file stream and return
3736
fclose(file);
3837
return content;
3938
}

engine/utils/math/mat/Mat4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct Mat<T, 4, 4>
216216

217217
static inline constexpr Mat<T, 4, 4> Scale(const Vec<T, 3>& scale)
218218
{
219-
Mat<T,4,4> mat = Mat<T,4,4>::Identity();
219+
Mat<T, 4, 4> mat = Mat<T, 4, 4>::Identity();
220220
mat[0][0] = scale.x;
221221
mat[1][1] = scale.y;
222222
mat[2][2] = scale.z;

packer/src/types/MathUtils.h renamed to packer/src/PackerUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
// https://opensource.org/licenses/Zlib
88
//
99

10-
#ifndef SIEGE_ENGINE_PACKER_MATHUTILS_H
11-
#define SIEGE_ENGINE_PACKER_MATHUTILS_H
10+
#ifndef SIEGE_ENGINE_PACKER_PACKERUTILS_H
11+
#define SIEGE_ENGINE_PACKER_PACKERUTILS_H
1212

13+
#include <assimp/types.h>
1314
#include <utils/math/mat/Mat4.h>
14-
#include <assimp/Importer.hpp>
1515

16-
static inline constexpr Siege::Mat4 AssimptMat4ToMat4(const aiMatrix4x4& matrix)
16+
static constexpr Siege::Mat4 AssimpMat4ToMat4(const aiMatrix4x4& matrix)
1717
{
1818
return {{matrix.a1, matrix.b1, matrix.c1, matrix.d1},
1919
{matrix.a2, matrix.b2, matrix.c2, matrix.d2},
2020
{matrix.a3, matrix.b3, matrix.c3, matrix.d3},
2121
{matrix.a4, matrix.b4, matrix.c4, matrix.d4}};
2222
}
2323

24-
#endif // SIEGE_ENGINE_PACKER_MATHUTILS_H
24+
#endif // SIEGE_ENGINE_PACKER_PACKERUTILS_H

packer/src/types/GenericFileDataPacker.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "GenericFileDataPacker.h"
1111

1212
#include <resources/GenericFileData.h>
13+
#include <utils/Defer.h>
1314
#include <utils/Logging.h>
1415

1516
#include <fstream>
@@ -25,9 +26,11 @@ void* PackGenericFile(const Siege::String& filePath)
2526

2627
// Since we consumed the entire file, we can tell the size by checking where
2728
// the file stream is reading from (which presumably is at the end of the file).
28-
uint32_t fileSize = static_cast<uint32_t>(file.tellg());
29+
uint32_t fileSize = file.tellg();
30+
31+
char* data = static_cast<char*>(malloc(fileSize));
32+
defer([&data] { free(data); });
2933

30-
char* data = static_cast<char*>(alloca(fileSize));
3134
file.seekg(0); // Move to the beginning of the file.
3235
file.read(data, fileSize);
3336
file.close();

packer/src/types/SceneDataPacker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "SceneDataPacker.h"
1111

1212
#include <resources/SceneData.h>
13+
#include <utils/Defer.h>
1314
#include <utils/FileSystem.h>
1415
#include <utils/Logging.h>
1516

@@ -36,7 +37,9 @@ void* PackSceneFile(const Siege::String& filePath)
3637
bodyString = Siege::FileSystem::StripNewLines(bodyString);
3738

3839
uint32_t fileSize = bodyString.Size() + 1;
39-
char* data = static_cast<char*>(alloca(fileSize));
40+
char* data = static_cast<char*>(malloc(fileSize));
41+
defer([&data] { free(data); });
42+
4043
memcpy(data, bodyString.Str(), fileSize);
4144

4245
Siege::SceneData* sceneData = Siege::SceneData::Create(&data[0], fileSize);

packer/src/types/StaticMeshDataPacker.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//
99

1010
#include "StaticMeshDataPacker.h"
11-
#include "MathUtils.h"
1211

1312
#include <assimp/postprocess.h>
1413
#include <assimp/scene.h>
@@ -17,8 +16,11 @@
1716
#include <utils/Logging.h>
1817

1918
#include <algorithm>
19+
#include <assimp/Importer.hpp>
2020
#include <fstream>
2121

22+
#include "../PackerUtils.h"
23+
2224
enum RequestPathStage
2325
{
2426
PARENT,
@@ -59,11 +61,7 @@ static void GetMeshData(const aiScene* scene,
5961
Siege::BaseVertex vertex {};
6062

6163
aiVector3t<ai_real> vert = mesh->mVertices[i];
62-
Siege::Vec4 v = {vert.x, vert.y, vert.z, 1.f};
63-
64-
v = mat * v;
65-
66-
vertex.position = {v.x, v.y, v.z};
64+
vertex.position = mat * Siege::Vec4(vert.x, vert.y, vert.z, 1.f);
6765

6866
aiVector3t<ai_real> norm = mesh->mNormals[i];
6967
vertex.normal = {norm.x, norm.y, norm.z};
@@ -112,7 +110,7 @@ static void GetMeshesForNode(const aiScene* scene,
112110

113111
if (nodePathStage == CHILD || nodePathStage == SELF)
114112
{
115-
matrix *= AssimptMat4ToMat4(node->mTransformation);
113+
matrix *= AssimpMat4ToMat4(node->mTransformation);
116114
}
117115

118116
for (uint32_t i = 0; i < node->mNumMeshes; i++)

tests/src/utils/test_Token.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ UTEST(test_Token, StaticallyRegistered)
2929
ASSERT_TRUE(tk2.IsValid());
3030
ASSERT_TRUE(tk3.IsValid());
3131
ASSERT_TRUE(tk4.IsValid());
32-
ASSERT_TRUE(!tk5.IsValid());
32+
ASSERT_FALSE(tk5.IsValid());
3333
ASSERT_TRUE(tk6.IsValid());
3434
ASSERT_TRUE(tk7.IsValid());
3535

@@ -89,7 +89,7 @@ UTEST(test_Token, DynamicallyRegistered)
8989
Siege::Token tk9 = Siege::Token::FindToken("FooBar");
9090

9191
ASSERT_TRUE(tk8.IsValid());
92-
ASSERT_TRUE(!tk9.IsValid());
92+
ASSERT_FALSE(tk9.IsValid());
9393

9494
ASSERT_STREQ("GoodbyeWorld", tk8.GetId());
9595

0 commit comments

Comments
 (0)