]> git.ipfire.org Git - thirdparty/squid.git/blob - src/filemap.cc
removed fm->last_file_number_allocated which wasnt really being used
[thirdparty/squid.git] / src / filemap.cc
1 /*
2 * $Id: filemap.cc,v 1.23 1998/02/10 22:29:51 wessels Exp $
3 *
4 * DEBUG: section 8 Swap File Bitmap
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
8 * --------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from the
11 * Internet community. Development is led by Duane Wessels of the
12 * National Laboratory for Applied Network Research and funded by
13 * the National Science Foundation.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31 /*
32 * Copyright (c) 1994, 1995. All rights reserved.
33 *
34 * The Harvest software was developed by the Internet Research Task
35 * Force Research Group on Resource Discovery (IRTF-RD):
36 *
37 * Mic Bowman of Transarc Corporation.
38 * Peter Danzig of the University of Southern California.
39 * Darren R. Hardy of the University of Colorado at Boulder.
40 * Udi Manber of the University of Arizona.
41 * Michael F. Schwartz of the University of Colorado at Boulder.
42 * Duane Wessels of the University of Colorado at Boulder.
43 *
44 * This copyright notice applies to software in the Harvest
45 * ``src/'' directory only. Users should consult the individual
46 * copyright notices in the ``components/'' subdirectories for
47 * copyright information about other software bundled with the
48 * Harvest source code distribution.
49 *
50 * TERMS OF USE
51 *
52 * The Harvest software may be used and re-distributed without
53 * charge, provided that the software origin and research team are
54 * cited in any use of the system. Most commonly this is
55 * accomplished by including a link to the Harvest Home Page
56 * (http://harvest.cs.colorado.edu/) from the query page of any
57 * Broker you deploy, as well as in the query result pages. These
58 * links are generated automatically by the standard Broker
59 * software distribution.
60 *
61 * The Harvest software is provided ``as is'', without express or
62 * implied warranty, and with no support nor obligation to assist
63 * in its use, correction, modification or enhancement. We assume
64 * no liability with respect to the infringement of copyrights,
65 * trade secrets, or any patents, and are not responsible for
66 * consequential damages. Proper use of the Harvest software is
67 * entirely the responsibility of the user.
68 *
69 * DERIVATIVE WORKS
70 *
71 * Users may make derivative works from the Harvest software, subject
72 * to the following constraints:
73 *
74 * - You must include the above copyright notice and these
75 * accompanying paragraphs in all forms of derivative works,
76 * and any documentation and other materials related to such
77 * distribution and use acknowledge that the software was
78 * developed at the above institutions.
79 *
80 * - You must notify IRTF-RD regarding your distribution of
81 * the derivative work.
82 *
83 * - You must clearly notify users that your are distributing
84 * a modified version and not the original Harvest software.
85 *
86 * - Any derivative product is also subject to these copyright
87 * and use restrictions.
88 *
89 * Note that the Harvest software is NOT in the public domain. We
90 * retain copyright, as specified above.
91 *
92 * HISTORY OF FREE SOFTWARE STATUS
93 *
94 * Originally we required sites to license the software in cases
95 * where they were going to build commercial products/services
96 * around Harvest. In June 1995 we changed this policy. We now
97 * allow people to use the core Harvest software (the code found in
98 * the Harvest ``src/'' directory) for free. We made this change
99 * in the interest of encouraging the widest possible deployment of
100 * the technology. The Harvest software is really a reference
101 * implementation of a set of protocols and formats, some of which
102 * we intend to standardize. We encourage commercial
103 * re-implementations of code complying to this set of standards.
104 */
105
106 #include "squid.h"
107
108 /* Number of bits in a long */
109 #if SIZEOF_LONG == 8
110 #define LONG_BIT_SHIFT 6
111 #define BITS_IN_A_LONG 0x40
112 #define LONG_BIT_MASK 0x3F
113 #define ALL_ONES (unsigned long) 0xFFFFFFFFFFFFFFFF
114 #elif SIZEOF_LONG == 4
115 #define LONG_BIT_SHIFT 5
116 #define BITS_IN_A_LONG 0x20
117 #define LONG_BIT_MASK 0x1F
118 #define ALL_ONES (unsigned long) 0xFFFFFFFF
119 #else
120 #define LONG_BIT_SHIFT 5
121 #define BITS_IN_A_LONG 0x20
122 #define LONG_BIT_MASK 0x1F
123 #define ALL_ONES (unsigned long) 0xFFFFFFFF
124 #endif
125
126 fileMap *
127 file_map_create(int n)
128 {
129 fileMap *fm = xcalloc(1, sizeof(fileMap));
130 fm->max_n_files = n;
131 fm->nwords = n >> LONG_BIT_SHIFT;
132 debug(8, 3) ("file_map_create: creating space for %d files\n", n);
133 debug(8, 5) ("--> %d words of %d bytes each\n",
134 fm->nwords, sizeof(unsigned long));
135 fm->file_map = xcalloc(fm->nwords, sizeof(unsigned long));
136 meta_data.misc += fm->nwords * sizeof(unsigned long);
137 return fm;
138 }
139
140 int
141 file_map_bit_set(fileMap * fm, int file_number)
142 {
143 unsigned long bitmask = (1L << (file_number & LONG_BIT_MASK));
144 fm->file_map[file_number >> LONG_BIT_SHIFT] |= bitmask;
145 fm->n_files_in_map++;
146 if (!fm->toggle && (fm->n_files_in_map > ((fm->max_n_files * 7) >> 3))) {
147 fm->toggle++;
148 debug(8, 0) ("You should increment MAX_SWAP_FILE\n");
149 } else if (fm->n_files_in_map > (fm->max_n_files - 100)) {
150 fatal("You've run out of swap file numbers.");
151 }
152 return (file_number);
153 }
154
155 void
156 file_map_bit_reset(fileMap * fm, int file_number)
157 {
158 unsigned long bitmask = (1L << (file_number & LONG_BIT_MASK));
159 fm->file_map[file_number >> LONG_BIT_SHIFT] &= ~bitmask;
160 fm->n_files_in_map--;
161 }
162
163 int
164 file_map_bit_test(fileMap * fm, int file_number)
165 {
166 unsigned long bitmask = (1L << (file_number & LONG_BIT_MASK));
167 /* be sure the return value is an int, not a u_long */
168 return (fm->file_map[file_number >> LONG_BIT_SHIFT] & bitmask ? 1 : 0);
169 }
170
171 int
172 file_map_allocate(fileMap * fm, int suggestion)
173 {
174 int word;
175 int bit;
176 int count;
177 if (suggestion > fm->max_n_files)
178 suggestion = 0;
179 if (!file_map_bit_test(fm, suggestion)) {
180 return file_map_bit_set(fm, suggestion);
181 }
182 word = suggestion >> LONG_BIT_SHIFT;
183 for (count = 0; count < fm->nwords; count++) {
184 if (fm->file_map[word] != ALL_ONES)
185 break;
186 word = (word + 1) % fm->nwords;
187 }
188 for (bit = 0; bit < BITS_IN_A_LONG; bit++) {
189 suggestion = ((unsigned long) word << LONG_BIT_SHIFT) | bit;
190 if (!file_map_bit_test(fm, suggestion)) {
191 return file_map_bit_set(fm, suggestion);
192 }
193 }
194 fatal("file_map_allocate: Exceeded filemap limit");
195 return 0; /* NOTREACHED */
196 }
197
198 void
199 filemapFreeMemory(fileMap * fm)
200 {
201 safe_free(fm->file_map);
202 safe_free(fm);
203 }
204
205 #ifdef TEST
206
207 #define TEST_SIZE 1<<16
208 main(argc, argv)
209 {
210 int i;
211
212 fm = file_map_create(TEST_SIZE);
213
214 for (i = 0; i < TEST_SIZE; ++i) {
215 file_map_bit_set(i);
216 assert(file_map_bit_test(i));
217 file_map_bit_reset(i);
218 }
219 }
220 #endif