From 29665ae79c862a1bc0818f625b59384273a9dac4 Mon Sep 17 00:00:00 2001 From: gibbed Date: Wed, 17 Jul 2019 17:51:36 -0500 Subject: [PATCH] [CPU] Minor LZX code cleanup. --- src/xenia/cpu/lzx.cc | 55 +++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/xenia/cpu/lzx.cc b/src/xenia/cpu/lzx.cc index ced471221..992daca43 100644 --- a/src/xenia/cpu/lzx.cc +++ b/src/xenia/cpu/lzx.cc @@ -22,19 +22,20 @@ #include "third_party/mspack/mspack.h" typedef struct mspack_memory_file_t { - struct mspack_system sys; + mspack_system sys; void* buffer; off_t buffer_size; off_t offset; } mspack_memory_file; -mspack_memory_file* mspack_memory_open(struct mspack_system* sys, void* buffer, + +mspack_memory_file* mspack_memory_open(mspack_system* sys, void* buffer, const size_t buffer_size) { assert_true(buffer_size < INT_MAX); if (buffer_size >= INT_MAX) { return NULL; } - mspack_memory_file* memfile = - (mspack_memory_file*)calloc(1, sizeof(mspack_memory_file)); + auto memfile = + (mspack_memory_file*)std::calloc(1, sizeof(mspack_memory_file)); if (!memfile) { return NULL; } @@ -43,36 +44,42 @@ mspack_memory_file* mspack_memory_open(struct mspack_system* sys, void* buffer, memfile->offset = 0; return memfile; } + void mspack_memory_close(mspack_memory_file* file) { - mspack_memory_file* memfile = (mspack_memory_file*)file; - free(memfile); + auto memfile = (mspack_memory_file*)file; + std::free(memfile); } -int mspack_memory_read(struct mspack_file* file, void* buffer, int chars) { - mspack_memory_file* memfile = (mspack_memory_file*)file; + +int mspack_memory_read(mspack_file* file, void* buffer, int chars) { + auto memfile = (mspack_memory_file*)file; const off_t remaining = memfile->buffer_size - memfile->offset; const off_t total = std::min(static_cast(chars), remaining); std::memcpy(buffer, (uint8_t*)memfile->buffer + memfile->offset, total); memfile->offset += total; return (int)total; } -int mspack_memory_write(struct mspack_file* file, void* buffer, int chars) { - mspack_memory_file* memfile = (mspack_memory_file*)file; + +int mspack_memory_write(mspack_file* file, void* buffer, int chars) { + auto memfile = (mspack_memory_file*)file; const off_t remaining = memfile->buffer_size - memfile->offset; const off_t total = std::min(static_cast(chars), remaining); std::memcpy((uint8_t*)memfile->buffer + memfile->offset, buffer, total); memfile->offset += total; return (int)total; } -void* mspack_memory_alloc(struct mspack_system* sys, size_t chars) { + +void* mspack_memory_alloc(mspack_system* sys, size_t chars) { return std::calloc(chars, 1); } -void mspack_memory_free(void* ptr) { free(ptr); } + +void mspack_memory_free(void* ptr) { std::free(ptr); } + void mspack_memory_copy(void* src, void* dest, size_t chars) { std::memcpy(dest, src, chars); } -struct mspack_system* mspack_memory_sys_create() { - struct mspack_system* sys = - (struct mspack_system*)std::calloc(1, sizeof(struct mspack_system)); + +mspack_system* mspack_memory_sys_create() { + auto sys = (mspack_system*)std::calloc(1, sizeof(mspack_system)); if (!sys) { return NULL; } @@ -83,19 +90,13 @@ struct mspack_system* mspack_memory_sys_create() { sys->copy = mspack_memory_copy; return sys; } + void mspack_memory_sys_destroy(struct mspack_system* sys) { free(sys); } int lzx_decompress(const void* lzx_data, size_t lzx_len, void* dest, size_t dest_len, uint32_t window_size, void* window_data, size_t window_data_len) { - uint32_t window_bits = 0; - uint32_t temp_sz = window_size; - for (size_t m = 0; m < 32; m++, window_bits++) { - temp_sz >>= 1; - if (temp_sz == 0x00000000) { - break; - } - } + uint32_t window_bits = xe::bit_count(window_size); int result_code = 1; @@ -103,9 +104,8 @@ int lzx_decompress(const void* lzx_data, size_t lzx_len, void* dest, mspack_memory_file* lzxsrc = mspack_memory_open(sys, (void*)lzx_data, lzx_len); mspack_memory_file* lzxdst = mspack_memory_open(sys, dest, dest_len); - lzxd_stream* lzxd = - lzxd_init(sys, (struct mspack_file*)lzxsrc, (struct mspack_file*)lzxdst, - window_bits, 0, 0x8000, (off_t)dest_len, 0); + lzxd_stream* lzxd = lzxd_init(sys, (mspack_file*)lzxsrc, (mspack_file*)lzxdst, + window_bits, 0, 0x8000, (off_t)dest_len, 0); if (lzxd) { if (window_data) { @@ -121,14 +121,17 @@ int lzx_decompress(const void* lzx_data, size_t lzx_len, void* dest, lzxd_free(lzxd); lzxd = NULL; } + if (lzxsrc) { mspack_memory_close(lzxsrc); lzxsrc = NULL; } + if (lzxdst) { mspack_memory_close(lzxdst); lzxdst = NULL; } + if (sys) { mspack_memory_sys_destroy(sys); sys = NULL;