Zero  0.1.0
stl_pool.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
33 #ifndef __STL_POOL
34 #define __STL_POOL
35 
36 #include <iostream>
37 #include "util/guard.h"
38 
40 class Pool {
41 public:
43  Pool(size_t granularity, size_t size);
44 
46  ~Pool();
47 
49  size_t GetGranularity() const {
50  return m_granularity;
51  }
52 
54  size_t GetSize() const {
55  return m_size;
56  }
57 
59  size_t GetUsed() const {
60  return m_used;
61  }
62 
64  size_t GetOverflow() const {
65  return m_overflow;
66  }
67 
69  void* Allocate();
70 
72  void Deallocate(void* block);
73 
75  template<typename T>
76  T* Construct() {
77  assert(sizeof(T) <= m_granularity);
78  T* block = reinterpret_cast<T*>( Allocate());
79  return new(block) T;
80  }
81 
83  template<typename T>
84  void Destroy(T* instance) {
85  assert(sizeof(T) <= m_granularity);
86  instance->~T();
87  Deallocate(instance);
88  }
89 
90 private:
92  bool IsFromPool(void const* instance) const {
93  char const* block = reinterpret_cast<char const*>( instance );
94  return m_storage.get() <= block && block < (m_storage.get() + m_size * m_granularity);
95  }
96 
97  size_t m_granularity;
98  size_t m_size;
99  size_t volatile m_used;
100  size_t volatile m_overflow;
101 
104 };
105 
106 #endif // __STL_POOL
Simple pool class.
Definition: stl_pool.h:40
void Destroy(T *instance)
Destructs an object back into the pool.
Definition: stl_pool.h:84
size_t volatile m_used
The number of pooled allocations.
Definition: stl_pool.h:99
T * Construct()
Constructs an object from the pool.
Definition: stl_pool.h:76
bool IsFromPool(void const *instance) const
Returns true if the given instance is from pooled storage.
Definition: stl_pool.h:92
size_t volatile m_overflow
The number of non-pooled allocations.
Definition: stl_pool.h:100
size_t m_size
The number of elements in pooled storage.
Definition: stl_pool.h:98
size_t GetOverflow() const
Gets the number of elements allocated from non-pooled storage.
Definition: stl_pool.h:64
array_guard_t< void * > m_slots
The free list.
Definition: stl_pool.h:103
size_t GetGranularity() const
Gets the pool granularity.
Definition: stl_pool.h:49
array_guard_t< char > m_storage
The pool storage.
Definition: stl_pool.h:102
~Pool()
Checks for emptiness before destructing.
Definition: stl_pool.cpp:51
size_t GetSize() const
Gets the pool size in elements.
Definition: stl_pool.h:54
Pool(size_t granularity, size_t size)
Allocates a pool with size elements each of granularity bytes.
Definition: stl_pool.cpp:36
void * Allocate()
Allocates memory from the pool without construction.
Definition: stl_pool.cpp:64
size_t GetUsed() const
Gets the number of elements allocated from pooled storage.
Definition: stl_pool.h:59
#define T
Definition: w_okvl_inl.h:45
void Deallocate(void *block)
Deallocates memory from the pool without destruction.
Definition: stl_pool.cpp:74
T get() const
Definition: guard.h:283
size_t m_granularity
The size of each element in the pool in bytes.
Definition: stl_pool.h:97