OSDN Git Service

2003-07-09 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / debug_allocator.h
1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31  * Copyright (c) 1996-1997
32  * Silicon Graphics Computer Systems, Inc.
33  *
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.
41  */
42
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.
46  */
47
48 #ifndef _DEBUG_ALLOCATOR_H
49 #define _DEBUG_ALLOCATOR_H 1
50
51 #include <bits/allocator_traits.h>
52
53 namespace __gnu_cxx
54 {
55   /**
56    *  @if maint
57    *  An adaptor for an underlying allocator (_Alloc) to check the size
58    *  arguments for debugging.
59    *
60    *  "There is some evidence that this can confuse Purify." - SGI comment
61    *
62    *  This adaptor is "SGI" style.  The _Alloc parameter must also be "SGI".
63    *  @endif
64    *  (See @link Allocators allocators info @endlink for more.)
65    */
66   template<typename _Alloc>
67     class __debug_alloc
68     {
69     private:
70       // Size of space used to store size.  Note that this must be
71       // large enough to preserve alignment.
72       enum {_S_extra = 8};
73
74     public:
75       static void*
76       allocate(size_t __n)
77       {
78         char* __result = (char*)_Alloc::allocate(__n + (int) _S_extra);
79         *(size_t*)__result = __n;
80         return __result + (int) _S_extra;
81       }
82
83       static void
84       deallocate(void* __p, size_t __n)
85       {
86         char* __real_p = (char*)__p - (int) _S_extra;
87         if (*(size_t*)__real_p != __n)
88           abort();
89         _Alloc::deallocate(__real_p, __n + (int) _S_extra);
90       }
91     };
92
93   //@{
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.
97    */
98   template<typename _Alloc>
99     inline bool
100     operator==(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
101     { return true; }
102
103   template<typename _Alloc>
104     inline bool
105     operator!=(const __debug_alloc<_Alloc>&, const __debug_alloc<_Alloc>&)
106     { return false; }
107   //@}
108 } // namespace __gnu_cxx
109
110 namespace std
111 {
112   //@{
113   /// Versions for the predefined "SGI" style allocators.
114   template<typename _Tp, typename _Alloc>
115     struct _Alloc_traits<_Tp, __gnu_cxx::__debug_alloc<_Alloc> >
116     {
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;
121     };
122   //@}
123
124   //@{
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> > >
130     {
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;
135     };
136   //@}
137 } // namespace std
138
139 #endif