2013-04-16 23:57:18 -04:00
/* -*- mode: C; c-basic-offset: 4 -*- */
2013-04-16 23:57:48 -04:00
# ident "Copyright (c) 2009 Tokutek Inc. All rights reserved."
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-16 23:57:18 -04:00
2013-04-16 23:57:20 -04:00
# include "includes.h"
2013-04-16 23:57:18 -04:00
// Here's a very simple implementation.
// It's not very fast at allocating or freeing.
2013-04-16 23:57:59 -04:00
// Previous implementation used next_fit, but now use first_fit since we are moving blocks around to reduce file size.
2013-04-16 23:57:18 -04:00
struct blockpair {
u_int64_t offset ;
u_int64_t size ;
} ;
struct block_allocator {
u_int64_t reserve_at_beginning ; // How much to reserve at the beginning
2013-04-16 23:57:18 -04:00
u_int64_t alignment ; // Block alignment
2013-04-16 23:57:18 -04:00
u_int64_t n_blocks ; // How many blocks
2013-04-16 23:57:41 -04:00
u_int64_t blocks_array_size ; // How big is the blocks_array. Must be >= n_blocks.
2013-04-16 23:57:18 -04:00
struct blockpair * blocks_array ; // These blocks are sorted by address.
2013-04-16 23:57:59 -04:00
u_int64_t n_bytes_in_use ; // including the reserve_at_beginning
2013-04-16 23:57:18 -04:00
} ;
void
block_allocator_validate ( BLOCK_ALLOCATOR ba ) {
u_int64_t i ;
2013-04-16 23:57:59 -04:00
u_int64_t n_bytes_in_use = ba - > reserve_at_beginning ;
2013-04-16 23:57:18 -04:00
for ( i = 0 ; i < ba - > n_blocks ; i + + ) {
2013-04-16 23:57:59 -04:00
n_bytes_in_use + = ba - > blocks_array [ i ] . size ;
2013-04-16 23:57:18 -04:00
if ( i > 0 ) {
assert ( ba - > blocks_array [ i ] . offset > ba - > blocks_array [ i - 1 ] . offset ) ;
assert ( ba - > blocks_array [ i ] . offset > = ba - > blocks_array [ i - 1 ] . offset + ba - > blocks_array [ i - 1 ] . size ) ;
}
}
2013-04-16 23:57:59 -04:00
assert ( n_bytes_in_use = = ba - > n_bytes_in_use ) ;
2013-04-16 23:57:18 -04:00
}
2013-04-16 23:57:18 -04:00
#if 0
# define VALIDATE(b) block_allocator_validate(b)
# else
# define VALIDATE(b) ((void)0)
# endif
2013-04-16 23:57:18 -04:00
#if 0
void
block_allocator_print ( BLOCK_ALLOCATOR ba ) {
u_int64_t i ;
for ( i = 0 ; i < ba - > n_blocks ; i + + ) {
printf ( " % " PRId64 " :% " PRId64 " " , ba - > blocks_array [ i ] . offset , ba - > blocks_array [ i ] . size ) ;
}
printf ( " \n " ) ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
}
# endif
void
2013-04-16 23:57:18 -04:00
create_block_allocator ( BLOCK_ALLOCATOR * ba , u_int64_t reserve_at_beginning , u_int64_t alignment ) {
2013-04-16 23:57:41 -04:00
BLOCK_ALLOCATOR XMALLOC ( result ) ;
2013-04-16 23:57:18 -04:00
result - > reserve_at_beginning = reserve_at_beginning ;
2013-04-16 23:57:18 -04:00
result - > alignment = alignment ;
2013-04-16 23:57:18 -04:00
result - > n_blocks = 0 ;
result - > blocks_array_size = 1 ;
XMALLOC_N ( result - > blocks_array_size , result - > blocks_array ) ;
2013-04-16 23:57:59 -04:00
result - > n_bytes_in_use = reserve_at_beginning ;
2013-04-16 23:57:18 -04:00
* ba = result ;
2013-04-16 23:57:18 -04:00
VALIDATE ( result ) ;
2013-04-16 23:57:18 -04:00
}
void
destroy_block_allocator ( BLOCK_ALLOCATOR * bap ) {
BLOCK_ALLOCATOR ba = * bap ;
* bap = 0 ;
toku_free ( ba - > blocks_array ) ;
toku_free ( ba ) ;
}
static void
grow_blocks_array ( BLOCK_ALLOCATOR ba ) {
if ( ba - > n_blocks > = ba - > blocks_array_size ) {
ba - > blocks_array_size * = 2 ;
XREALLOC_N ( ba - > blocks_array_size , ba - > blocks_array ) ;
}
}
void
block_allocator_alloc_block_at ( BLOCK_ALLOCATOR ba , u_int64_t size , u_int64_t offset ) {
2013-04-16 23:57:18 -04:00
assert ( offset % ba - > alignment = = 0 ) ;
2013-04-16 23:57:18 -04:00
u_int64_t i ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
assert ( offset > = ba - > reserve_at_beginning ) ;
2013-04-16 23:57:18 -04:00
grow_blocks_array ( ba ) ;
// Just do a linear search for the block
2013-04-16 23:57:59 -04:00
ba - > n_bytes_in_use + = size ;
2013-04-16 23:57:18 -04:00
for ( i = 0 ; i < ba - > n_blocks ; i + + ) {
if ( ba - > blocks_array [ i ] . offset > offset ) {
// allocate it in that slot
// Don't do error checking, since we require that the blocks don't overlap.
// Slide everything over
2013-04-16 23:57:18 -04:00
memmove ( ba - > blocks_array + i + 1 , ba - > blocks_array + i , ( ba - > n_blocks - i ) * sizeof ( struct blockpair ) ) ;
2013-04-16 23:57:18 -04:00
ba - > blocks_array [ i ] . offset = offset ;
ba - > blocks_array [ i ] . size = size ;
ba - > n_blocks + + ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
return ;
}
}
// Goes at the end
ba - > blocks_array [ ba - > n_blocks ] . offset = offset ;
ba - > blocks_array [ ba - > n_blocks ] . size = size ;
ba - > n_blocks + + ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
}
2013-04-16 23:57:41 -04:00
2013-04-16 23:57:18 -04:00
static inline u_int64_t
2013-04-16 23:57:59 -04:00
align ( u_int64_t value , BLOCK_ALLOCATOR ba )
// Effect: align a value by rounding up.
{
2013-04-16 23:57:18 -04:00
return ( ( value + ba - > alignment - 1 ) / ba - > alignment ) * ba - > alignment ;
}
2013-04-16 23:57:18 -04:00
void
block_allocator_alloc_block ( BLOCK_ALLOCATOR ba , u_int64_t size , u_int64_t * offset ) {
grow_blocks_array ( ba ) ;
2013-04-16 23:57:59 -04:00
ba - > n_bytes_in_use + = size ;
2013-04-16 23:57:18 -04:00
if ( ba - > n_blocks = = 0 ) {
2013-04-16 23:57:59 -04:00
assert ( ba - > n_bytes_in_use = = ba - > reserve_at_beginning + size ) ; // we know exactly how many are in use
ba - > blocks_array [ 0 ] . offset = align ( ba - > reserve_at_beginning , ba ) ;
2013-04-16 23:57:18 -04:00
ba - > blocks_array [ 0 ] . size = size ;
2013-04-16 23:57:59 -04:00
* offset = ba - > blocks_array [ 0 ] . offset ;
2013-04-16 23:57:18 -04:00
ba - > n_blocks + + ;
return ;
}
2013-04-16 23:57:59 -04:00
// Implement first fit.
{
u_int64_t end_of_reserve = align ( ba - > reserve_at_beginning , ba ) ;
if ( end_of_reserve + size < = ba - > blocks_array [ 0 ] . offset ) {
// Check to see if the space immediately after the reserve is big enough to hold the new block.
struct blockpair * bp = & ba - > blocks_array [ 0 ] ;
memmove ( bp + 1 , bp , ( ba - > n_blocks ) * sizeof ( struct blockpair ) ) ;
bp [ 0 ] . offset = end_of_reserve ;
bp [ 0 ] . size = size ;
ba - > n_blocks + + ;
* offset = end_of_reserve ;
VALIDATE ( ba ) ;
return ;
}
}
for ( u_int64_t blocknum = 0 ; blocknum + 1 < ba - > n_blocks ; blocknum + + ) {
2013-04-16 23:57:18 -04:00
// Consider the space after blocknum
struct blockpair * bp = & ba - > blocks_array [ blocknum ] ;
u_int64_t this_offset = bp [ 0 ] . offset ;
u_int64_t this_size = bp [ 0 ] . size ;
2013-04-16 23:57:18 -04:00
u_int64_t answer_offset = align ( this_offset + this_size , ba ) ;
2013-04-16 23:57:18 -04:00
if ( answer_offset + size > bp [ 1 ] . offset ) continue ; // The block we want doesn't fit after this block.
// It fits, so allocate it here.
memmove ( bp + 2 , bp + 1 , ( ba - > n_blocks - blocknum - 1 ) * sizeof ( struct blockpair ) ) ;
bp [ 1 ] . offset = answer_offset ;
bp [ 1 ] . size = size ;
ba - > n_blocks + + ;
* offset = answer_offset ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
return ;
}
// It didn't fit anywhere, so fit it on the end.
struct blockpair * bp = & ba - > blocks_array [ ba - > n_blocks ] ;
2013-04-16 23:57:18 -04:00
u_int64_t answer_offset = align ( bp [ - 1 ] . offset + bp [ - 1 ] . size , ba ) ;
2013-04-16 23:57:18 -04:00
bp - > offset = answer_offset ;
bp - > size = size ;
ba - > n_blocks + + ;
* offset = answer_offset ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
}
static int64_t
find_block ( BLOCK_ALLOCATOR ba , u_int64_t offset )
// Find the index in the blocks array that has a particular offset. Requires that the block exist.
// Use binary search so it runs fast.
{
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
if ( ba - > n_blocks = = 1 ) {
assert ( ba - > blocks_array [ 0 ] . offset = = offset ) ;
return 0 ;
}
u_int64_t lo = 0 ;
u_int64_t hi = ba - > n_blocks ;
while ( 1 ) {
assert ( lo < hi ) ; // otherwise no such block exists.
u_int64_t mid = ( lo + hi ) / 2 ;
u_int64_t thisoff = ba - > blocks_array [ mid ] . offset ;
//printf("lo=%" PRId64 " hi=%" PRId64 " mid=%" PRId64 " thisoff=%" PRId64 " offset=%" PRId64 "\n", lo, hi, mid, thisoff, offset);
if ( thisoff < offset ) {
lo = mid + 1 ;
} else if ( thisoff > offset ) {
hi = mid ;
} else {
return mid ;
}
}
}
void
block_allocator_free_block ( BLOCK_ALLOCATOR ba , u_int64_t offset ) {
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
int64_t bn = find_block ( ba , offset ) ;
assert ( bn > = 0 ) ; // we require that there is a block with that offset. Might as well abort if no such block exists.
2013-04-16 23:57:59 -04:00
ba - > n_bytes_in_use - = ba - > blocks_array [ bn ] . size ;
2013-04-16 23:57:18 -04:00
memmove ( & ba - > blocks_array [ bn ] , & ba - > blocks_array [ bn + 1 ] , ( ba - > n_blocks - bn - 1 ) * sizeof ( struct blockpair ) ) ;
ba - > n_blocks - - ;
2013-04-16 23:57:18 -04:00
VALIDATE ( ba ) ;
2013-04-16 23:57:18 -04:00
}
u_int64_t
block_allocator_block_size ( BLOCK_ALLOCATOR ba , u_int64_t offset ) {
int64_t bn = find_block ( ba , offset ) ;
assert ( bn > = 0 ) ; // we require that there is a block with that offset. Might as well abort if no such block exists.
return ba - > blocks_array [ bn ] . size ;
}
2013-04-16 23:57:18 -04:00
u_int64_t
block_allocator_allocated_limit ( BLOCK_ALLOCATOR ba ) {
if ( ba - > n_blocks = = 0 ) return ba - > reserve_at_beginning ;
else {
struct blockpair * last = & ba - > blocks_array [ ba - > n_blocks - 1 ] ;
return last - > offset + last - > size ;
}
}
2013-04-16 23:57:59 -04:00
int
block_allocator_get_nth_block_in_layout_order ( BLOCK_ALLOCATOR ba , u_int64_t b , u_int64_t * offset , u_int64_t * size )
// Effect: Consider the blocks in sorted order. The reserved block at the beginning is number 0. The next one is number 1 and so forth.
// Return the offset and size of the block with that number.
// Return 0 if there is a block that big, return nonzero if b is too big.
{
if ( b = = 0 ) {
* offset = 0 ;
* size = ba - > reserve_at_beginning ;
return 0 ;
} else if ( b > ba - > n_blocks ) {
return - 1 ;
} else {
* offset = ba - > blocks_array [ b - 1 ] . offset ;
* size = ba - > blocks_array [ b - 1 ] . size ;
return 0 ;
}
}