1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2001, 2002
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Suresh Duddi <[email protected]>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * nsRecyclingAllocator
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include "nsRecyclingAllocator.h"
|
---|
47 | #include "nsIMemory.h"
|
---|
48 | #include "nsAutoLock.h"
|
---|
49 | #include "prprf.h"
|
---|
50 | #include "nsITimer.h"
|
---|
51 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
52 | # include <iprt/mem.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #define NS_SEC_TO_MS(s) ((s) * 1000)
|
---|
56 |
|
---|
57 | void
|
---|
58 | nsRecyclingAllocator::nsRecycleTimerCallback(nsITimer *aTimer, void *aClosure)
|
---|
59 | {
|
---|
60 | nsRecyclingAllocator *obj = (nsRecyclingAllocator *) aClosure;
|
---|
61 | if (!obj->mTouched)
|
---|
62 | {
|
---|
63 | if (obj->mFreeList)
|
---|
64 | obj->FreeUnusedBuckets();
|
---|
65 |
|
---|
66 | // If we are holding no more memory, there is no need for the timer.
|
---|
67 | // We will revive the timer on the next allocation.
|
---|
68 | // XXX Unfortunately there is no way to Cancel and restart the same timer.
|
---|
69 | // XXX So we pretty much kill it and create a new one later.
|
---|
70 | if (!obj->mFreeList && obj->mRecycleTimer)
|
---|
71 | {
|
---|
72 | obj->mRecycleTimer->Cancel();
|
---|
73 | NS_RELEASE(obj->mRecycleTimer);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | // Clear touched so the next time the timer fires we can test whether
|
---|
79 | // the allocator was used or not.
|
---|
80 | obj->Untouch();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | nsRecyclingAllocator::nsRecyclingAllocator(PRUint32 nbucket, PRUint32 recycleAfter, const char *id) :
|
---|
86 | mMaxBlocks(nbucket), mBlocks(nsnull), mFreeList(nsnull), mNotUsedList(nsnull),
|
---|
87 | mRecycleTimer(nsnull), mRecycleAfter(recycleAfter), mTouched(0), mId(id)
|
---|
88 | #ifdef DEBUG
|
---|
89 | ,mNAllocated(0)
|
---|
90 | #endif
|
---|
91 | {
|
---|
92 | NS_ASSERTION(mMaxBlocks <= NS_MAX_BLOCKS, "Too many blocks. This will affect the allocator's performance.");
|
---|
93 |
|
---|
94 | mLock = PR_NewLock();
|
---|
95 | NS_ASSERTION(mLock, "Recycling allocator cannot get lock");
|
---|
96 |
|
---|
97 | Init(nbucket, recycleAfter, id);
|
---|
98 | }
|
---|
99 |
|
---|
100 | nsresult
|
---|
101 | nsRecyclingAllocator::Init(PRUint32 nbucket, PRUint32 recycleAfter, const char *id)
|
---|
102 | {
|
---|
103 | nsAutoLock lock(mLock);
|
---|
104 |
|
---|
105 | // Free all memory held, if any
|
---|
106 | while(mFreeList)
|
---|
107 | {
|
---|
108 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
109 | RTMemFree(mFreeList->block);
|
---|
110 | #else
|
---|
111 | free(mFreeList->block);
|
---|
112 | #endif
|
---|
113 | mFreeList = mFreeList->next;
|
---|
114 | }
|
---|
115 | mFreeList = nsnull;
|
---|
116 |
|
---|
117 | if (mBlocks)
|
---|
118 | delete [] mBlocks;
|
---|
119 |
|
---|
120 | // Reinitialize everything
|
---|
121 | mMaxBlocks = nbucket;
|
---|
122 | if (nbucket)
|
---|
123 | {
|
---|
124 | // Create memory for our bookkeeping
|
---|
125 | mBlocks = new BlockStoreNode[mMaxBlocks];
|
---|
126 | if (!mBlocks)
|
---|
127 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
128 | // Link them together
|
---|
129 | mNotUsedList = mBlocks;
|
---|
130 | for (PRUint32 i=0; i < mMaxBlocks-1; i++)
|
---|
131 | mBlocks[i].next = &(mBlocks[i+1]);
|
---|
132 | }
|
---|
133 |
|
---|
134 | mRecycleAfter = recycleAfter;
|
---|
135 | mId = id;
|
---|
136 |
|
---|
137 | return NS_OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | nsRecyclingAllocator::~nsRecyclingAllocator()
|
---|
141 | {
|
---|
142 | // Cancel and destroy recycle timer
|
---|
143 | if (mRecycleTimer)
|
---|
144 | {
|
---|
145 | mRecycleTimer->Cancel();
|
---|
146 | NS_RELEASE(mRecycleTimer);
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Free all memory held, if any
|
---|
150 | while(mFreeList)
|
---|
151 | {
|
---|
152 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
153 | RTMemFree(mFreeList->block);
|
---|
154 | #else
|
---|
155 | free(mFreeList->block);
|
---|
156 | #endif
|
---|
157 | mFreeList = mFreeList->next;
|
---|
158 | }
|
---|
159 | mFreeList = nsnull;
|
---|
160 |
|
---|
161 | if (mBlocks)
|
---|
162 | delete [] mBlocks;
|
---|
163 |
|
---|
164 | if (mLock)
|
---|
165 | {
|
---|
166 | PR_DestroyLock(mLock);
|
---|
167 | mLock = nsnull;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | // Allocation and free routines
|
---|
172 | void*
|
---|
173 | nsRecyclingAllocator::Malloc(PRSize bytes, PRBool zeroit)
|
---|
174 | {
|
---|
175 | // Mark that we are using. This will prevent any
|
---|
176 | // timer based release of unused memory.
|
---|
177 | Touch();
|
---|
178 |
|
---|
179 | Block* freeBlock = FindFreeBlock(bytes);
|
---|
180 | if (freeBlock)
|
---|
181 | {
|
---|
182 | void *data = DATA(freeBlock);
|
---|
183 |
|
---|
184 | if (zeroit)
|
---|
185 | memset(data, 0, bytes);
|
---|
186 | return data;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // We need to do an allocation
|
---|
190 | // Add 4 bytes to what we allocate to hold the bucket index
|
---|
191 | PRSize allocBytes = bytes + NS_ALLOCATOR_OVERHEAD_BYTES;
|
---|
192 |
|
---|
193 | // We dont have that memory already. Allocate.
|
---|
194 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
195 | Block *ptr = (Block *) (zeroit ? RTMemAllocZ(allocBytes) : RTMemAlloc(allocBytes));
|
---|
196 | #else
|
---|
197 | Block *ptr = (Block *) (zeroit ? calloc(1, allocBytes) : malloc(allocBytes));
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | // Deal with no memory situation
|
---|
201 | if (!ptr)
|
---|
202 | return ptr;
|
---|
203 |
|
---|
204 | // This is the first allocation we are holding.
|
---|
205 | // Setup timer for releasing memory
|
---|
206 | // If this fails, then we wont have a timer to release unused
|
---|
207 | // memory. We can live with that. Also, the next allocation
|
---|
208 | // will try again to set the timer.
|
---|
209 | if (mRecycleAfter && !mRecycleTimer)
|
---|
210 | {
|
---|
211 | // known only to stuff in xpcom.
|
---|
212 | extern nsresult NS_NewTimer(nsITimer* *aResult, nsTimerCallbackFunc aCallback, void *aClosure,
|
---|
213 | PRUint32 aDelay, PRUint32 aType);
|
---|
214 |
|
---|
215 | (void) NS_NewTimer(&mRecycleTimer, nsRecycleTimerCallback, this,
|
---|
216 | NS_SEC_TO_MS(mRecycleAfter),
|
---|
217 | nsITimer::TYPE_REPEATING_SLACK);
|
---|
218 | NS_ASSERTION(mRecycleTimer, "nsRecyclingAllocator: Creating timer failed.\n");
|
---|
219 | }
|
---|
220 |
|
---|
221 | #ifdef DEBUG
|
---|
222 | mNAllocated++;
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | // Store size and return data portion
|
---|
226 | ptr->bytes = bytes;
|
---|
227 | return DATA(ptr);
|
---|
228 | }
|
---|
229 |
|
---|
230 | void
|
---|
231 | nsRecyclingAllocator::Free(void *ptr)
|
---|
232 | {
|
---|
233 | // Mark that we are using the allocator. This will prevent any
|
---|
234 | // timer based release of unused memory.
|
---|
235 | Touch();
|
---|
236 |
|
---|
237 | Block* block = DATA_TO_BLOCK(ptr);
|
---|
238 |
|
---|
239 | if (!AddToFreeList(block))
|
---|
240 | {
|
---|
241 | // We are holding more than max. Failover to free
|
---|
242 | #ifdef DEBUG_dp
|
---|
243 | char buf[1024];
|
---|
244 | // Warn if we are failing over to malloc/free and not storing it
|
---|
245 | // This says we have a misdesigned memory pool. The intent was
|
---|
246 | // once the pool was full, we would never fail over to calloc.
|
---|
247 | PR_snprintf(buf, sizeof(buf), "nsRecyclingAllocator(%s) FAILOVER 0x%p (%d) - %d allocations, %d max\n",
|
---|
248 | mId, (char *)ptr, block->bytes, mNAllocated, mMaxBlocks);
|
---|
249 | NS_WARNING(buf);
|
---|
250 | mNAllocated--;
|
---|
251 | #endif
|
---|
252 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
253 | RTMemFree(block);
|
---|
254 | #else
|
---|
255 | free(block);
|
---|
256 | #endif
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* FreeUnusedBuckets
|
---|
261 | *
|
---|
262 | * Frees any bucket memory that isn't in use
|
---|
263 | */
|
---|
264 |
|
---|
265 | void
|
---|
266 | nsRecyclingAllocator::FreeUnusedBuckets()
|
---|
267 | {
|
---|
268 | #ifdef DEBUG_dp
|
---|
269 | printf("DEBUG: nsRecyclingAllocator(%s) FreeUnusedBuckets: ", mId);
|
---|
270 | #endif
|
---|
271 | nsAutoLock lock(mLock);
|
---|
272 |
|
---|
273 | // We will run through the freelist and free all blocks
|
---|
274 | BlockStoreNode* node = mFreeList;
|
---|
275 | while (node)
|
---|
276 | {
|
---|
277 | // Free the allocated block
|
---|
278 | #ifdef VBOX_USE_IPRT_IN_XPCOM
|
---|
279 | RTMemFree(node->block);
|
---|
280 | #else
|
---|
281 | free(node->block);
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | #ifdef DEBUG_dp
|
---|
285 | printf("%d ", node->bytes);
|
---|
286 | #endif
|
---|
287 | // Clear Node
|
---|
288 | node->block = nsnull;
|
---|
289 | node->bytes = 0;
|
---|
290 | node = node->next;
|
---|
291 | }
|
---|
292 |
|
---|
293 | // remake the lists
|
---|
294 | mNotUsedList = mBlocks;
|
---|
295 | for (PRUint32 i=0; i < mMaxBlocks-1; i++)
|
---|
296 | mBlocks[i].next = &(mBlocks[i+1]);
|
---|
297 | mBlocks[mMaxBlocks-1].next = nsnull;
|
---|
298 | mFreeList = nsnull;
|
---|
299 |
|
---|
300 | #ifdef DEBUG
|
---|
301 | mNAllocated = 0;
|
---|
302 | #endif
|
---|
303 | #ifdef DEBUG_dp
|
---|
304 | printf("\n");
|
---|
305 | #endif
|
---|
306 | }
|
---|
307 |
|
---|
308 | nsRecyclingAllocator::Block*
|
---|
309 | nsRecyclingAllocator::FindFreeBlock(PRSize bytes)
|
---|
310 | {
|
---|
311 | // We dont enter lock for this check. This is intentional.
|
---|
312 | // Here is my logic: we are checking if (!mFreeList). Doing this check
|
---|
313 | // without locking can lead to unpredictable results. YES. But the effect
|
---|
314 | // of the unpredictedness are ok. here is why:
|
---|
315 | //
|
---|
316 | // a) if the check returned NULL when there is stuff in freelist
|
---|
317 | // We would just end up reallocating.
|
---|
318 | //
|
---|
319 | // b) if the check returned nonNULL when our freelist is empty
|
---|
320 | // This is the more likely and dangerous case. The code for
|
---|
321 | // FindFreeBlock() will enter lock, while (null) and return null.
|
---|
322 | //
|
---|
323 | // The reason why I chose to not enter lock for this check was that when
|
---|
324 | // the allocator is full, we dont want to impose any more overhead than
|
---|
325 | // we already are for failing over to malloc/free.
|
---|
326 |
|
---|
327 | if (!mFreeList)
|
---|
328 | return NULL;
|
---|
329 |
|
---|
330 | Block *block = nsnull;
|
---|
331 |
|
---|
332 | nsAutoLock lock(mLock);
|
---|
333 | BlockStoreNode* freeNode = mFreeList;
|
---|
334 | BlockStoreNode** prevp = &mFreeList;
|
---|
335 |
|
---|
336 | while (freeNode)
|
---|
337 | {
|
---|
338 | if (freeNode->bytes >= bytes)
|
---|
339 | {
|
---|
340 | // Found the best fit free block
|
---|
341 | block = freeNode->block;
|
---|
342 |
|
---|
343 | // Clear the free node
|
---|
344 | freeNode->block = nsnull;
|
---|
345 | freeNode->bytes = 0;
|
---|
346 |
|
---|
347 | // Remove free node from free list
|
---|
348 | *prevp = freeNode->next;
|
---|
349 |
|
---|
350 | // Add removed BlockStoreNode to not used list
|
---|
351 | freeNode->next = mNotUsedList;
|
---|
352 | mNotUsedList = freeNode;
|
---|
353 |
|
---|
354 | break;
|
---|
355 | }
|
---|
356 |
|
---|
357 | prevp = &(freeNode->next);
|
---|
358 | freeNode = freeNode->next;
|
---|
359 | }
|
---|
360 | return block;
|
---|
361 | }
|
---|
362 |
|
---|
363 | PRInt32
|
---|
364 | nsRecyclingAllocator::AddToFreeList(Block* block)
|
---|
365 | {
|
---|
366 | nsAutoLock lock(mLock);
|
---|
367 |
|
---|
368 | if (!mNotUsedList)
|
---|
369 | return PR_FALSE;
|
---|
370 |
|
---|
371 | // Pick a node from the not used list
|
---|
372 | BlockStoreNode *node = mNotUsedList;
|
---|
373 | mNotUsedList = mNotUsedList->next;
|
---|
374 |
|
---|
375 | // Initialize the node
|
---|
376 | node->bytes = block->bytes;
|
---|
377 | node->block = block;
|
---|
378 |
|
---|
379 | // Find the right spot in the sorted list.
|
---|
380 | BlockStoreNode* freeNode = mFreeList;
|
---|
381 | BlockStoreNode** prevp = &mFreeList;
|
---|
382 | while (freeNode)
|
---|
383 | {
|
---|
384 | if (freeNode->bytes >= block->bytes)
|
---|
385 | break;
|
---|
386 | prevp = &(freeNode->next);
|
---|
387 | freeNode = freeNode->next;
|
---|
388 | }
|
---|
389 |
|
---|
390 | // Needs to be inserted between *prevp and freeNode
|
---|
391 | *prevp = node;
|
---|
392 | node->next = freeNode;
|
---|
393 |
|
---|
394 | return PR_TRUE;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | // ----------------------------------------------------------------------
|
---|
399 | // Wrapping the recyling allocator with nsIMemory
|
---|
400 | // ----------------------------------------------------------------------
|
---|
401 |
|
---|
402 | // nsIMemory methods
|
---|
403 | NS_IMPL_THREADSAFE_ISUPPORTS2(nsRecyclingAllocatorImpl, nsIMemory, nsIRecyclingAllocator)
|
---|
404 |
|
---|
405 | NS_IMETHODIMP_(void *)
|
---|
406 | nsRecyclingAllocatorImpl::Alloc(PRSize size)
|
---|
407 | {
|
---|
408 | return nsRecyclingAllocatorImpl::Malloc(size, PR_FALSE);
|
---|
409 | }
|
---|
410 |
|
---|
411 | NS_IMETHODIMP_(void *)
|
---|
412 | nsRecyclingAllocatorImpl::Realloc(void *ptr, PRSize size)
|
---|
413 | {
|
---|
414 | // XXX Not yet implemented
|
---|
415 | return NULL;
|
---|
416 | }
|
---|
417 |
|
---|
418 | NS_IMETHODIMP_(void)
|
---|
419 | nsRecyclingAllocatorImpl::Free(void *ptr)
|
---|
420 | {
|
---|
421 | nsRecyclingAllocator::Free(ptr);
|
---|
422 | }
|
---|
423 |
|
---|
424 | NS_IMETHODIMP
|
---|
425 | nsRecyclingAllocatorImpl::Init(size_t nbuckets, size_t recycleAfter, const char *id)
|
---|
426 | {
|
---|
427 | return nsRecyclingAllocator::Init((PRUint32) nbuckets, (PRUint32) recycleAfter, id);
|
---|
428 | }
|
---|
429 |
|
---|
430 | NS_IMETHODIMP
|
---|
431 | nsRecyclingAllocatorImpl::HeapMinimize(PRBool immediate)
|
---|
432 | {
|
---|
433 | // XXX Not yet implemented
|
---|
434 | return NS_ERROR_NOT_IMPLEMENTED;
|
---|
435 | }
|
---|
436 |
|
---|
437 | NS_IMETHODIMP
|
---|
438 | nsRecyclingAllocatorImpl::IsLowMemory(PRBool *lowmemoryb_ptr)
|
---|
439 | {
|
---|
440 | // XXX Not yet implemented
|
---|
441 | return NS_ERROR_NOT_IMPLEMENTED;
|
---|
442 | }
|
---|
443 |
|
---|