new_allocator.h revision 169691
1132720Skan// Allocator that wraps operator new -*- C++ -*- 2132720Skan 3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 4132720Skan// 5132720Skan// This file is part of the GNU ISO C++ Library. This library is free 6132720Skan// software; you can redistribute it and/or modify it under the 7132720Skan// terms of the GNU General Public License as published by the 8132720Skan// Free Software Foundation; either version 2, or (at your option) 9132720Skan// any later version. 10132720Skan 11132720Skan// This library is distributed in the hope that it will be useful, 12132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of 13132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14132720Skan// GNU General Public License for more details. 15132720Skan 16132720Skan// You should have received a copy of the GNU General Public License along 17132720Skan// with this library; see the file COPYING. If not, write to the Free 18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19132720Skan// USA. 20132720Skan 21132720Skan// As a special exception, you may use this file as part of a free software 22132720Skan// library without restriction. Specifically, if other files instantiate 23132720Skan// templates or use macros or inline functions from this file, or you compile 24132720Skan// this file and link it with other files to produce an executable, this 25132720Skan// file does not by itself cause the resulting executable to be covered by 26132720Skan// the GNU General Public License. This exception does not however 27132720Skan// invalidate any other reasons why the executable file might be covered by 28132720Skan// the GNU General Public License. 29132720Skan 30169691Skan/** @file ext/new_allocator.h 31169691Skan * This file is a GNU extension to the Standard C++ Library. 32169691Skan */ 33169691Skan 34132720Skan#ifndef _NEW_ALLOCATOR_H 35132720Skan#define _NEW_ALLOCATOR_H 1 36132720Skan 37132720Skan#include <new> 38169691Skan#include <bits/functexcept.h> 39132720Skan 40169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 41169691Skan 42169691Skan using std::size_t; 43169691Skan using std::ptrdiff_t; 44169691Skan 45132720Skan /** 46132720Skan * @brief An allocator that uses global new, as per [20.4]. 47132720Skan * 48132720Skan * This is precisely the allocator defined in the C++ Standard. 49132720Skan * - all allocation calls operator new 50132720Skan * - all deallocation calls operator delete 51132720Skan */ 52132720Skan template<typename _Tp> 53132720Skan class new_allocator 54132720Skan { 55132720Skan public: 56132720Skan typedef size_t size_type; 57132720Skan typedef ptrdiff_t difference_type; 58132720Skan typedef _Tp* pointer; 59132720Skan typedef const _Tp* const_pointer; 60132720Skan typedef _Tp& reference; 61132720Skan typedef const _Tp& const_reference; 62132720Skan typedef _Tp value_type; 63132720Skan 64132720Skan template<typename _Tp1> 65132720Skan struct rebind 66132720Skan { typedef new_allocator<_Tp1> other; }; 67132720Skan 68132720Skan new_allocator() throw() { } 69132720Skan 70132720Skan new_allocator(const new_allocator&) throw() { } 71132720Skan 72132720Skan template<typename _Tp1> 73132720Skan new_allocator(const new_allocator<_Tp1>&) throw() { } 74132720Skan 75132720Skan ~new_allocator() throw() { } 76132720Skan 77132720Skan pointer 78132720Skan address(reference __x) const { return &__x; } 79132720Skan 80132720Skan const_pointer 81132720Skan address(const_reference __x) const { return &__x; } 82132720Skan 83132720Skan // NB: __n is permitted to be 0. The C++ standard says nothing 84132720Skan // about what the return value is when __n == 0. 85132720Skan pointer 86132720Skan allocate(size_type __n, const void* = 0) 87169691Skan { 88169691Skan if (__builtin_expect(__n > this->max_size(), false)) 89169691Skan std::__throw_bad_alloc(); 90132720Skan 91169691Skan return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); 92169691Skan } 93169691Skan 94132720Skan // __p is not permitted to be a null pointer. 95132720Skan void 96132720Skan deallocate(pointer __p, size_type) 97132720Skan { ::operator delete(__p); } 98132720Skan 99132720Skan size_type 100132720Skan max_size() const throw() 101132720Skan { return size_t(-1) / sizeof(_Tp); } 102132720Skan 103132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 104132720Skan // 402. wrong new expression in [some_] allocator::construct 105132720Skan void 106132720Skan construct(pointer __p, const _Tp& __val) 107132720Skan { ::new(__p) _Tp(__val); } 108132720Skan 109132720Skan void 110132720Skan destroy(pointer __p) { __p->~_Tp(); } 111132720Skan }; 112132720Skan 113132720Skan template<typename _Tp> 114132720Skan inline bool 115132720Skan operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 116132720Skan { return true; } 117132720Skan 118132720Skan template<typename _Tp> 119132720Skan inline bool 120132720Skan operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 121132720Skan { return false; } 122132720Skan 123169691Skan_GLIBCXX_END_NAMESPACE 124169691Skan 125132720Skan#endif 126