Back to index

lightning-sunbird  0.9+nobinonly
nsZlibAllocator.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* 
00039  * Allocator for zlib optimized for mozilla's use
00040  *
00041  */
00042 
00043 #define NBUCKETS 6
00044 #define BY4ALLOC_ITEMS 320
00045 
00046 class nsZlibAllocator;
00047 extern nsZlibAllocator *gZlibAllocator;
00048 
00049 class nsZlibAllocator {
00050  public:
00051   struct nsMemBucket {
00052     void *ptr;
00053     PRUint32 size;
00054 
00055     // Is this bucket available ?
00056     // 1 : free
00057     // 0 or negative : in use
00058     // This is an int because we use atomic inc/dec to operate on it
00059     PRInt32 available;
00060   };
00061 
00062   nsMemBucket mMemBucket[NBUCKETS];
00063 
00064   nsZlibAllocator() {
00065     memset(mMemBucket, 0, sizeof(mMemBucket));
00066     for (PRUint32 i = 0; i < NBUCKETS; i++)
00067       mMemBucket[i].available = 1;
00068     return;
00069   }
00070 
00071   ~nsZlibAllocator() {
00072     for (PRUint32 i = 0; i < NBUCKETS; i++)
00073     {
00074       PRBool claimed = Claim(i);
00075       
00076       // ASSERT that we claimed the bucked. If we cannot, then the bucket is in use.
00077       // We dont attempt to free this ptr.
00078       // This will most likely cause a leak of this memory.
00079       PR_ASSERT(claimed);
00080       
00081       // Free bucket memory if not in use
00082       if (claimed && mMemBucket[i].ptr)
00083         free(mMemBucket[i].ptr);
00084     }
00085   }
00086 
00087   // zlib allocators
00088   void* zAlloc(PRUint32 items, PRUint32 size);
00089   void  zFree(void *ptr);
00090 
00091   // Bucket handling.
00092   PRBool Claim(PRUint32 i) {
00093     PRBool claimed = (PR_AtomicDecrement(&mMemBucket[i].available) == 0);
00094     // Undo the claim, if claim failed
00095     if (!claimed)
00096       Unclaim(i);
00097 
00098     return claimed;
00099   }
00100 
00101   void Unclaim(PRUint32 i) { PR_AtomicIncrement(&mMemBucket[i].available); }
00102 };
00103