1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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/debug_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.
48 #ifndef _MALLOC_ALLOCATOR_H
49 #define _MALLOC_ALLOCATOR_H 1
51 #include <bits/allocator_traits.h>
57 * A malloc-based allocator. Typically slower than the
58 * __pool_alloc (below). Typically thread-safe and more
59 * storage efficient. The template argument is unused and is only present
60 * to permit multiple instantiations (but see __pool_alloc
61 * for caveats). "SGI" style, plus __set_malloc_handler for OOM conditions.
63 * (See @link Allocators allocators info @endlink for more.)
69 static void* _S_oom_malloc(size_t);
70 static void (* __malloc_alloc_oom_handler)();
76 void* __result = malloc(__n);
77 if (__builtin_expect(__result == 0, 0))
78 __result = _S_oom_malloc(__n);
83 deallocate(void* __p, size_t /* __n */)
86 static void (* __set_malloc_handler(void (*__f)()))()
88 void (* __old)() = __malloc_alloc_oom_handler;
89 __malloc_alloc_oom_handler = __f;
94 // malloc_alloc out-of-memory handling
96 void (* __malloc_alloc<__inst>::__malloc_alloc_oom_handler)() = 0;
100 __malloc_alloc<__inst>::
101 _S_oom_malloc(size_t __n)
103 void (* __my_malloc_handler)();
108 __my_malloc_handler = __malloc_alloc_oom_handler;
109 if (__builtin_expect(__my_malloc_handler == 0, 0))
111 (*__my_malloc_handler)();
112 __result = malloc(__n);
118 /** Comparison operators for all of the predifined SGI-style allocators.
119 * This ensures that __allocator<malloc_alloc> (for example) will work
120 * correctly. As required, all allocators compare equal.
124 operator==(const __malloc_alloc<inst>&, const __malloc_alloc<inst>&)
129 operator!=(const __malloc_alloc<__inst>&, const __malloc_alloc<__inst>&)
132 } // namespace __gnu_cxx
137 /// Versions for the predefined "SGI" style allocators.
138 template<typename _Tp, int __inst>
139 struct _Alloc_traits<_Tp, __gnu_cxx::__malloc_alloc<__inst> >
141 static const bool _S_instanceless = true;
142 typedef __gnu_cxx:: __malloc_alloc<__inst> base_alloc_type;
143 typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
144 typedef __allocator<_Tp, base_alloc_type> allocator_type;
149 /// Versions for the __allocator adaptor used with the predefined
150 /// "SGI" style allocators.
151 template<typename _Tp, typename _Tp1, int __inst>
152 struct _Alloc_traits<_Tp, __allocator<_Tp1,
153 __gnu_cxx::__malloc_alloc<__inst> > >
155 static const bool _S_instanceless = true;
156 typedef __gnu_cxx:: __malloc_alloc<__inst> base_alloc_type;
157 typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
158 typedef __allocator<_Tp, base_alloc_type> allocator_type;