Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Welcome to the Rockbox Technical Forums!

+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Using buflib
« previous next »
  • Print
Pages: [1]

Author Topic: Using buflib  (Read 1849 times)

Offline weegee

  • Member
  • *
  • Posts: 2
  • if(noob == true) throw pwn;
Using buflib
« on: February 05, 2015, 01:11:29 PM »
I know I'm going to sound like a noob, but here it is.
*deep breath*
How do I use buflib? I know that I can use it to allocate buffers, which is what I need to do, but I have no idea how to use it. I plan to use it for a 3d graphics library I'm writing to write a game for Rockbox (yes, I'm aware how slow it will be). Can anybody tell me how to use it or point me to a tutorial?
Logged
if(noob == true) throw pwn;
elseif(troll == true) throw avoid;

Offline __builtin

  • Developer
  • Member
  • *
  • Posts: 385
  • iPod 6G, c200v1 (RIP), e200v1 (RIP)
    • FWEI.TK
Re: Using buflib
« Reply #1 on: February 05, 2015, 05:03:54 PM »
Quote from: weegee on February 05, 2015, 01:11:29 PM
How do I use buflib? I know that I can use it to allocate buffers, which is what I need to do, but I have no idea how to use it. I plan to use it for a 3d graphics library I'm writing to write a game for Rockbox (yes, I'm aware how slow it will be). Can anybody tell me how to use it or point me to a tutorial?

First of all, ask yourself this: "do I really need dynamic memory allocs/frees?"

If you still think you do, read this: http://www.rockbox.org/wiki/Main/WhyNoMalloc to help understand the problems associated with malloc and free, and then this: http://www.rockbox.org/wiki/Main/AllocatingRAM to look at some alternatives to buflib.

Most of the time, when you don't plan on free()'ing your allocs, you can usually get away with a pointer-arithmetic-based malloc() or TLSF.

However, when that's not enough, then it's time to use buflib.

Take a look at http://www.rockbox.org/wiki/BuflibMemoryManagement and the plugin API's buflib* functions.
Logged
No support questions by PM, please.

Offline weegee

  • Member
  • *
  • Posts: 2
  • if(noob == true) throw pwn;
Re: Using buflib
« Reply #2 on: February 05, 2015, 10:53:38 PM »
I have looked over the API, but it confuses me. Take this for example:

Code: [Select]
void buflib_init(struct buflib_context *context, void *buf, size_t size);
and
Code: [Select]
void
buflib_init(struct buflib_context *ctx, void *buf, size_t size)
{
    union buflib_data *bd_buf = buf;

    /* Align on sizeof(buflib_data), to prevent unaligned access */
    ALIGN_BUFFER(bd_buf, size, sizeof(union buflib_data));
    size /= sizeof(union buflib_data);
    /* The handle table is initialized with no entries */
    ctx->handle_table = bd_buf + size;
    ctx->last_handle = bd_buf + size;
    ctx->first_free_handle = bd_buf + size - 1;
    ctx->first_free_block = bd_buf;
    ctx->buf_start = bd_buf;
    /* A marker is needed for the end of allocated data, to make sure that it
     * does not collide with the handle table, and to detect end-of-buffer.
     */
    ctx->alloc_end = bd_buf;
    ctx->compact = true;
}
From the source, I assume that I don't need to manually set up the buflib context structure, but what is the void pointer about? What do I set it to? Should I even set it?

Actually, the API kinda makes sense once I look over buflib.c, but it still doesn't explain everything.
Logged
if(noob == true) throw pwn;
elseif(troll == true) throw avoid;

Offline ploco

  • Member
  • *
  • Posts: 18
Re: Using buflib
« Reply #3 on: February 06, 2015, 01:36:59 AM »
to malloc,

handle = core_alloc("my_buffer_name",sizeof(int32_t) * total_len);

if handle less than 0, means allocation failed.
otherwise we can use this handle to access the block address by calling

core_get_data(handle);

this will return the pointer of the block. And the block is movable by default.

to free, simply do

core_free(handle);

after this step, the handle value will no longer valid.
« Last Edit: February 06, 2015, 01:47:02 AM by ploco »
Logged

  • Print
Pages: [1]
« previous next »
+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Using buflib
 

  • SMF 2.0.17 | SMF © 2019, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 0.079 seconds with 15 queries.