|
|
||||||||||||||||||||||||||||||||||||
Different ways to allocate RAM usage for your featureRockbox has a "No Malloc" policy you can read about at WhyNoMalloc but sometimes you really do need to grab some buffer possibly temporarily. This page will explain the different methods available.Compile-time RAM layoutAs an example, this list shows the sections in a compiledrockbox.elf, indicating how RAM is allocated at compile-time. The section arrangement varies somewhat between platforms and is determined by the linker script (firmware/target/arm/as3525/app.lds in this case, for the Sansa e200v2). The interesting parts for memory allocation are audiobuf, codecbuf, and pluginbuf.
Global variablesBy far the easiest method:static char my_buffer[BUFFER_SIZE];This can be used anywhere. In a codec or plugin, space will be reserved in codecbuf or pluginbuf until the codec/plugin is unloaded. Otherwise, space will be reserved permanently in .data, .rodata, or .bss.
Allocate from soundbufchar *my_buffer = buffer_alloc(BUFFER_SIZE);This takes some space from the front of soundbuf. It can't be used by plugins or codecs, since it can't be freed. This method is useful when the size required is known at boot-time.
Use the rest of the codec/plugin buffersize_t size_available; // In codec: void *my_buffer = ci->codec_get_buffer(&size_available); // In plugin: void *my_buffer = rb->plugin_get_buffer(&size_available);This is used by codecs/plugins to get the rest of the codec/plugin buffer. This space is reserved for the codec/plugin anyway, so you may as well use it!
Steal the sound buffersize_t size_available; void *my_buffer = rb->plugin_get_audio_buffer(&size_available);If a plugin stops playback, it can use the sound buffer.
dynamic allocation like "malloc()" (bufalloc())Most frowned-on method of allocation, use this carefully... The buffer is allocated as a handle in the playback buffer and could be moving around the buffer as playback advances.Pro:
Cons:
r3 - 29 Mar 2011 - 03:45:15 - SeanBartell
Copyright © by the contributing authors.
|