Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/vcbuffer.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vchdr.h"
21
22
/* Managing buffered data for a handle.
23
**
24
** Written by Kiem-Phong Vo ([email protected])
25
*/
26
27
#if __STD_C
28
Vcchar_t* _vcbuffer(Vcodex_t* vc, Vcchar_t* trunc, ssize_t size, ssize_t head)
29
#else
30
Vcchar_t* _vcbuffer(vc, trunc, size, head)
31
Vcodex_t* vc;
32
Vcchar_t* trunc; /* if != NULL, a buffer to be truncated */
33
ssize_t size; /* the size needed for buffered data */
34
ssize_t head; /* head room in front of buffer */
35
#endif
36
{
37
Vcbuffer_t *b, *n;
38
/**/DEBUG_DECLARE(static ssize_t, Busy=0);
39
#ifdef VMFL
40
/**/DEBUG_DECLARE(Vmstat_t, statb); DEBUG_ASSERT(vmstat(Vmregion, &statb) >= 0);
41
#endif
42
43
if(!vc)
44
return NIL(Vcchar_t*);
45
46
if(trunc) /* truncating a buffer */
47
{ /* find the buffer */
48
for(; vc; vc = vc->coder)
49
{ for(n = NIL(Vcbuffer_t*), b = vc->list; b; n = b, b = b->next)
50
if(trunc >= b->buf && trunc < b->buf+b->size)
51
break;
52
if(!b) /* not in this handle */
53
continue;
54
55
if(n) /* isolate b from buffer pool */
56
n->next = b->next;
57
else vc->list = b->next;
58
59
if(size < 0 ) /* just free the buffer */
60
{ /**/DEBUG_SET(Busy, Busy - b->size);
61
/**/DEBUG_PRINT(2,"free: file=%s ", b->file);
62
/**/DEBUG_PRINT(2,"line=%d ",b->line);
63
/**/DEBUG_PRINT(2,"size=%d\n",b->size);
64
65
vc->busy -= b->size;
66
vc->nbuf -= 1;
67
free(b);
68
return NIL(Vcchar_t*);
69
}
70
71
if(trunc+size > b->buf+b->size) /* no extension */
72
{ b->next = vc->list;
73
vc->list = b;
74
return NIL(Vcchar_t*);
75
}
76
77
size += (head = trunc - (Vcchar_t*)b->buf);
78
if(size < 3*b->size/4 )
79
{ if(!(n = (Vcbuffer_t*)realloc(b, sizeof(Vcbuffer_t)+size)) )
80
RETURN(NIL(Vcchar_t*));
81
/**/DEBUG_SET(Busy, Busy - b->size + size);
82
/**/DEBUG_PRINT(2,"realloc: file=%s ", b->file);
83
/**/DEBUG_PRINT(2,"line=%d ",b->line);
84
/**/DEBUG_PRINT(2,"oldsize=%d ",b->size);
85
/**/DEBUG_PRINT(2,"newsize=%d\n",size);
86
87
vc->busy -= n->size - size; /* n->size is old b->size */
88
n->size = size;
89
if(n != b)
90
b = n;
91
}
92
93
b->next = vc->list;
94
vc->list = b;
95
return (Vcchar_t*)(&b->buf[head]);
96
}
97
98
return NIL(Vcchar_t*);
99
}
100
else if(size < 0) /* free all buffers */
101
{ for(; vc; vc = vc->coder)
102
{ if(vc->meth->eventf) /* tell vc to free its internal buffers */
103
(*vc->meth->eventf)(vc, VC_FREEBUFFER, 0);
104
105
for(b = vc->list; b; b = n)
106
{ n = b->next;
107
108
/**/DEBUG_SET(Busy, Busy - b->size);
109
/**/DEBUG_PRINT(2,"free: file=%s ", b->file);
110
/**/DEBUG_PRINT(2,"line=%d ",b->line);
111
/**/DEBUG_PRINT(2,"size=%d\n",b->size);
112
free(b);
113
}
114
115
vc->list = NIL(Vcbuffer_t*);
116
vc->busy = 0;
117
vc->nbuf = 0;
118
}
119
120
return NIL(Vcchar_t*);
121
}
122
else
123
{ head = (head <= 0 ? 0 : head) + vc->head; /* required head room */
124
if(!(b = (Vcbuffer_t*)malloc(sizeof(Vcbuffer_t)+head+size)) )
125
RETURN(NIL(Vcchar_t*));
126
b->size = head+size;
127
b->next = vc->list;
128
b->file = vc->file; vc->file = NIL(char*);
129
b->line = vc->line; vc->line = 0;
130
/**/DEBUG_SET(Busy, Busy + b->size);
131
/**/DEBUG_PRINT(2,"alloc: file=%s ", b->file);
132
/**/DEBUG_PRINT(2,"line=%d ",b->line);
133
/**/DEBUG_PRINT(2,"size=%d\n",b->size);
134
135
vc->list = b;
136
vc->busy += b->size;
137
vc->nbuf += 1;
138
return (Vcchar_t*)(&b->buf[head]);
139
}
140
}
141
142