~ubuntu-branches/ubuntu/maverick/squid3/maverick-updates

« back to all changes in this revision

Viewing changes to src/esi/Segment.cc

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2010-07-07 15:21:06 UTC
  • mfrom: (20.3.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100707152106-ozv23qjzn7q1jpaj
Tags: 3.1.5-2
* debian/control
  - Added build dependency on libltdl-dev fixing FTBFS on most archs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * $Id$
 
4
 *
 
5
 * DEBUG: section 86    ESI processing
 
6
 * AUTHOR: Robert Collins
 
7
 *
 
8
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 
9
 * ----------------------------------------------------------
 
10
 *
 
11
 *  Squid is the result of efforts by numerous individuals from
 
12
 *  the Internet community; see the CONTRIBUTORS file for full
 
13
 *  details.   Many organizations have provided support for Squid's
 
14
 *  development; see the SPONSORS file for full details.  Squid is
 
15
 *  Copyrighted (C) 2001 by the Regents of the University of
 
16
 *  California; see the COPYRIGHT file for full details.  Squid
 
17
 *  incorporates software developed and/or copyrighted by other
 
18
 *  sources; see the CREDITS file for full details.
 
19
 *
 
20
 *  This program is free software; you can redistribute it and/or modify
 
21
 *  it under the terms of the GNU General Public License as published by
 
22
 *  the Free Software Foundation; either version 2 of the License, or
 
23
 *  (at your option) any later version.
 
24
 *
 
25
 *  This program is distributed in the hope that it will be useful,
 
26
 ;  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
28
 *  GNU General Public License for more details.
 
29
 *
 
30
 *  You should have received a copy of the GNU General Public License
 
31
 *  along with this program; if not, write to the Free Software
 
32
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 
33
 *
 
34
 */
 
35
 
 
36
#include "squid.h"
 
37
#include "esi/Segment.h"
 
38
#include "SquidString.h"
 
39
 
 
40
CBDATA_TYPE(ESISegment);
 
41
 
 
42
/* ESISegment */
 
43
void
 
44
ESISegmentFreeList (ESISegment::Pointer &head)
 
45
{
 
46
    while (head.getRaw()) {
 
47
        ESISegment::Pointer temp = head;
 
48
        head = head->next;
 
49
        temp->next = NULL;
 
50
    }
 
51
}
 
52
 
 
53
size_t
 
54
ESISegment::space() const
 
55
{
 
56
    assert (len <= sizeof(buf));
 
57
    return sizeof (buf) - len;
 
58
}
 
59
 
 
60
void
 
61
ESISegment::adsorbList (ESISegment::Pointer from)
 
62
{
 
63
    assert (next.getRaw() == NULL);
 
64
    assert (from.getRaw() != NULL);
 
65
    /* prevent worst case */
 
66
    assert (!(len == 0 && from->len == space() ));
 
67
    Pointer copyFrom = from;
 
68
 
 
69
    while (copyFrom.getRaw() && space() >= copyFrom->len) {
 
70
        assert (append (copyFrom) == copyFrom->len);
 
71
        copyFrom = copyFrom->next;
 
72
    }
 
73
 
 
74
    next = copyFrom;
 
75
}
 
76
 
 
77
void
 
78
ESISegment::ListTransfer (ESISegment::Pointer &from, ESISegment::Pointer &to)
 
79
{
 
80
    if (!to.getRaw()) {
 
81
        to = from;
 
82
        from = NULL;
 
83
        return;
 
84
    }
 
85
 
 
86
    ESISegment::Pointer temp = to->tail();
 
87
    temp->adsorbList (from);
 
88
    from = NULL;
 
89
}
 
90
 
 
91
size_t
 
92
ESISegment::listLength() const
 
93
{
 
94
    size_t result = 0;
 
95
    ESISegment const* temp = this;
 
96
 
 
97
    while (temp) {
 
98
        result += temp->len;
 
99
        temp = temp->next.getRaw();
 
100
    }
 
101
 
 
102
    return result;
 
103
}
 
104
 
 
105
char *
 
106
ESISegment::listToChar() const
 
107
{
 
108
    size_t length = listLength();
 
109
    char *rv = (char *)xmalloc (length + 1);
 
110
    assert (rv);
 
111
    rv [length] = '\0';
 
112
 
 
113
    ESISegment::Pointer temp = this;
 
114
    size_t pos = 0;
 
115
 
 
116
    while (temp.getRaw()) {
 
117
        xmemcpy (&rv[pos], temp->buf, temp->len);
 
118
        pos += temp->len;
 
119
        temp = temp->next;
 
120
    }
 
121
 
 
122
    return rv;
 
123
}
 
124
 
 
125
void
 
126
ESISegment::listAppend (char const *s, size_t length)
 
127
{
 
128
    assert (next.getRaw() == NULL);
 
129
    ESISegment::Pointer output = this;
 
130
    /* copy the string to output */
 
131
    size_t pos=0;
 
132
 
 
133
    while (pos < length) {
 
134
        if (output->space() == 0) {
 
135
            assert (output->next.getRaw() == NULL);
 
136
            output->next = new ESISegment;
 
137
            output = output->next;
 
138
        }
 
139
 
 
140
        pos += output->append(s + pos, length - pos);
 
141
    }
 
142
}
 
143
 
 
144
void
 
145
ESISegment::ListAppend (ESISegment::Pointer &head, char const *s, size_t len)
 
146
{
 
147
    if (!head.getRaw())
 
148
        head = new ESISegment;
 
149
 
 
150
    head->tail()->listAppend (s, len);
 
151
}
 
152
 
 
153
void *
 
154
ESISegment::operator new(size_t byteCount)
 
155
{
 
156
    assert (byteCount == sizeof (ESISegment));
 
157
    void *rv;
 
158
    CBDATA_INIT_TYPE(ESISegment);
 
159
    rv = (void *)cbdataAlloc (ESISegment);
 
160
    return rv;
 
161
}
 
162
 
 
163
void
 
164
ESISegment::operator delete (void *address)
 
165
{
 
166
    cbdataFree (address);
 
167
}
 
168
 
 
169
/* XXX: if needed, make this iterative */
 
170
ESISegment::Pointer
 
171
ESISegment::cloneList () const
 
172
{
 
173
    ESISegment::Pointer result = new ESISegment (*this);
 
174
    result->next = next.getRaw() ? next->cloneList() : NULL;
 
175
    return result;
 
176
}
 
177
 
 
178
size_t
 
179
ESISegment::append(char const *appendBuffer, size_t appendLength)
 
180
{
 
181
    size_t toCopy = min(appendLength, space());
 
182
    xmemcpy (&buf[len], appendBuffer, toCopy);
 
183
    len += toCopy;
 
184
    return toCopy;
 
185
}
 
186
 
 
187
size_t
 
188
ESISegment::append(ESISegment::Pointer from)
 
189
{
 
190
    return append (from->buf, from->len);
 
191
}
 
192
 
 
193
ESISegment const *
 
194
ESISegment::tail() const
 
195
{
 
196
    ESISegment const *result = this;
 
197
 
 
198
    while (result->next.getRaw())
 
199
        result = result->next.getRaw();
 
200
 
 
201
    return result;
 
202
}
 
203
 
 
204
ESISegment *
 
205
ESISegment::tail()
 
206
{
 
207
    ESISegment::Pointer result = this;
 
208
 
 
209
    while (result->next.getRaw())
 
210
        result = result->next;
 
211
 
 
212
    return result.getRaw();
 
213
}
 
214
 
 
215
ESISegment::ESISegment() : len(0), next(NULL)
 
216
{}
 
217
 
 
218
ESISegment::ESISegment(ESISegment const &old) : len (0), next(NULL)
 
219
{
 
220
    append (old.buf, old.len);
 
221
}
 
222
 
 
223
void
 
224
ESISegment::dumpToLog() const
 
225
{
 
226
    ESISegment::Pointer temp = this;
 
227
 
 
228
    while (temp.getRaw()) {
 
229
        temp->dumpOne();
 
230
        temp = temp->next;
 
231
    }
 
232
}
 
233
 
 
234
void
 
235
ESISegment::dumpOne() const
 
236
{
 
237
    String temp;
 
238
    temp.limitInit(buf, len);
 
239
    debugs(86, 9, "ESISegment::dumpOne: \"" << temp << "\"");
 
240
}