[ox/mc] Fix MC read alloca in a loop
This commit is contained in:
parent
3eec0a149d
commit
d386bc8c91
8
deps/ox/src/ox/mc/read.cpp
vendored
8
deps/ox/src/ox/mc/read.cpp
vendored
@ -6,6 +6,14 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <ox/model/modelhandleradaptor.hpp>
|
||||
#include <ox/std/buffer.hpp>
|
||||
#include <ox/std/reader.hpp>
|
||||
|
||||
#include "read.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
template class ModelHandlerInterface<MetalClawReaderTemplate<BufferReader>>;
|
||||
|
||||
}
|
||||
|
2
deps/ox/src/ox/mc/read.hpp
vendored
2
deps/ox/src/ox/mc/read.hpp
vendored
@ -549,4 +549,6 @@ Result<T> readMC(const Buffer &buff) noexcept {
|
||||
return readMC<T>(buff.data(), buff.size());
|
||||
}
|
||||
|
||||
extern template class ModelHandlerInterface<MetalClawReaderTemplate<BufferReader>>;
|
||||
|
||||
}
|
||||
|
14
deps/ox/src/ox/mc/write.hpp
vendored
14
deps/ox/src/ox/mc/write.hpp
vendored
@ -359,15 +359,19 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, const HashMap<String
|
||||
ModelHandlerInterface handler{&writer};
|
||||
// double len for both key and value
|
||||
oxReturnError(handler.setTypeInfo("Map", 0, {}, len * 2));
|
||||
// write the array
|
||||
for (std::size_t i = 0; i < len; i++) {
|
||||
const auto &key = keys[i];
|
||||
// this loop body needs to be in a lambda because of the potential alloca call
|
||||
constexpr auto loopBody = [](auto &handler, auto const&key, auto const&val) -> ox::Error {
|
||||
const auto keyLen = ox_strlen(key);
|
||||
auto wkey = ox_malloca(keyLen + 1, char, 0);
|
||||
memcpy(wkey, key.c_str(), keyLen + 1);
|
||||
oxReturnError(handler.fieldCString("", wkey.get(), keyLen));
|
||||
oxRequireM(value, val->at(key));
|
||||
oxReturnError(handler.field("", value));
|
||||
oxRequireM(value, val.at(key));
|
||||
return handler.field("", value);
|
||||
};
|
||||
// write the array
|
||||
for (std::size_t i = 0; i < len; i++) {
|
||||
auto const&key = keys[i];
|
||||
oxReturnError(loopBody(handler, key, *val));
|
||||
}
|
||||
oxReturnError(writer.finalize());
|
||||
fieldSet = true;
|
||||
|
Loading…
Reference in New Issue
Block a user