~ubuntu-branches/ubuntu/wily/libde265/wily

« back to all changes in this revision

Viewing changes to libde265/alloc_pool.cc

  • Committer: Package Import Robot
  • Author(s): Joachim Bauch
  • Date: 2015-07-16 11:07:46 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150716110746-76vsv24j3yux7tnu
Tags: 1.0.2-1
* Imported Upstream version 1.0.2
* Added new files to copyright information.
* Only export decoder API and update symbols for new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * H.265 video codec.
 
3
 * Copyright (c) 2014 struktur AG, Dirk Farin <farin@struktur.de>
 
4
 *
 
5
 * Authors: Dirk Farin <farin@struktur.de>
 
6
 *
 
7
 * This file is part of libde265.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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/>.
 
21
 */
 
22
 
 
23
#include "libde265/alloc_pool.h"
 
24
#include "libde265/util.h"
 
25
#include <assert.h>
 
26
#include <stdio.h>
 
27
 
 
28
#define DEBUG_MEMORY 1
 
29
 
 
30
 
 
31
alloc_pool::alloc_pool(size_t objSize, int poolSize, bool grow)
 
32
  : mObjSize(objSize),
 
33
    mPoolSize(poolSize),
 
34
    mGrow(grow)
 
35
{
 
36
  m_freeList.reserve(poolSize);
 
37
  m_memBlocks.reserve(8);
 
38
 
 
39
  add_memory_block();
 
40
}
 
41
 
 
42
 
 
43
void alloc_pool::add_memory_block()
 
44
{
 
45
  uint8_t* p = new uint8_t[mObjSize * mPoolSize];
 
46
  m_memBlocks.push_back(p);
 
47
 
 
48
  for (int i=0;i<mPoolSize;i++)
 
49
    {
 
50
      m_freeList.push_back(p + (mPoolSize-1-i) * mObjSize);
 
51
    }
 
52
}
 
53
 
 
54
alloc_pool::~alloc_pool()
 
55
{
 
56
  FOR_LOOP(uint8_t*, p, m_memBlocks) {
 
57
    delete[] p;
 
58
  }
 
59
}
 
60
 
 
61
 
 
62
void* alloc_pool::new_obj(const size_t size)
 
63
{
 
64
  if (size != mObjSize) {
 
65
    return ::operator new(size);
 
66
  }
 
67
 
 
68
  if (m_freeList.size()==0) {
 
69
    if (mGrow) {
 
70
      add_memory_block();
 
71
      if (DEBUG_MEMORY) { fprintf(stderr,"additional block allocated in memory pool\n"); }
 
72
    }
 
73
    else {
 
74
      return NULL;
 
75
    }
 
76
  }
 
77
 
 
78
  assert(!m_freeList.empty());
 
79
 
 
80
  void* p = m_freeList.back();
 
81
  m_freeList.pop_back();
 
82
 
 
83
  return p;
 
84
}
 
85
 
 
86
 
 
87
void  alloc_pool::delete_obj(void* obj)
 
88
{
 
89
  int memBlockSize = mObjSize * mPoolSize;
 
90
 
 
91
  FOR_LOOP(uint8_t*, memBlk, m_memBlocks) {
 
92
    if (memBlk <= obj && obj < memBlk + memBlockSize) {
 
93
      m_freeList.push_back(obj);
 
94
      return;
 
95
    }
 
96
  }
 
97
 
 
98
  ::operator delete(obj);
 
99
}