1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1996-1997
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
43 /** @file ext/pool_allocator.h
44 * This file is a GNU extension to the Standard C++ Library.
45 * You should only include this header if you are using GCC 3 or later.
47 #ifndef _POOL_ALLOCATOR_H
48 #define _POOL_ALLOCATOR_H 1
50 #include <bits/c++config.h>
52 #include <bits/functexcept.h>
53 #include <bits/stl_threads.h>
54 #include <bits/atomicity.h>
58 using std::__throw_bad_alloc;
62 * Default node allocator. "SGI" style. Uses various allocators to
63 * fulfill underlying requests (and makes as few requests as possible
64 * when in default high-speed pool mode).
66 * Important implementation properties:
67 * 0. If globally mandated, then allocate objects from new
68 * 1. If the clients request an object of size > _S_max_bytes, the resulting
69 * object will be obtained directly from new
70 * 2. In all other cases, we allocate an object of size exactly
71 * _S_round_up(requested_size). Thus the client has enough size
72 * information that we can return the object to the proper free list
73 * without permanently losing part of the object.
75 * The first template parameter specifies whether more than one thread may
76 * use this allocator. It is safe to allocate an object from one instance
77 * of a default_alloc and deallocate it with another one. This effectively
78 * transfers its ownership to the second one. This may have undesirable
79 * effects on reference locality.
81 * The second parameter is unused and serves only to allow the
82 * creation of multiple default_alloc instances. Note that
83 * containers built on different allocator instances have different
84 * types, limiting the utility of this approach. If you do not
85 * wish to share the free lists with the main default_alloc
86 * instance, instantiate this with a non-zero __inst.
89 * (See @link Allocators allocators info @endlink for more.)
91 template<bool __threads, int __inst>
96 enum {_S_max_bytes = 128};
97 enum {_S_freelists = _S_max_bytes / _S_align};
101 union _Obj* _M_free_list_link;
102 char _M_client_data[1]; // The client sees this.
105 static _Obj* volatile _S_free_list[_S_freelists];
107 // Chunk allocation state.
108 static char* _S_start_free;
109 static char* _S_end_free;
110 static size_t _S_heap_size;
112 static _STL_mutex_lock _S_lock;
113 static _Atomic_word _S_force_new;
116 _S_round_up(size_t __bytes)
117 { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
120 _S_freelist_index(size_t __bytes)
121 { return ((__bytes + (size_t)_S_align - 1)/(size_t)_S_align - 1); }
123 // Returns an object of size __n, and optionally adds to size __n
126 _S_refill(size_t __n);
128 // Allocates a chunk for nobjs of size size. nobjs may be reduced
129 // if it is inconvenient to allocate the requested number.
131 _S_chunk_alloc(size_t __n, int& __nobjs);
133 // It would be nice to use _STL_auto_lock here. But we need a
134 // test whether threads are in use.
137 _Lock() { if (__threads) _S_lock._M_acquire_lock(); }
138 ~_Lock() { if (__threads) _S_lock._M_release_lock(); }
139 } __attribute__ ((__unused__));
145 allocate(size_t __n);
149 deallocate(void* __p, size_t __n);
152 template<bool __threads, int __inst>
154 operator==(const __pool_alloc<__threads,__inst>&,
155 const __pool_alloc<__threads,__inst>&)
158 template<bool __threads, int __inst>
160 operator!=(const __pool_alloc<__threads,__inst>&,
161 const __pool_alloc<__threads,__inst>&)
165 // Allocate memory in large chunks in order to avoid fragmenting the
166 // heap too much. Assume that __n is properly aligned. We hold
167 // the allocation lock.
168 template<bool __threads, int __inst>
170 __pool_alloc<__threads, __inst>::_S_chunk_alloc(size_t __n, int& __nobjs)
173 size_t __total_bytes = __n * __nobjs;
174 size_t __bytes_left = _S_end_free - _S_start_free;
176 if (__bytes_left >= __total_bytes)
178 __result = _S_start_free;
179 _S_start_free += __total_bytes;
182 else if (__bytes_left >= __n)
184 __nobjs = (int)(__bytes_left/__n);
185 __total_bytes = __n * __nobjs;
186 __result = _S_start_free;
187 _S_start_free += __total_bytes;
192 size_t __bytes_to_get =
193 2 * __total_bytes + _S_round_up(_S_heap_size >> 4);
194 // Try to make use of the left-over piece.
195 if (__bytes_left > 0)
197 _Obj* volatile* __free_list =
198 _S_free_list + _S_freelist_index(__bytes_left);
200 ((_Obj*)(void*)_S_start_free)->_M_free_list_link = *__free_list;
201 *__free_list = (_Obj*)(void*)_S_start_free;
203 _S_start_free = static_cast<char*>(::operator new(__bytes_to_get));
204 if (_S_start_free == 0)
207 _Obj* volatile* __free_list;
209 // Try to make do with what we have. That can't hurt. We
210 // do not try smaller requests, since that tends to result
211 // in disaster on multi-process machines.
213 for (; __i <= (size_t) _S_max_bytes; __i += (size_t) _S_align)
215 __free_list = _S_free_list + _S_freelist_index(__i);
219 *__free_list = __p -> _M_free_list_link;
220 _S_start_free = (char*)__p;
221 _S_end_free = _S_start_free + __i;
222 return _S_chunk_alloc(__n, __nobjs);
223 // Any leftover piece will eventually make it to the
227 _S_end_free = 0; // In case of exception.
228 _S_start_free = static_cast<char*>(::operator new(__bytes_to_get));
229 // This should either throw an exception or remedy the situation.
230 // Thus we assume it succeeded.
232 _S_heap_size += __bytes_to_get;
233 _S_end_free = _S_start_free + __bytes_to_get;
234 return _S_chunk_alloc(__n, __nobjs);
238 // Returns an object of size __n, and optionally adds to "size
239 // __n"'s free list. We assume that __n is properly aligned. We
240 // hold the allocation lock.
241 template<bool __threads, int __inst>
243 __pool_alloc<__threads, __inst>::_S_refill(size_t __n)
246 char* __chunk = _S_chunk_alloc(__n, __nobjs);
247 _Obj* volatile* __free_list;
255 __free_list = _S_free_list + _S_freelist_index(__n);
257 // Build free list in chunk.
258 __result = (_Obj*)(void*)__chunk;
259 *__free_list = __next_obj = (_Obj*)(void*)(__chunk + __n);
260 for (__i = 1; ; __i++)
262 __current_obj = __next_obj;
263 __next_obj = (_Obj*)(void*)((char*)__next_obj + __n);
264 if (__nobjs - 1 == __i)
266 __current_obj -> _M_free_list_link = 0;
270 __current_obj -> _M_free_list_link = __next_obj;
275 template<bool __threads, int __inst>
277 __pool_alloc<__threads, __inst>::allocate(size_t __n)
281 // If there is a race through here, assume answer from getenv
282 // will resolve in same direction. Inspired by techniques
283 // to efficiently support threading found in basic_string.h.
284 if (_S_force_new == 0)
286 if (getenv("GLIBCXX_FORCE_NEW"))
287 __atomic_add(&_S_force_new, 1);
289 __atomic_add(&_S_force_new, -1);
292 if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
293 __ret = ::operator new(__n);
296 _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
297 // Acquire the lock here with a constructor call. This
298 // ensures that it is released in exit or during stack
300 _Lock __lock_instance;
301 _Obj* __restrict__ __result = *__free_list;
302 if (__builtin_expect(__result == 0, 0))
303 __ret = _S_refill(_S_round_up(__n));
306 *__free_list = __result -> _M_free_list_link;
309 if (__builtin_expect(__ret == 0, 0))
315 template<bool __threads, int __inst>
317 __pool_alloc<__threads, __inst>::deallocate(void* __p, size_t __n)
319 if ((__n > (size_t) _S_max_bytes) || (_S_force_new > 0))
320 ::operator delete(__p);
323 _Obj* volatile* __free_list = _S_free_list + _S_freelist_index(__n);
324 _Obj* __q = (_Obj*)__p;
326 // Acquire the lock here with a constructor call. This
327 // ensures that it is released in exit or during stack
329 _Lock __lock_instance;
330 __q -> _M_free_list_link = *__free_list;
335 template<bool __threads, int __inst>
336 typename __pool_alloc<__threads, __inst>::_Obj* volatile
337 __pool_alloc<__threads, __inst>::_S_free_list[_S_freelists];
339 template<bool __threads, int __inst>
340 char* __pool_alloc<__threads, __inst>::_S_start_free = 0;
342 template<bool __threads, int __inst>
343 char* __pool_alloc<__threads, __inst>::_S_end_free = 0;
345 template<bool __threads, int __inst>
346 size_t __pool_alloc<__threads, __inst>::_S_heap_size = 0;
348 template<bool __threads, int __inst>
350 __pool_alloc<__threads, __inst>::_S_lock __STL_MUTEX_INITIALIZER;
352 template<bool __threads, int __inst> _Atomic_word
353 __pool_alloc<__threads, __inst>::_S_force_new = 0;
355 // Inhibit implicit instantiations for required instantiations,
356 // which are defined via explicit instantiations elsewhere.
357 // NB: This syntax is a GNU extension.
358 #if _GLIBCXX_EXTERN_TEMPLATE
359 extern template class __pool_alloc<true, 0>;
361 } // namespace __gnu_cxx