|
43 | 43 | #include <cassert> |
44 | 44 | #include <cstddef> |
45 | 45 |
|
| 46 | +#include <stdlib.h> |
| 47 | + |
46 | 48 | #include "ojph_file.h" |
47 | 49 | #include "ojph_message.h" |
48 | 50 |
|
@@ -110,7 +112,11 @@ namespace ojph { |
110 | 112 | mem_outfile::~mem_outfile() |
111 | 113 | { |
112 | 114 | if (buf) |
| 115 | +#ifdef OJPH_OS_WINDOWS |
| 116 | + _aligned_free(buf); |
| 117 | +#else |
113 | 118 | free(buf); |
| 119 | +#endif |
114 | 120 | is_open = clear_mem = false; |
115 | 121 | buf_size = used_size = 0; |
116 | 122 | buf = cur_ptr = NULL; |
@@ -198,15 +204,29 @@ namespace ojph { |
198 | 204 | /** */ |
199 | 205 | void mem_outfile::expand_storage(size_t needed_size, bool clear_all) |
200 | 206 | { |
| 207 | + u8 * new_buf = nullptr; |
201 | 208 | if (needed_size > buf_size) |
202 | 209 | { |
203 | 210 | needed_size += (needed_size + 1) >> 1; // x1.5 |
204 | | - si64 used_size = tell(); // current used size |
205 | | - |
206 | | - if (this->buf) |
207 | | - this->buf = (ui8*)realloc(this->buf, needed_size); |
208 | | - else |
209 | | - this->buf = (ui8*)malloc(needed_size); |
| 211 | + size_t used_size = (size_t)tell(); // current used size |
| 212 | + |
| 213 | +#ifdef OJPH_OS_WINDOWS |
| 214 | + new_buf = (ui8*)_aligned_malloc(4096, needed_size); |
| 215 | +#else |
| 216 | + new_buf = (ui8*)aligned_alloc(4096, needed_size); |
| 217 | +#endif |
| 218 | + if (new_buf == nullptr) |
| 219 | + OJPH_ERROR(0x00060004, "failed to allocate memory (%zu bytes)", needed_size); |
| 220 | + |
| 221 | + if (this->buf != nullptr && this->buf != new_buf && !clear_all){ |
| 222 | + memcpy(new_buf, this->buf, used_size); |
| 223 | +#ifdef OJPH_OS_WINDOWS |
| 224 | + _aligned_free(this->buf); |
| 225 | +#else |
| 226 | + free(this->buf); |
| 227 | +#endif |
| 228 | + } |
| 229 | + this->buf = new_buf; |
210 | 230 |
|
211 | 231 | if (clear_mem && !clear_all) // will be cleared later |
212 | 232 | memset(this->buf + buf_size, 0, needed_size - this->buf_size); |
|
0 commit comments