1 //===-- Support/MallocAllocator.h - Allocator using malloc/free -*- 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 SUPPORT_MALLOCALLOCATOR_H
21 #define SUPPORT_MALLOCALLOCATOR_H
27 struct MallocAllocator {
28 typedef size_t size_type;
29 typedef ptrdiff_t difference_type;
31 typedef const T* const_pointer;
33 typedef const T& const_reference;
35 template <class U> struct rebind {
36 typedef MallocAllocator<U> other;
39 pointer address(reference x) const { return &x; }
40 const_pointer address(const_reference x) const { return &x; }
41 size_type max_size() const { return ~0 / sizeof(T); }
43 pointer allocate(size_t n, void* hint = 0) {
44 return (pointer)malloc(n*sizeof(T));
47 void deallocate(pointer p, size_t n) {
51 void construct(pointer p, const T &val) {
54 void destroy(pointer p) {
60 inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) {
64 inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) {