rename init_stream(...) -> init_bitstream(...).

init_bitstream without allocation.
emit_symbol and emit_chunck without reallocation.
This commit is contained in:
Gregor Weiss 2024-07-01 11:01:44 +02:00
parent ad3a1ad061
commit 0673dafb64
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756
4 changed files with 49 additions and 113 deletions

View file

@ -92,7 +92,7 @@
\************************************************************************************************/ \************************************************************************************************/
uint64 bytes_used (bitstream const *const stream); uint64 bytes_used (bitstream const *const stream);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
bitstream* init_stream (uchar *const memory, bitstream* init_bitstream (uchar *const memory,
uint32 const size, uint32 const size,
char const instr); char const instr);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================

View file

@ -107,8 +107,6 @@ bytes_used(bitstream const *const stream)
} }
/*----------------------------------------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------------------------------------*\
! FUNCTION NAME: bitstream* bwc_init_stream(uchar* memory, uint32 size, char instr) !
! -------------- !
! ! ! !
! DESCRIPTION: ! ! DESCRIPTION: !
! ------------ ! ! ------------ !
@ -145,7 +143,7 @@ bytes_used(bitstream const *const stream)
! ! ! !
\*----------------------------------------------------------------------------------------------------------*/ \*----------------------------------------------------------------------------------------------------------*/
bitstream* bitstream*
init_stream(uchar* memory, uint32 size, char instr) init_bitstream(uchar* memory, uint32 size, char instr)
{ {
/*-----------------------*\ /*-----------------------*\
! DEFINE STRUCTS: ! ! DEFINE STRUCTS: !
@ -155,6 +153,7 @@ init_stream(uchar* memory, uint32 size, char instr)
/*-----------------------*\ /*-----------------------*\
! DEFINE ASSERTIONS: ! ! DEFINE ASSERTIONS: !
\*-----------------------*/ \*-----------------------*/
assert(memory);
assert(instr == 'c' || instr == 'd'); assert(instr == 'c' || instr == 'd');
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
@ -168,39 +167,11 @@ init_stream(uchar* memory, uint32 size, char instr)
return NULL; return NULL;
} }
// TODO: Here is a problem. Memory might be allocated here or coming from outside.
// Violates RAII. Can lead to problem in terminate_stream.
/*--------------------------------------------------------*\
! Evaluate if a valid memory handle has been passed by the !
! function caller. !
\*--------------------------------------------------------*/
if(!memory)
{
/*--------------------------------------------------------*\
! If no valid memory handle has been passed, allocate a !
! memory block with the specifiec stream size. !
\*--------------------------------------------------------*/
stream->memory = calloc(size, sizeof(uchar));
if(!stream->memory)
{
// memory allocation error
fprintf(stderr, MEMERROR);
return NULL;
}
}
else
{
/*--------------------------------------------------------*\
! If a valid memory handle has been passed for decoding, !
! save the memory handle in the bwc stream structure. !
\*--------------------------------------------------------*/
stream->memory = memory;
}
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Initialize the byte buffer counter, stream size and size ! ! Initialize the byte buffer counter, stream size and size !
! increment for the current stream. ! ! increment for the current stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
stream->memory = memory;
stream->t = (instr == 'c') ? 8 : 0; stream->t = (instr == 'c') ? 8 : 0;
stream->Lmax = size; stream->Lmax = size;
stream->size_incr = (uint64)(size / 2); stream->size_incr = (uint64)(size / 2);
@ -249,11 +220,6 @@ init_stream(uchar* memory, uint32 size, char instr)
void void
emit_chunck(bitstream *const stream, const uchar* chunck, const uint64 size) emit_chunck(bitstream *const stream, const uchar* chunck, const uint64 size)
{ {
/*-----------------------*\
! DEFINE INT VARIABLES: !
\*-----------------------*/
uint64 Lreq;
/*-----------------------*\ /*-----------------------*\
! DEFINE ASSERTIONS: ! ! DEFINE ASSERTIONS: !
\*-----------------------*/ \*-----------------------*/
@ -261,54 +227,30 @@ emit_chunck(bitstream *const stream, const uchar* chunck, const uint64 size)
assert(chunck); assert(chunck);
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Evaluate the memory block size if the current chunck of ! ! Check if an error was encountered in a previous writing !
! data is written to the stream. ! ! operation !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
Lreq = (bytes_used(stream) + size); if(!stream->error)
/*--------------------------------------------------------*\
! Check if the enough memory has been allocated for the !
! stream to store the additional data chunck. !
\*--------------------------------------------------------*/
if(Lreq > stream->Lmax)
{ {
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! If the stream is not large enough, check if this is due ! ! Check if the enough memory has been allocated for the !
! to an error encountered in a previous writing operation ! ! stream to store the additional symbol. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
if(!stream->error) if((bytes_used(stream) + size) > stream->Lmax)
{ {
/*--------------------------------------------------------*\ // memory allocation error
! If the error flag is not set, increase the stream size ! stream->error |= 1;
! until it is large enough to store the additional data ! stream->Lmax = 0;
! chunck. !
\*--------------------------------------------------------*/
while(Lreq > stream->Lmax)
{
stream->Lmax += stream->size_incr + size;
stream->size_incr = (uint64)(stream->Lmax / 2);
}
/*--------------------------------------------------------*\
! Reallocate the stream data block. !
\*--------------------------------------------------------*/
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return; return;
} }
} }
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return;
}
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Copy the additional data to the stream memory block. ! ! Copy the additional data to the stream memory block. !
@ -372,44 +314,30 @@ emit_symbol(bitstream *const stream, const uint64 symbol, const uint8 size)
assert(stream); assert(stream);
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Check if the enough memory has been allocated for the ! ! Check if an error was encountered in a previous writing !
! stream to store the additional symbol. ! ! operation !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
if((bytes_used(stream) + size) > stream->Lmax) if(!stream->error)
{ {
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! If the stream is not large enough, check if this is due ! ! Check if the enough memory has been allocated for the !
! to an error encountered in a previous writing operation ! ! stream to store the additional symbol. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
if(!stream->error) if((bytes_used(stream) + size) > stream->Lmax)
{ {
/*--------------------------------------------------------*\ // memory allocation error
! If the error flag is not set, increment the stream size ! stream->error |= 1;
! to store the additional symbol. ! stream->Lmax = 0;
\*--------------------------------------------------------*/
stream->Lmax += stream->size_incr;
stream->size_incr = (uint64)(stream->Lmax / 2);
/*--------------------------------------------------------*\
! Reallocate the stream data block. !
\*--------------------------------------------------------*/
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return; return;
} }
} }
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return;
}
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Copy the additional symbol to the stream memory block ! ! Copy the additional symbol to the stream memory block !

View file

@ -1395,7 +1395,7 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
! Initialize the final codestream and emit the header ! ! Initialize the final codestream and emit the header !
! bytes. ! ! bytes. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
stream = init_stream(data->out, control->codestreamSize, 'c'); stream = init_bitstream(data->out, control->codestreamSize, 'c');
codestream_write_header(stream, field, data); codestream_write_header(stream, field, data);
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
@ -1414,7 +1414,7 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
assemble_tile(field, tile, stream); assemble_tile(field, tile, stream);
} }
emit_symbol(stream, EOC, 2); emit_symbol(&(*stream), EOC, 2);
packed_stream = calloc(1, sizeof(bwc_span)); packed_stream = calloc(1, sizeof(bwc_span));
if(!packed_stream) if(!packed_stream)
@ -1484,7 +1484,7 @@ parse_codestream(bwc_codec *const codec, bwc_stream *const data, uint8 const lay
! Initialize a bitstream used to parse the packed code- ! ! Initialize a bitstream used to parse the packed code- !
! stream. ! ! stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
stream = init_stream(data->inp, 10, 'd'); stream = init_bitstream(data->inp, 10, 'd');
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Parse the main header and set up the codec structure for ! ! Parse the main header and set up the codec structure for !

View file

@ -450,6 +450,7 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
int16 *cp_contr; int16 *cp_contr;
int16 delta_z; int16 delta_z;
int8 m; int8 m;
uchar *memory;
/*-----------------------*\ /*-----------------------*\
! DEFINE STRUCTS: ! ! DEFINE STRUCTS: !
@ -482,7 +483,14 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
! Initialize the stream that is used to assemble the pack- ! ! Initialize the stream that is used to assemble the pack- !
! et header. ! ! et header. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
header = init_stream(NULL, PACKET_HEADER_SIZE, 'c'); memory = calloc(PACKET_HEADER_SIZE, sizeof(uchar));
if(!memory)
{
// memory allocation error
fprintf(stderr, MEMERROR);
return -1;
}
header = init_bitstream(memory, PACKET_HEADER_SIZE, 'c');
if(!header) if(!header)
{ {
// memory allocation error // memory allocation error
@ -1392,7 +1400,7 @@ parse_packet(bwc_codec *const field, bwc_tile *const tile,
! Initialize the stream that is used to parse the packet ! ! Initialize the stream that is used to parse the packet !
! codestream. ! ! codestream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
header = init_stream(packet->header.memory, body_size, 'd'); header = init_bitstream(packet->header.memory, body_size, 'd');
if(!header) if(!header)
{ {
// memory allocation error // memory allocation error