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.

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

Returns

the pointer to the buffer or NULL on failure

wiced_bt_get_buffer_from_heap(heap, size)

Allocates a buffer from the requested heap.

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

  • size[in] : size to be allocated

Returns

the pointer to the buffer or NULL on failure

wiced_bt_get_buffer(size)

Allocates a buffer from the DEFAULT heap .

Parameters
  • size[in] : size to be allocated

Returns

the pointer to the buffer or NULL on failure

wiced_bt_free_buffer

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

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

Returns

None

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.

Returns

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.

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

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

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

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

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

Returns

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

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.

Parameters

p_heap[in] : Heap created with wiced_bt_create_heap

Returns

void

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.

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

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

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

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

Returns

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

void wiced_bt_delete_pool(wiced_bt_pool_t *p_pool)

Deletes a buffer pool created using wiced_bt_create_pool.

Parameters

p_pool[in] : pointer of type wiced_bt_pool_t returned through a call to wiced_bt_create_pool

Returns

void

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.

Parameters

p_pool[in] : pool pointer

Returns

the number of buffers available in the pool

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.

Parameters

p_heap[in] : heap pointer

Returns

the size of the largest buffer available in the heap

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.

Parameters

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

Returns

the buffer size

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.

Returns

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.

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

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

Returns

void

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.

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

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

Returns

void

wiced_bt_buffer_t *wiced_bt_dequeue(wiced_bt_buffer_q_t *p_q)

Dequeues a buffer from the head of a queue.

Parameters

p_q[in] : pointer to a queue.

Returns

NULL if queue is empty, else buffer

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.

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

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

Returns

NULL if queue is empty, else buffer

wiced_bt_buffer_t *wiced_bt_getfirst(wiced_bt_buffer_q_t *p_q)

Return a pointer to the first buffer in a queue.

Parameters

p_q[in] : pointer to a queue.

Returns

NULL if queue is empty, else buffer address

wiced_bt_buffer_t *wiced_bt_getlast(wiced_bt_buffer_q_t *p_q)

Return a pointer to the last buffer in a queue.

Parameters

p_q[in] : pointer to a queue.

Returns

NULL if queue is empty, else buffer address

wiced_bt_buffer_t *wiced_bt_getnext(wiced_bt_buffer_t *p_buf)

Return a pointer to the next buffer in a queue.

Parameters

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

Returns

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

uint32_t wiced_bt_queue_is_empty(wiced_bt_buffer_q_t *p_q)

Check the status of a queue.

Parameters

p_q[in] : pointer to a queue.

Returns

TRUE if queue is empty, else FALSE

uint32_t wiced_bt_queue_get_count(wiced_bt_buffer_q_t *p_q)

Get the number of items in the queue.

Parameters

p_q[in] : pointer to a queue.

Returns

number of items in the queue

wiced_bool_t wiced_bt_get_heap_statistics(void *p_heap, wiced_bt_heap_statistics_t *p_stats)

Get/Print the heap stats.

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

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

Returns

TRUE in case of valid stats returned in p_stats

void wiced_set_exception_callback(pf_wiced_exception pf_handler)

Set the exception callback.

Parameters

pf_handler[in] : Exception callback function

Returns

void

struct wiced_bt_pool_statistics_t
#include <>

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 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 <>

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.