home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kallocator.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-08  |  5.3 KB  |  141 lines

  1. /*
  2.     This file is part of the KDE libraries
  3.  
  4.     Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
  5.     Copyright (C) 2002 Michael Matz (matz@kde.org)
  6.               
  7.     This library is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU Library General Public
  9.     License as published by the Free Software Foundation; either
  10.     version 2 of the License, or (at your option) any later version.
  11.  
  12.     This library is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.     Library General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU Library General Public License
  18.     along with this library; see the file COPYING.LIB.  If not, write to
  19.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20.     Boston, MA 02110-1301, USA.
  21. */
  22. //----------------------------------------------------------------------------
  23. //
  24. // KDE Memory Allocator
  25.  
  26. #ifndef KALLOCATOR_H
  27. #define KALLOCATOR_H
  28.  
  29. #include <qvaluelist.h>
  30. #include "kdelibs_export.h"
  31.  
  32. class KZoneAllocatorPrivate;
  33.  
  34.  
  35. /**
  36.  * Memory allocator for large groups of small objects.
  37.  * This should be used for large groups of objects that are created and
  38.  * destroyed together. When used carefully for this purpose it is faster
  39.  * and more memory efficient than malloc.  Additionally to a usual obstack
  40.  * like allocator you can also free the objects individually.  Because it
  41.  * does no compaction it still is faster then malloc()/free().  Depending
  42.  * on the exact usage pattern that might come at the expense of some
  43.  * memory though.
  44.  * @author Waldo Bastian <bastian@kde.org>, Michael Matz <matz@kde.org>
  45.  */
  46. class KDECORE_EXPORT KZoneAllocator
  47. {
  48. public:
  49.     /**
  50.      * Creates a KZoneAllocator object.
  51.      * @param _blockSize Size in bytes of the blocks requested from malloc.
  52.      */
  53.     KZoneAllocator(unsigned long _blockSize = 8*1024);
  54.  
  55.     /**
  56.      * Destructs the ZoneAllocator and free all memory allocated by it.
  57.      */
  58.     ~KZoneAllocator();
  59.  
  60.     /**
  61.      * Allocates a memory block.
  62.      * @param _size Size in bytes of the memory block. Memory is aligned to
  63.      * the size of a pointer.
  64.      */
  65.     void* allocate(size_t _size);
  66.  
  67.     /**
  68.      * Gives back a block returned by allocate() to the zone
  69.      * allocator, and possibly deallocates the block holding it (when it's
  70.      * empty).  The first deallocate() after many allocate() calls
  71.      * (or the first at all) builds an internal data structure for speeding
  72.      * up deallocation.  The consistency of that structure is maintained
  73.      * from then on (by allocate() and deallocate()) unless many
  74.      * more objects are allocated without any intervening deallocation, in
  75.      * which case it's thrown away and rebuilt at the next deallocate().
  76.      *
  77.      * The effect of this is, that such initial deallocate() calls take
  78.      * more time then the normal calls, and that after this list is built, i.e.
  79.      * generally if deallocate() is used at all, also allocate() is a
  80.      * little bit slower.  This means, that if you want to squeeze out the last
  81.      * bit performance you would want to use KZoneAllocator as an obstack, i.e.
  82.      * just use the functions allocate() and free_since().  All the
  83.      * remaining memory is returned to the system if the zone allocator
  84.      * is destroyed.
  85.      * @param ptr Pointer as returned by allocate().
  86.      */
  87.     void deallocate(void *ptr);
  88.  
  89.     /**
  90.      * Deallocate many objects at once.
  91.      * free_since() deallocates all objects allocated after @p ptr, 
  92.      * @em including @p ptr itself.
  93.      *
  94.      * The intended use is something along the lines of:
  95.      * \code
  96.      * KZoneAllocator alloc(8192);
  97.      * void *remember_me = alloc.allocate(0);
  98.      * for (int i = 0; i < 1000; i++)
  99.      *   do_something_with (alloc.allocate(12));
  100.      * alloc.free_since (remember_me);
  101.      * \endcode
  102.      * Note, that we don't need to remember all the pointers to the 12-byte
  103.      * objects for freeing them.  The free_since() does deallocate them
  104.      * all at once.
  105.      * @param ptr Pointer as returned by allocate().  It acts like
  106.      * a kind of mark of a certain position in the stack of all objects,
  107.      * off which you can throw away everything above that mark.
  108.      */
  109.     void free_since(void *ptr);
  110.  
  111. protected:
  112.     /** A single chunk of memory from the heap. @internal */
  113.     class MemBlock;
  114.     /**< A list of chunks. @internal */
  115.     typedef QValueList<MemBlock *> MemList;
  116.     void addBlock(MemBlock *b);
  117.     void delBlock(MemBlock *b);
  118.     void insertHash(MemBlock *b);
  119.     void initHash();
  120.     /** One block is 'current' to satisfy requests. @internal */
  121.     MemBlock *currentBlock; 
  122.     /** Store block size from constructor. @internal */
  123.     unsigned long blockSize; 
  124.     /** Store offset into current block; size-offset is free. @internal */
  125.     unsigned long blockOffset;
  126.     /** base-2 log of the block size. @internal */
  127.     unsigned int log2;
  128.     /** Count total number of allocated blocks. @internal */
  129.     unsigned int num_blocks;
  130.     /** Collection of lists of blocks, for lookups. @internal */
  131.     MemList **hashList;
  132.     /** Count of hashes. @internal */
  133.     unsigned int hashSize;
  134.     /** Flag the hashes as in need of reorganization. @internal */
  135.     bool hashDirty;
  136. private:
  137.     KZoneAllocatorPrivate *d;
  138. };
  139.  
  140. #endif
  141.