Memory Management

group wiced_mem

Helper APIs to create heaps and pools and allocate/free buffers from those pools or heaps.

When a heap or a pool is created, this utility allocates required chunk of memory from the system and manages it for the creator.

Defines

WICED_MEMORY_DEBUG_ENABLE

Debug Memory Level.

WICED_DEFAULT_HEAP

If an application wants to get a buffer from the default heap, he can use this.

wiced_bt_get_buffer_from_pool(pool)

Allocates a buffer from the requested pool.

Return

the pointer to the buffer or NULL on failure

Parameters
  • [in] pool: : pointer to pool from which to get the buffer

wiced_bt_get_buffer_from_heap(heap, size)

Allocates a buffer from the requested heap.

Return

the pointer to the buffer or NULL on failure

Parameters
  • [in] heap: : pointer to heap from which to get the buffer

  • [in] size: : size to be allocated

wiced_bt_get_buffer(size)

Allocates a buffer from the DEFAULT heap .

Return

the pointer to the buffer or NULL on failure

Parameters
  • [in] size: : size to be allocated

wiced_bt_free_buffer

Frees a buffer back to the pool or heap it came from.

Return

None

Parameters
  • [in] free_buf: : pointer to the start of the (pool/heap) buffer to be freed

Typedefs

typedef void wiced_bt_buffer_t

WICED BT Buffer.

typedef struct t_wiced_bt_pool wiced_bt_pool_t

WICED BT Pool.

typedef struct t_wiced_bt_heap wiced_bt_heap_t

WICED BT Heap.

Functions

uint32_t wiced_memory_get_free_bytes(void)

Returns the number of free bytes of RAM left.

Return

the number of free bytes of RAM left

wiced_bt_heap_t *wiced_bt_create_heap(const char *name, void *p_area, int size, wiced_bt_lock_t *p_lock, wiced_bool_t b_make_default)

Initializes dynamic memory area.

Application reserves an area for dynamic variable memory allocations with this call. Application can now allocate variable sized buffers as against fixed sized buffer allocations from the pools initialized with calls to wiced_bt_create_pool and allocated using wiced_bt_get_buffer_from_pool.

Return

wiced_bt_heap_t * - pointer to heap, or NULL if the heap creation failed.

Parameters
  • [in] name: : Friendly name of the heap

  • [in] p_area: : Pointer to area to use for the heap. If NULL, WICED will allocate the area.

  • [in] size: : Size the area passed in. If no area passed in, this is the size of the heap desired.

  • [in] p_lock: : Pointers to lock functions to use during heap manipulation. If NULL, then it is assumed that the application handles disabling of preemption.

  • [in] b_make_default: Flag as to whether the heap will be the default heap.

void wiced_bt_delete_heap(wiced_bt_heap_t *p_heap)

Deletes the heap created with wiced_bt_create_heap.

The heap area is freed back to the platform in case the wiced_bt_create_heap was called with p_area set to NULL.

Return

void

Parameters

wiced_bt_pool_t *wiced_bt_create_pool(const char *name, uint32_t buffer_size, uint32_t buffer_cnt, wiced_bt_lock_t *p_lock)

Creates a buffer pool for application usage.

Return

pointer to the created pool on success, or NULL on failure

Parameters
  • [in] name: : Friendly name of the heap

  • [in] buffer_size: Size of the buffers in the pool

  • [in] buffer_cnt: : Number of buffers in the pool

  • [in] p_lock: : Pointers to lock functions to use during heap manipulation. If NULL, then it is assumed that the application handles disabling of preemption.

void wiced_bt_delete_pool(wiced_bt_pool_t *p_pool)

Deletes a buffer pool created using wiced_bt_create_pool.

Return

void

Parameters

wiced_bt_buffer_t *wiced_bt_get_buffer_from_pool_no_trace(wiced_bt_pool_t *p_pool)

Get buffer from requested pool.

wiced_bt_buffer_t *wiced_bt_get_buffer_from_heap_no_trace(wiced_bt_heap_t *p_heap, uint32_t size)

Get buffer from requested heap.

uint32_t wiced_bt_get_pool_free_count(wiced_bt_pool_t *p_pool)

To get the number of buffers available in the pool.

Return

the number of buffers available in the pool

Parameters
  • [in] p_pool: : pool pointer

uint32_t wiced_bt_get_largest_heap_buffer(wiced_bt_heap_t *p_heap)

To get the size of the largest buffer available in the heap.

Return

the size of the largest buffer available in the heap

Parameters
  • [in] p_heap: : heap pointer

void wiced_bt_free_buffer_no_trace(wiced_bt_buffer_t *p_buf)

Free Buffer.

uint32_t wiced_bt_get_buffer_size(wiced_bt_buffer_t *p_buf)

Gets the buffer size.

Return

the buffer size

Parameters
  • [in] p_buf: : pointer to the start of the buffer

void wiced_bt_init_q(wiced_bt_buffer_q_t *p_q, wiced_bt_lock_t *p_lock)

Called by an application to initialize a WICED buffer queue.

Pointers to lock and unlock functions may be NULL if application has handled preemption outside of the queue management code.

Return

void

void wiced_bt_enqueue(wiced_bt_buffer_q_t *p_q, wiced_bt_buffer_t *p_buf)

Enqueue a buffer at the tail of the queue.

Return

void

Parameters
  • [in] p_q: : pointer to a queue.

  • [in] p_buf: : address of the buffer to enqueue

void wiced_bt_enqueue_head(wiced_bt_buffer_q_t *p_q, wiced_bt_buffer_t *p_buf)

Enqueue a buffer at the head of the queue.

Return

void

Parameters
  • [in] p_q: : pointer to a queue.

  • [in] p_buf: : address of the buffer to enqueue

wiced_bt_buffer_t *wiced_bt_dequeue(wiced_bt_buffer_q_t *p_q)

Dequeues a buffer from the head of a queue.

Return

NULL if queue is empty, else buffer

Parameters
  • [in] p_q: : pointer to a queue.

wiced_bt_buffer_t *wiced_bt_remove_from_queue(wiced_bt_buffer_q_t *p_q, wiced_bt_buffer_t *p_buf)

Dequeue a buffer from the middle of the queue.

Return

NULL if queue is empty, else buffer

Parameters
  • [in] p_q: : pointer to a queue.

  • [in] p_buf: : address of the buffer to dequeue

wiced_bt_buffer_t *wiced_bt_getfirst(wiced_bt_buffer_q_t *p_q)

Return a pointer to the first buffer in a queue.

Return

NULL if queue is empty, else buffer address

Parameters
  • [in] p_q: : pointer to a queue.

wiced_bt_buffer_t *wiced_bt_getlast(wiced_bt_buffer_q_t *p_q)

Return a pointer to the last buffer in a queue.

Return

NULL if queue is empty, else buffer address

Parameters
  • [in] p_q: : pointer to a queue.

wiced_bt_buffer_t *wiced_bt_getnext(wiced_bt_buffer_t *p_buf)

Return a pointer to the next buffer in a queue.

Return

NULL if no more buffers in the queue, else next buffer address

Parameters
  • [in] p_buf: : pointer to the buffer to find the next one from.

uint32_t wiced_bt_queue_is_empty(wiced_bt_buffer_q_t *p_q)

Check the status of a queue.

Return

TRUE if queue is empty, else FALSE

Parameters
  • [in] p_q: : pointer to a queue.

uint32_t wiced_bt_queue_get_count(wiced_bt_buffer_q_t *p_q)

Get the number of items in the queue.

Return

number of items in the queue

Parameters
  • [in] p_q: : pointer to a queue.

wiced_bool_t wiced_bt_get_heap_statistics(void *p_heap, wiced_bt_heap_statistics_t *p_stats)

Get/Print the heap stats.

Return

TRUE in case of valid stats returned in p_stats

Parameters
  • [in] p_heap: : heap pointer (output of wiced_bt_create_heap)

  • [out] p_stats: : pointer to receive the heap statistics

void wiced_set_exception_callback(pf_wiced_exception pf_handler)

Set the exception callback.

Return

void

Parameters
  • [in] pf_handler: : Exception callback function

struct wiced_bt_pool_statistics_t
#include <wiced_memory.h>

wiced bt buffer pool statistics

Public Members

uint16_t pool_size

pool buffer size

uint16_t pool_count

total number of buffers created in the pool

uint16_t current_allocated_count

number of buffers currently allocated

uint16_t max_allocated_count

maximum number of buffers ever allocated

struct wiced_bt_heap_statistics_t
#include <wiced_memory.h>

wiced bt heap statistics

Public Members

uint16_t heap_size

heap size

uint16_t max_single_allocation

max individual size allocated from the heap till now

uint16_t max_heap_size_used

high watermark/peak heap size used.

if this size is > 80% then we are running close to edge

uint16_t allocation_failure_count

number of times allocation failed

uint16_t current_num_allocations

number of fragments currently allocated

uint16_t current_size_allocated

total size of current allocations

uint16_t current_largest_free_size

largest free fragment size, which can be allocated

uint16_t current_num_free_fragments

num of free fragments

uint16_t current_free_size

total free size of all fragments

struct wiced_bt_buffer_q_t
#include <wiced_memory.h>

This queue is a general purpose buffer queue, for application use.

Public Members

wiced_bt_buffer_t *p_first

Pointer to first buffer.

wiced_bt_buffer_t *p_last

Pointer to Last buffer.

uint32_t count

Number of buffer in queue.

wiced_bt_lock_t lock

Lock provided by appliction.