1169691Skan// array allocator -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the
7169691Skan// terms of the GNU General Public License as published by the
8169691Skan// Free Software Foundation; either version 2, or (at your option)
9169691Skan// any later version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful,
12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169691Skan// GNU General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License along
17169691Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19169691Skan// USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free software
22169691Skan// library without restriction.  Specifically, if other files instantiate
23169691Skan// templates or use macros or inline functions from this file, or you compile
24169691Skan// this file and link it with other files to produce an executable, this
25169691Skan// file does not by itself cause the resulting executable to be covered by
26169691Skan// the GNU General Public License.  This exception does not however
27169691Skan// invalidate any other reasons why the executable file might be covered by
28169691Skan// the GNU General Public License.
29169691Skan
30169691Skan/** @file ext/array_allocator.h
31169691Skan *  This file is a GNU extension to the Standard C++ Library.
32169691Skan */
33169691Skan
34169691Skan#ifndef _ARRAY_ALLOCATOR_H
35169691Skan#define _ARRAY_ALLOCATOR_H 1
36169691Skan
37169691Skan#include <cstddef>
38169691Skan#include <new>
39169691Skan#include <bits/functexcept.h>
40169691Skan#include <tr1/array>
41169691Skan
42169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
43169691Skan
44169691Skan using std::size_t;
45169691Skan using std::ptrdiff_t;
46169691Skan
47169691Skan  /// @brief  Base class.
48169691Skan template<typename _Tp>
49169691Skan    class array_allocator_base
50169691Skan    {
51169691Skan    public:
52169691Skan      typedef size_t     	size_type;
53169691Skan      typedef ptrdiff_t  	difference_type;
54169691Skan      typedef _Tp*       	pointer;
55169691Skan      typedef const _Tp* 	const_pointer;
56169691Skan      typedef _Tp&       	reference;
57169691Skan      typedef const _Tp&	const_reference;
58169691Skan      typedef _Tp        	value_type;
59169691Skan
60169691Skan      pointer
61169691Skan      address(reference __x) const { return &__x; }
62169691Skan
63169691Skan      const_pointer
64169691Skan      address(const_reference __x) const { return &__x; }
65169691Skan
66169691Skan      void
67169691Skan      deallocate(pointer, size_type)
68169691Skan      {
69169691Skan	// Does nothing.
70169691Skan      }
71169691Skan
72169691Skan      size_type
73169691Skan      max_size() const throw()
74169691Skan      { return size_t(-1) / sizeof(_Tp); }
75169691Skan
76169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
77169691Skan      // 402. wrong new expression in [some_] allocator::construct
78169691Skan      void
79169691Skan      construct(pointer __p, const _Tp& __val)
80169691Skan      { ::new(__p) value_type(__val); }
81169691Skan
82169691Skan      void
83169691Skan      destroy(pointer __p) { __p->~_Tp(); }
84169691Skan    };
85169691Skan
86169691Skan  /**
87169691Skan   *  @brief  An allocator that uses previously allocated memory.
88169691Skan   *  This memory can be externally, globally, or otherwise allocated.
89169691Skan   */
90169691Skan  template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
91169691Skan    class array_allocator : public array_allocator_base<_Tp>
92169691Skan    {
93169691Skan    public:
94169691Skan      typedef size_t     	size_type;
95169691Skan      typedef ptrdiff_t  	difference_type;
96169691Skan      typedef _Tp*       	pointer;
97169691Skan      typedef const _Tp* 	const_pointer;
98169691Skan      typedef _Tp&       	reference;
99169691Skan      typedef const _Tp& 	const_reference;
100169691Skan      typedef _Tp        	value_type;
101169691Skan      typedef _Array		array_type;
102169691Skan
103169691Skan    private:
104169691Skan      array_type* 	_M_array;
105169691Skan      size_type 	_M_used;
106169691Skan
107169691Skan    public:
108169691Skan     template<typename _Tp1, typename _Array1 = _Array>
109169691Skan        struct rebind
110169691Skan        { typedef array_allocator<_Tp1, _Array1> other; };
111169691Skan
112169691Skan      array_allocator(array_type* __array = NULL) throw()
113169691Skan      : _M_array(__array), _M_used(size_type()) { }
114169691Skan
115169691Skan      array_allocator(const array_allocator& __o)  throw()
116169691Skan      : _M_array(__o._M_array), _M_used(__o._M_used) { }
117169691Skan
118169691Skan      template<typename _Tp1, typename _Array1>
119169691Skan        array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
120169691Skan	: _M_array(NULL), _M_used(size_type()) { }
121169691Skan
122169691Skan      ~array_allocator() throw() { }
123169691Skan
124169691Skan      pointer
125169691Skan      allocate(size_type __n, const void* = 0)
126169691Skan      {
127169691Skan	if (_M_array == 0 || _M_used + __n > _M_array->size())
128169691Skan	  std::__throw_bad_alloc();
129169691Skan	pointer __ret = _M_array->begin() + _M_used;
130169691Skan	_M_used += __n;
131169691Skan	return __ret;
132169691Skan      }
133169691Skan    };
134169691Skan
135169691Skan  template<typename _Tp, typename _Array>
136169691Skan    inline bool
137169691Skan    operator==(const array_allocator<_Tp, _Array>&,
138169691Skan	       const array_allocator<_Tp, _Array>&)
139169691Skan    { return true; }
140169691Skan
141169691Skan  template<typename _Tp, typename _Array>
142169691Skan    inline bool
143169691Skan    operator!=(const array_allocator<_Tp, _Array>&,
144169691Skan	       const array_allocator<_Tp, _Array>&)
145169691Skan    { return false; }
146169691Skan
147169691Skan_GLIBCXX_END_NAMESPACE
148169691Skan
149169691Skan#endif
150