Back to index

lightning-sunbird  0.9+nobinonly
nsFixedSizeAllocator.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Chris Waterson <waterson@netscape.com
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040 
00041   Implementation for nsFixedSizeAllocator
00042 
00043 */
00044 
00045 #include "nsCRT.h"
00046 #include "nsFixedSizeAllocator.h"
00047 
00048 nsFixedSizeAllocator::Bucket *
00049 nsFixedSizeAllocator::AddBucket(size_t aSize)
00050 {
00051     void* p;
00052     PL_ARENA_ALLOCATE(p, &mPool, sizeof(Bucket));
00053     if (! p)
00054         return nsnull;
00055 
00056     Bucket* bucket = NS_STATIC_CAST(Bucket*, p);
00057     bucket->mSize  = aSize;
00058     bucket->mFirst = nsnull;
00059     bucket->mNext  = mBuckets;
00060 
00061     mBuckets = bucket;
00062     return bucket;
00063 }
00064 
00065 nsresult
00066 nsFixedSizeAllocator::Init(const char* aName,
00067                            const size_t* aBucketSizes,
00068                            PRInt32 aNumBuckets,
00069                            PRInt32 aInitialSize,
00070                            PRInt32 aAlign)
00071 {
00072     NS_PRECONDITION(aNumBuckets > 0, "no buckets");
00073     if (aNumBuckets <= 0)
00074         return NS_ERROR_INVALID_ARG;
00075 
00076     // Blow away the old pool if we're being re-initialized.
00077     if (mBuckets)
00078         PL_FinishArenaPool(&mPool);
00079 
00080     PRInt32 bucketspace = aNumBuckets * sizeof(Bucket);
00081     PL_InitArenaPool(&mPool, aName, bucketspace + aInitialSize, aAlign);
00082 
00083     mBuckets = nsnull;
00084     for (PRInt32 i = 0; i < aNumBuckets; ++i)
00085         AddBucket(aBucketSizes[i]);
00086 
00087     return NS_OK;
00088 }
00089 
00090 nsFixedSizeAllocator::Bucket *
00091 nsFixedSizeAllocator::FindBucket(size_t aSize)
00092 {
00093     Bucket** link = &mBuckets;
00094     Bucket* bucket;
00095 
00096     while ((bucket = *link) != nsnull) {
00097         if (aSize == bucket->mSize) {
00098             // Promote to the head of the list, under the assumption
00099             // that we'll allocate same-sized object contemporaneously.
00100             *link = bucket->mNext;
00101             bucket->mNext = mBuckets;
00102             mBuckets = bucket;
00103             return bucket;
00104         }
00105 
00106         link = &bucket->mNext;
00107     }
00108     return nsnull;
00109 }
00110 
00111 void*
00112 nsFixedSizeAllocator::Alloc(size_t aSize)
00113 {
00114     Bucket* bucket = FindBucket(aSize);
00115     if (! bucket) {
00116         // Oops, we don't carry that size. Let's fix that.
00117         bucket = AddBucket(aSize);
00118         if (! bucket)
00119             return nsnull;
00120     }
00121 
00122     void* next;
00123     if (bucket->mFirst) {
00124         next = bucket->mFirst;
00125         bucket->mFirst = bucket->mFirst->mNext;
00126     }
00127     else {
00128         PL_ARENA_ALLOCATE(next, &mPool, aSize);
00129         if (!next)
00130             return nsnull;
00131     }
00132 
00133 #ifdef DEBUG
00134     memset(next, 0xc8, aSize);
00135 #endif
00136 
00137     return next;
00138 }
00139 
00140 void
00141 nsFixedSizeAllocator::Free(void* aPtr, size_t aSize)
00142 {
00143     FreeEntry* entry = NS_REINTERPRET_CAST(FreeEntry*, aPtr);
00144     Bucket* bucket = FindBucket(aSize);
00145 
00146 #ifdef DEBUG
00147     NS_ASSERTION(bucket && bucket->mSize == aSize, "ack! corruption! bucket->mSize != aSize!");
00148     memset(aPtr, 0xd8, bucket->mSize);
00149 #endif
00150 
00151     entry->mNext = bucket->mFirst;
00152     bucket->mFirst = entry;
00153 }