Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
mempool_allocator.h
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* -*-Mode: c++;-*- (Tell emacs to use c++ mode) */
00037 
00038 #ifndef mempool_allocator_INCLUDED
00039 #define mempool_allocator_INCLUDED
00040 
00041 #ifndef mempool_INCLUDED
00042 #include "mempool.h"
00043 #endif
00044 
00045 template <class T>
00046 class mempool_allocator {
00047 private:
00048   MEM_POOL* pool;
00049   template <class U> friend class mempool_allocator;
00050 
00051 public:
00052   typedef size_t    size_type;
00053   typedef ptrdiff_t difference_type;
00054   typedef T*        pointer;
00055   typedef const T*  const_pointer;
00056   typedef T&        reference;
00057   typedef const T&  const_reference;
00058   typedef T         value_type;
00059 
00060   template <class U> struct rebind {
00061     typedef mempool_allocator<U> other;
00062   };
00063 
00064   mempool_allocator() : pool(Malloc_Mem_Pool) {} // The default value.
00065   mempool_allocator(MEM_POOL* p) : pool(p) {}
00066   mempool_allocator(const mempool_allocator& a) : pool(a.pool) {}
00067   template <class U> mempool_allocator(const mempool_allocator<U>& a)
00068     : pool(a.pool) {}
00069   ~mempool_allocator() {}
00070 
00071   pointer address(reference x) const { return &x; }
00072   const_pointer address(const_reference x) const { return &x; }
00073 
00074 
00075 // Solaris CC workaround
00076 // There are two important changes, one to allocate(), the other to deallocate().
00077 // First, the first argument of deallocate() was changed to void*;
00078 // second, the return type of allocate() was changed to void*.
00079 // Both change are according to the default Allocator in <memory>
00080 
00081 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00082   void*   allocate(size_type n, const void* = 0)
00083     { return n != 0 ? TYPE_MEM_POOL_ALLOC_N(T, pool, n) : pointer(0); }
00084 #else
00085   pointer allocate(size_type n, const void* = 0) 
00086     { return n != 0 ? TYPE_MEM_POOL_ALLOC_N(T, pool, n) : pointer(0); }
00087 #endif
00088 
00089 
00090 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00091   void deallocate(void*  p, size_type) {
00092     if (p)
00093       MEM_POOL_FREE(pool, p);
00094   }
00095 #else
00096   void deallocate(pointer p, size_type ) {
00097     if (p)
00098       MEM_POOL_FREE(pool, p);
00099   }
00100 #endif
00101 
00102 
00103 // Solaris CC workaround
00104 // when compiling fb_whirl.cxx, the compiler complains "Error: Cstd/./memory line 490: Error: 
00105 // Too many arguments in call to "mempool_allocator<FB_Info_Switch>::max_size() const"".
00106 // It seems that alloctor_interface<> call mempool_allocator<>::max_size() with a size_t 
00107 // argument. So overloaded this function.
00108 
00109 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00110   size_type max_size(size_t) const { return size_t(-1) / sizeof(T); }
00111 #endif
00112 
00113   size_type max_size() const { return size_t(-1) / sizeof(T); }
00114 
00115   void construct(pointer p, const T& val) { new(p) T(val); }
00116   void destroy(pointer p) { p->~T(); }
00117 };
00118 
00119 template<>
00120 class mempool_allocator<void> {
00121 public:
00122   typedef size_t      size_type;
00123   typedef ptrdiff_t   difference_type;
00124   typedef void*       pointer;
00125   typedef const void* const_pointer;
00126   typedef void        value_type;
00127 
00128   template <class U> struct rebind {
00129     typedef mempool_allocator<U> other;
00130   };
00131 };
00132 #endif //  mempool_allocator_INCLUDED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines