OLD | NEW |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* This Source Code Form is subject to the terms of the Mozilla Public | 2 /* This Source Code Form is subject to the terms of the Mozilla Public |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | 3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | 5 |
6 #include "primpl.h" | 6 #include "primpl.h" |
7 | 7 |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 /*****************************************************************************/ | 10 /*****************************************************************************/ |
(...skipping 13 matching lines...) Expand all Loading... |
24 ** preserved before they are recycled. The environment variables are | 24 ** preserved before they are recycled. The environment variables are |
25 ** NSPR_FD_CACHE_SIZE_LOW and NSPR_FD_CACHE_SIZE_HIGH. The former sets | 25 ** NSPR_FD_CACHE_SIZE_LOW and NSPR_FD_CACHE_SIZE_HIGH. The former sets |
26 ** the number of descriptors NSPR will allocate before beginning to | 26 ** the number of descriptors NSPR will allocate before beginning to |
27 ** recycle. The latter is the maximum number permitted in the cache | 27 ** recycle. The latter is the maximum number permitted in the cache |
28 ** (exclusive of those in use) at a time. | 28 ** (exclusive of those in use) at a time. |
29 */ | 29 */ |
30 typedef struct _PR_Fd_Cache | 30 typedef struct _PR_Fd_Cache |
31 { | 31 { |
32 PRLock *ml; | 32 PRLock *ml; |
33 PRIntn count; | 33 PRIntn count; |
34 PRStack *stack; | |
35 PRFileDesc *head, *tail; | 34 PRFileDesc *head, *tail; |
36 PRIntn limit_low, limit_high; | 35 PRIntn limit_low, limit_high; |
37 } _PR_Fd_Cache; | 36 } _PR_Fd_Cache; |
38 | 37 |
39 static _PR_Fd_Cache _pr_fd_cache; | 38 static _PR_Fd_Cache _pr_fd_cache; |
40 static PRFileDesc **stack2fd = &(((PRFileDesc*)NULL)->higher); | |
41 | 39 |
42 | 40 |
43 /* | 41 /* |
44 ** Get a FileDescriptor from the cache if one exists. If not allocate | 42 ** Get a FileDescriptor from the cache if one exists. If not allocate |
45 ** a new one from the heap. | 43 ** a new one from the heap. |
46 */ | 44 */ |
47 PRFileDesc *_PR_Getfd(void) | 45 PRFileDesc *_PR_Getfd(void) |
48 { | 46 { |
49 PRFileDesc *fd; | 47 PRFileDesc *fd; |
50 /* | 48 /* |
51 ** $$$ | 49 ** $$$ |
52 ** This may look a little wasteful. We'll see. Right now I want to | 50 ** This may look a little wasteful. We'll see. Right now I want to |
53 ** be able to toggle between caching and not at runtime to measure | 51 ** be able to toggle between caching and not at runtime to measure |
54 ** the differences. If it isn't too annoying, I'll leave it in. | 52 ** the differences. If it isn't too annoying, I'll leave it in. |
55 ** $$$$ | 53 ** $$$$ |
56 ** | 54 ** |
57 ** The test is against _pr_fd_cache.limit_high. If that's zero, | 55 ** The test is against _pr_fd_cache.limit_high. If that's zero, |
58 ** we're not doing the extended cache but going for performance. | 56 ** we're not doing the extended cache but going for performance. |
59 */ | 57 */ |
60 if (0 == _pr_fd_cache.limit_high) | 58 if (0 == _pr_fd_cache.limit_high) |
61 { | 59 { |
62 PRStackElem *pop; | 60 goto allocate; |
63 PR_ASSERT(NULL != _pr_fd_cache.stack); | |
64 pop = PR_StackPop(_pr_fd_cache.stack); | |
65 if (NULL == pop) goto allocate; | |
66 fd = (PRFileDesc*)((PRPtrdiff)pop - (PRPtrdiff)stack2fd); | |
67 } | 61 } |
68 else | 62 else |
69 { | 63 { |
70 do | 64 do |
71 { | 65 { |
72 if (NULL == _pr_fd_cache.head) goto allocate; /* nothing there */ | 66 if (NULL == _pr_fd_cache.head) goto allocate; /* nothing there */ |
73 if (_pr_fd_cache.count < _pr_fd_cache.limit_low) goto allocate; | 67 if (_pr_fd_cache.count < _pr_fd_cache.limit_low) goto allocate; |
74 | 68 |
75 /* we "should" be able to extract an fd from the cache */ | 69 /* we "should" be able to extract an fd from the cache */ |
76 PR_Lock(_pr_fd_cache.ml); /* need the lock to do this safely */ | 70 PR_Lock(_pr_fd_cache.ml); /* need the lock to do this safely */ |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 ** Return a file descriptor to the cache unless there are too many in | 115 ** Return a file descriptor to the cache unless there are too many in |
122 ** there already. If put in cache, clear the fields first. | 116 ** there already. If put in cache, clear the fields first. |
123 */ | 117 */ |
124 void _PR_Putfd(PRFileDesc *fd) | 118 void _PR_Putfd(PRFileDesc *fd) |
125 { | 119 { |
126 PR_ASSERT(PR_NSPR_IO_LAYER == fd->identity); | 120 PR_ASSERT(PR_NSPR_IO_LAYER == fd->identity); |
127 fd->methods = &_pr_faulty_methods; | 121 fd->methods = &_pr_faulty_methods; |
128 fd->identity = PR_INVALID_IO_LAYER; | 122 fd->identity = PR_INVALID_IO_LAYER; |
129 fd->secret->state = _PR_FILEDESC_FREED; | 123 fd->secret->state = _PR_FILEDESC_FREED; |
130 | 124 |
131 if (0 == _pr_fd_cache.limit_high) | 125 if (0 != _pr_fd_cache.limit_high) |
132 { | 126 { |
133 PR_StackPush(_pr_fd_cache.stack, (PRStackElem*)(&fd->higher)); | 127 if (_pr_fd_cache.count < _pr_fd_cache.limit_high) |
134 } | |
135 else | |
136 { | |
137 if (_pr_fd_cache.count > _pr_fd_cache.limit_high) | |
138 { | |
139 PR_Free(fd->secret); | |
140 PR_Free(fd); | |
141 } | |
142 else | |
143 { | 128 { |
144 PR_Lock(_pr_fd_cache.ml); | 129 PR_Lock(_pr_fd_cache.ml); |
145 if (NULL == _pr_fd_cache.tail) | 130 if (NULL == _pr_fd_cache.tail) |
146 { | 131 { |
147 PR_ASSERT(0 == _pr_fd_cache.count); | 132 PR_ASSERT(0 == _pr_fd_cache.count); |
148 PR_ASSERT(NULL == _pr_fd_cache.head); | 133 PR_ASSERT(NULL == _pr_fd_cache.head); |
149 _pr_fd_cache.head = _pr_fd_cache.tail = fd; | 134 _pr_fd_cache.head = _pr_fd_cache.tail = fd; |
150 } | 135 } |
151 else | 136 else |
152 { | 137 { |
153 PR_ASSERT(NULL == _pr_fd_cache.tail->higher); | 138 PR_ASSERT(NULL == _pr_fd_cache.tail->higher); |
154 _pr_fd_cache.tail->higher = fd; | 139 _pr_fd_cache.tail->higher = fd; |
155 _pr_fd_cache.tail = fd; /* new value */ | 140 _pr_fd_cache.tail = fd; /* new value */ |
156 } | 141 } |
157 fd->higher = NULL; /* always so */ | 142 fd->higher = NULL; /* always so */ |
158 _pr_fd_cache.count += 1; /* count the new entry */ | 143 _pr_fd_cache.count += 1; /* count the new entry */ |
159 PR_Unlock(_pr_fd_cache.ml); | 144 PR_Unlock(_pr_fd_cache.ml); |
| 145 return; |
160 } | 146 } |
161 } | 147 } |
| 148 |
| 149 PR_Free(fd->secret); |
| 150 PR_Free(fd); |
162 } /* _PR_Putfd */ | 151 } /* _PR_Putfd */ |
163 | 152 |
164 PR_IMPLEMENT(PRStatus) PR_SetFDCacheSize(PRIntn low, PRIntn high) | 153 PR_IMPLEMENT(PRStatus) PR_SetFDCacheSize(PRIntn low, PRIntn high) |
165 { | 154 { |
166 /* | 155 /* |
167 ** This can be called at any time, may adjust the cache sizes, | 156 ** This can be called at any time, may adjust the cache sizes, |
168 ** turn the caches off, or turn them on. It is not dependent | 157 ** turn the caches off, or turn them on. It is not dependent |
169 ** on the compilation setting of DEBUG. | 158 ** on the compilation setting of DEBUG. |
170 */ | 159 */ |
171 if (!_pr_initialized) _PR_ImplicitInitialization(); | 160 if (!_pr_initialized) _PR_ImplicitInitialization(); |
172 | 161 |
173 if (low > high) low = high; /* sanity check the params */ | 162 if (low > high) low = high; /* sanity check the params */ |
174 | 163 |
175 PR_Lock(_pr_fd_cache.ml); | 164 PR_Lock(_pr_fd_cache.ml); |
176 if (0 == high) /* shutting down or staying down */ | 165 _pr_fd_cache.limit_high = high; |
177 { | 166 _pr_fd_cache.limit_low = low; |
178 if (0 != _pr_fd_cache.limit_high) /* shutting down */ | |
179 { | |
180 _pr_fd_cache.limit_high = 0; /* stop use */ | |
181 /* | |
182 ** Hold the lock throughout - nobody's going to want it | |
183 ** other than another caller to this routine. Just don't | |
184 ** let that happen. | |
185 ** | |
186 ** Put all the cached fds onto the new cache. | |
187 */ | |
188 while (NULL != _pr_fd_cache.head) | |
189 { | |
190 PRFileDesc *fd = _pr_fd_cache.head; | |
191 _pr_fd_cache.head = fd->higher; | |
192 PR_StackPush(_pr_fd_cache.stack, (PRStackElem*)(&fd->higher)); | |
193 } | |
194 _pr_fd_cache.limit_low = 0; | |
195 _pr_fd_cache.tail = NULL; | |
196 _pr_fd_cache.count = 0; | |
197 } | |
198 } | |
199 else /* starting up or just adjusting parameters */ | |
200 { | |
201 PRBool was_using_stack = (0 == _pr_fd_cache.limit_high); | |
202 _pr_fd_cache.limit_low = low; | |
203 _pr_fd_cache.limit_high = high; | |
204 if (was_using_stack) /* was using stack - feed into cache */ | |
205 { | |
206 PRStackElem *pop; | |
207 while (NULL != (pop = PR_StackPop(_pr_fd_cache.stack))) | |
208 { | |
209 PRFileDesc *fd = (PRFileDesc*) | |
210 ((PRPtrdiff)pop - (PRPtrdiff)stack2fd); | |
211 if (NULL == _pr_fd_cache.tail) _pr_fd_cache.tail = fd; | |
212 fd->higher = _pr_fd_cache.head; | |
213 _pr_fd_cache.head = fd; | |
214 _pr_fd_cache.count += 1; | |
215 } | |
216 } | |
217 } | |
218 PR_Unlock(_pr_fd_cache.ml); | 167 PR_Unlock(_pr_fd_cache.ml); |
219 return PR_SUCCESS; | 168 return PR_SUCCESS; |
220 } /* PR_SetFDCacheSize */ | 169 } /* PR_SetFDCacheSize */ |
221 | 170 |
222 void _PR_InitFdCache(void) | 171 void _PR_InitFdCache(void) |
223 { | 172 { |
224 /* | 173 /* |
225 ** The fd caching is enabled by default for DEBUG builds, | 174 ** The fd caching is enabled by default for DEBUG builds, |
226 ** disabled by default for OPT builds. That default can | 175 ** disabled by default for OPT builds. That default can |
227 ** be overridden at runtime using environment variables | 176 ** be overridden at runtime using environment variables |
(...skipping 23 matching lines...) Expand all Loading... |
251 _pr_fd_cache.limit_low = FD_SETSIZE; | 200 _pr_fd_cache.limit_low = FD_SETSIZE; |
252 | 201 |
253 if (_pr_fd_cache.limit_high > FD_SETSIZE) | 202 if (_pr_fd_cache.limit_high > FD_SETSIZE) |
254 _pr_fd_cache.limit_high = FD_SETSIZE; | 203 _pr_fd_cache.limit_high = FD_SETSIZE; |
255 | 204 |
256 if (_pr_fd_cache.limit_high < _pr_fd_cache.limit_low) | 205 if (_pr_fd_cache.limit_high < _pr_fd_cache.limit_low) |
257 _pr_fd_cache.limit_high = _pr_fd_cache.limit_low; | 206 _pr_fd_cache.limit_high = _pr_fd_cache.limit_low; |
258 | 207 |
259 _pr_fd_cache.ml = PR_NewLock(); | 208 _pr_fd_cache.ml = PR_NewLock(); |
260 PR_ASSERT(NULL != _pr_fd_cache.ml); | 209 PR_ASSERT(NULL != _pr_fd_cache.ml); |
261 _pr_fd_cache.stack = PR_CreateStack("FD"); | |
262 PR_ASSERT(NULL != _pr_fd_cache.stack); | |
263 | 210 |
264 } /* _PR_InitFdCache */ | 211 } /* _PR_InitFdCache */ |
265 | 212 |
266 void _PR_CleanupFdCache(void) | 213 void _PR_CleanupFdCache(void) |
267 { | 214 { |
268 PRFileDesc *fd, *next; | 215 PRFileDesc *fd, *next; |
269 PRStackElem *pop; | 216 PRStackElem *pop; |
270 | 217 |
271 for (fd = _pr_fd_cache.head; fd != NULL; fd = next) | 218 for (fd = _pr_fd_cache.head; fd != NULL; fd = next) |
272 { | 219 { |
273 next = fd->higher; | 220 next = fd->higher; |
274 PR_DELETE(fd->secret); | 221 PR_DELETE(fd->secret); |
275 PR_DELETE(fd); | 222 PR_DELETE(fd); |
276 } | 223 } |
277 _pr_fd_cache.head = NULL; | 224 _pr_fd_cache.head = NULL; |
278 _pr_fd_cache.tail = NULL; | 225 _pr_fd_cache.tail = NULL; |
279 _pr_fd_cache.count = 0; | 226 _pr_fd_cache.count = 0; |
280 PR_DestroyLock(_pr_fd_cache.ml); | 227 PR_DestroyLock(_pr_fd_cache.ml); |
281 _pr_fd_cache.ml = NULL; | 228 _pr_fd_cache.ml = NULL; |
282 while ((pop = PR_StackPop(_pr_fd_cache.stack)) != NULL) | |
283 { | |
284 fd = (PRFileDesc*)((PRPtrdiff)pop - (PRPtrdiff)stack2fd); | |
285 PR_DELETE(fd->secret); | |
286 PR_DELETE(fd); | |
287 } | |
288 PR_DestroyStack(_pr_fd_cache.stack); | |
289 _pr_fd_cache.stack = NULL; | |
290 } /* _PR_CleanupFdCache */ | 229 } /* _PR_CleanupFdCache */ |
291 | 230 |
292 /* prfdcach.c */ | 231 /* prfdcach.c */ |
OLD | NEW |