This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: patch for ext/debug_allocator.h (second attempt)


On Wed, 2004-02-04 at 01:15, Benjamin Kosnik wrote:
> >This patch makes the debug_allocator<> compile now. 
> 
> testsuite/ext/allocators.cc seemed to work before, but I agree, something's fishy...
> 
> > Earlier, the debug_allocator<> had code like this:
> >
> >size_type n;
> >pointer x;
> >*x = n;
> >
> >Which made no sense.
> 
> Right.
> 
> However, we're trying to stay away from static data members, and ugly
> casting. Try this.

The patch is fine except for the modification in the deallocate
function. You can not deref. a pointer and then static_cast<> it to an
integer. I changed that to a reinterpret_cast before deref. and delayed
the dref. to after the casting has been done. I've attached the patch
for the modified debug allocator against the current version in CVS.

Is another change log reqd?




-- 
	-Dhruv Matani.
http://www.geocities.com/dhruvbird/


*** debug_allocator.h_old	Wed Feb  4 13:53:04 2004
--- debug_allocator.h	Wed Feb  4 13:45:50 2004
***************
*** 1,6 ****
  // Allocators -*- C++ -*-
  
! // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  //
  // This file is part of the GNU ISO C++ Library.  This library is free
  // software; you can redistribute it and/or modify it under the
--- 1,6 ----
  // Allocators -*- C++ -*-
  
! // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
  //
  // This file is part of the GNU ISO C++ Library.  This library is free
  // software; you can redistribute it and/or modify it under the
***************
*** 48,54 ****
  #ifndef _DEBUG_ALLOCATOR_H
  #define _DEBUG_ALLOCATOR_H 1
  
! #include <memory>
  
  namespace __gnu_cxx
  {
--- 48,54 ----
  #ifndef _DEBUG_ALLOCATOR_H
  #define _DEBUG_ALLOCATOR_H 1
  
! #include <cstdlib>
  
  namespace __gnu_cxx
  {
*************** namespace __gnu_cxx
*** 74,101 ****
        typedef typename _Alloc::value_type       value_type;
  
      private:
!       // Size of space used to store size.  Note that this must be
!       // large enough to preserve alignment.
!       const size_t 		_M_extra;
        
        _Alloc			_M_allocator;
  
      public:
!       debug_allocator() : _M_extra(8) { }
  
        pointer
!       allocate(size_type __n, std::allocator<void>::const_pointer = 0)
        {
!         pointer __result = _M_allocator.allocate(__n + _M_extra);
!         *__result = __n;
!         return __result + _M_extra;
        }
  
        void
        deallocate(pointer __p, size_type __n)
        {
          pointer __real_p = __p - _M_extra;
!         if (*__real_p != __n)
            abort();
          _M_allocator.deallocate(__real_p, __n + _M_extra);
        }
--- 74,115 ----
        typedef typename _Alloc::value_type       value_type;
  
      private:
!       // _M_extra is the number of objects that correspond to the
!       // extra space where debug information is stored.
!       size_type 		_M_extra;
        
        _Alloc			_M_allocator;
  
      public:
!       debug_allocator()
!       {
! 	const size_t __obj_size = sizeof(value_type);
! 	_M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size; 
!       }
!       
!       pointer
!       allocate(size_type __n)
!       {
!         pointer __res = _M_allocator.allocate(__n + _M_extra);      
! 	size_type* __ps = reinterpret_cast<size_type*>(__res);
! 	*__ps = __n;
!         return __res + _M_extra;
!       }
  
        pointer
!       allocate(size_type __n, const void* __hint)
        {
!         pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
! 	size_type* __ps = reinterpret_cast<size_type*>(__res);
! 	*__ps = __n;
!         return __res + _M_extra;
        }
  
        void
        deallocate(pointer __p, size_type __n)
        {
          pointer __real_p = __p - _M_extra;
!         if (*reinterpret_cast<size_type*>(__real_p) != __n)
            abort();
          _M_allocator.deallocate(__real_p, __n + _M_extra);
        }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]