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 _DEBUG_ALLOCATOR_H
49 #define _DEBUG_ALLOCATOR_H 1
51 #include <bits/allocator_traits.h>
57 * An adaptor for an underlying allocator (_Alloc) to check the size
58 * arguments for debugging.
60 * "There is some evidence that this can confuse Purify." - SGI comment
62 * This adaptor is "SGI" style. The _Alloc parameter must also be "SGI".
64 * (See @link Allocators allocators info @endlink for more.)
66 template<typename _Alloc>
70 // Size of space used to store size. Note that this must be
71 // large enough to preserve alignment.
78 char* __result = (char*)_Alloc::allocate(__n + (int) _S_extra);
79 *(size_t*)__result = __n;
80 return __result + (int) _S_extra;
84 deallocate(void* __p, size_t __n)
86 char* __real_p = (char*)__p - (int) _S_extra;
87 if (*(size_t*)__real_p != __n)
89 _Alloc::deallocate(__real_p, __n + (int) _S_extra);
94 /** Comparison operators for all of the predifined SGI-style allocators.
95 * This ensures that __allocator<malloc_alloc> (for example) will work
96 * correctly. As required, all allocators compare equal.
98 template<typename _Alloc>
100 operator==(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
103 template<typename _Alloc>
105 operator!=(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
108 } // namespace __gnu_cxx
113 /// Versions for the predefined "SGI" style allocators.
114 template<typename _Tp, typename _Alloc>
115 struct _Alloc_traits<_Tp, __gnu_cxx::__debug_alloc<_Alloc> >
117 static const bool _S_instanceless = true;
118 typedef __gnu_cxx::__debug_alloc<_Alloc> base_alloc_type;
119 typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
120 typedef __allocator<_Tp, base_alloc_type> allocator_type;
125 /// Versions for the __allocator adaptor used with the predefined
126 /// "SGI" style allocators.
127 template<typename _Tp, typename _Tp1, typename _Alloc>
128 struct _Alloc_traits<_Tp, __allocator<_Tp1,
129 __gnu_cxx::__debug_alloc<_Alloc> > >
131 static const bool _S_instanceless = true;
132 typedef __gnu_cxx::__debug_alloc<_Alloc> base_alloc_type;
133 typedef __simple_alloc<_Tp, base_alloc_type> _Alloc_type;
134 typedef __allocator<_Tp, base_alloc_type> allocator_type;