1 //===-- llvm/Support/MallocAllocator.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines MallocAllocator class, an STL compatible allocator which
11 // just uses malloc/free to get and release memory. The default allocator uses
12 // the STL pool allocator runtime library, this explicitly avoids it.
14 // This file is used for variety of purposes, including the pool allocator
15 // project and testing, regardless of whether or not it's used directly in the
16 // LLVM code, so don't delete this from CVS if you think it's unused!
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_MALLOCALLOCATOR_H
21 #define LLVM_SUPPORT_MALLOCALLOCATOR_H
29 struct MallocAllocator {
30 typedef size_t size_type;
31 typedef ptrdiff_t difference_type;
33 typedef const T* const_pointer;
35 typedef const T& const_reference;
37 template <class U> struct rebind {
38 typedef MallocAllocator<U> other;
42 MallocAllocator(const MallocAllocator<R> &) {}
45 pointer address(reference x) const { return &x; }
46 const_pointer address(const_reference x) const { return &x; }
47 size_type max_size() const { return ~0 / sizeof(T); }
49 static pointer allocate(size_t n, void* hint = 0) {
50 return static_cast<pointer>(malloc(n*sizeof(T)));
53 static void deallocate(pointer p, size_t n) {
54 free(static_cast<void*>(p));
57 void construct(pointer p, const T &val) {
58 new(static_cast<void*>(p)) T(val);
60 void destroy(pointer p) {
66 inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) {
70 inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) {
73 } // End llvm namespace
76 * This specialization was used for optimization earlier, but doesn't appear
77 * to work with newer versions of GCC, Linux or otherwise.
79 * This can be re-enabled if desired, but by default, it won't be included.
83 template<typename Type, typename Type2>
84 struct _Alloc_traits<Type, ::llvm::MallocAllocator<Type2> > {
85 static const bool _S_instanceless = true;
86 typedef ::llvm::MallocAllocator<Type> base_alloc_type;
87 typedef ::llvm::MallocAllocator<Type> _Alloc_type;
88 typedef ::llvm::MallocAllocator<Type> allocator_type;