Buflib: Clarification about invalid handles

* Enhance allocation function comments to better state the return value and what an invalid value is
* Change clients to check for "< 0" instead of "<= 0" or "== 0"
* Return -1 or -2 depending on the exact failure in buflib_alloc_ex.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30469 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2011-09-07 23:16:29 +00:00
parent 6a989b8d4d
commit 5296af838c
5 changed files with 15 additions and 10 deletions

View file

@ -344,7 +344,7 @@ void filetype_init(void)
strdup_bufsize = filesize(fd); strdup_bufsize = filesize(fd);
strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops); strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops);
if (strdup_handle <= 0) if (strdup_handle < 0)
return; return;
read_builtin_types(); read_builtin_types();
read_config(fd); read_config(fd);

View file

@ -1520,9 +1520,11 @@ int read_pfraw(char* filename, int prio)
sizeof( pix_t ) * bmph.width * bmph.height; sizeof( pix_t ) * bmph.width * bmph.height;
int hid; int hid;
while (!(hid = rb->buflib_alloc(&buf_ctx, size)) && free_slide_prio(prio)); do {
hid = rb->buflib_alloc(&buf_ctx, size);
} while (hid < 0 && free_slide_prio(prio));
if (!hid) { if (hid < 0) {
rb->close( fh ); rb->close( fh );
return 0; return 0;
} }

View file

@ -255,7 +255,7 @@ int scrobbler_init(void)
return -1; return -1;
scrobbler_cache = core_alloc("scrobbler", SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN); scrobbler_cache = core_alloc("scrobbler", SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
if (scrobbler_cache <= 0) if (scrobbler_cache < 0)
{ {
logf("SCROOBLER: OOM"); logf("SCROOBLER: OOM");
return -1; return -1;

View file

@ -425,7 +425,7 @@ handle_alloc:
goto handle_alloc; goto handle_alloc;
} }
} }
return 0; return -1;
} }
} }
@ -471,7 +471,7 @@ buffer_alloc:
} else { } else {
handle->val=1; handle->val=1;
handle_free(ctx, handle); handle_free(ctx, handle);
return 0; return -2;
} }
} }

View file

@ -153,7 +153,8 @@ size_t buflib_available(struct buflib_context *ctx);
* *
* size: How many bytes to allocate * size: How many bytes to allocate
* *
* Returns: An integer handle identifying this allocation * Returns: A positive integer handle identifying this allocation, or
* a negative value on error (0 is also not a valid handle)
*/ */
int buflib_alloc(struct buflib_context *context, size_t size); int buflib_alloc(struct buflib_context *context, size_t size);
@ -166,7 +167,8 @@ int buflib_alloc(struct buflib_context *context, size_t size);
* size: How many bytes to allocate * size: How many bytes to allocate
* ops: a struct with pointers to callback functions (see above) * ops: a struct with pointers to callback functions (see above)
* *
* Returns: An integer handle identifying this allocation * Returns: A positive integer handle identifying this allocation, or
* a negative value on error (0 is also not a valid handle)
*/ */
int buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name, int buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name,
struct buflib_callbacks *ops); struct buflib_callbacks *ops);
@ -188,7 +190,8 @@ int buflib_alloc_ex(struct buflib_context *ctx, size_t size, const char *name,
* size: The actual size will be returned into size * size: The actual size will be returned into size
* ops: a struct with pointers to callback functions * ops: a struct with pointers to callback functions
* *
* Returns: An integer handle identifying this allocation * Returns: A positive integer handle identifying this allocation, or
* a negative value on error (0 is also not a valid handle)
*/ */
int buflib_alloc_maximum(struct buflib_context* ctx, const char* name, int buflib_alloc_maximum(struct buflib_context* ctx, const char* name,
size_t *size, struct buflib_callbacks *ops); size_t *size, struct buflib_callbacks *ops);
@ -233,7 +236,7 @@ bool buflib_shrink(struct buflib_context *ctx, int handle, void* newstart, size_
/** /**
* Frees memory associated with the given handle * Frees memory associated with the given handle
* *
* Returns: 0 (to invalidate handles in one line) * Returns: 0 (to invalidate handles in one line, 0 is not a valid handle)
*/ */
int buflib_free(struct buflib_context *context, int handle); int buflib_free(struct buflib_context *context, int handle);