L4Re - L4 Runtime Environment
std_alloc
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /*
3  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
4  * Alexander Warg <warg@os.inf.tu-dresden.de>
5  * economic rights: Technische Universit├Ąt Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  *
11  * As a special exception, you may use this file as part of a free software
12  * library without restriction. Specifically, if other files instantiate
13  * templates or use macros or inline functions from this file, or you compile
14  * this file and link it with other files to produce an executable, this
15  * file does not by itself cause the resulting executable to be covered by
16  * the GNU General Public License. This exception does not however
17  * invalidate any other reasons why the executable file might be covered by
18  * the GNU General Public License.
19  */
20 
21 #pragma once
22 
23 #include <stddef.h>
24 namespace cxx {
25 /**
26  * \ingroup cxx_api
27  * \brief Helper type to distinguish the <c> oeprator new </c> version
28  * that does not throw exceptions.
29  */
30 class Nothrow {};
31 }
32 
33 /**
34  * \ingroup cxx_api
35  * \brief Simple placement new operator.
36  * \param mem the address of the memory block to place the new object.
37  * \return the address given by \a mem.
38  */
39 inline void *operator new (size_t, void *mem, cxx::Nothrow const &) throw()
40 { return mem; }
41 
42 /**
43  * \ingroup cxx_api
44  * \brief New operator that does not throw exceptions.
45  */
46 void *operator new (size_t, cxx::Nothrow const &) throw();
47 
48 namespace cxx {
49 
50 
51 /**
52  * \ingroup cxx_api
53  * \brief Standard allocator based on <c>operator new () </c>.
54  *
55  * This allocator is the default allocator used for the \em cxx
56  * \em Containers, such as cxx::Avl_set and cxx::Avl_map, to allocate
57  * the internal data structures.
58  */
59 template< typename _Type >
60 class New_allocator
61 {
62 public:
63  enum { can_free = true };
64 
65  New_allocator() throw() {}
66  New_allocator(New_allocator const &) throw() {}
67 
68  ~New_allocator() throw() {}
69 
70  _Type *alloc() throw()
71  { return static_cast<_Type*>(::operator new(sizeof (_Type), cxx::Nothrow())); }
72 
73  void free(_Type *t) throw()
74  { ::operator delete(t); }
75 };
76 
77 }
78