Open Lighting Architecture  Latest Git
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 
36  public:
37  explicit MemoryBlockPool(unsigned int block_size = DEFAULT_BLOCK_SIZE)
38  : m_block_size(block_size),
39  m_blocks_allocated(0) {
40  }
41  ~MemoryBlockPool() {
42  Purge();
43  }
44 
45  // Allocate a new MemoryBlock from the pool. May return NULL if allocation
46  // fails.
47  MemoryBlock *Allocate() {
48  if (m_free_blocks.empty()) {
49  uint8_t* data = new uint8_t[m_block_size];
50  OLA_DEBUG << "new block allocated at @" << reinterpret_cast<int*>(data);
51  if (data) {
52  m_blocks_allocated++;
53  return new MemoryBlock(data, m_block_size);
54  } else {
55  return NULL;
56  }
57  } else {
58  MemoryBlock *block = m_free_blocks.front();
59  m_free_blocks.pop();
60  return block;
61  }
62  }
63 
64  // Release a MemoryBlock back to the pool.
65  void Release(MemoryBlock *block) {
66  m_free_blocks.push(block);
67  }
68 
69  // Returns the number of free blocks in the pool.
70  unsigned int FreeBlocks() const {
71  return static_cast<unsigned int>(m_free_blocks.size());
72  }
73 
74  // Deletes all free blocks.
75  void Purge() {
76  Purge(0);
77  }
78 
79  // Delete all but remaining free blocks.
80  void Purge(unsigned int remaining) {
81  while (m_free_blocks.size() != remaining) {
82  MemoryBlock *block = m_free_blocks.front();
83  m_blocks_allocated--;
84  delete block;
85  m_free_blocks.pop();
86  }
87  }
88 
89  unsigned int BlocksAllocated() const { return m_blocks_allocated; }
90 
91  // default to 1k blocks
92  static const unsigned int DEFAULT_BLOCK_SIZE = 1024;
93 
94  private:
95  std::queue<MemoryBlock*> m_free_blocks;
96  const unsigned int m_block_size;
97  unsigned int m_blocks_allocated;
98 };
99 } // namespace io
100 } // namespace ola
101 #endif // INCLUDE_OLA_IO_MEMORYBLOCKPOOL_H_
Wraps a memory region.
#define OLA_DEBUG
Definition: Logging.h:91
A MemoryBlock encapsulates a chunk of memory. It&#39;s used by the IOQueue and IOStack classes...
Definition: MemoryBlock.h:41
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Header file for OLA Logging.
MemoryBlockPool. This class is not thread safe.
Definition: MemoryBlockPool.h:35