refactor aux buffer and macros

This commit is contained in:
Gregor Weiss 2024-10-18 11:23:12 +02:00
parent a5d67f8470
commit 26c00bd4e3
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756
2 changed files with 45 additions and 49 deletions

View file

@ -332,9 +332,13 @@
} field;
// TODO: implement aux as queue
uchar *uchar_aux;
uint32 aux_pos;
uint32 aux_len;
struct aux
{
uchar *ptr;
uint32 pos;
uint32 len;
} aux;
} eas3_data;
/************************************************************************************************************\

View file

@ -93,25 +93,25 @@
! information. !
! !
\*----------------------------------------------------------------------------------------------------------*/
#define aux_enqueue(aux, aux_pos, aux_len, chunck, chunck_len) \
#define aux_enqueue(aux, chunck, chunck_len) \
{ \
if (aux_pos + chunck_len > aux_len) \
if (aux.pos + chunck_len > aux.len) \
{ \
while(aux_pos + chunck_len > aux_len) \
while(aux.pos + chunck_len > aux.len) \
{ \
aux_len += aux_len / 2; \
aux.len += aux.len / 2; \
} \
aux = realloc(aux, aux_len); \
aux.ptr = realloc(aux.ptr, aux.len); \
} \
memcpy(aux + aux_pos, chunck, chunck_len); \
aux_pos += chunck_len; \
memcpy(aux.ptr + aux.pos, chunck, chunck_len); \
aux.pos += chunck_len; \
}
#define aux_dequeue(aux, aux_pos, aux_len, chunck, chunck_len) \
#define aux_dequeue(aux, chunck, chunck_len) \
{ \
if(aux_pos + chunck_len <= aux_len) { \
memcpy(chunck, aux + aux_pos, chunck_len); \
aux_pos += chunck_len; \
if(aux.pos + chunck_len <= aux.len) { \
memcpy(chunck, aux.ptr + aux.pos, chunck_len); \
aux.pos += chunck_len; \
} else { \
fprintf(stderr, MEMERROR); \
} \
@ -395,10 +395,10 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! block has been chosen arbitrarily and should be large !
! enough to prevent excessive reallocation. !
\*--------------------------------------------------------*/
data->uchar_aux = calloc(AUX_SIZE, sizeof(uchar));
data->aux_pos = 0;
data->aux_len = AUX_SIZE;
if(!data->uchar_aux)
data->aux.ptr = calloc(AUX_SIZE, sizeof(uchar));
data->aux.pos = 0;
data->aux.len = AUX_SIZE;
if(!data->aux.ptr)
{
// memory allocation error
fprintf(stderr, MEMERROR);
@ -436,8 +436,7 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! Emit the standard parameters to the auxiliary informa- !
! tion information memory block. !
\*--------------------------------------------------------*/
aux_enqueue(data->uchar_aux, data->aux_pos, data->aux_len,
(uchar*)params, 176);
aux_enqueue(data->aux, (uchar*)params, 176);
/*--------------------------------------------------------*\
! Convert the parameters required for the bwc compression !
@ -501,8 +500,7 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! Emit the time step array to the auxiliary information !
! memory block. !
\*--------------------------------------------------------*/
aux_enqueue(data->uchar_aux, data->aux_pos, data->aux_len, buffer_char,
params->nts * sizeof(uint64));
aux_enqueue(data->aux, buffer_char, params->nts * sizeof(uint64));
/*--------------------------------------------------------*\
! Check if any attributes have been specified in the eas3 !
@ -535,8 +533,7 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! Emit the timestep attribute array to the auxiliary infor-!
! mation memory block. !
\*--------------------------------------------------------*/
aux_enqueue(data->uchar_aux, data->aux_pos, data->aux_len, buffer_char,
params->nts * ATTRLEN * sizeof(char));
aux_enqueue(data->aux, buffer_char, params->nts * ATTRLEN * sizeof(char));
for(i = 0; i < params->npar; ++i)
{
@ -607,8 +604,7 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! Emit the remaining header information the the auxiliary !
! information stream. !
\*--------------------------------------------------------*/
aux_enqueue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, Lread);
aux_enqueue(data->aux, buffer_char, Lread);
/*--------------------------------------------------------*\
! Free the buffer character array. !
@ -620,15 +616,15 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
! ful, the address to the aux memory block stored is !
! stored in the file structure alongside its size. !
\*--------------------------------------------------------*/
if(data->aux_pos != data->aux_len)
if(data->aux.pos != data->aux.len)
{
data->aux_len = data->aux_pos;
data->uchar_aux = realloc(data->uchar_aux, data->aux_len);
if(!data->uchar_aux)
data->aux.len = data->aux.pos;
data->aux.ptr = realloc(data->aux.ptr, data->aux.len);
if(!data->aux.ptr)
{
// memory allocation error
fprintf(stderr, MEMERROR);
data->aux_len = 0;
data->aux.len = 0;
return 1;
}
}
@ -706,7 +702,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
}
// Rewind aux
data->aux_pos = 0;
data->aux.pos = 0;
/*--------------------------------------------------------*\
! Get the standard parameters from the auxiliary informa- !
@ -720,8 +716,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(params);
return 1;
}
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
params, sizeof(eas3_std_params));
aux_dequeue(data->aux, params, sizeof(eas3_std_params));
if(fwrite(params, sizeof(uint64), 22, fp) != 22)
{
@ -757,8 +752,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char);
return 1;
}
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, data->params.nts * sizeof(uint64));
aux_dequeue(data->aux, buffer_char, data->params.nts * sizeof(uint64));
if(fwrite(buffer_char, sizeof(uint64), data->params.nts, fp) != data->params.nts)
{
@ -788,8 +782,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char);
return 1;
}
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, data->params.nts * ATTRLEN);
aux_dequeue(data->aux, buffer_char, data->params.nts * ATTRLEN);
if(fwrite(buffer_char, sizeof(uchar), data->params.nts * ATTRLEN, fp) != (data->params.nts * ATTRLEN))
{
@ -854,8 +847,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char);
return 1;
}
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, Lwrite);
aux_dequeue(data->aux, buffer_char, Lwrite);
if(fwrite(buffer_char, sizeof(uchar), Lwrite, fp) != Lwrite)
{