Commit 8d09b0b8 authored by Lee Shallis's avatar Lee Shallis
Browse files

Buffer never felt right for the memory management system I was using,

also was prone to mistyping it as BUffer, Block feels more right and I
hardly ever mis-type that so changed the nameing scheme there, forgot
there was some gl stuff that used BUFFER in their defines and did a
global replace, fixed all the compile time errors that occured as a
result and plenty of run-time errors that are likely a result of me not
having the allocation list before, somehow still getting a runtime error
though as traits that should have locations are causing an error when I
attempt to use the locations given by the API
parent ae832cdd
......@@ -18,11 +18,11 @@ void * defaultAlloc( void *ud, void *mem, size_t now, size_t cap )
return defaultUlloc( ud, mem, cap );
}
void * ExpandBuffer( BUFFER *Buffer, int nsize, int nwant )
void * ExpandBlock( BLOCK *Block, int nsize, int nwant )
{
ALLOC *Alloc = Buffer->Alloc;
uchar *mem = Buffer->addr;
size_t get, had = Buffer->size;
ALLOC *Alloc = Block->Alloc;
uchar *mem = Block->addr;
size_t get, had = Block->size;
nwant = (nwant > 0) ? nwant : 0;
get = nwant * nsize;
......@@ -40,9 +40,9 @@ void * ExpandBuffer( BUFFER *Buffer, int nsize, int nwant )
if ( get > had )
memset( mem + had, 0, get - had );
Buffer->addr = mem;
Buffer->size = get;
Buffer->have = get / nsize;
Block->addr = mem;
Block->size = get;
Block->have = nwant;
return mem;
}
......@@ -50,42 +50,40 @@ void * ExpandBuffer( BUFFER *Buffer, int nsize, int nwant )
return NULL;
}
void * ShrinkBuffer( BUFFER *Buffer, int nsize, int nwant )
void * ShrinkBlock( BLOCK *Block, int nsize, int nwant )
{
ALLOC *Alloc = Buffer->Alloc;
ALLOC *Alloc = Block->Alloc;
size_t need;
void * addr;
nwant = (nwant > 0) ? nwant : 0;
need = nsize * nwant;
if ( Buffer->size < need )
if ( Block->size < need )
return NULL;
else if ( Buffer->size == need )
return Buffer->addr;
else if ( Block->size == need )
return Block->addr;
addr = Alloc->ulloc( Alloc->ud, Buffer->addr, need );
addr = Alloc->ulloc( Alloc->ud, Block->addr, need );
if ( !need )
{
memset( Buffer, 0, sizeof(BUFFER) );
Buffer->Alloc = Alloc;
memset( Block, 0, sizeof(BLOCK) );
Block->Alloc = Alloc;
}
else if ( !addr )
return NULL;
else
else if ( addr )
{
Buffer->addr = addr;
Buffer->size = need;
Block->addr = addr;
Block->size = need;
}
return NULL;
return addr;
}
void * ShrinkBinary( struct _BINARY *Binary, intmax_t want )
{
BIT Want;
BUFFER *Buffer = &(Binary->Buffer);
BLOCK *Block = &(Binary->Block);
if ( want < 1 )
want = 0;
......@@ -93,15 +91,15 @@ void * ShrinkBinary( struct _BINARY *Binary, intmax_t want )
if ( want > Binary->Used.abs )
return NULL;
SetBit( &Want, Buffer->addr, want );
return ShrinkBuffer( Buffer, 1, want ? Want.pos + !!(Want.mask) : 0 );
SetBit( &Want, Block->addr, want );
return ShrinkBlock( Block, 1, want ? Want.pos + !!(Want.mask) : 0 );
}
void * ExpandBinary( struct _BINARY *Binary, intmax_t want )
{
BIT Used;
intmax_t Have = Binary->Have.abs;
uchar *ptr = Binary->Buffer.addr;
uchar *ptr = Binary->Block.addr;
if ( want < 1 )
return NULL;
......@@ -112,12 +110,12 @@ void * ExpandBinary( struct _BINARY *Binary, intmax_t want )
SetBit( &Want, NULL, want );
ptr = ExpandBuffer( &(Binary->Buffer), 1, Want.pos + 1 );
ptr = ExpandBlock( &(Binary->Block), 1, Want.pos + 1 );
if ( !ptr )
return NULL;
Want.abs = Binary->Buffer.size;
Want.abs = Binary->Block.size;
Want.abs *= CHAR_BIT;
SetBit( &(Binary->Used), ptr, Binary->Used.abs );
......@@ -134,151 +132,171 @@ void * ExpandBinary( struct _BINARY *Binary, intmax_t want )
void * ExpandSeries( struct _SERIES *Series, int size, int want )
{
Series->Buffer.Alloc = Series->Alloc;
Series->Active.Buffer.Alloc = Series->Alloc;
BLOCK *Block = &(Series->Block);
if ( size < 1 )
{
if ( Block->have < 1 )
return NULL;
if ( !ExpandBinary( &(Series->Active), want ) )
size = Block->size / Block->have;
}
Series->Block.Alloc = Series->Alloc;
Series->InUse.Block.Alloc = Series->Alloc;
if ( !ExpandBinary( &(Series->InUse), want ) )
return NULL;
return ExpandBuffer( &(Series->Buffer), size, want );
return ExpandBlock( &(Series->Block), size, want );
}
int ExtraBuffers( BUFFERS *Buffers, int want )
int ExtraBlocks( BLOCKS *Blocks, int want )
{
BUFFER *buffers;
BINARY *Active = &(Buffers->Active);
BUFFER *Buffer = &(Buffers->Buffer);
BINARY *Active = &(Blocks->InUse);
BIT *Used = &(Active->Used);
int num = Used->abs + want;
if ( num < 0 )
return ERANGE;
buffers = ExpandSeries( Buffers, sizeof(BUFFER), num );
if ( !buffers )
if ( !ExpandSeries( Blocks, sizeof(BLOCK), num ) )
return ENOMEM;
Buffer->used = num;
AddBit( Used, Active->Buffer.addr, want );
return 0;
}
int ObtainBuffer( BUFFERS *Buffers )
int ObtainSpare( SERIES *Series, int size, int add )
{
int i, num = Series->InUse.Used.abs;
uchar *active = Series->InUse.Block.addr;
BIT pos;
BINARY *Active = &(Buffers->Active);
int num = Active->Used.abs, i = 1;
BUFFER *buffers = Buffers->Buffer.addr, *buffer;
for
(
SetBit( &pos, Active->Buffer.addr, i );
i < num;
++i, IncBit( &pos, Active->Buffer.addr )
)
SetBit( &pos, active, 0 );
for ( i = 1; i < num; ++i, IncBit( &pos, active ) )
{
if ( *(pos.byte) & pos.mask )
continue;
*((uchar*)(pos.byte)) |= pos.mask;
buffer = buffers + i;
buffer->have = 0;
buffer->used = 0;
buffer->Alloc = Buffers->Alloc;
if ( buffer->addr )
memset( buffer->addr, 0, buffer->size );
return i;
}
++num;
if ( add < 1 ) add = 1;
if ( !ExpandSeries( Series, size, i + add ) )
return -1;
IncBit( &(Series->InUse.Used), active );
Series->Block.used++;
return i;
}
int ObtainBlock( BLOCKS *Blocks )
{
uchar *data;
int num = 32, i = ObtainSpare( Blocks, sizeof(BLOCK), num );
BLOCK *blocks = Blocks->Block.addr, *Block;
if ( num > Active->Have.abs )
if ( i < 0 )
{
int err = ExtraBuffers( Buffers, 32 );
blocks = ExpandSeries( Blocks, sizeof(BLOCK), num );
if ( err )
{
FILE *out = Buffers->Alloc->out;
ECHO( out, ECHO_ERR( out, err ) );
if ( !blocks )
return -1;
}
buffers = Buffers->Buffer.addr;
i = ObtainSpare( Blocks, sizeof(BLOCK), num );
if ( i < 0 )
return -1;
}
IncBit( &(Active->Used), Active->Buffer.addr );
Buffers->Buffer.used++;
Block = blocks + i;
Block->Alloc = Blocks->Alloc;
Block->have = 0;
Block->used = 0;
data = Block->addr;
SetBit( &pos, Active->Buffer.addr, i );
*((uchar*)(pos.byte)) |= pos.mask;
buffer = buffers + i;
buffer->have = 0;
buffer->used = 0;
buffer->Alloc = Buffers->Alloc;
if ( data )
{
memset( data, 0, Block->size );
return i;
}
if ( buffer->addr )
memset( buffer->addr, 0, buffer->size );
Block->size = 0;
return i;
}
BUFFER * AccessBuffer( BUFFERS *Buffers, int i )
BLOCK * AccessBlock( BLOCKS *Blocks, int i )
{
BUFFER * buffers = Buffers->Buffer.addr;
return (i > 0) ? buffers + i : NULL;
if ( Blocks && i > 0 )
{
BLOCK * blocks = Blocks->Block.addr;
return blocks + i;
}
return NULL;
}
void GiveUpBuffer( BUFFERS *Buffers, int i )
void GiveUpBlock( BLOCKS *Blocks, int i )
{
BIT pos;
BINARY *Active = &(Buffers->Active);
if ( Blocks )
{
BIT pos;
BINARY *Active = &(Blocks->InUse);
if ( i < 1 || Active->Used.abs <= i )
return;
if ( i < 1 || Active->Used.abs <= i )
return;
SetBit( &pos, Active->Buffer.addr, i );
*((uchar*)(pos.byte)) &= ~(pos.mask);
SetBit( &pos, Active->Block.addr, i );
*((uchar*)(pos.byte)) &= ~(pos.mask);
}
}
void * ShrinkSeries( struct _SERIES *Series, int size, int want )
{
BINARY *Active = &(Series->Active);
BUFFER *Buffer = &(Series->Buffer);
if ( Series )
{
BINARY *Active = &(Series->InUse);
BLOCK *Block = &(Series->Block);
ShrinkBinary( Active, want );
return ShrinkBlock( Block, size, want );
}
ShrinkBinary( Active, want );
return ShrinkBuffer( Buffer, size, want );
return NULL;
}
void EmptyBuffers( BUFFERS *Buffers )
void EmptyBlocks( BLOCKS *Blocks )
{
int i;
ALLOC *Alloc = Buffers->Alloc;
BUFFER *buffers = Buffers->Buffer.addr;
ALLOC *Alloc = Blocks->Alloc;
BLOCK *blocks = Blocks->Block.addr;
Buffers->Buffer.Alloc = Alloc;
Buffers->Active.Buffer.Alloc = Alloc;
Blocks->Block.Alloc = Alloc;
Blocks->InUse.Block.Alloc = Alloc;
for ( i = 0; i < Buffers->Active.Used.abs; ++i )
for ( i = 0; i < Blocks->InUse.Used.abs; ++i )
{
BUFFER *Buffer = buffers + 1;
Buffer->Alloc = Alloc;
ShrinkBuffer( Buffer, 1, 0 );
BLOCK *Block = blocks + 1;
Block->Alloc = Alloc;
ShrinkBlock( Block, 1, 0 );
}
ShrinkSeries( Buffers, sizeof(BUFFER), 0 );
ShrinkSeries( Blocks, sizeof(BLOCK), 0 );
}
void EchoBufferDetails( FILE *file, BUFFER *Buffer )
void EchoBlockDetails( FILE *file, BLOCK *Block )
{
fprintf
(
file,
"Buffer Details: addr = %p, "
"Block Details: addr = %p, "
"used = %d, have = %d, size = %" PRIuMAX "\n",
Buffer->addr, Buffer->used, Buffer->have, (uintmax_t)(Buffer->size)
Block->addr, Block->used, Block->have, (uintmax_t)(Block->size)
);
}
......@@ -61,23 +61,23 @@ void ForeBits( void *DST, void const *SRC, intmax_t pos, intmax_t num );
void BackBits( void *DST, void const *SRC, intmax_t pos, intmax_t num );
void FlipBits( void *SRC, intmax_t num );
typedef struct _BUFFER
typedef struct _BLOCK
{
ALLOC *Alloc;
size_t size;
int have;
int used;
void *addr;
} BUFFER, CSTR, WSTR, TSTR;
} BLOCK, CSTR, WSTR, TSTR;
void EchoBufferDetails( FILE *file, BUFFER *Buffer );
void * ExpandBuffer( struct _BUFFER *Buffer, int size, int want );
void * ShrinkBuffer( struct _BUFFER *Buffer, int size, int want );
void EchoBlockDetails( FILE *file, BLOCK *Block );
void * ExpandBlock( struct _BLOCK *Block, int size, int want );
void * ShrinkBlock( struct _BLOCK *Block, int size, int want );
typedef struct _BINARY
{
BIT Used, Have;
BUFFER Buffer;
BLOCK Block;
} BINARY;
void * ExpandBinary( struct _BINARY *Binary, intmax_t want );
......@@ -86,18 +86,20 @@ void * ShrinkBinary( struct _BINARY *Binary, intmax_t want );
typedef struct _SERIES
{
ALLOC *Alloc;
BINARY Active;
BUFFER Buffer;
} SERIES, BUFFERS;
BINARY InUse;
BLOCK Block;
} SERIES, BLOCKS;
void * ExpandSeries( struct _SERIES *Series, int size, int want );
void * ShrinkSeries( struct _SERIES *Series, int size, int want );
int ExtraBuffers( BUFFERS *Buffers, int want );
void EmptyBuffers( BUFFERS *Buffers );
int ObtainBuffer( BUFFERS *Buffers );
void GiveUpBuffer( BUFFERS *Buffers, int i );
int ObtainSpare( SERIES *Series, int size, int add );
int ExtraBlocks( BLOCKS *Blocks, int want );
void EmptyBlocks( BLOCKS *Blocks );
int ObtainBlock( BLOCKS *Blocks );
void GiveUpBlock( BLOCKS *Blocks, int i );
/* Does NOT check if in bounds, e.g. buffer_list_ptr + i */
BUFFER *AccessBuffer( BUFFERS *Buffers, int i );
BLOCK *AccessBlock( BLOCKS *Blocks, int i );
typedef struct _DATA
{
......
......@@ -2,7 +2,7 @@
long IStreamDataFromBufferObject( void *mem, void *ud, long pos, long num )
{
BUFFER *Data = ud;
BLOCK *Data = ud;
uchar *data;
long upto;
......@@ -19,14 +19,14 @@ long IStreamDataFromBufferObject( void *mem, void *ud, long pos, long num )
long IStreamDataFromBufferIndex( void *mem, void *ud, long pos, long num )
{
ISTREAM_BUFFER_ID *Id = ud;
BUFFER *Data = AccessBuffer( Id->Buffers, Id->id );
ISTREAM_BLOCK_ID *Id = ud;
BLOCK *Data = AccessBlock( Id->Blocks, Id->id );
return IStreamDataFromBufferObject( mem, Data, pos, num );
}
long OStreamDataIntoBufferObject( void *mem, void *ud, long pos, long num )
{
BUFFER *Data = ud;
BLOCK *Data = ud;
uchar *data;
long end = pos + num;
......@@ -36,7 +36,7 @@ long OStreamDataIntoBufferObject( void *mem, void *ud, long pos, long num )
if ( (size_t)end >= Data->size )
{
long get = intervalof( end, BUFSIZ );
data = ExpandBuffer( Data, 1, get );
data = ExpandBlock( Data, 1, get );
if ( !data )
return -1;
......@@ -52,16 +52,16 @@ long OStreamDataIntoBufferObject( void *mem, void *ud, long pos, long num )
long OStreamDataIntoBufferIndex( void *mem, void *ud, long pos, long num )
{
ISTREAM_BUFFER_ID *Id = ud;
BUFFER *Data = AccessBuffer( Id->Buffers, Id->id );
ISTREAM_BLOCK_ID *Id = ud;
BLOCK *Data = AccessBlock( Id->Blocks, Id->id );
return IStreamDataFromBufferObject( mem, Data, pos, num );
}
int OStreamPrepFromBufferObject( void *ud, size_t size )
{
BUFFER *Data = ud;
BLOCK *Data = ud;
if ( !ExpandBuffer( Data, 1, size ) )
if ( !ExpandBlock( Data, 1, size ) )
return ENOMEM;
return 0;
......@@ -69,8 +69,8 @@ int OStreamPrepFromBufferObject( void *ud, size_t size )
int OStreamPrepFromBufferIndex( void *ud, size_t size )
{
ISTREAM_BUFFER_ID *Id = ud;
BUFFER *Data = AccessBuffer( Id->Buffers, Id->id );
ISTREAM_BLOCK_ID *Id = ud;
BLOCK *Data = AccessBlock( Id->Blocks, Id->id );
return OStreamPrepFromBufferObject( Data, size );
}
......
......@@ -4,11 +4,11 @@
#include "common.h"
#include "memory.h"
typedef struct _ISTREAM_BUFFER_ID
typedef struct _ISTREAM_BLOCK_ID
{
int id;
BUFFERS *Buffers;
} ISTREAM_BUFFER_ID;
BLOCKS *Blocks;
} ISTREAM_BLOCK_ID;
/* In ISTREAM objects 'mem' refers to where to copy to, In OSTREAM objects
* 'mem' refers to where to copy from */
......
......@@ -45,17 +45,17 @@ size_t textlen( tch const * text )
return leng;
}
BUFFER * text2buff( BUFFER *Buffer, tch const * const text )
BLOCK * text2buff( BLOCK *Block, tch const * const text )
{
uint leng = textlen( text );
tch * into = ExpandBuffer( Buffer, sizeof(tch), leng + 1 );
tch * into = ExpandBlock( Block, sizeof(tch), leng + 1 );
if ( !into )
return NULL;
memset( into, 0, Buffer->size );
memset( into, 0, Block->size );
memcpy( into, text, leng + 1 );
return Buffer;
return Block;
}
int _wide2ansi
......
......@@ -70,6 +70,6 @@ int text2wide( wch *into, size_t size, tch const *text );
int ansi2text( tch *into, size_t size, ach const *text );
int wide2text( tch *into, size_t size, wch const *text );
int text2text( tch *into, size_t size, tch const *text );
BUFFER *text2buff( BUFFER *Buffer, tch const *text );
BLOCK *text2buff( BLOCK *Block, tch const *text );
#endif
......@@ -108,24 +108,24 @@ void ZlibFullSymbol( ZLIB_FULL_SYMBOL *full, ZLIB_SYMBOL *part )
full->cpy = cpy;
}
int SetupZlibObject( ZLIB *zlib, BUFFERS *Buffers )
int SetupZlibObject( ZLIB *zlib, BLOCKS *Blocks )
{
int id;
ZLIB_SYMBOL *symbols = zlib->symbols;
ZLIB_SYMBOLS *Symbols;
if ( !Buffers )
if ( !Blocks )
{
ECHO
(
stderr,
ECHO( stderr, EINVAL );
fputs( "'Buffers' must not be NULL!\n", stderr )
fputs( "'Blocks' must not be NULL!\n", stderr )
);
return EINVAL;
}
zlib->Buffers = Buffers;
zlib->Blocks = Blocks;
for ( id = 0; id < ZLIB_SYMBOLS_ID_COUNT; ++id )
{
......@@ -134,7 +134,7 @@ int SetupZlibObject( ZLIB *zlib, BUFFERS *Buffers )
memset( Symbols, 0, sizeof(ZLIB_SYMBOLS) );
Symbols->id = id;
Symbols->Buffers = Buffers;
Symbols->Blocks = Blocks;
}
Symbols = &(zlib->Symbols[ZLIB_SYMBOLS_ID_TYPE]);
......@@ -197,10 +197,10 @@ void InitZlibSymbolUIDs( ZLIB_SYMBOLS *Symbols )
ZLIB_SYMBOL * SeekZlibSymbol( ISTREAM *IStream, ZLIB_SYMBOLS *Symbols )
{
BUFFERS *Buffers = Symbols->Buffers;
BLOCKS *Blocks = Symbols->Blocks;
ZLIB_SYMBOL *symbols = Symbols->symbols;
FILE *out = Buffers->Alloc->out;
uint len, uid = 0, lod = quiet_zlib ? 0 : Buffers->Alloc->lod;
FILE *out = Blocks->Alloc->out;
uint len, uid = 0, lod = quiet_zlib ? 0 : Blocks->Alloc->lod;
for ( len = 1; len <= Symbols->longest; ++len )
{
......@@ -483,13 +483,13 @@ void EchoZlibSymbolsListDetails( FILE *dst, ZLIB *zlib, int id )
int ExpandZlibIStreamType0( ZLIB *zlib )
{
BUFFERS *Buffers = zlib->Buffers;
BUFFER *Into = AccessBuffer( Buffers, zlib->IntoID );
BLOCKS *Blocks = zlib->Blocks;
BLOCK *Into = AccessBlock( Blocks, zlib->IntoID );
ISTREAM *IStream = zlib->IStream;
uint_least8_t *into = Into->addr;
uint i, leng, nlen, left = IStream->used % 8,
lod = quiet_zlib ? 0 : Buffers->Alloc->lod;
FILE *out = zlib->Buffers->Alloc->out;
lod = quiet_zlib ? 0 : Blocks->Alloc->lod;