From 2e5faae9dab051269d77ccb4f53b039bc63a732b Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 16 Apr 2013 11:46:06 +0200 Subject: [PATCH] Avoid unnecessary large memory allocations. Because the size of a dive isn't known in advance, we use the worst case value of 0xFFFFFF (or nearly 16MB). However in practice, the real size is many orders of magnitude smaller, even after the decompression. Instead of pre-allocating a huge memory buffer, we now start with a much smaller one, and increase when necessary. For the predator and petrel manifests, where the size is known in advance, we continue to pre-allocate the exact amount of memory as before. --- src/shearwater_common.c | 2 +- src/shearwater_predator.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/shearwater_common.c b/src/shearwater_common.c index 0a73891..fef8460 100644 --- a/src/shearwater_common.c +++ b/src/shearwater_common.c @@ -338,7 +338,7 @@ shearwater_common_download (shearwater_common_device_t *device, dc_buffer_t *buf unsigned char response[SZ_PACKET]; // Erase the current contents of the buffer. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, size)) { + if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } diff --git a/src/shearwater_predator.c b/src/shearwater_predator.c index 81895e4..f0b1e99 100644 --- a/src/shearwater_predator.c +++ b/src/shearwater_predator.c @@ -134,6 +134,12 @@ shearwater_predator_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { shearwater_common_device_t *device = (shearwater_common_device_t *) abstract; + // Erase the current contents of the buffer. + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { + ERROR (abstract->context, "Insufficient buffer space available."); + return DC_STATUS_NOMEMORY; + } + return shearwater_common_download (device, buffer, 0xDD000000, SZ_MEMORY, 0); }