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