home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
MODEL
/
BUFFER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-21
|
4KB
|
198 lines
/*
* データバッファ管理
*
* Copyright T.Kobayashi 1994.6.18
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib.h"
#include "alloc.h"
#include "buffer.h"
#define FLAG_USE 0x1234
#define FLAG_NOUSE 0x4321
static char *BufTop, *BufEnd, *BufNext ;
/* データバッファの初期化 */
void BufferInit( size )
int size ;
{
BufTop = BufNext = MemoryAlloc( size );
BufEnd = BufTop + size ;
}
/* データバッファの解放 */
void BufferExit()
{
MemoryFree( BufTop );
}
/* データバッファの確保 */
void *BufferAppend( size )
int size ;
{
DataBuffer *ret ;
assert( size > sizeof( DataBuffer ) );
assert( ( size & 3 ) == 0 );
if ( BufNext + size < BufEnd )
{
ret = (DataBuffer*)BufNext ;
ret->flag = FLAG_USE ;
ret->size = size ;
BufNext += size ;
return (void*)ret ;
}
else
{
MessageError( "データバッファがオーバフローしました(BufferAppend)" );
return NULL ;
}
}
/* バッファの削除 */
void BufferDelete( buf )
void *buf ;
{
assert( ((DataBuffer*)buf)->flag == FLAG_USE );
assert( BufTop <= (char*)buf && (char*)buf < BufNext );
((DataBuffer*)buf)->flag = FLAG_NOUSE ;
}
/* バッファ削除後の修正(ゴミ集め) */
void BufferDeleteCorrect()
{
int size ;
DataBuffer *src, *dst ;
src = (DataBuffer*)BufTop ;
dst = (DataBuffer*)BufTop ;
while( (char*)src < BufNext )
{
assert( dst <= src );
size = src->size ;
if ( src->flag == FLAG_USE )
{
if ( dst < src )
memmove( dst, src, size );
dst = (DataBuffer*)( (char*)dst + size );
}
else
{
assert( src->flag == FLAG_NOUSE );
}
src = (DataBuffer*)( (char*)src + size );
}
BufNext = (char*)dst ;
}
/* バッファの挿入 */
void *BufferInsert( dbuf, size )
void *dbuf ;
int size ;
{
DataBuffer *buf = dbuf ;
assert( buf->flag == FLAG_USE );
assert( BufTop <= (char*)buf && (char*)buf < BufNext );
if ( BufNext + size < BufEnd )
{
memmove( (char*)buf + size, (char*)buf, BufNext - (char*)buf );
buf->flag = FLAG_USE ;
buf->size = size ;
BufNext += size ;
return (void*)buf ;
}
else
{
MessageError( "データバッファがオーバフローしました(BufferInsert)" );
return NULL ;
}
}
/* バッファのコピー */
void BufferCopy( dst, src, size )
void *dst ;
void *src ;
int size ;
{
memcpy( (char*)dst + sizeof( short )*2, (char*)src + sizeof( short )*2,
size - sizeof( short )*2 );
}
/* バッファのサイズ変更 */
void *BufferResize( dbuf, size )
void *dbuf ;
int size ;
{
int dsize ;
DataBuffer *buf = dbuf ;
assert( buf->flag == FLAG_USE );
assert( BufTop <= (char*)buf && (char*)buf < BufNext );
dsize = size - buf->size ;
if ( dsize <= 0 )
return (void*)buf ;
if ( BufNext + dsize < BufEnd )
{
memmove( (char*)buf + dsize, (char*)buf, BufNext - (char*)buf );
buf->size = size ;
BufNext += dsize ;
return (void*)buf ;
}
else
{
MessageError( "データバッファがオーバフローしました(BufferInsert)" );
return NULL ;
}
}
/* 先頭のバッファ */
void *BufferTop()
{
if ( BufTop == BufNext )
return NULL ;
else
return (void*)BufTop ;
}
/* 次のバッファ */
void *BufferNext( dbuf )
void *dbuf ;
{
DataBuffer *ret ;
DataBuffer *buf = dbuf ;
assert( buf->flag == FLAG_USE );
assert( BufTop <= (char*)buf && (char*)buf < BufNext );
ret = (DataBuffer*)( (char*)buf + buf->size );
if ( (char*)ret < BufNext )
return (void*)ret ;
else
return NULL ;
}
/* バッファのチェック */
void BufferCheck()
{
DataBuffer *buf ;
buf = (DataBuffer*)BufTop ;
while( (char*)buf < BufNext )
{
assert( buf->flag == FLAG_USE );
buf = (DataBuffer*)( (char*)buf + buf->size );
}
}