oct-alloc.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_oct_alloc_h)
00024 #define octave_oct_alloc_h 1
00025 
00026 #include <cstddef>
00027 
00028 class
00029 OCTAVE_API
00030 octave_allocator
00031 {
00032 public:
00033 
00034   octave_allocator (size_t item_sz, int grow_sz = 256)
00035     : head (0), grow_size (grow_sz),
00036       item_size (item_sz > sizeof (link *) ? item_sz : sizeof (link *))
00037   { }
00038 
00039   // Get an object from the free list, possibly increasing the size of
00040   // the free list.
00041   void *alloc (size_t size);
00042 
00043   // Put objects back on the free list.
00044   void free (void *p, size_t size);
00045 
00046 private:
00047 
00048   // Structure for internal free list management.
00049   struct link { link *next; };
00050 
00051   // Front of the free list.
00052   link *head;
00053 
00054   // How many objects to get each time we call the global operator new.
00055   int grow_size;
00056 
00057   // The size of each item on the list (or, if that is smaller than
00058   // the size of list*, the size of list*.
00059   size_t item_size;
00060 
00061   // How to grow the free list.
00062   bool grow (void);
00063 };
00064 
00065 #if defined (HAVE_PLACEMENT_DELETE)
00066 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00067     void operator delete (void *p, void *) \
00068       { ::operator delete (p, static_cast<void*> (0)); }
00069 #else
00070 #define DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00071     void operator delete (void *p, void *) \
00072       { ::operator delete (p); }
00073 #endif
00074 
00075 #if defined (USE_OCTAVE_ALLOCATOR)
00076 
00077 #define DECLARE_OCTAVE_ALLOCATOR \
00078   public: \
00079     void *operator new (size_t size, void *p) \
00080       { return ::operator new (size, p); } \
00081     DECLARE_OCTAVE_ALLOCATOR_PLACEMENT_DELETE \
00082     void *operator new (size_t size) { return allocator.alloc (size); } \
00083     void operator delete (void *p, size_t size) { allocator.free (p, size); } \
00084   private: \
00085     static octave_allocator allocator;
00086 
00087 #define DEFINE_OCTAVE_ALLOCATOR(t) \
00088   octave_allocator t::allocator (sizeof (t))
00089 
00090 #define DEFINE_OCTAVE_ALLOCATOR2(t, s) \
00091   octave_allocator t::allocator (sizeof (t), s)
00092 
00093 #else
00094 
00095 #define DECLARE_OCTAVE_ALLOCATOR
00096 #define DEFINE_OCTAVE_ALLOCATOR(t)
00097 #define DEFINE_OCTAVE_ALLOCATOR2(t, s)
00098 
00099 #endif
00100 
00101 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines