Home | History | Annotate | Download | only in ext
      1 // Allocator that wraps "C" malloc -*- C++ -*-
      2 
      3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
      4 // Free Software Foundation, Inc.
      5 //
      6 // This file is part of the GNU ISO C++ Library.  This library is free
      7 // software; you can redistribute it and/or modify it under the
      8 // terms of the GNU General Public License as published by the
      9 // Free Software Foundation; either version 3, or (at your option)
     10 // 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
     15 // GNU General Public License for more details.
     16 
     17 // Under Section 7 of GPL version 3, you are granted additional
     18 // permissions described in the GCC Runtime Library Exception, version
     19 // 3.1, as published by the Free Software Foundation.
     20 
     21 // You should have received a copy of the GNU General Public License and
     22 // a copy of the GCC Runtime Library Exception along with this program;
     23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 // <http://www.gnu.org/licenses/>.
     25 
     26 /** @file ext/malloc_allocator.h
     27  *  This file is a GNU extension to the Standard C++ Library.
     28  */
     29 
     30 #ifndef _MALLOC_ALLOCATOR_H
     31 #define _MALLOC_ALLOCATOR_H 1
     32 
     33 #include <cstdlib>
     34 #include <new>
     35 #include <bits/functexcept.h>
     36 #include <bits/move.h>
     37 
     38 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     39 
     40   using std::size_t;
     41   using std::ptrdiff_t;
     42 
     43   /**
     44    *  @brief  An allocator that uses malloc.
     45    *  @ingroup allocators
     46    *
     47    *  This is precisely the allocator defined in the C++ Standard.
     48    *    - all allocation calls malloc
     49    *    - all deallocation calls free
     50    */
     51   template<typename _Tp>
     52     class malloc_allocator
     53     {
     54     public:
     55       typedef size_t     size_type;
     56       typedef ptrdiff_t  difference_type;
     57       typedef _Tp*       pointer;
     58       typedef const _Tp* const_pointer;
     59       typedef _Tp&       reference;
     60       typedef const _Tp& const_reference;
     61       typedef _Tp        value_type;
     62 
     63       template<typename _Tp1>
     64         struct rebind
     65         { typedef malloc_allocator<_Tp1> other; };
     66 
     67       malloc_allocator() throw() { }
     68 
     69       malloc_allocator(const malloc_allocator&) throw() { }
     70 
     71       template<typename _Tp1>
     72         malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
     73 
     74       ~malloc_allocator() throw() { }
     75 
     76       pointer
     77       address(reference __x) const { return &__x; }
     78 
     79       const_pointer
     80       address(const_reference __x) const { return &__x; }
     81 
     82       // NB: __n is permitted to be 0.  The C++ standard says nothing
     83       // about what the return value is when __n == 0.
     84       pointer
     85       allocate(size_type __n, const void* = 0)
     86       {
     87 	if (__builtin_expect(__n > this->max_size(), false))
     88 	  std::__throw_bad_alloc();
     89 
     90 	pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
     91 	if (!__ret)
     92 	  std::__throw_bad_alloc();
     93 	return __ret;
     94       }
     95 
     96       // __p is not permitted to be a null pointer.
     97       void
     98       deallocate(pointer __p, size_type)
     99       { std::free(static_cast<void*>(__p)); }
    100 
    101       size_type
    102       max_size() const throw()
    103       { return size_t(-1) / sizeof(_Tp); }
    104 
    105       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    106       // 402. wrong new expression in [some_] allocator::construct
    107       void
    108       construct(pointer __p, const _Tp& __val)
    109       { ::new((void *)__p) value_type(__val); }
    110 
    111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    112       template<typename... _Args>
    113         void
    114         construct(pointer __p, _Args&&... __args)
    115 	{ ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
    116 #endif
    117 
    118       void
    119       destroy(pointer __p) { __p->~_Tp(); }
    120     };
    121 
    122   template<typename _Tp>
    123     inline bool
    124     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
    125     { return true; }
    126 
    127   template<typename _Tp>
    128     inline bool
    129     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
    130     { return false; }
    131 
    132 _GLIBCXX_END_NAMESPACE
    133 
    134 #endif
    135