diff options
author | Chris Lattner <sabre@nondot.org> | 2003-11-04 22:38:28 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2003-11-04 22:38:28 +0000 |
commit | c2630f73141076fb3a771d7106cc10fd009edfb7 (patch) | |
tree | 27e205f8a71d6dc2745467b7cc5108522d3c54a1 | |
parent | 2855ecdf04a671785b8cf120e5b85ae416822157 (diff) |
New file, for use by the pool allocator project
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9702 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/Support/MallocAllocator.h | 68 | ||||
-rw-r--r-- | include/llvm/Support/MallocAllocator.h | 68 |
2 files changed, 136 insertions, 0 deletions
diff --git a/include/Support/MallocAllocator.h b/include/Support/MallocAllocator.h new file mode 100644 index 0000000000..98957d4c8e --- /dev/null +++ b/include/Support/MallocAllocator.h @@ -0,0 +1,68 @@ +//===-- Support/MallocAllocator.h - Allocator using malloc/free -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines MallocAllocator class, an STL compatible allocator which +// just uses malloc/free to get and release memory. The default allocator uses +// the STL pool allocator runtime library, this explicitly avoids it. +// +// This file is used for variety of purposes, including the pool allocator +// project and testing, regardless of whether or not it's used directly in the +// LLVM code, so don't delete this from CVS if you think it's unused! +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_MALLOCALLOCATOR_H +#define SUPPORT_MALLOCALLOCATOR_H + +#include <cstdlib> +#include <memory> + +template<typename T> +struct MallocAllocator { + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef T value_type; + template <class U> struct rebind { + typedef MallocAllocator<U> other; + }; + + pointer address(reference x) const { return &x; } + const_pointer address(const_reference x) const { return &x; } + size_type max_size() const { return ~0 / sizeof(T); } + + pointer allocate(size_t n, void* hint = 0) { + return (pointer)malloc(n*sizeof(T)); + } + + void deallocate(pointer p, size_t n) { + free((void*)p); + } + + void construct(pointer p, const T &val) { + new((void*)p) T(val); + } + void destroy(pointer p) { + p->~T(); + } +}; + +template<typename T> +inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) { + return true; +} +template<typename T> +inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) { + return false; +} + +#endif diff --git a/include/llvm/Support/MallocAllocator.h b/include/llvm/Support/MallocAllocator.h new file mode 100644 index 0000000000..98957d4c8e --- /dev/null +++ b/include/llvm/Support/MallocAllocator.h @@ -0,0 +1,68 @@ +//===-- Support/MallocAllocator.h - Allocator using malloc/free -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines MallocAllocator class, an STL compatible allocator which +// just uses malloc/free to get and release memory. The default allocator uses +// the STL pool allocator runtime library, this explicitly avoids it. +// +// This file is used for variety of purposes, including the pool allocator +// project and testing, regardless of whether or not it's used directly in the +// LLVM code, so don't delete this from CVS if you think it's unused! +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_MALLOCALLOCATOR_H +#define SUPPORT_MALLOCALLOCATOR_H + +#include <cstdlib> +#include <memory> + +template<typename T> +struct MallocAllocator { + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef T value_type; + template <class U> struct rebind { + typedef MallocAllocator<U> other; + }; + + pointer address(reference x) const { return &x; } + const_pointer address(const_reference x) const { return &x; } + size_type max_size() const { return ~0 / sizeof(T); } + + pointer allocate(size_t n, void* hint = 0) { + return (pointer)malloc(n*sizeof(T)); + } + + void deallocate(pointer p, size_t n) { + free((void*)p); + } + + void construct(pointer p, const T &val) { + new((void*)p) T(val); + } + void destroy(pointer p) { + p->~T(); + } +}; + +template<typename T> +inline bool operator==(const MallocAllocator<T> &, const MallocAllocator<T> &) { + return true; +} +template<typename T> +inline bool operator!=(const MallocAllocator<T>&, const MallocAllocator<T>&) { + return false; +} + +#endif |