summaryrefslogtreecommitdiffstats
path: root/src/libs/xpcom18a4/xpcom/ds/nsRecyclingAllocator.cpp
blob: 241f795c4b0dee3c9a95e62fd7ec5adecb23bd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001, 2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *	Suresh Duddi <dp@netscape.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/*
 * nsRecyclingAllocator
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "nsRecyclingAllocator.h"
#include "nsIMemory.h"
#include "nsAutoLock.h"
#include "prprf.h"
#include "nsITimer.h"
#ifdef VBOX_USE_IPRT_IN_XPCOM
# include <iprt/mem.h>
#endif

#define NS_SEC_TO_MS(s) ((s) * 1000)

void
nsRecyclingAllocator::nsRecycleTimerCallback(nsITimer *aTimer, void *aClosure)
{
    nsRecyclingAllocator *obj = (nsRecyclingAllocator *) aClosure;
    if (!obj->mTouched)
    {
        if (obj->mFreeList)
            obj->FreeUnusedBuckets();

        // If we are holding no more memory, there is no need for the timer.
        // We will revive the timer on the next allocation.
        // XXX Unfortunately there is no way to Cancel and restart the same timer.
        // XXX So we pretty much kill it and create a new one later.
        if (!obj->mFreeList && obj->mRecycleTimer)
        {
            obj->mRecycleTimer->Cancel();
            NS_RELEASE(obj->mRecycleTimer);
        }
    }
    else
    {
        // Clear touched so the next time the timer fires we can test whether
        // the allocator was used or not.
        obj->Untouch();
    }
}


nsRecyclingAllocator::nsRecyclingAllocator(PRUint32 nbucket, PRUint32 recycleAfter, const char *id) :
    mMaxBlocks(nbucket), mBlocks(nsnull), mFreeList(nsnull), mNotUsedList(nsnull),
    mRecycleTimer(nsnull), mRecycleAfter(recycleAfter), mTouched(0), mId(id)
#ifdef DEBUG
     ,mNAllocated(0)
#endif
{
    NS_ASSERTION(mMaxBlocks <= NS_MAX_BLOCKS, "Too many blocks. This will affect the allocator's performance.");

    mLock = PR_NewLock();
    NS_ASSERTION(mLock, "Recycling allocator cannot get lock");

    Init(nbucket, recycleAfter, id);
}

nsresult
nsRecyclingAllocator::Init(PRUint32 nbucket, PRUint32 recycleAfter, const char *id)
{
    nsAutoLock lock(mLock);

    // Free all memory held, if any
    while(mFreeList)
    {
#ifdef VBOX_USE_IPRT_IN_XPCOM
        RTMemFree(mFreeList->block);
#else
        free(mFreeList->block);
#endif
        mFreeList = mFreeList->next;
    }
    mFreeList = nsnull;

    if (mBlocks)
        delete [] mBlocks;

    // Reinitialize everything
    mMaxBlocks = nbucket;
    if (nbucket)
    {
        // Create memory for our bookkeeping
        mBlocks = new BlockStoreNode[mMaxBlocks];
        if (!mBlocks)
            return NS_ERROR_OUT_OF_MEMORY;
        // Link them together
        mNotUsedList = mBlocks;
        for (PRUint32 i=0; i < mMaxBlocks-1; i++)
            mBlocks[i].next = &(mBlocks[i+1]);
    }

    mRecycleAfter = recycleAfter;
    mId = id;

    return NS_OK;
}

nsRecyclingAllocator::~nsRecyclingAllocator()
{
    // Cancel and destroy recycle timer
    if (mRecycleTimer)
    {
        mRecycleTimer->Cancel();
        NS_RELEASE(mRecycleTimer);
    }

    // Free all memory held, if any
    while(mFreeList)
    {
#ifdef VBOX_USE_IPRT_IN_XPCOM
        RTMemFree(mFreeList->block);
#else
        free(mFreeList->block);
#endif
        mFreeList = mFreeList->next;
    }
    mFreeList = nsnull;

    if (mBlocks)
        delete [] mBlocks;

    if (mLock)
    {
        PR_DestroyLock(mLock);
        mLock = nsnull;
    }
}

// Allocation and free routines
void*
nsRecyclingAllocator::Malloc(PRSize bytes, PRBool zeroit)
{
    // Mark that we are using. This will prevent any
    // timer based release of unused memory.
    Touch();

    Block* freeBlock = FindFreeBlock(bytes);
    if (freeBlock)
    {
        void *data = DATA(freeBlock);

        if (zeroit)
            memset(data, 0, bytes);
        return data;
    }

    // We need to do an allocation
    // Add 4 bytes to what we allocate to hold the bucket index
    PRSize allocBytes = bytes + NS_ALLOCATOR_OVERHEAD_BYTES;

    // We dont have that memory already. Allocate.
#ifdef VBOX_USE_IPRT_IN_XPCOM
    Block *ptr = (Block *) (zeroit ? RTMemAllocZ(allocBytes) : RTMemAlloc(allocBytes));
#else
    Block *ptr = (Block *) (zeroit ? calloc(1, allocBytes) : malloc(allocBytes));
#endif

    // Deal with no memory situation
    if (!ptr)
        return ptr;

    // This is the first allocation we are holding.
    // Setup timer for releasing memory
    // If this fails, then we wont have a timer to release unused
    // memory. We can live with that. Also, the next allocation
    // will try again to set the timer.
    if (mRecycleAfter && !mRecycleTimer)
    {
        // known only to stuff in xpcom.
        extern nsresult NS_NewTimer(nsITimer* *aResult, nsTimerCallbackFunc aCallback, void *aClosure,
                                    PRUint32 aDelay, PRUint32 aType);

        (void) NS_NewTimer(&mRecycleTimer, nsRecycleTimerCallback, this,
                           NS_SEC_TO_MS(mRecycleAfter),
                           nsITimer::TYPE_REPEATING_SLACK);
        NS_ASSERTION(mRecycleTimer, "nsRecyclingAllocator: Creating timer failed.\n");
    }

#ifdef DEBUG
    mNAllocated++;
#endif

    // Store size and return data portion
    ptr->bytes = bytes;
    return DATA(ptr);
}

void
nsRecyclingAllocator::Free(void *ptr)
{
    // Mark that we are using the allocator. This will prevent any
    // timer based release of unused memory.
    Touch();

    Block* block = DATA_TO_BLOCK(ptr);

    if (!AddToFreeList(block))
    {
        // We are holding more than max. Failover to free
#ifdef DEBUG_dp
        char buf[1024];
        // Warn if we are failing over to malloc/free and not storing it
        // This says we have a misdesigned memory pool. The intent was
        // once the pool was full, we would never fail over to calloc.
        PR_snprintf(buf, sizeof(buf), "nsRecyclingAllocator(%s) FAILOVER 0x%p (%d) - %d allocations, %d max\n",
                    mId, (char *)ptr, block->bytes, mNAllocated, mMaxBlocks);
        NS_WARNING(buf);
        mNAllocated--;
#endif
#ifdef VBOX_USE_IPRT_IN_XPCOM
        RTMemFree(block);
#else
        free(block);
#endif
    }
}

/* FreeUnusedBuckets
 *
 * Frees any bucket memory that isn't in use
 */

void
nsRecyclingAllocator::FreeUnusedBuckets()
{
#ifdef DEBUG_dp
    printf("DEBUG: nsRecyclingAllocator(%s) FreeUnusedBuckets: ", mId);
#endif
    nsAutoLock lock(mLock);

    // We will run through the freelist and free all blocks
    BlockStoreNode* node = mFreeList;
    while (node)
    {
        // Free the allocated block
#ifdef VBOX_USE_IPRT_IN_XPCOM
        RTMemFree(node->block);
#else
        free(node->block);
#endif

#ifdef DEBUG_dp
        printf("%d ", node->bytes);
#endif
        // Clear Node
        node->block = nsnull;
        node->bytes = 0;
        node = node->next;
    }

    // remake the lists
    mNotUsedList = mBlocks;
    for (PRUint32 i=0; i < mMaxBlocks-1; i++)
        mBlocks[i].next = &(mBlocks[i+1]);
    mBlocks[mMaxBlocks-1].next = nsnull;
    mFreeList = nsnull;

#ifdef DEBUG
    mNAllocated = 0;
#endif
#ifdef DEBUG_dp
    printf("\n");
#endif
}

nsRecyclingAllocator::Block*
nsRecyclingAllocator::FindFreeBlock(PRSize bytes)
{
    // We dont enter lock for this check. This is intentional.
    // Here is my logic: we are checking if (!mFreeList). Doing this check
    // without locking can lead to unpredictable results. YES. But the effect
    // of the unpredictedness are ok. here is why:
    //
    // a) if the check returned NULL when there is stuff in freelist
    //    We would just end up reallocating.
    //
    // b) if the check returned nonNULL when our freelist is empty
    //    This is the more likely and dangerous case. The code for
    //    FindFreeBlock() will enter lock, while (null) and return null.
    //
    // The reason why I chose to not enter lock for this check was that when
    // the allocator is full, we dont want to impose any more overhead than
    // we already are for failing over to malloc/free.

    if (!mFreeList)
        return NULL;

    Block *block = nsnull;

    nsAutoLock lock(mLock);
    BlockStoreNode* freeNode = mFreeList;
    BlockStoreNode** prevp = &mFreeList;

    while (freeNode)
    {
        if (freeNode->bytes >= bytes)
        {
            // Found the best fit free block
            block = freeNode->block;

            // Clear the free node
            freeNode->block = nsnull;
            freeNode->bytes = 0;

            // Remove free node from free list
            *prevp = freeNode->next;

            // Add removed BlockStoreNode to not used list
            freeNode->next = mNotUsedList;
            mNotUsedList = freeNode;

            break;
        }

        prevp = &(freeNode->next);
        freeNode = freeNode->next;
    }
    return block;
}

PRInt32
nsRecyclingAllocator::AddToFreeList(Block* block)
{
    nsAutoLock lock(mLock);

    if (!mNotUsedList)
        return PR_FALSE;

    // Pick a node from the not used list
    BlockStoreNode *node = mNotUsedList;
    mNotUsedList = mNotUsedList->next;

    // Initialize the node
    node->bytes = block->bytes;
    node->block = block;

    // Find the right spot in the sorted list.
    BlockStoreNode* freeNode = mFreeList;
    BlockStoreNode** prevp = &mFreeList;
    while (freeNode)
    {
        if (freeNode->bytes >= block->bytes)
            break;
        prevp = &(freeNode->next);
        freeNode = freeNode->next;
    }

    // Needs to be inserted between *prevp and freeNode
    *prevp = node;
    node->next = freeNode;

    return PR_TRUE;
}


// ----------------------------------------------------------------------
// Wrapping the recyling allocator with nsIMemory
// ----------------------------------------------------------------------

// nsIMemory methods
NS_IMPL_THREADSAFE_ISUPPORTS2(nsRecyclingAllocatorImpl, nsIMemory, nsIRecyclingAllocator)

NS_IMETHODIMP_(void *)
nsRecyclingAllocatorImpl::Alloc(PRSize size)
{
    return nsRecyclingAllocatorImpl::Malloc(size, PR_FALSE);
}

NS_IMETHODIMP_(void *)
nsRecyclingAllocatorImpl::Realloc(void *ptr, PRSize size)
{
    // XXX Not yet implemented
    return NULL;
}

NS_IMETHODIMP_(void)
nsRecyclingAllocatorImpl::Free(void *ptr)
{
    nsRecyclingAllocator::Free(ptr);
}

NS_IMETHODIMP
nsRecyclingAllocatorImpl::Init(size_t nbuckets, size_t recycleAfter, const char *id)
{
    return nsRecyclingAllocator::Init((PRUint32) nbuckets, (PRUint32) recycleAfter, id);
}

NS_IMETHODIMP
nsRecyclingAllocatorImpl::HeapMinimize(PRBool immediate)
{
    // XXX Not yet implemented
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsRecyclingAllocatorImpl::IsLowMemory(PRBool *lowmemoryb_ptr)
{
    // XXX Not yet implemented
    return NS_ERROR_NOT_IMPLEMENTED;
}