1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2009 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfhdr.h"
/* Management of pools of streams.
** If pf is not nil, f is pooled with pf and f becomes current;
** otherwise, f is isolated from its pool. flag can be one of
** 0 or SF_SHARE.
**
** Written by Kiem-Phong Vo.
*/
/* Note that we do not free the space for a pool once it is allocated.
** This is to prevent memory faults in calls such as sfsync(NULL) that walk the pool
** link list and during such walks may free up streams&pools. Free pools will be
** reused in newpool().
*/
#if __STD_C
static int delpool(reg Sfpool_t* p)
#else
static int delpool(p)
reg Sfpool_t* p;
#endif
{
POOLMTXENTER(p);
if(p->s_sf && p->sf != p->array)
free((Void_t*)p->sf);
p->mode = SF_AVAIL;
POOLMTXRETURN(p,0);
}
#if __STD_C
static Sfpool_t* newpool(reg int mode)
#else
static Sfpool_t* newpool(mode)
reg int mode;
#endif
{
reg Sfpool_t *p, *last = &_Sfpool;
/* look to see if there is a free pool */
for(last = &_Sfpool, p = last->next; p; last = p, p = p->next)
{ if(p->mode == SF_AVAIL )
{ p->mode = 0;
break;
}
}
if(!p)
{ POOLMTXLOCK(last);
if(!(p = (Sfpool_t*) malloc(sizeof(Sfpool_t))) )
{ POOLMTXUNLOCK(last);
return NIL(Sfpool_t*);
}
(void)vtmtxopen(&p->mutex, VT_INIT); /* initialize mutex */
p->mode = 0;
p->n_sf = 0;
p->next = NIL(Sfpool_t*);
last->next = p;
POOLMTXUNLOCK(last);
}
POOLMTXENTER(p);
p->mode = mode&SF_SHARE;
p->s_sf = sizeof(p->array)/sizeof(p->array[0]);
p->sf = p->array;
POOLMTXRETURN(p,p);
}
/* move a stream to head */
#if __STD_C
static int _sfphead(Sfpool_t* p, Sfio_t* f, int n)
#else
static int _sfphead(p, f, n)
Sfpool_t* p; /* the pool */
Sfio_t* f; /* the stream */
int n; /* current position in pool */
#endif
{
reg Sfio_t* head;
reg ssize_t k, w, v;
reg int rv;
POOLMTXENTER(p);
if(n == 0)
POOLMTXRETURN(p,0);
head = p->sf[0];
if(SFFROZEN(head) )
POOLMTXRETURN(p,-1);
SFLOCK(head,0);
rv = -1;
if(!(p->mode&SF_SHARE) || (head->mode&SF_READ) || (f->mode&SF_READ) )
{ if(SFSYNC(head) < 0)
goto done;
}
else /* shared pool of write-streams, data can be moved among streams */
{ if(SFMODE(head,1) != SF_WRITE && _sfmode(head,SF_WRITE,1) < 0)
goto done;
/**/ASSERT(f->next == f->data);
v = head->next - head->data; /* pending data */
if((k = v - (f->endb-f->data)) <= 0)
k = 0;
else /* try to write out amount exceeding f's capacity */
{ if((w = SFWR(head,head->data,k,head->disc)) == k)
v -= k;
else /* write failed, recover buffer then quit */
{ if(w > 0)
{ v -= w;
memcpy(head->data,(head->data+w),v);
}
head->next = head->data+v;
goto done;
}
}
/* move data from head to f */
if((head->data+k) != f->data )
memcpy(f->data,(head->data+k),v);
f->next = f->data+v;
}
f->mode &= ~SF_POOL;
head->mode |= SF_POOL;
head->next = head->endr = head->endw = head->data; /* clear write buffer */
p->sf[n] = head;
p->sf[0] = f;
rv = 0;
done:
head->mode &= ~SF_LOCK; /* partially unlock because it's no longer head */
POOLMTXRETURN(p,rv);
}
/* delete a stream from its pool */
#if __STD_C
static int _sfpdelete(Sfpool_t* p, Sfio_t* f, int n)
#else
static int _sfpdelete(p, f, n)
Sfpool_t* p; /* the pool */
Sfio_t* f; /* the stream */
int n; /* position in pool */
#endif
{
POOLMTXENTER(p);
p->n_sf -= 1;
for(; n < p->n_sf; ++n)
p->sf[n] = p->sf[n+1];
f->pool = NIL(Sfpool_t*);
f->mode &= ~SF_POOL;
if(p->n_sf == 0 || p == &_Sfpool)
{ if(p != &_Sfpool)
delpool(p);
goto done;
}
/* !_Sfpool, make sure head stream is an open stream */
for(n = 0; n < p->n_sf; ++n)
if(!SFFROZEN(p->sf[n]))
break;
if(n < p->n_sf && n > 0)
{ f = p->sf[n];
p->sf[n] = p->sf[0];
p->sf[0] = f;
}
/* head stream has SF_POOL off */
f = p->sf[0];
f->mode &= ~SF_POOL;
if(!SFFROZEN(f))
_SFOPEN(f);
/* if only one stream left, delete pool */
if(p->n_sf == 1 )
{ _sfpdelete(p,f,0);
_sfsetpool(f);
}
done:
POOLMTXRETURN(p,0);
}
#if __STD_C
static int _sfpmove(reg Sfio_t* f, reg int type)
#else
static int _sfpmove(f,type)
reg Sfio_t* f;
reg int type; /* <0 : deleting, 0: move-to-front, >0: inserting */
#endif
{
reg Sfpool_t* p;
reg int n;
if(type > 0)
return _sfsetpool(f);
else
{ if(!(p = f->pool) )
return -1;
for(n = p->n_sf-1; n >= 0; --n)
if(p->sf[n] == f)
break;
if(n < 0)
return -1;
return type == 0 ? _sfphead(p,f,n) : _sfpdelete(p,f,n);
}
}
#if __STD_C
Sfio_t* sfpool(reg Sfio_t* f, reg Sfio_t* pf, reg int mode)
#else
Sfio_t* sfpool(f,pf,mode)
reg Sfio_t* f;
reg Sfio_t* pf;
reg int mode;
#endif
{
int k;
Sfpool_t* p;
Sfio_t* rv;
_Sfpmove = _sfpmove;
if(!f) /* return head of pool of pf regardless of lock states */
{ if(!pf)
return NIL(Sfio_t*);
else if(!pf->pool || pf->pool == &_Sfpool)
return pf;
else return pf->pool->sf[0];
}
if(f) /* check for permissions */
{ SFMTXLOCK(f);
if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
{ SFMTXUNLOCK(f);
return NIL(Sfio_t*);
}
if(f->disc == _Sfudisc)
(void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
}
if(pf)
{ SFMTXLOCK(pf);
if((pf->mode&SF_RDWR) != pf->mode && _sfmode(pf,0,0) < 0)
{ if(f)
SFMTXUNLOCK(f);
SFMTXUNLOCK(pf);
return NIL(Sfio_t*);
}
if(pf->disc == _Sfudisc)
(void)sfclose((*_Sfstack)(pf,NIL(Sfio_t*)));
}
/* f already in the same pool with pf */
if(f == pf || (pf && f->pool == pf->pool && f->pool != &_Sfpool) )
{ if(f)
SFMTXUNLOCK(f);
if(pf)
SFMTXUNLOCK(pf);
return pf;
}
/* lock streams before internal manipulations */
rv = NIL(Sfio_t*);
SFLOCK(f,0);
if(pf)
SFLOCK(pf,0);
if(!pf) /* deleting f from its current pool */
{ if((p = f->pool) != NIL(Sfpool_t*) && p != &_Sfpool)
for(k = 0; k < p->n_sf && pf == NIL(Sfio_t*); ++k)
if(p->sf[k] != f) /* a stream != f represents the pool */
pf = p->sf[k];
if(!pf) /* already isolated */
{ rv = f; /* just return self */
goto done;
}
if(_sfpmove(f,-1) < 0 || _sfsetpool(f) < 0)
goto done; /* can't delete */
if(!pf->pool || pf->pool == &_Sfpool || pf->pool->n_sf <= 0 )
rv = pf;
else rv = pf->pool->sf[0]; /* return head of old pool */
goto done;
}
if(pf->pool && pf->pool != &_Sfpool) /* always use current mode */
mode = pf->pool->mode;
if(mode&SF_SHARE) /* can only have write streams */
{ if(SFMODE(f,1) != SF_WRITE && _sfmode(f,SF_WRITE,1) < 0)
goto done;
if(SFMODE(pf,1) != SF_WRITE && _sfmode(pf,SF_WRITE,1) < 0)
goto done;
if(f->next > f->data && SFSYNC(f) < 0) /* start f clean */
goto done;
}
if(_sfpmove(f,-1) < 0) /* isolate f from current pool */
goto done;
if(!(p = pf->pool) || p == &_Sfpool) /* making a new pool */
{ if(!(p = newpool(mode)) )
goto done;
if(_sfpmove(pf,-1) < 0) /* isolate pf from its current pool */
goto done;
pf->pool = p;
p->sf[0] = pf;
p->n_sf += 1;
}
f->pool = p; /* add f to pf's pool */
if(_sfsetpool(f) < 0)
goto done;
/**/ASSERT(p->sf[0] == pf && p->sf[p->n_sf-1] == f);
SFOPEN(pf,0);
SFOPEN(f,0);
if(_sfpmove(f,0) < 0) /* make f head of pool */
goto done;
rv = pf;
done:
if(f)
{ SFOPEN(f,0);
SFMTXUNLOCK(f);
}
if(pf)
{ SFOPEN(pf,0);
SFMTXUNLOCK(pf);
}
return rv;
}
|