Stxxl  1.3.2
mng.h
1 /***************************************************************************
2  * include/stxxl/bits/mng/mng.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2007 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007, 2009 Johannes Singler <[email protected]>
8  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_MNG_HEADER
16 #define STXXL_MNG_HEADER
17 
18 #include <memory>
19 #include <iostream>
20 #include <iomanip>
21 #include <fstream>
22 #include <vector>
23 #include <map>
24 #include <algorithm>
25 #include <string>
26 #include <cstdlib>
27 
28 #ifdef STXXL_BOOST_CONFIG
29  #include <boost/config.hpp>
30 #endif
31 
32 #ifdef BOOST_MSVC
33 #include <memory.h>
34 #endif
35 
36 #include <stxxl/bits/deprecated.h>
37 #include <stxxl/bits/io/request.h>
38 #include <stxxl/bits/io/file.h>
39 #include <stxxl/bits/io/create_file.h>
40 #include <stxxl/bits/noncopyable.h>
41 #include <stxxl/bits/singleton.h>
42 #include <stxxl/bits/mng/bid.h>
43 #include <stxxl/bits/mng/diskallocator.h>
44 #include <stxxl/bits/mng/block_alloc.h>
45 #include <stxxl/bits/mng/config.h>
46 
47 
48 __STXXL_BEGIN_NAMESPACE
49 
54 
56 
59 class block_manager : public singleton<block_manager>
60 {
61  friend class singleton<block_manager>;
62 
63  DiskAllocator ** disk_allocators;
64  file ** disk_files;
65 
66  unsigned ndisks;
67  block_manager();
68 
69 protected:
70  template <class BIDType, class DiskAssignFunctor, class BIDIteratorClass>
71  void new_blocks_int(
72  const unsigned_type nblocks,
73  const DiskAssignFunctor & functor,
74  unsigned_type offset,
75  BIDIteratorClass out);
76 
77 public:
79 
89  template <class DiskAssignFunctor, class BIDIteratorClass>
90  void new_blocks(
91  const DiskAssignFunctor & functor,
92  BIDIteratorClass bidbegin,
93  BIDIteratorClass bidend,
94  unsigned_type offset = 0)
95  {
96  typedef typename std::iterator_traits<BIDIteratorClass>::value_type bid_type;
97  new_blocks_int<bid_type>(std::distance(bidbegin, bidend), functor, offset, bidbegin);
98  }
99 
111  template <class BlockType, class DiskAssignFunctor, class BIDIteratorClass>
113  const unsigned_type nblocks,
114  const DiskAssignFunctor & functor,
115  BIDIteratorClass out,
116  unsigned_type offset = 0)
117  {
118  typedef typename BlockType::bid_type bid_type;
119  new_blocks_int<bid_type>(nblocks, functor, offset, out);
120  }
121 
130  template <typename DiskAssignFunctor, unsigned BLK_SIZE>
131  void new_block(const DiskAssignFunctor & functor, BID<BLK_SIZE> & bid, unsigned_type offset = 0)
132  {
133  new_blocks_int<BID<BLK_SIZE> >(1, functor, offset, &bid);
134  }
135 
137 
141  template <class BIDIteratorClass>
142  void delete_blocks(const BIDIteratorClass & bidbegin, const BIDIteratorClass & bidend);
143 
146  template <unsigned BLK_SIZE>
147  void delete_block(const BID<BLK_SIZE> & bid);
148 
149  ~block_manager();
150 };
151 
152 
153 template <class BIDType, class DiskAssignFunctor, class OutputIterator>
154 void block_manager::new_blocks_int(
155  const unsigned_type nblocks,
156  const DiskAssignFunctor & functor,
157  unsigned_type offset,
158  OutputIterator out)
159 {
160  typedef BIDType bid_type;
161  typedef BIDArray<bid_type::t_size> bid_array_type;
162 
163  int_type * bl = new int_type[ndisks];
164  bid_array_type * disk_bids = new bid_array_type[ndisks];
165  file ** disk_ptrs = new file *[nblocks];
166 
167  memset(bl, 0, ndisks * sizeof(int_type));
168 
169  unsigned_type i;
170  for (i = 0; i < nblocks; ++i)
171  {
172  const int disk = functor(offset + i);
173  disk_ptrs[i] = disk_files[disk];
174  bl[disk]++;
175  }
176 
177  for (i = 0; i < ndisks; ++i)
178  {
179  if (bl[i])
180  {
181  disk_bids[i].resize(bl[i]);
182  disk_allocators[i]->new_blocks(disk_bids[i]);
183  }
184  }
185 
186  memset(bl, 0, ndisks * sizeof(int_type));
187 
188  OutputIterator it = out;
189  for (i = 0; i != nblocks; ++it, ++i)
190  {
191  const int disk = disk_ptrs[i]->get_allocator_id();
192  bid_type bid(disk_ptrs[i], disk_bids[disk][bl[disk]++].offset);
193  *it = bid;
194  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:new " << FMT_BID(bid));
195  }
196 
197  delete[] bl;
198  delete[] disk_bids;
199  delete[] disk_ptrs;
200 }
201 
202 
203 template <unsigned BLK_SIZE>
205 {
206  // do not uncomment it
207  //assert(bid.storage->get_allocator_id() < config::get_instance()->disks_number());
208  if (!bid.is_managed())
209  return; // self managed disk
210  STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:delete " << FMT_BID(bid));
211  assert(bid.storage->get_allocator_id() >= 0);
212  disk_allocators[bid.storage->get_allocator_id()]->delete_block(bid);
213  disk_files[bid.storage->get_allocator_id()]->discard(bid.offset, bid.size);
214 }
215 
216 
217 template <class BIDIteratorClass>
219  const BIDIteratorClass & bidbegin,
220  const BIDIteratorClass & bidend)
221 {
222  for (BIDIteratorClass it = bidbegin; it != bidend; it++)
223  {
224  delete_block(*it);
225  }
226 }
227 
228 // in bytes
229 #ifndef STXXL_DEFAULT_BLOCK_SIZE
230  #define STXXL_DEFAULT_BLOCK_SIZE(type) (2 * 1024 * 1024) // use traits
231 #endif
232 
233 
234 class FileCreator
235 {
236 public:
237  _STXXL_DEPRECATED(
238  static file * create(const std::string & io_impl,
239  const std::string & filename,
240  int options,
241  int queue_id = file::DEFAULT_QUEUE,
242  int allocator_id = file::NO_ALLOCATOR)
243  )
244  {
245  return create_file(io_impl, filename, options, queue_id, allocator_id);
246  }
247 };
248 
250 
251 __STXXL_END_NAMESPACE
252 
253 #endif // !STXXL_MNG_HEADER
254 // vim: et:ts=4:sw=4
virtual void discard(offset_type offset, offset_type size)
Discard a region of the file (mark it unused) some specialized file types may need to know freed regi...
Definition: file.h:190
Defines interface of file.
Definition: file.h:90
Block size.
Definition: bid.h:44
file * storage
pointer to the file of the block
Definition: bid.h:48
virtual int get_allocator_id() const =0
Returns the file&#39;s allocator.
void delete_blocks(const BIDIteratorClass &bidbegin, const BIDIteratorClass &bidend)
Deallocates blocks.
Definition: mng.h:218
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:49
void new_blocks(const DiskAssignFunctor &functor, BIDIteratorClass bidbegin, BIDIteratorClass bidend, unsigned_type offset=0)
Allocates new blocks.
Definition: mng.h:90
void new_block(const DiskAssignFunctor &functor, BID< BLK_SIZE > &bid, unsigned_type offset=0)
Definition: mng.h:131
void delete_block(const BID< BLK_SIZE > &bid)
Deallocates a block.
Definition: mng.h:204
void new_blocks(const unsigned_type nblocks, const DiskAssignFunctor &functor, BIDIteratorClass out, unsigned_type offset=0)
Definition: mng.h:112
Block manager class.
Definition: mng.h:59