1*8975f5c5SAndroid Build Coastguard Worker // 2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2002 The ANGLE Project Authors. All rights reserved. 3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file. 5*8975f5c5SAndroid Build Coastguard Worker // 6*8975f5c5SAndroid Build Coastguard Worker 7*8975f5c5SAndroid Build Coastguard Worker #ifndef COMPILER_TRANSLATOR_POOLALLOC_H_ 8*8975f5c5SAndroid Build Coastguard Worker #define COMPILER_TRANSLATOR_POOLALLOC_H_ 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard Worker // 11*8975f5c5SAndroid Build Coastguard Worker // This header defines the pool_allocator class that allows STL containers 12*8975f5c5SAndroid Build Coastguard Worker // to use the angle::PoolAllocator class by using the pool_allocator 13*8975f5c5SAndroid Build Coastguard Worker // class as the allocator (second) template argument. 14*8975f5c5SAndroid Build Coastguard Worker // 15*8975f5c5SAndroid Build Coastguard Worker // It also defines functions for managing the GlobalPoolAllocator used by the compiler. 16*8975f5c5SAndroid Build Coastguard Worker // 17*8975f5c5SAndroid Build Coastguard Worker 18*8975f5c5SAndroid Build Coastguard Worker #include <stddef.h> 19*8975f5c5SAndroid Build Coastguard Worker #include <string.h> 20*8975f5c5SAndroid Build Coastguard Worker #include <vector> 21*8975f5c5SAndroid Build Coastguard Worker 22*8975f5c5SAndroid Build Coastguard Worker #include "common/PoolAlloc.h" 23*8975f5c5SAndroid Build Coastguard Worker 24*8975f5c5SAndroid Build Coastguard Worker // 25*8975f5c5SAndroid Build Coastguard Worker // There could potentially be many pools with pops happening at 26*8975f5c5SAndroid Build Coastguard Worker // different times. But a simple use is to have a global pop 27*8975f5c5SAndroid Build Coastguard Worker // with everyone using the same global allocator. 28*8975f5c5SAndroid Build Coastguard Worker // 29*8975f5c5SAndroid Build Coastguard Worker extern angle::PoolAllocator *GetGlobalPoolAllocator(); 30*8975f5c5SAndroid Build Coastguard Worker extern void SetGlobalPoolAllocator(angle::PoolAllocator *poolAllocator); 31*8975f5c5SAndroid Build Coastguard Worker 32*8975f5c5SAndroid Build Coastguard Worker // 33*8975f5c5SAndroid Build Coastguard Worker // This STL compatible allocator is intended to be used as the allocator 34*8975f5c5SAndroid Build Coastguard Worker // parameter to templatized STL containers, like vector and map. 35*8975f5c5SAndroid Build Coastguard Worker // 36*8975f5c5SAndroid Build Coastguard Worker // It will use the pools for allocation, and not 37*8975f5c5SAndroid Build Coastguard Worker // do any deallocation, but will still do destruction. 38*8975f5c5SAndroid Build Coastguard Worker // 39*8975f5c5SAndroid Build Coastguard Worker template <class T> 40*8975f5c5SAndroid Build Coastguard Worker class pool_allocator 41*8975f5c5SAndroid Build Coastguard Worker { 42*8975f5c5SAndroid Build Coastguard Worker public: 43*8975f5c5SAndroid Build Coastguard Worker typedef size_t size_type; 44*8975f5c5SAndroid Build Coastguard Worker typedef ptrdiff_t difference_type; 45*8975f5c5SAndroid Build Coastguard Worker typedef T *pointer; 46*8975f5c5SAndroid Build Coastguard Worker typedef const T *const_pointer; 47*8975f5c5SAndroid Build Coastguard Worker typedef T &reference; 48*8975f5c5SAndroid Build Coastguard Worker typedef const T &const_reference; 49*8975f5c5SAndroid Build Coastguard Worker typedef T value_type; 50*8975f5c5SAndroid Build Coastguard Worker 51*8975f5c5SAndroid Build Coastguard Worker template <class Other> 52*8975f5c5SAndroid Build Coastguard Worker struct rebind 53*8975f5c5SAndroid Build Coastguard Worker { 54*8975f5c5SAndroid Build Coastguard Worker typedef pool_allocator<Other> other; 55*8975f5c5SAndroid Build Coastguard Worker }; address(reference x)56*8975f5c5SAndroid Build Coastguard Worker pointer address(reference x) const { return &x; } address(const_reference x)57*8975f5c5SAndroid Build Coastguard Worker const_pointer address(const_reference x) const { return &x; } 58*8975f5c5SAndroid Build Coastguard Worker pool_allocator()59*8975f5c5SAndroid Build Coastguard Worker pool_allocator() {} 60*8975f5c5SAndroid Build Coastguard Worker 61*8975f5c5SAndroid Build Coastguard Worker template <class Other> pool_allocator(const pool_allocator<Other> & p)62*8975f5c5SAndroid Build Coastguard Worker pool_allocator(const pool_allocator<Other> &p) 63*8975f5c5SAndroid Build Coastguard Worker {} 64*8975f5c5SAndroid Build Coastguard Worker 65*8975f5c5SAndroid Build Coastguard Worker template <class Other> 66*8975f5c5SAndroid Build Coastguard Worker pool_allocator<T> &operator=(const pool_allocator<Other> &p) 67*8975f5c5SAndroid Build Coastguard Worker { 68*8975f5c5SAndroid Build Coastguard Worker return *this; 69*8975f5c5SAndroid Build Coastguard Worker } 70*8975f5c5SAndroid Build Coastguard Worker 71*8975f5c5SAndroid Build Coastguard Worker #if defined(__SUNPRO_CC) && !defined(_RWSTD_ALLOCATOR) 72*8975f5c5SAndroid Build Coastguard Worker // libCStd on some platforms have a different allocate/deallocate interface. 73*8975f5c5SAndroid Build Coastguard Worker // Caller pre-bakes sizeof(T) into 'n' which is the number of bytes to be 74*8975f5c5SAndroid Build Coastguard Worker // allocated, not the number of elements. allocate(size_type n)75*8975f5c5SAndroid Build Coastguard Worker void *allocate(size_type n) { return getAllocator().allocate(n); } allocate(size_type n,const void *)76*8975f5c5SAndroid Build Coastguard Worker void *allocate(size_type n, const void *) { return getAllocator().allocate(n); } deallocate(void *,size_type)77*8975f5c5SAndroid Build Coastguard Worker void deallocate(void *, size_type) {} 78*8975f5c5SAndroid Build Coastguard Worker #else allocate(size_type n)79*8975f5c5SAndroid Build Coastguard Worker pointer allocate(size_type n) 80*8975f5c5SAndroid Build Coastguard Worker { 81*8975f5c5SAndroid Build Coastguard Worker return static_cast<pointer>(getAllocator().allocate(n * sizeof(T))); 82*8975f5c5SAndroid Build Coastguard Worker } allocate(size_type n,const void *)83*8975f5c5SAndroid Build Coastguard Worker pointer allocate(size_type n, const void *) 84*8975f5c5SAndroid Build Coastguard Worker { 85*8975f5c5SAndroid Build Coastguard Worker return static_cast<pointer>(getAllocator().allocate(n * sizeof(T))); 86*8975f5c5SAndroid Build Coastguard Worker } deallocate(pointer,size_type)87*8975f5c5SAndroid Build Coastguard Worker void deallocate(pointer, size_type) {} 88*8975f5c5SAndroid Build Coastguard Worker #endif // _RWSTD_ALLOCATOR 89*8975f5c5SAndroid Build Coastguard Worker construct(pointer p,const T & val)90*8975f5c5SAndroid Build Coastguard Worker void construct(pointer p, const T &val) { new ((void *)p) T(val); } destroy(pointer p)91*8975f5c5SAndroid Build Coastguard Worker void destroy(pointer p) { p->T::~T(); } 92*8975f5c5SAndroid Build Coastguard Worker 93*8975f5c5SAndroid Build Coastguard Worker bool operator==(const pool_allocator &rhs) const { return true; } 94*8975f5c5SAndroid Build Coastguard Worker bool operator!=(const pool_allocator &rhs) const { return false; } 95*8975f5c5SAndroid Build Coastguard Worker max_size()96*8975f5c5SAndroid Build Coastguard Worker size_type max_size() const { return static_cast<size_type>(-1) / sizeof(T); } max_size(int size)97*8975f5c5SAndroid Build Coastguard Worker size_type max_size(int size) const { return static_cast<size_type>(-1) / size; } 98*8975f5c5SAndroid Build Coastguard Worker getAllocator()99*8975f5c5SAndroid Build Coastguard Worker angle::PoolAllocator &getAllocator() const { return *GetGlobalPoolAllocator(); } 100*8975f5c5SAndroid Build Coastguard Worker }; 101*8975f5c5SAndroid Build Coastguard Worker 102*8975f5c5SAndroid Build Coastguard Worker #endif // COMPILER_TRANSLATOR_POOLALLOC_H_ 103