// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Implement a very basic memory allocator that make direct system calls // instead of relying on libc. // This allocator is not thread-safe. #ifndef ALLOCATOR_H__ #define ALLOCATOR_H__ #include namespace playground { class SystemAllocatorHelper { protected: static void *sys_allocate(size_t size); static void sys_deallocate(void* p, size_t size); }; template class SystemAllocator : SystemAllocatorHelper { public: typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef size_t size_type; typedef std::ptrdiff_t difference_type; template struct rebind { typedef SystemAllocator other; }; pointer address(reference value) const { return &value; } const_pointer address(const_reference value) const { return &value; } SystemAllocator() throw() { } SystemAllocator(const SystemAllocator& src) throw() { } template SystemAllocator(const SystemAllocator& src) throw() { } ~SystemAllocator() throw() { } size_type max_size() const throw() { return (1 << 30) / sizeof(T); } pointer allocate(size_type num, const void* = 0) { if (num > max_size()) { return NULL; } return (pointer)sys_allocate(num * sizeof(T)); } void construct(pointer p, const T& value) { new(reinterpret_cast(p))T(value); } void destroy(pointer p) { p->~T(); } void deallocate(pointer p, size_type num) { sys_deallocate(p, num * sizeof(T)); } }; template bool operator== (const SystemAllocator&, const SystemAllocator&) throw() { return true; } template bool operator!= (const SystemAllocator&, const SystemAllocator&) throw() { return false; } } // namespace #endif // ALLOCATOR_H__