forked from len0rd/rockbox
buflib: Change buflib_available() and add buflib_allocatable().
buflib_allocatable() is what buflib_available() was before (it was in fact simply renamed). It returns the largest contiguous block of memory. This can be allocated and will definitely succeed, although larger allocations may also succeed if the buffer can be compacted and shrinked. buflib_available() now counts all free bytes, contiguous or not. This better matches the description and how the caller use it. Change-Id: I511e4eb5f4cf1821d957b3f4ef8a685ce40fe289 Reviewed-on: http://gerrit.rockbox.org/481 Reviewed-by: Thomas Martitz <kugel@rockbox.org> Tested-by: Thomas Martitz <kugel@rockbox.org>
This commit is contained in:
parent
f9e47c6886
commit
af4e408555
4 changed files with 41 additions and 7 deletions
|
|
@ -647,9 +647,9 @@ free_space_at_end(struct buflib_context* ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Return the maximum allocatable memory in bytes */
|
||||
/* Return the maximum allocatable contiguous memory in bytes */
|
||||
size_t
|
||||
buflib_available(struct buflib_context* ctx)
|
||||
buflib_allocatable(struct buflib_context* ctx)
|
||||
{
|
||||
union buflib_data *this;
|
||||
size_t free_space = 0, max_free_space = 0;
|
||||
|
|
@ -687,6 +687,29 @@ buflib_available(struct buflib_context* ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Return the amount of unallocated memory in bytes (even if not contiguous) */
|
||||
size_t
|
||||
buflib_available(struct buflib_context* ctx)
|
||||
{
|
||||
union buflib_data *this;
|
||||
size_t free_space = 0;
|
||||
|
||||
/* now look if there's free in holes */
|
||||
for(this = find_first_free(ctx); this < ctx->alloc_end; this += abs(this->val))
|
||||
{
|
||||
if (this->val < 0)
|
||||
{
|
||||
free_space += -this->val;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
free_space *= sizeof(union buflib_data); /* make it bytes */
|
||||
free_space += free_space_at_end(ctx);
|
||||
|
||||
return free_space;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate all available (as returned by buflib_available()) memory and return
|
||||
* a handle to it
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue