This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/os/pl-buffer.h

105 lines
3.3 KiB
C
Raw Normal View History

2008-12-22 12:02:22 +00:00
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: jan@swi.psy.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2002, University of Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
2012-01-26 04:15:01 +00:00
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2008-12-22 12:02:22 +00:00
*/
#ifndef BUFFER_H_INCLUDED
#define BUFFER_H_INCLUDED
#define STATIC_BUFFER_SIZE (512)
typedef struct
{ char * base; /* allocated base */
char * top; /* pointer to top */
char * max; /* current location */
char static_buffer[STATIC_BUFFER_SIZE];
} tmp_buffer, *TmpBuffer;
typedef struct
{ char * base; /* allocated base */
char * top; /* pointer to top */
char * max; /* current location */
char static_buffer[sizeof(char *)];
2012-01-26 04:15:01 +00:00
} MAY_ALIAS buffer, *Buffer;
2008-12-22 12:02:22 +00:00
2012-01-26 04:15:01 +00:00
int growBuffer(Buffer b, size_t minfree);
2008-12-22 12:02:22 +00:00
#define addBuffer(b, obj, type) \
do \
{ if ( (b)->top + sizeof(type) > (b)->max ) \
2012-01-26 04:15:01 +00:00
{ if ( !growBuffer((Buffer)b, sizeof(type)) ) \
outOfCore(); \
} \
2008-12-22 12:02:22 +00:00
*((type *)(b)->top) = obj; \
(b)->top += sizeof(type); \
} while(0)
2011-02-10 00:01:19 +00:00
2008-12-22 12:02:22 +00:00
#define addMultipleBuffer(b, ptr, times, type) \
do \
{ size_t _tms = (times); \
size_t _len = _tms * sizeof(type); \
type *_d, *_s = (type *)ptr; \
if ( (b)->top + _len > (b)->max ) \
2012-01-26 04:15:01 +00:00
{ if ( !growBuffer((Buffer)b, _len) ) \
outOfCore(); \
} \
2008-12-22 12:02:22 +00:00
_d = (type *)(b)->top; \
while ( _tms-- ) \
*_d++ = *_s++; \
(b)->top = (char *)_d; \
} while(0)
2011-02-10 00:01:19 +00:00
2008-12-22 12:02:22 +00:00
#define baseBuffer(b, type) ((type *) (b)->base)
#define topBuffer(b, type) ((type *) (b)->top)
#define inBuffer(b, addr) ((char *) (addr) >= (b)->base && \
(char *) (addr) < (b)->top)
#define fetchBuffer(b, i, type) (baseBuffer(b, type)[i])
#define seekBuffer(b, cnt, type) ((b)->top = sizeof(type) * (cnt) + (b)->base)
#define sizeOfBuffer(b) ((b)->top - (b)->base)
#define freeSpaceBuffer(b) ((b)->max - (b)->top)
#define entriesBuffer(b, type) (sizeOfBuffer(b) / sizeof(type))
#define initBuffer(b) ((b)->base = (b)->top = (b)->static_buffer, \
(b)->max = (b)->base + \
sizeof((b)->static_buffer))
#define emptyBuffer(b) ((b)->top = (b)->base)
#define isEmptyBuffer(b) ((b)->top == (b)->base)
#define discardBuffer(b) \
do \
{ if ( (b)->base && (b)->base != (b)->static_buffer ) \
{ free((b)->base); \
(b)->base = (b)->static_buffer; \
} \
} while(0)
2011-02-10 00:01:19 +00:00
/*******************************
* FUNCTIONS *
*******************************/
COMMON(Buffer) findBuffer(int flags);
COMMON(int) unfindBuffer(int flags);
COMMON(char *) buffer_string(const char *s, int flags);
2008-12-22 12:02:22 +00:00
#endif /*BUFFER_H_INCLUDED*/