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/library/dialect/swi/os/pl-buffer.c

108 lines
2.4 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
*/
#include "pl-incl.h"
2012-01-26 04:15:01 +00:00
int
2008-12-22 12:02:22 +00:00
growBuffer(Buffer b, size_t minfree)
{ size_t osz = b->max - b->base, sz = osz;
size_t top = b->top - b->base;
2013-01-15 17:13:41 +00:00
char *new;
2008-12-22 12:02:22 +00:00
if ( b->max - b->top >= (int)minfree )
2012-01-26 04:15:01 +00:00
return TRUE;
2008-12-22 12:02:22 +00:00
if ( sz < 512 )
sz = 512; /* minimum reasonable size */
while( top + minfree > sz )
sz *= 2;
2013-01-15 17:13:41 +00:00
if ( b->base == b->static_buffer )
{ if ( !(new = malloc(sz)) )
2012-01-26 04:15:01 +00:00
return FALSE;
2011-02-10 00:01:19 +00:00
2008-12-22 12:02:22 +00:00
memcpy(new, b->static_buffer, osz);
2013-01-15 17:13:41 +00:00
} else
{ if ( !(new = realloc(b->base, sz)) )
return FALSE;
2008-12-22 12:02:22 +00:00
}
2013-01-15 17:13:41 +00:00
b->base = new;
2008-12-22 12:02:22 +00:00
b->top = b->base + top;
b->max = b->base + sz;
2012-01-26 04:15:01 +00:00
return TRUE;
2008-12-22 12:02:22 +00:00
}
2011-02-10 00:01:19 +00:00
/*******************************
* BUFFER RING *
*******************************/
2013-01-15 17:13:41 +00:00
#define discardable_buffer (LD->fli._discardable_buffer)
2011-02-10 00:01:19 +00:00
#define buffer_ring (LD->fli._buffer_ring)
#define current_buffer_id (LD->fli._current_buffer_id)
Buffer
findBuffer(int flags)
{ GET_LD
Buffer b;
if ( flags & BUF_RING )
{ if ( ++current_buffer_id == BUFFER_RING_SIZE )
current_buffer_id = 0;
b = &buffer_ring[current_buffer_id];
} else
b = &discardable_buffer;
if ( !b->base )
initBuffer(b);
emptyBuffer(b);
return b;
}
2008-12-22 12:02:22 +00:00
char *
buffer_string(const char *s, int flags)
{ Buffer b = findBuffer(flags);
size_t l = strlen(s) + 1;
addMultipleBuffer(b, s, l, char);
return baseBuffer(b, char);
}
2011-02-10 00:01:19 +00:00
int
unfindBuffer(int flags)
{ GET_LD
if ( flags & BUF_RING )
{ if ( --current_buffer_id <= 0 )
current_buffer_id = BUFFER_RING_SIZE-1;
}
fail;
}