]> git.ipfire.org Git - thirdparty/squid.git/blame - src/stmem.cc
add range header, Range objects represent [start,end)
[thirdparty/squid.git] / src / stmem.cc
CommitLineData
df92bd2a 1
30a4f2a8 2/*
c4b7a5a9 3 * $Id: stmem.cc,v 1.73 2002/10/14 08:16:59 robertc Exp $
30a4f2a8 4 *
6f6f0853 5 * DEBUG: section 19 Store Memory Primitives
30a4f2a8 6 * AUTHOR: Harvest Derived
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
30a4f2a8 10 *
2b6662ba 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.
30a4f2a8 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
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
019dd986 34 */
ed43818f 35
44a47c6e 36#include "squid.h"
090089c4 37
d89d1fb6 38void
3f6c0fb2 39stmemFree(mem_hdr * mem)
090089c4 40{
18fe65d0 41 mem_node *p;
42 while ((p = mem->head)) {
43 mem->head = p->next;
18fe65d0 44 store_mem_size -= SM_PAGE_SIZE;
7f6ffd15 45 if (p) {
46 memFree(p, MEM_MEM_NODE);
47 p = NULL;
48 }
090089c4 49 }
18fe65d0 50 mem->head = mem->tail = NULL;
51 mem->origin_offset = 0;
090089c4 52}
53
d89d1fb6 54int
3f6c0fb2 55stmemFreeDataUpto(mem_hdr * mem, int target_offset)
090089c4 56{
57 int current_offset = mem->origin_offset;
f1dc9b30 58 mem_node *lastp;
59 mem_node *p = mem->head;
090089c4 60 while (p && ((current_offset + p->len) <= target_offset)) {
61 if (p == mem->tail) {
62 /* keep the last one to avoid change to other part of code */
63 mem->head = mem->tail;
64 mem->origin_offset = current_offset;
65 return current_offset;
66 } else {
67 lastp = p;
68 p = p->next;
69 current_offset += lastp->len;
b93de483 70 store_mem_size -= SM_PAGE_SIZE;
7f6ffd15 71 if (lastp) {
72 memFree(lastp, MEM_MEM_NODE);
73 lastp = NULL;
74 }
090089c4 75 }
76 }
090089c4 77 mem->head = p;
78 mem->origin_offset = current_offset;
79 if (current_offset < target_offset) {
80 /* there are still some data left. */
81 return current_offset;
82 }
8350fe9b 83 assert(current_offset == target_offset);
090089c4 84 return current_offset;
090089c4 85}
86
090089c4 87/* Append incoming data. */
e6e5d90d 88void
3f6c0fb2 89stmemAppend(mem_hdr * mem, const char *data, int len)
090089c4 90{
090089c4 91 int avail_len;
92 int len_to_copy;
a3d5953d 93 debug(19, 6) ("memAppend: len %d\n", len);
090089c4 94 /* Does the last block still contain empty space?
95 * If so, fill out the block before dropping into the
96 * allocation loop */
090089c4 97 if (mem->head && mem->tail && (mem->tail->len < SM_PAGE_SIZE)) {
98 avail_len = SM_PAGE_SIZE - (mem->tail->len);
0254ee29 99 len_to_copy = XMIN(avail_len, len);
30a4f2a8 100 xmemcpy((mem->tail->data + mem->tail->len), data, len_to_copy);
090089c4 101 /* Adjust the ptr and len according to what was deposited in the page */
102 data += len_to_copy;
103 len -= len_to_copy;
104 mem->tail->len += len_to_copy;
105 }
106 while (len > 0) {
0254ee29 107 len_to_copy = XMIN(len, SM_PAGE_SIZE);
e6ccf245 108 mem_node *p = static_cast<mem_node *>(memAllocate(MEM_MEM_NODE));
090089c4 109 p->next = NULL;
110 p->len = len_to_copy;
b93de483 111 store_mem_size += SM_PAGE_SIZE;
30a4f2a8 112 xmemcpy(p->data, data, len_to_copy);
090089c4 113 if (!mem->head) {
114 /* The chain is empty */
115 mem->head = mem->tail = p;
116 } else {
117 /* Append it to existing chain */
118 mem->tail->next = p;
119 mem->tail = p;
120 }
121 len -= len_to_copy;
122 data += len_to_copy;
123 }
090089c4 124}
125
02be0294 126ssize_t
3f6c0fb2 127stmemCopy(const mem_hdr * mem, off_t offset, char *buf, size_t size)
090089c4 128{
f1dc9b30 129 mem_node *p = mem->head;
d89d1fb6 130 off_t t_off = mem->origin_offset;
131 size_t bytes_to_go = size;
c4b7a5a9 132 debug(19, 6) ("memCopy: offset %ld: size %u\n", (long int) offset, (int)size);
30a4f2a8 133 if (p == NULL)
11d6f6c0 134 return 0;
edce4d98 135 /* RC: the next assert is useless */
97a88399 136 assert(size > 0);
090089c4 137 /* Seek our way into store */
138 while ((t_off + p->len) < offset) {
139 t_off += p->len;
0e473d70 140 if (!p->next) {
141 debug(19, 1) ("memCopy: p->next == NULL\n");
6cf028ab 142 return 0;
143 }
d89d1fb6 144 assert(p->next);
145 p = p->next;
090089c4 146 }
090089c4 147 /* Start copying begining with this block until
148 * we're satiated */
e6ccf245 149 int bytes_into_this_packet = offset - t_off;
150 assert (p->len >= bytes_into_this_packet);
151 int bytes_from_this_packet = XMIN(bytes_to_go, (size_t)(p->len - bytes_into_this_packet));
30a4f2a8 152 xmemcpy(buf, p->data + bytes_into_this_packet, bytes_from_this_packet);
090089c4 153 bytes_to_go -= bytes_from_this_packet;
e6ccf245 154 char *ptr_to_buf = buf + bytes_from_this_packet;
090089c4 155 p = p->next;
090089c4 156 while (p && bytes_to_go > 0) {
e6ccf245 157 if ((int)bytes_to_go > p->len) {
30a4f2a8 158 xmemcpy(ptr_to_buf, p->data, p->len);
090089c4 159 ptr_to_buf += p->len;
160 bytes_to_go -= p->len;
161 } else {
30a4f2a8 162 xmemcpy(ptr_to_buf, p->data, bytes_to_go);
e6ccf245 163 bytes_to_go = 0;
090089c4 164 }
165 p = p->next;
166 }
d89d1fb6 167 return size - bytes_to_go;
090089c4 168}