TPIE

2362a60
freespace_collection.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet cino+=(0 :
3 // Copyright 2014, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_BLOCKS_FREESPACE_COLLECTION_H
25 #define _TPIE_BLOCKS_FREESPACE_COLLECTION_H
26 
27 #include <tpie/tpie.h>
28 #include <tpie/tpie_assert.h>
29 #include <tpie/blocks/block.h>
30 #include <tpie/stack.h>
31 #include <limits>
32 
33 namespace tpie {
34 
35 namespace blocks {
36 
37 namespace bits {
38 
40 private:
42  stream_size_type m_end;
43  memory_size_type m_blockSize;
44 public:
45  freespace_collection(const std::string & path, const memory_size_type blockSize)
46  : m_free(path)
47  , m_end(0)
48  , m_blockSize(blockSize)
49  {
50  if(m_free.size() > 0) { // when closed the top element of the stacked is an infinitely large block representing the end of the file
51  m_end = m_free.pop().position;
52  }
53  }
54 
56  // when closed the top element of the stacked is an infinitely large block representing the end of the file
57  m_free.push(block_handle(m_end, std::numeric_limits<stream_size_type>::max()));
58  }
59 
60  void free(block_handle handle) {
61  tp_assert(handle.size == m_blockSize, "the size of the given handle is incorrect");
62  m_free.push(handle);
63  }
64 
65  block_handle alloc() {
66  if(!m_free.empty())
67  return m_free.pop();
68 
69  block_handle h(m_end, m_blockSize);
70  m_end += m_blockSize;
71  return h;
72  }
73 
74  stream_size_type size() {
75  return m_end;
76  }
77 };
78 
79 } // bits namespace
80 
81 } // blocks namespace
82 
83 } // tpie namespace
84 
85 #endif // _TPIE_BLOCKS_FREESPACE_COLLECTION_H
Defines the tp_assert macro.
tpie_init and tpie_finish.
An implementation of an external-memory stack.
Definition: stack.h:40
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
External memory stack.