htcp

Properties of water suspended in clouds
git clone git://git.meso-star.fr/htcp.git
Log | Files | Refs | README | LICENSE

commit 919404f768af19476a90f673c6ae821d351cf18d
parent c9af5295aace4ee097f90c75ce59bc60a1d0045b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 30 Apr 2018 11:34:28 +0200

On htcop release, ensure that mapped memory is unmapped

Diffstat:
Msrc/htcop.c | 52++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 42 insertions(+), 10 deletions(-)

diff --git a/src/htcop.c b/src/htcop.c @@ -40,9 +40,10 @@ struct htcop { double* RCT; /* Mapped memory */ double* RVT; /* Mapped memory */ + size_t RCT_length; /* In bytes */ + size_t RVT_length; /* In bytes */ size_t pagesize_os; /* Page size of the os */ - int verbose; /* Verbosity level */ struct logger* logger; struct mem_allocator* allocator; @@ -77,6 +78,28 @@ log_err(const struct htcop* htcop, const char* msg, ...) va_end(vargs_list); } +static void +reset_htcop(struct htcop* htcop) +{ + htcop->pagesize = 0; + htcop->irregular_z = 0; + htcop->definition[0] = 0; + htcop->definition[1] = 0; + htcop->definition[2] = 0; + htcop->lower[0] = -1; + htcop->lower[1] = -1; + htcop->lower[2] = -1; + htcop->vxsz[0] = -1; + htcop->vxsz[1] = -1; + darray_double_clear(&htcop->vxsz_z); + if(htcop->RCT) munmap(htcop->RCT, htcop->RCT_length); + if(htcop->RVT) munmap(htcop->RVT, htcop->RVT_length); + htcop->RCT = NULL; + htcop->RVT = NULL; + htcop->RCT_length = 0; + htcop->RVT_length = 0; +} + static res_T load_stream(struct htcop* htcop, FILE* stream, const char* stream_name) { @@ -86,6 +109,8 @@ load_stream(struct htcop* htcop, FILE* stream, const char* stream_name) res_T res = RES_OK; ASSERT(htcop && stream && stream_name); + reset_htcop(htcop); + #define READ(Var, N, Name) { \ if(fread((Var), sizeof(*(Var)), (N), stream) != (N)) { \ fprintf(stderr, "%s: could not read the %s\n", stream_name, Name); \ @@ -112,13 +137,25 @@ load_stream(struct htcop* htcop, FILE* stream, const char* stream_name) READ(&htcop->irregular_z, 1, "'irregular Z' flag"); READ(htcop->definition, 4, "spatial and time definitions"); + if(htcop->definition[0] <= 0 + || htcop->definition[1] <= 0 + || htcop->definition[2] <= 0 + || htcop->definition[3] <= 0) { + log_err(htcop, +"%s: the spatial/time definition cannot be negative or null -- spatial " +"definition: %i %i %i; time definition: %i\n", + stream_name, SPLIT3(htcop->definition), htcop->definition[TIME]); + res = RES_BAD_ARG; + goto error; + } + READ(htcop->lower, 3, "lower position"); READ(htcop->vxsz, 2, "XY voxel size "); nz = htcop->irregular_z ? (size_t)htcop->definition[Z] : 1; res = darray_double_resize(&htcop->vxsz_z, nz); if(res != RES_OK) { - fprintf(stderr, + log_err(htcop, "%s: could not allocate memory to store the size of the voxels in Z.\n", stream_name); res = RES_MEM_ERR; @@ -136,13 +173,6 @@ load_stream(struct htcop* htcop, FILE* stream, const char* stream_name) /* Align sizeof mapped data onto page size */ map_len = ALIGN_SIZE(map_len, (size_t)htcop->pagesize); - if(map_len == 0) { - fprintf(stderr, "%s: the spatial/time definition cannot be null.\n", - stream_name); - res = RES_BAD_ARG; - goto error; - } - /* Ensure that offset is align on page size */ offset = ALIGN_SIZE(ftell(stream), htcop->pagesize); @@ -150,11 +180,12 @@ load_stream(struct htcop* htcop, FILE* stream, const char* stream_name) htcop->Var = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE|MAP_POPULATE, \ fileno(stream), offset); \ if(htcop->Var == MAP_FAILED) { \ - fprintf(stderr, "%s: could not map the "STR(Var)" data -- %s.\n", \ + log_err(htcop, "%s: could not map the "STR(Var)" data -- %s.\n", \ stream_name, strerror(errno)); \ res = RES_IO_ERR; \ goto error; \ } \ + htcop->Var##_length = map_len; \ } (void)0 MMAP(RVT); offset += (off_t)map_len; MMAP(RCT); offset += (off_t)map_len; @@ -174,6 +205,7 @@ release_htcop(ref_T* ref) struct htcop* htcop; ASSERT(ref); htcop = CONTAINER_OF(ref, struct htcop, ref); + reset_htcop(htcop); darray_double_release(&htcop->vxsz_z); MEM_RM(htcop->allocator, htcop); }