Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryBlockPool.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * MemoryBlockPool.h
17  * Allocates and Releases MemoryBlocks.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_IO_MEMORYBLOCKPOOL_H_
22 #define INCLUDE_OLA_IO_MEMORYBLOCKPOOL_H_
23 
24 #include <ola/Logging.h>
25 #include <ola/io/MemoryBlock.h>
26 #include <queue>
27 
28 namespace ola {
29 namespace io {
30 
35  public:
36  explicit MemoryBlockPool(unsigned int block_size = DEFAULT_BLOCK_SIZE)
37  : m_block_size(block_size),
38  m_blocks_allocated(0) {
39  }
40  ~MemoryBlockPool() {
41  Purge();
42  }
43 
44  // Allocate a new MemoryBlock from the pool. May return NULL if allocation
45  // fails.
46  MemoryBlock *Allocate() {
47  if (m_free_blocks.empty()) {
48  uint8_t* data = new uint8_t[m_block_size];
49  OLA_DEBUG << "new block allocated at @" << reinterpret_cast<int*>(data);
50  if (data) {
51  m_blocks_allocated++;
52  return new MemoryBlock(data, m_block_size);
53  } else {
54  return NULL;
55  }
56  } else {
57  MemoryBlock *block = m_free_blocks.front();
58  m_free_blocks.pop();
59  return block;
60  }
61  }
62 
63  // Release a MemoryBlock back to the pool.
64  void Release(MemoryBlock *block) {
65  m_free_blocks.push(block);
66  }
67 
68  // Returns the number of free blocks in the pool.
69  unsigned int FreeBlocks() const {
70  return static_cast<unsigned int>(m_free_blocks.size());
71  }
72 
73  // Deletes all free blocks.
74  void Purge() {
75  Purge(0);
76  }
77 
78  // Delete all but remaining free blocks.
79  void Purge(unsigned int remaining) {
80  while (m_free_blocks.size() != remaining) {
81  MemoryBlock *block = m_free_blocks.front();
82  m_blocks_allocated--;
83  delete block;
84  m_free_blocks.pop();
85  }
86  }
87 
88  unsigned int BlocksAllocated() const { return m_blocks_allocated; }
89 
90  // default to 1k blocks
91  static const unsigned int DEFAULT_BLOCK_SIZE = 1024;
92 
93  private:
94  std::queue<MemoryBlock*> m_free_blocks;
95  const unsigned int m_block_size;
96  unsigned int m_blocks_allocated;
97 };
98 } // namespace io
99 } // namespace ola
100 #endif // INCLUDE_OLA_IO_MEMORYBLOCKPOOL_H_