1*404b540aSrobert // Allocator details.
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert //
31*404b540aSrobert // ISO C++ 14882:
32*404b540aSrobert //
33*404b540aSrobert 
34*404b540aSrobert #include <bits/c++config.h>
35*404b540aSrobert #include <cstdlib>
36*404b540aSrobert #include <ext/pool_allocator.h>
37*404b540aSrobert 
38*404b540aSrobert namespace
39*404b540aSrobert {
40*404b540aSrobert   __gnu_cxx::__mutex palloc_init_mutex;
41*404b540aSrobert } // anonymous namespace
42*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)43*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44*404b540aSrobert 
45*404b540aSrobert   // Definitions for __pool_alloc_base.
46*404b540aSrobert   __pool_alloc_base::_Obj* volatile*
47*404b540aSrobert   __pool_alloc_base::_M_get_free_list(size_t __bytes)
48*404b540aSrobert   {
49*404b540aSrobert     size_t __i = ((__bytes + (size_t)_S_align - 1) / (size_t)_S_align - 1);
50*404b540aSrobert     return _S_free_list + __i;
51*404b540aSrobert   }
52*404b540aSrobert 
53*404b540aSrobert   __mutex&
_M_get_mutex()54*404b540aSrobert   __pool_alloc_base::_M_get_mutex()
55*404b540aSrobert   { return palloc_init_mutex; }
56*404b540aSrobert 
57*404b540aSrobert   // Allocate memory in large chunks in order to avoid fragmenting the
58*404b540aSrobert   // heap too much.  Assume that __n is properly aligned.  We hold the
59*404b540aSrobert   // allocation lock.
60*404b540aSrobert   char*
_M_allocate_chunk(size_t __n,int & __nobjs)61*404b540aSrobert   __pool_alloc_base::_M_allocate_chunk(size_t __n, int& __nobjs)
62*404b540aSrobert   {
63*404b540aSrobert     char* __result;
64*404b540aSrobert     size_t __total_bytes = __n * __nobjs;
65*404b540aSrobert     size_t __bytes_left = _S_end_free - _S_start_free;
66*404b540aSrobert 
67*404b540aSrobert     if (__bytes_left >= __total_bytes)
68*404b540aSrobert       {
69*404b540aSrobert 	__result = _S_start_free;
70*404b540aSrobert 	_S_start_free += __total_bytes;
71*404b540aSrobert 	return __result ;
72*404b540aSrobert       }
73*404b540aSrobert     else if (__bytes_left >= __n)
74*404b540aSrobert       {
75*404b540aSrobert 	__nobjs = (int)(__bytes_left / __n);
76*404b540aSrobert 	__total_bytes = __n * __nobjs;
77*404b540aSrobert 	__result = _S_start_free;
78*404b540aSrobert 	_S_start_free += __total_bytes;
79*404b540aSrobert 	return __result;
80*404b540aSrobert       }
81*404b540aSrobert     else
82*404b540aSrobert       {
83*404b540aSrobert 	// Try to make use of the left-over piece.
84*404b540aSrobert 	if (__bytes_left > 0)
85*404b540aSrobert 	  {
86*404b540aSrobert 	    _Obj* volatile* __free_list = _M_get_free_list(__bytes_left);
87*404b540aSrobert 	    ((_Obj*)(void*)_S_start_free)->_M_free_list_link = *__free_list;
88*404b540aSrobert 	    *__free_list = (_Obj*)(void*)_S_start_free;
89*404b540aSrobert 	  }
90*404b540aSrobert 
91*404b540aSrobert 	size_t __bytes_to_get = (2 * __total_bytes
92*404b540aSrobert 				 + _M_round_up(_S_heap_size >> 4));
93*404b540aSrobert 	try
94*404b540aSrobert 	  {
95*404b540aSrobert 	    _S_start_free = static_cast<char*>(::operator new(__bytes_to_get));
96*404b540aSrobert 	  }
97*404b540aSrobert 	catch (...)
98*404b540aSrobert 	  {
99*404b540aSrobert 	    // Try to make do with what we have.  That can't hurt.  We
100*404b540aSrobert 	    // do not try smaller requests, since that tends to result
101*404b540aSrobert 	    // in disaster on multi-process machines.
102*404b540aSrobert 	    size_t __i = __n;
103*404b540aSrobert 	    for (; __i <= (size_t) _S_max_bytes; __i += (size_t) _S_align)
104*404b540aSrobert 	      {
105*404b540aSrobert 		_Obj* volatile* __free_list = _M_get_free_list(__i);
106*404b540aSrobert 		_Obj* __p = *__free_list;
107*404b540aSrobert 		if (__p != 0)
108*404b540aSrobert 		  {
109*404b540aSrobert 		    *__free_list = __p->_M_free_list_link;
110*404b540aSrobert 		    _S_start_free = (char*)__p;
111*404b540aSrobert 		    _S_end_free = _S_start_free + __i;
112*404b540aSrobert 		    return _M_allocate_chunk(__n, __nobjs);
113*404b540aSrobert 		    // Any leftover piece will eventually make it to the
114*404b540aSrobert 		    // right free list.
115*404b540aSrobert 		  }
116*404b540aSrobert 	      }
117*404b540aSrobert 	    // What we have wasn't enough.  Rethrow.
118*404b540aSrobert 	    _S_start_free = _S_end_free = 0;   // We have no chunk.
119*404b540aSrobert 	    __throw_exception_again;
120*404b540aSrobert 	  }
121*404b540aSrobert 	_S_heap_size += __bytes_to_get;
122*404b540aSrobert 	_S_end_free = _S_start_free + __bytes_to_get;
123*404b540aSrobert 	return _M_allocate_chunk(__n, __nobjs);
124*404b540aSrobert       }
125*404b540aSrobert   }
126*404b540aSrobert 
127*404b540aSrobert   // Returns an object of size __n, and optionally adds to "size
128*404b540aSrobert   // __n"'s free list.  We assume that __n is properly aligned.  We
129*404b540aSrobert   // hold the allocation lock.
130*404b540aSrobert   void*
_M_refill(size_t __n)131*404b540aSrobert   __pool_alloc_base::_M_refill(size_t __n)
132*404b540aSrobert   {
133*404b540aSrobert     int __nobjs = 20;
134*404b540aSrobert     char* __chunk = _M_allocate_chunk(__n, __nobjs);
135*404b540aSrobert     _Obj* volatile* __free_list;
136*404b540aSrobert     _Obj* __result;
137*404b540aSrobert     _Obj* __current_obj;
138*404b540aSrobert     _Obj* __next_obj;
139*404b540aSrobert 
140*404b540aSrobert     if (__nobjs == 1)
141*404b540aSrobert       return __chunk;
142*404b540aSrobert     __free_list = _M_get_free_list(__n);
143*404b540aSrobert 
144*404b540aSrobert     // Build free list in chunk.
145*404b540aSrobert     __result = (_Obj*)(void*)__chunk;
146*404b540aSrobert     *__free_list = __next_obj = (_Obj*)(void*)(__chunk + __n);
147*404b540aSrobert     for (int __i = 1; ; __i++)
148*404b540aSrobert       {
149*404b540aSrobert 	__current_obj = __next_obj;
150*404b540aSrobert 	__next_obj = (_Obj*)(void*)((char*)__next_obj + __n);
151*404b540aSrobert 	if (__nobjs - 1 == __i)
152*404b540aSrobert 	  {
153*404b540aSrobert 	    __current_obj->_M_free_list_link = 0;
154*404b540aSrobert 	    break;
155*404b540aSrobert 	  }
156*404b540aSrobert 	else
157*404b540aSrobert 	  __current_obj->_M_free_list_link = __next_obj;
158*404b540aSrobert       }
159*404b540aSrobert     return __result;
160*404b540aSrobert   }
161*404b540aSrobert 
162*404b540aSrobert   __pool_alloc_base::_Obj* volatile __pool_alloc_base::_S_free_list[_S_free_list_size];
163*404b540aSrobert 
164*404b540aSrobert   char* __pool_alloc_base::_S_start_free = 0;
165*404b540aSrobert 
166*404b540aSrobert   char* __pool_alloc_base::_S_end_free = 0;
167*404b540aSrobert 
168*404b540aSrobert   size_t __pool_alloc_base::_S_heap_size = 0;
169*404b540aSrobert 
170*404b540aSrobert   // Instantiations.
171*404b540aSrobert   template class __pool_alloc<char>;
172*404b540aSrobert   template class __pool_alloc<wchar_t>;
173*404b540aSrobert 
174*404b540aSrobert _GLIBCXX_END_NAMESPACE
175