http://stxxl.sourceforge.net
<dementiev@mpi-sb.mpg.de>
<singler@ira.uka.de>
<beckmann@cs.uni-frankfurt.de>
http://www.boost.org/LICENSE_1_0.txt
#ifndef STXXL_MNG_HEADER
#define STXXL_MNG_HEADER
#include <memory>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cstdlib>
#ifdef STXXL_BOOST_CONFIG
#include <boost/config.hpp>
#endif
#ifdef BOOST_MSVC
#include <memory.h>
#endif
#include <stxxl/bits/deprecated.h>
#include <stxxl/bits/io/request.h>
#include <stxxl/bits/io/file.h>
#include <stxxl/bits/io/create_file.h>
#include <stxxl/bits/noncopyable.h>
#include <stxxl/bits/singleton.h>
#include <stxxl/bits/mng/bid.h>
#include <stxxl/bits/mng/diskallocator.h>
#include <stxxl/bits/mng/block_alloc.h>
#include <stxxl/bits/mng/config.h>
__STXXL_BEGIN_NAMESPACE
class block_manager : public singleton<block_manager>
{
friend class singleton<block_manager>;
DiskAllocator ** disk_allocators;
file ** disk_files;
unsigned ndisks;
block_manager();
unsigned long long m_totalalloc, m_maxalloc;
protected:
template <class BIDType, class DiskAssignFunctor, class BIDIteratorClass>
void new_blocks_int(
const unsigned_type nblocks,
const DiskAssignFunctor & functor,
unsigned_type offset,
BIDIteratorClass out);
public:
template <class DiskAssignFunctor, class BIDIteratorClass>
void new_blocks(
const DiskAssignFunctor & functor,
BIDIteratorClass bidbegin,
BIDIteratorClass bidend,
unsigned_type offset = 0)
{
typedef typename std::iterator_traits<BIDIteratorClass>::value_type bid_type;
new_blocks_int<bid_type>(std::distance(bidbegin, bidend), functor, offset, bidbegin);
}
template <class BlockType, class DiskAssignFunctor, class BIDIteratorClass>
void new_blocks(
const unsigned_type nblocks,
const DiskAssignFunctor & functor,
BIDIteratorClass out,
unsigned_type offset = 0)
{
typedef typename BlockType::bid_type bid_type;
new_blocks_int<bid_type>(nblocks, functor, offset, out);
}
template <typename DiskAssignFunctor, unsigned BLK_SIZE>
void new_block(const DiskAssignFunctor & functor, BID<BLK_SIZE> & bid, unsigned_type offset = 0)
{
new_blocks_int<BID<BLK_SIZE> >(1, functor, offset, &bid);
}
template <class BIDIteratorClass>
void delete_blocks(const BIDIteratorClass & bidbegin, const BIDIteratorClass & bidend);
template <unsigned BLK_SIZE>
void delete_block(const BID<BLK_SIZE> & bid);
~block_manager();
unsigned long long current_allocated() const
{
return m_totalalloc;
}
unsigned long long max_allocated() const
{
return m_maxalloc;
}
};
template <class BIDType, class DiskAssignFunctor, class OutputIterator>
void block_manager::new_blocks_int(
const unsigned_type nblocks,
const DiskAssignFunctor & functor,
unsigned_type offset,
OutputIterator out)
{
typedef BIDType bid_type;
typedef BIDArray<bid_type::t_size> bid_array_type;
int_type * bl = new int_type[ndisks];
bid_array_type * disk_bids = new bid_array_type[ndisks];
file ** disk_ptrs = new file *[nblocks];
memset(bl, 0, ndisks * sizeof(int_type));
unsigned_type i;
for (i = 0; i < nblocks; ++i)
{
const int disk = functor(offset + i);
disk_ptrs[i] = disk_files[disk];
bl[disk]++;
}
for (i = 0; i < ndisks; ++i)
{
if (bl[i])
{
disk_bids[i].resize(bl[i]);
disk_allocators[i]->new_blocks(disk_bids[i]);
}
}
m_totalalloc += nblocks * BIDType::size;
m_maxalloc = std::max(m_maxalloc, m_totalalloc);
memset(bl, 0, ndisks * sizeof(int_type));
OutputIterator it = out;
for (i = 0; i != nblocks; ++it, ++i)
{
const int disk = disk_ptrs[i]->get_allocator_id();
bid_type bid(disk_ptrs[i], disk_bids[disk][bl[disk]++].offset);
*it = bid;
STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:new " << FMT_BID(bid));
}
delete[] bl;
delete[] disk_bids;
delete[] disk_ptrs;
}
template <unsigned BLK_SIZE>
void block_manager::delete_block(const BID<BLK_SIZE> & bid)
{
m_totalalloc -= BLK_SIZE;
if (!bid.is_managed())
return;
STXXL_VERBOSE_BLOCK_LIFE_CYCLE("BLC:delete " << FMT_BID(bid));
assert(bid.storage->get_allocator_id() >= 0);
disk_allocators[bid.storage->get_allocator_id()]->delete_block(bid);
disk_files[bid.storage->get_allocator_id()]->discard(bid.offset, bid.size);
}
template <class BIDIteratorClass>
void block_manager::delete_blocks(
const BIDIteratorClass & bidbegin,
const BIDIteratorClass & bidend)
{
for (BIDIteratorClass it = bidbegin; it != bidend; it++)
{
delete_block(*it);
}
}
#ifndef STXXL_DEFAULT_BLOCK_SIZE
#define STXXL_DEFAULT_BLOCK_SIZE(type) (2 * 1024 * 1024)
#endif
class FileCreator
{
public:
_STXXL_DEPRECATED(
static file * create(const std::string & io_impl,
const std::string & filename,
int options,
int queue_id = file::DEFAULT_QUEUE,
int allocator_id = file::NO_ALLOCATOR)
)
{
return create_file(io_impl, filename, options, queue_id, allocator_id);
}
};
__STXXL_END_NAMESPACE
#endif