3
* Copyright (c) 2014 struktur AG, Dirk Farin <farin@struktur.de>
5
* Authors: Dirk Farin <farin@struktur.de>
7
* This file is part of libde265.
9
* libde265 is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as
11
* published by the Free Software Foundation, either version 3 of
12
* the License, or (at your option) any later version.
14
* libde265 is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
23
#include "libde265/alloc_pool.h"
24
#include "libde265/util.h"
28
#define DEBUG_MEMORY 1
31
alloc_pool::alloc_pool(size_t objSize, int poolSize, bool grow)
36
m_freeList.reserve(poolSize);
37
m_memBlocks.reserve(8);
43
void alloc_pool::add_memory_block()
45
uint8_t* p = new uint8_t[mObjSize * mPoolSize];
46
m_memBlocks.push_back(p);
48
for (int i=0;i<mPoolSize;i++)
50
m_freeList.push_back(p + (mPoolSize-1-i) * mObjSize);
54
alloc_pool::~alloc_pool()
56
FOR_LOOP(uint8_t*, p, m_memBlocks) {
62
void* alloc_pool::new_obj(const size_t size)
64
if (size != mObjSize) {
65
return ::operator new(size);
68
if (m_freeList.size()==0) {
71
if (DEBUG_MEMORY) { fprintf(stderr,"additional block allocated in memory pool\n"); }
78
assert(!m_freeList.empty());
80
void* p = m_freeList.back();
81
m_freeList.pop_back();
87
void alloc_pool::delete_obj(void* obj)
89
int memBlockSize = mObjSize * mPoolSize;
91
FOR_LOOP(uint8_t*, memBlk, m_memBlocks) {
92
if (memBlk <= obj && obj < memBlk + memBlockSize) {
93
m_freeList.push_back(obj);
98
::operator delete(obj);