Commit 56766e0d by Dhruv Matani Committed by Benjamin Kosnik

debug_allocator.h: _M_extra now stands for the number of extra objects instead of the number of...


2004-02-04  Dhruv Matani  <dhruvbird@gmx.net>

	* include/ext/debug_allocator.h: _M_extra now stands for the
	number of extra objects instead of the number of extra bytes.
	(debug_allocator::allocate): Adjust.
	(debug_allocator::deallocate): Adjust.

	* include/ext/pool_allocator.h: Fix typo.

From-SVN: r77256
parent 7bdff56f
2004-02-04 Dhruv Matani <dhruvbird@gmx.net>
* include/ext/debug_allocator.h: _M_extra now stands for the
number of extra objects instead of the number of extra bytes.
(debug_allocator::allocate): Adjust.
(debug_allocator::deallocate): Adjust.
* include/ext/pool_allocator.h: Fix typo.
2004-02-03 Felix Yen <fwy@alumni.brown.edu> 2004-02-03 Felix Yen <fwy@alumni.brown.edu>
Benjamin Kosnik <bkoz@redhat.com> Benjamin Kosnik <bkoz@redhat.com>
......
// Allocators -*- C++ -*- // Allocators -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
#ifndef _DEBUG_ALLOCATOR_H #ifndef _DEBUG_ALLOCATOR_H
#define _DEBUG_ALLOCATOR_H 1 #define _DEBUG_ALLOCATOR_H 1
#include <memory> #include <cstdlib>
namespace __gnu_cxx namespace __gnu_cxx
{ {
...@@ -74,28 +74,42 @@ namespace __gnu_cxx ...@@ -74,28 +74,42 @@ namespace __gnu_cxx
typedef typename _Alloc::value_type value_type; typedef typename _Alloc::value_type value_type;
private: private:
// Size of space used to store size. Note that this must be // _M_extra is the number of objects that correspond to the
// large enough to preserve alignment. // extra space where debug information is stored.
const size_t _M_extra; size_type _M_extra;
_Alloc _M_allocator; _Alloc _M_allocator;
public: public:
debug_allocator() : _M_extra(8) { } 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 pointer
allocate(size_type __n, std::allocator<void>::const_pointer = 0) allocate(size_type __n, const void* __hint)
{ {
pointer __result = _M_allocator.allocate(__n + _M_extra); pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
*__result = __n; size_type* __ps = reinterpret_cast<size_type*>(__res);
return __result + _M_extra; *__ps = __n;
return __res + _M_extra;
} }
void void
deallocate(pointer __p, size_type __n) deallocate(pointer __p, size_type __n)
{ {
pointer __real_p = __p - _M_extra; pointer __real_p = __p - _M_extra;
if (*__real_p != __n) if (*reinterpret_cast<size_type*>(__real_p) != __n)
abort(); abort();
_M_allocator.deallocate(__real_p, __n + _M_extra); _M_allocator.deallocate(__real_p, __n + _M_extra);
} }
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
* purpose. It is provided "as is" without express or implied warranty. * purpose. It is provided "as is" without express or implied warranty.
*/ */
/** @file ext/debug_allocator.h /** @file ext/pool_allocator.h
* This file is a GNU extension to the Standard C++ Library. * This file is a GNU extension to the Standard C++ Library.
* You should only include this header if you are using GCC 3 or later. * You should only include this header if you are using GCC 3 or later.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment