summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-03 08:03:57 +0100
committerskal <pascal.massimino@gmail.com>2026-02-03 08:03:57 +0100
commit6d4aeb4120230899589326b5fd87afca58654c05 (patch)
tree2b14534b9ab7bf0d1be203a0a8195dcc49a5bbfe
parentf7609d0bdc3df851195c378eabc2715cb4c30bbe (diff)
feat(assets): Enforce 16-byte alignment and string safety
Updates asset_packer to align static asset arrays to 16 bytes and append a null-terminator. This allows assets to be safely reinterpreted as typed pointers (e.g., float*, const char*) without copying. Updates AssetManager documentation to reflect these guarantees.
-rw-r--r--src/util/asset_manager.h4
-rw-r--r--tools/asset_packer.cc11
2 files changed, 12 insertions, 3 deletions
diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h
index b9cd778..062cd0f 100644
--- a/src/util/asset_manager.h
+++ b/src/util/asset_manager.h
@@ -25,5 +25,9 @@ struct AssetRecord {
};
// Generic interface
+// Retrieves a pointer to the asset data.
+// - Static assets are guaranteed to be 16-byte aligned.
+// - Static assets are guaranteed to be null-terminated (safe as C-strings).
+// - 'out_size' returns the original asset size (excluding the null terminator).
const uint8_t* GetAsset(AssetId asset_id, size_t* out_size = nullptr);
void DropAsset(AssetId asset_id, const uint8_t* asset);
diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc
index 2b90ecc..d86e29b 100644
--- a/tools/asset_packer.cc
+++ b/tools/asset_packer.cc
@@ -210,7 +210,12 @@ int main(int argc, char* argv[]) {
}
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(asset_file)),
std::istreambuf_iterator<char>());
- fprintf(assets_data_cc_file, "static const uint8_t %s[] = {\n ",
+ size_t original_size = buffer.size();
+ buffer.push_back(0); // Null terminator for safety
+
+ fprintf(assets_data_cc_file, "const size_t ASSET_SIZE_%s = %zu;\n",
+ info.name.c_str(), original_size);
+ fprintf(assets_data_cc_file, "alignas(16) static const uint8_t %s[] = {\n ",
info.data_array_name.c_str());
for (size_t i = 0; i < buffer.size(); ++i) {
if (i > 0 && i % 12 == 0)
@@ -241,8 +246,8 @@ int main(int argc, char* argv[]) {
info.func_name_str_name.c_str(), info.params_array_name.c_str(),
info.proc_params.size());
} else {
- fprintf(assets_data_cc_file, "%s, sizeof(%s), false, nullptr, nullptr, 0",
- info.data_array_name.c_str(), info.data_array_name.c_str());
+ fprintf(assets_data_cc_file, "%s, ASSET_SIZE_%s, false, nullptr, nullptr, 0",
+ info.data_array_name.c_str(), info.name.c_str());
}
fprintf(assets_data_cc_file, " },\n");
}