1 /*  $Id$
2 
3     Part of SWI-Prolog
4 
5     Author:        Jan Wielemaker
6     E-mail:        jan@swi.psy.uva.nl
7     WWW:           http://www.swi-prolog.org
8     Copyright (C): 1985-2002, University of Amsterdam
9 
10     This library is free software; you can redistribute it and/or
11     modify it under the terms of the GNU Lesser General Public
12     License as published by the Free Software Foundation; either
13     version 2.1 of the License, or (at your option) any later version.
14 
15     This library is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18     Lesser General Public License for more details.
19 
20     You should have received a copy of the GNU Lesser General Public
21     License along with this library; if not, write to the Free Software
22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24 
25 #include "pl-incl.h"
26 
27 // vsc: changed from SWI
28 #define discardable_buffer 	(LD->discardable_buffer)
29 #define buffer_ring		(LD->buffer_ring)
30 #define current_buffer_id	(LD->current_buffer_id)
31 
32 Buffer
findBuffer(int flags)33 findBuffer(int flags)
34 { GET_LD
35   Buffer b;
36 
37   if ( flags & BUF_RING )
38   { if ( ++current_buffer_id == BUFFER_RING_SIZE )
39       current_buffer_id = 0;
40     b = &buffer_ring[current_buffer_id];
41   } else
42     b = &discardable_buffer;
43 
44   if ( !b->base )
45     initBuffer(b);
46 
47   emptyBuffer(b);
48   return b;
49 }
50 
51 int
unfindBuffer(int flags)52 unfindBuffer(int flags)
53 { GET_LD
54   if ( flags & BUF_RING )
55   { if ( --current_buffer_id <= 0 )
56       current_buffer_id = BUFFER_RING_SIZE-1;
57   }
58 
59   fail;
60 }
61 
62 
63 
64 void
growBuffer(Buffer b,size_t minfree)65 growBuffer(Buffer b, size_t minfree)
66 { size_t osz = b->max - b->base, sz = osz;
67   size_t top = b->top - b->base;
68 
69   if ( b->max - b->top >= (int)minfree )
70     return;
71 
72   if ( sz < 512 )
73     sz = 512;				/* minimum reasonable size */
74   while( top + minfree > sz )
75     sz *= 2;
76 
77   if ( b->base != b->static_buffer )
78   {
79 #ifdef BUFFER_USES_MALLOC
80     b->base = realloc(b->base, sz);
81     if ( !b->base )
82       outOfCore();
83 #else
84     char *old = b->base;
85     b->base = allocHeap(sz);
86     memcpy(b->base, old, osz);
87 #endif
88   } else			/* from static buffer */
89   { char *new;
90 #ifdef BUFFER_USES_MALLOC
91     if ( !(new = malloc(sz)) )
92       outOfCore();
93 #else
94     new = allocHeap(sz);
95 #endif
96     memcpy(new, b->static_buffer, osz);
97     b->base = new;
98   }
99 
100   b->top = b->base + top;
101   b->max = b->base + sz;
102 }
103 
104 char *
buffer_string(const char * s,int flags)105 buffer_string(const char *s, int flags)
106 { Buffer b = findBuffer(flags);
107   size_t l = strlen(s) + 1;
108 
109   addMultipleBuffer(b, s, l, char);
110 
111   return baseBuffer(b, char);
112 }
113 
114 
115