16 #ifndef FOXXLL_MNG_DISK_BLOCK_ALLOCATOR_HEADER 17 #define FOXXLL_MNG_DISK_BLOCK_ALLOCATOR_HEADER 47 constexpr
static bool debug =
false;
94 template <
size_t BlockSize>
100 template <
typename BIDIterator>
101 void new_blocks(BIDIterator begin, BIDIterator end);
103 template <
size_t BlockSize>
106 for (
size_t i = 0; i < bids.
size(); ++i)
110 template <
size_t BlockSize>
113 std::unique_lock<std::mutex> lock(
mutex_);
115 TLX_LOG0 <<
"disk_block_allocator::delete_block<" << BlockSize
116 <<
">(pos=" << bid.
offset <<
", size=" << size_t(bid.
size)
124 using place = std::pair<uint64_t, uint64_t>;
139 uint64_t block_pos, uint64_t block_size,
140 const space_map_type::iterator& pred,
141 const space_map_type::iterator& succ)
const;
149 if (extend_bytes == 0)
152 storage_->
set_size(disk_bytes_ + extend_bytes);
154 disk_bytes_ += extend_bytes;
158 template <
typename BIDIterator>
161 uint64_t requested_size = 0;
163 for (BIDIterator cur = begin; cur != end; ++cur)
165 TLX_LOG <<
"Asking for a block with size: " << cur->size;
166 requested_size += cur->size;
169 std::unique_lock<std::mutex> lock(
mutex_);
171 TLX_LOG <<
"disk_block_allocator::new_blocks<BlockSize>" 172 ", BlockSize = " << begin->size <<
174 ", blocks: " << (end - begin) <<
175 ", requested_size=" << requested_size;
182 "Out of external memory error: " << requested_size <<
184 "Maybe enable autogrow_ flags?" 189 <<
"External memory block allocation error: " << requested_size
191 <<
" bytes free. Trying to extend the external memory space...";
198 space_map_type::iterator space =
201 [requested_size](
const place& entry) {
202 return (entry.second >= requested_size);
206 if (space ==
free_space_.end() && begin + 1 == end)
209 TLX_LOG1 <<
"Warning: Severe external memory space fragmentation!";
212 TLX_LOG1 <<
"External memory block allocation error: " << requested_size <<
214 " bytes free. Trying to extend the external memory space...";
219 space = std::find_if(
221 [requested_size](
const place& entry) {
222 return (entry.second >= requested_size);
229 uint64_t region_pos = (*space).first;
230 uint64_t region_size = (*space).second;
233 if (region_size > requested_size)
234 free_space_[region_pos + requested_size] = region_size - requested_size;
236 for (uint64_t pos = region_pos; begin != end; ++begin)
250 TLX_LOG <<
"Warning, when allocating an external memory space, no" 251 "contiguous region found. It might harm the performance";
253 assert(end - begin > 1);
257 BIDIterator middle = begin + ((end - begin) / 2);
266 #endif // !FOXXLL_MNG_DISK_BLOCK_ALLOCATOR_HEADER external_size_type size
file size to initially allocate
virtual void set_size(offset_type newsize)=0
iterator end() noexcept
return mutable iterator beyond last element
void add_free_region(uint64_t block_pos, uint64_t block_size)
#define FOXXLL_THROW(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message]".
Simpler non-growing vector without initialization.
external_size_type offset
offset within the file of the block (uint64_t)
disk_block_allocator(file *storage, const disk_config &cfg)
static constexpr bool debug
bool has_available_space(uint64_t bytes) const
std::map< uint64_t, uint64_t > space_map_type
iterator begin() noexcept
return mutable iterator to first element
void delete_blocks(const BIDArray< BlockSize > &bids)
void deallocation_error(uint64_t block_pos, uint64_t block_size, const space_map_type::iterator &pred, const space_map_type::iterator &succ) const
size_type size() const noexcept
return number of items in vector
#define TLX_LOG0
Override default output: never or always output log.
void new_blocks(BIDArray< BlockSize > &bids)
disk_block_allocator & operator=(const disk_block_allocator &)=delete
non-copyable: delete assignment operator
static const size_t bytes
number of bytes in uint_pair
space_map_type free_space_
map of free space as places
void grow_file(uint64_t extend_bytes)
uint64_t free_bytes() const
void delete_block(const BID< BlockSize > &bid)
std::pair< uint64_t, uint64_t > place
pair (offset, size) used for free space calculation
uint64_t used_bytes() const
bool autogrow() const
Returns autogrow.
static constexpr size_t size
Block size.
uint64_t total_bytes() const
This class manages allocation of blocks onto a single disk.
#define TLX_LOG
Default logging method: output if the local debug variable is true.