From 0ce639aa3cd7433ab15e36d5cb9a581e8d6623d7 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Mon, 1 Nov 2021 18:07:28 +0800 Subject: [PATCH] thneed: use util::read_file to read from the file (#22757) old-commit-hash: 07ad6d7444729c172c201a7a04fc4edd886a5cf4 --- selfdrive/modeld/thneed/serialize.cc | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/selfdrive/modeld/thneed/serialize.cc b/selfdrive/modeld/thneed/serialize.cc index 327c6d6cd3..a52aa0accf 100644 --- a/selfdrive/modeld/thneed/serialize.cc +++ b/selfdrive/modeld/thneed/serialize.cc @@ -2,6 +2,7 @@ #include #include "json11.hpp" +#include "selfdrive/common/util.h" #include "selfdrive/modeld/thneed/thneed.h" using namespace json11; @@ -10,23 +11,16 @@ extern map g_program_source; void Thneed::load(const char *filename) { printf("Thneed::load: loading from %s\n", filename); - FILE *f = fopen(filename, "rb"); - fseek(f, 0L, SEEK_END); - int sz = ftell(f); - fseek(f, 0L, SEEK_SET); - char *buf = (char*)malloc(sz); - fread(buf, 1, sz, f); - fclose(f); - - int jsz = *(int *)buf; - string jj(buf+4, jsz); + string buf = util::read_file(filename); + int jsz = *(int *)buf.data(); string err; + string jj(buf.data() + sizeof(int), jsz); Json jdat = Json::parse(jj, err); map real_mem; real_mem[NULL] = NULL; - int ptr = 4+jsz; + int ptr = sizeof(int)+jsz; for (auto &obj : jdat["objects"].array_items()) { auto mobj = obj.object_items(); int sz = mobj["size"].int_value(); @@ -135,7 +129,6 @@ void Thneed::load(const char *filename) { kq.push_back(kk); } - free(buf); clFinish(command_queue); }