15 #ifndef STXXL_DISKALLOCATOR_HEADER
16 #define STXXL_DISKALLOCATOR_HEADER
22 #include <stxxl/bits/noncopyable.h>
23 #include <stxxl/bits/parallel.h>
24 #include <stxxl/bits/mng/bid.h>
25 #include <stxxl/bits/verbose.h>
26 #include <stxxl/bits/io/file.h>
29 __STXXL_BEGIN_NAMESPACE
34 class DiskAllocator :
private noncopyable
36 typedef std::pair<stxxl::int64, stxxl::int64> place;
38 struct FirstFit :
public std::binary_function<place, stxxl::int64, bool>
42 const stxxl::int64 size)
const
44 return (entry.second >= size);
48 typedef std::map<stxxl::int64, stxxl::int64> sortseq;
52 stxxl::int64 free_bytes;
53 stxxl::int64 disk_bytes;
54 stxxl::file * storage;
59 void deallocation_error(
60 stxxl::int64 block_pos, stxxl::int64 block_size,
61 const sortseq::iterator & pred,
const sortseq::iterator & succ)
const;
64 void add_free_region(stxxl::int64 block_pos, stxxl::int64 block_size);
67 void grow_file(stxxl::int64 extend_bytes)
72 storage->set_size(disk_bytes + extend_bytes);
73 add_free_region(disk_bytes, extend_bytes);
74 disk_bytes += extend_bytes;
78 DiskAllocator(stxxl::file * storage, stxxl::int64 disk_size) :
82 autogrow(disk_size == 0)
87 inline stxxl::int64 get_free_bytes()
const
92 inline stxxl::int64 get_used_bytes()
const
94 return disk_bytes - free_bytes;
97 inline stxxl::int64 get_total_bytes()
const
102 template <
unsigned BLK_SIZE>
103 void new_blocks(BIDArray<BLK_SIZE> & bids)
105 new_blocks(bids.begin(), bids.end());
108 template <
unsigned BLK_SIZE>
112 template <
unsigned BLK_SIZE>
113 void delete_blocks(
const BIDArray<BLK_SIZE> & bids)
115 for (
unsigned i = 0; i < bids.size(); ++i)
116 delete_block(bids[i]);
120 template <
unsigned BLK_SIZE>
125 STXXL_VERBOSE2(
"DiskAllocator::delete_block<" << BLK_SIZE <<
126 ">(pos=" << bid.
offset <<
", size=" << bid.
size <<
127 "), free:" << free_bytes <<
" total:" << disk_bytes);
133 template <
unsigned BLK_SIZE>
136 stxxl::int64 requested_size = 0;
140 STXXL_VERBOSE2(
"Asking for a block with size: " << (cur->size));
141 requested_size += cur->size;
146 STXXL_VERBOSE2(
"DiskAllocator::new_blocks<BLK_SIZE>, BLK_SIZE = " << BLK_SIZE <<
147 ", free:" << free_bytes <<
" total:" << disk_bytes <<
148 ", blocks: " << (end - begin) <<
149 " begin: " << static_cast<void *>(begin) <<
150 " end: " << static_cast<void *>(end) <<
151 ", requested_size=" << requested_size);
153 if (free_bytes < requested_size)
156 STXXL_ERRMSG(
"External memory block allocation error: " << requested_size <<
157 " bytes requested, " << free_bytes <<
158 " bytes free. Trying to extend the external memory space...");
161 grow_file(requested_size);
166 sortseq::iterator space;
167 space = std::find_if(free_space.begin(), free_space.end(),
168 bind2nd(FirstFit(), requested_size) _STXXL_FORCE_SEQUENTIAL);
170 if (space == free_space.end() && requested_size == BLK_SIZE)
172 assert(end - begin == 1);
174 STXXL_ERRMSG(
"Warning: Severe external memory space fragmentation!");
177 STXXL_ERRMSG(
"External memory block allocation error: " << requested_size <<
178 " bytes requested, " << free_bytes <<
179 " bytes free. Trying to extend the external memory space...");
183 space = std::find_if(free_space.begin(), free_space.end(),
184 bind2nd(FirstFit(), requested_size) _STXXL_FORCE_SEQUENTIAL);
187 if (space != free_space.end())
189 stxxl::int64 region_pos = (*space).first;
190 stxxl::int64 region_size = (*space).second;
191 free_space.erase(space);
192 if (region_size > requested_size)
193 free_space[region_pos + requested_size] = region_size - requested_size;
195 for (stxxl::int64 pos = region_pos; begin != end; ++begin)
200 free_bytes -= requested_size;
207 STXXL_VERBOSE1(
"Warning, when allocating an external memory space, no contiguous region found");
208 STXXL_VERBOSE1(
"It might harm the performance");
210 assert(requested_size > BLK_SIZE);
211 assert(end - begin > 1);
216 new_blocks(begin, middle);
217 new_blocks(middle, end);
222 __STXXL_END_NAMESPACE
224 #endif // !STXXL_DISKALLOCATOR_HEADER
Block size.
Definition: bid.h:44
Aquire a lock that's valid until the end of scope.
Definition: mutex.h:82
stxxl::int64 offset
offset within the file of the block
Definition: bid.h:49