Allocating Memory for Buffers
|Top Previous Next|
Each buffer has a certain size, i.e, a memory capacity. This capacity is allocated upon request from your program. When the device initially boots, no memory is allocated to buffers at all.
Memory for buffers is allocated in pages. A page is 256 bytes of memory. Allocating memory for a buffer is a two-step process: first you have to request for a specific allocation (a number of pages) and then you have to perform the actual allocation.
The allocation method (sys.buffalloc) applies to all buffers previously specified, in one fell swoop.
Actual memory allocation takes up to 100ms, so it is usually done just once, on boot, for all required buffers. If you do not require some buffer, you may choose not to allocate any memory to it. In effect, it will be disabled.
You may not always get the full amount of memory you have requested. Memory is not an infinite resource, and if you have already requested (and received) allocations for 95% of the memory for your platform, your next request will get up to 5% of memory, even if you requested for 10%.
There is a small overhead for each buffer. Meaning, not 100% of the memory allocated to a buffer is actually available for use. A certain number of bytes in each buffer is reserved for variables needed to administer this buffer. This number is 17 for 16-bit platforms and 33 for 32-bit platforms. Platform type is specified in your platform documentation.
Thus, if we requested (and received) a buffer with 2 pages (256 * 2 = 512), then we can only store 512 - 16 or 512 - 32 bytes in this buffer, depending on the platform type.