初始版本

This commit is contained in:
xiaozhengsheng
2025-08-19 09:49:41 +08:00
parent 10f1ddf1c1
commit 6df0f7d96e
2974 changed files with 1712873 additions and 54 deletions

View File

@@ -0,0 +1,258 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "nrf_memobj.h"
#include "nrf_atomic.h"
#include "nrf_assert.h"
typedef struct memobj_elem_s memobj_elem_t;
/** @brief Standard chunk header. */
typedef struct
{
memobj_elem_t * p_next; ///< Pointer to the next element.
} memobj_header_t;
/** @brief Head header extension fields. */
typedef struct
{
uint8_t user_cnt; ///< User counter (see @ref nrf_memobj_get and @ref nrf_memobj_put).
uint8_t chunk_cnt; ///< Number of chunks in the object.
uint16_t chunk_size; ///< Single chunk size
} memobj_head_header_fields_t;
/** @brief Head header extension. */
typedef struct
{
union
{
nrf_atomic_u32_t atomic_user_cnt;
memobj_head_header_fields_t fields;
} data;
} memobj_head_header_t;
/** @brief Head chunk structure. */
typedef struct
{
memobj_header_t header; ///< Standard header.
memobj_head_header_t head_header; ///< Head-specific header part.
uint8_t data[1]; ///< Data.
} memobj_head_t;
STATIC_ASSERT(sizeof(memobj_header_t) == NRF_MEMOBJ_STD_HEADER_SIZE);
/** @brief Standard chunk structure. */
struct memobj_elem_s
{
memobj_header_t header; ///< Standard header.
uint8_t data[1]; ///< Data.
};
ret_code_t nrf_memobj_pool_init(nrf_memobj_pool_t const * p_pool)
{
return nrf_balloc_init((nrf_balloc_t const *)p_pool);
}
nrf_memobj_t * nrf_memobj_alloc(nrf_memobj_pool_t const * p_pool,
size_t size)
{
uint32_t bsize = (uint32_t)NRF_BALLOC_ELEMENT_SIZE((nrf_balloc_t const *)p_pool) - sizeof(memobj_header_t);
uint8_t num_of_chunks = (uint8_t)CEIL_DIV(size + sizeof(memobj_head_header_t), bsize);
memobj_head_t * p_head = nrf_balloc_alloc((nrf_balloc_t const *)p_pool);
if (p_head == NULL)
{
return NULL;
}
p_head->head_header.data.fields.user_cnt = 0;
p_head->head_header.data.fields.chunk_cnt = 1;
p_head->head_header.data.fields.chunk_size = bsize;
memobj_header_t * p_prev = (memobj_header_t *)p_head;
memobj_header_t * p_curr;
uint32_t i;
uint32_t chunk_less1 = (uint32_t)num_of_chunks - 1;
p_prev->p_next = (memobj_elem_t *)p_pool;
for (i = 0; i < chunk_less1; i++)
{
p_curr = (memobj_header_t *)nrf_balloc_alloc((nrf_balloc_t const *)p_pool);
if (p_curr)
{
(p_head->head_header.data.fields.chunk_cnt)++;
p_prev->p_next = (memobj_elem_t *)p_curr;
p_curr->p_next = (memobj_elem_t *)p_pool;
p_prev = p_curr;
}
else
{
//Could not allocate all requested buffers
nrf_memobj_free((nrf_memobj_t *)p_head);
return NULL;
}
}
return (nrf_memobj_t *)p_head;
}
void nrf_memobj_free(nrf_memobj_t * p_obj)
{
memobj_head_t * p_head = (memobj_head_t *)p_obj;
uint8_t chunk_cnt = p_head->head_header.data.fields.chunk_cnt;
uint32_t i;
memobj_header_t * p_curr = (memobj_header_t *)p_obj;
memobj_header_t * p_next;
uint32_t chunk_less1 = (uint32_t)chunk_cnt - 1;
for (i = 0; i < chunk_less1; i++)
{
p_curr = (memobj_header_t *)p_curr->p_next;
}
nrf_balloc_t const * p_pool2 = (nrf_balloc_t const *)p_curr->p_next;
p_curr = (memobj_header_t *)p_obj;
for (i = 0; i < chunk_cnt; i++)
{
p_next = (memobj_header_t *)p_curr->p_next;
nrf_balloc_free(p_pool2, p_curr);
p_curr = p_next;
}
}
void nrf_memobj_get(nrf_memobj_t const * p_obj)
{
memobj_head_t * p_head = (memobj_head_t *)p_obj;
(void)nrf_atomic_u32_add(&p_head->head_header.data.atomic_user_cnt, 1);
}
void nrf_memobj_put(nrf_memobj_t * p_obj)
{
memobj_head_t * p_head = (memobj_head_t *)p_obj;
uint32_t user_cnt = nrf_atomic_u32_sub(&p_head->head_header.data.atomic_user_cnt, 1);
memobj_head_header_fields_t * p_fields = (memobj_head_header_fields_t *)&user_cnt;
if (p_fields->user_cnt == 0)
{
nrf_memobj_free(p_obj);
}
}
static void memobj_op(nrf_memobj_t * p_obj,
void * p_data,
size_t * p_len,
size_t offset,
bool read)
{
ASSERT(p_obj);
memobj_head_t * p_head = (memobj_head_t *)p_obj;
memobj_elem_t * p_curr_chunk = (memobj_elem_t *)p_obj;
size_t obj_capacity;
size_t chunk_size;
size_t chunk_idx;
size_t chunk_offset;
size_t len;
obj_capacity = (p_head->head_header.data.fields.chunk_size *
p_head->head_header.data.fields.chunk_cnt) -
sizeof(memobj_head_header_fields_t);
ASSERT(offset < obj_capacity);
chunk_size = p_head->head_header.data.fields.chunk_size;
chunk_idx = (offset + sizeof(memobj_head_header_fields_t)) / chunk_size;
chunk_offset = (offset + sizeof(memobj_head_header_fields_t)) % chunk_size;
len = ((*p_len + offset) > obj_capacity) ? obj_capacity - offset : *p_len;
//Return number of available bytes
*p_len = len;
//Move to the first chunk to be used
while (chunk_idx > 0)
{
p_curr_chunk = p_curr_chunk->header.p_next;
chunk_idx--;
}
size_t user_mem_offset = 0;
size_t curr_cpy_size = chunk_size - chunk_offset;
curr_cpy_size = curr_cpy_size > len ? len : curr_cpy_size;
while (len)
{
void * p_user_mem = &((uint8_t *)p_data)[user_mem_offset];
void * p_obj_mem = &p_curr_chunk->data[chunk_offset];
if (read)
{
memcpy(p_user_mem, p_obj_mem, curr_cpy_size);
}
else
{
memcpy(p_obj_mem, p_user_mem, curr_cpy_size);
}
chunk_offset = 0;
p_curr_chunk = p_curr_chunk->header.p_next;
len -= curr_cpy_size;
user_mem_offset += curr_cpy_size;
curr_cpy_size = (chunk_size > len) ? len : chunk_size;
}
}
void nrf_memobj_write(nrf_memobj_t * p_obj,
void * p_data,
size_t len,
size_t offset)
{
size_t op_len = len;
memobj_op(p_obj, p_data, &op_len, offset, false);
ASSERT(op_len == len);
}
void nrf_memobj_read(nrf_memobj_t * p_obj,
void * p_data,
size_t len,
size_t offset)
{
size_t op_len = len;
memobj_op(p_obj, p_data, &op_len, offset, true);
ASSERT(op_len == len);
}

View File

@@ -0,0 +1,203 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef NRF_MEMOBJ_H
#define NRF_MEMOBJ_H
/**
* @defgroup nrf_memobj Memory Object module
* @{
* @ingroup app_common
* @brief Functions for controlling a memory object.
*/
#include <stdint.h>
#include <stdlib.h>
#include "sdk_errors.h"
#include "nrf_balloc.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* A memory object can consist of multiple chunks with the same size. Each object has a header part
* and a data part. The first element in a memory object is memory object head which has a special header.
* The remaining objects have a header of the same size.
*
@verbatim
_____________________ _____________________ _____________________
| | | | | |
|4 B head header | --> |4 B p_next |------->|4 B p_memobj_pool |
|_____________________| | |_____________________| |_____________________|
| | | | | | |
|4 B p_next |--| | | | |
|_____________________| | | .... | |
| | | data | | data |
| | | | | |
| data | | | | |
| | | | | |
|_____________________| |_____________________| |_____________________|
head mid_element last_element
@endverbatim
*
*/
#define NRF_MEMOBJ_STD_HEADER_SIZE sizeof(uint32_t)
/**
* @brief Macro for creating an nrf_memobj pool.
*
* This macro declares an nrf_balloc object. The element in the pool contains a user-defined data part and
* a memory object header.
*
* @param _name Name of the instance.
* @param _chunk_size Size of a single chunk.
* @param _pool_size Number of chunks in the pool.
*/
#define NRF_MEMOBJ_POOL_DEF(_name, _chunk_size, _pool_size) \
STATIC_ASSERT((_chunk_size) > sizeof(uint32_t)); \
NRF_BALLOC_DEF(_name, ((_chunk_size)+NRF_MEMOBJ_STD_HEADER_SIZE), (_pool_size))
/**
* @brief Pool of memory objects.
*/
typedef nrf_balloc_t nrf_memobj_pool_t;
/**
* @brief Memory object handle.
*/
typedef void * nrf_memobj_t;
/**
* @brief Function for initializing the memobj pool instance.
*
* This function initializes the pool.
*
* @param[in] p_pool Pointer to the memobj pool instance structure.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
ret_code_t nrf_memobj_pool_init(nrf_memobj_pool_t const * p_pool);
/**
* @brief Function for allocating a memobj with a requested size.
*
* Fixed length elements in the pool are linked together to provide the amount of memory requested by
* the user. If a memory object is successfully allocated, then the users can use the memory.
* However, it is fragmented into multiple objects so it must be accessed through the API:
* @ref nrf_memobj_write and @ref nrf_memobj_read.
*
* @param[in] p_pool Pointer to the memobj pool instance structure.
* @param[in] size Data size of requested object.
*
* @return Pointer to a memory object or NULL if the requested size cannot be allocated.
*/
nrf_memobj_t * nrf_memobj_alloc(nrf_memobj_pool_t const * p_pool,
size_t size);
/**
* @brief Function for indicating that a memory object is used and cannot be freed.
*
* Memory object can be shared and reused between multiple modules and this mechanism ensures that
* object is freed when no longer used by any module. Memory object has a counter which is incremented
* whenever this function is called. @ref nrf_memobj_put function decrements the counter.
*
* @param[in] p_obj Pointer to memory object.
*/
void nrf_memobj_get(nrf_memobj_t const * p_obj);
/**
* @brief Function for indicated that memory object is no longer used by the module and can be freed
* if no other module is using it.
*
* Memory object is returned to the pool if internal counter reaches 0 after decrementing. It means
* that no other module is needing it anymore.
*
* @note Memory object holds pointer to the pool which was used to allocate it so it does not have
* to be provided explicitly to this function.
*
* @param[in] p_obj Pointer to memory object.
*/
void nrf_memobj_put(nrf_memobj_t * p_obj);
/**
* @brief Function for forcing freeing of the memory object.
*
* @note This function should be use with caution because it can lead to undefined behavior of the
* modules since modules using the memory object are not aware that it has been freed.
*
* @param[in] p_obj Pointer to memory object.
*/
void nrf_memobj_free(nrf_memobj_t * p_obj);
/**
* @brief Function for writing data to the memory object.
*
* @param[in] p_obj Pointer to memory object.
* @param[in] p_data Pointer to data to be written to the memory object.
* @param[in] len Amount of data to be written to the memory object.
* @param[in] offset Offset.
*/
void nrf_memobj_write(nrf_memobj_t * p_obj,
void * p_data,
size_t len,
size_t offset);
/**
* @brief Function for reading data from the memory object.
*
* @param[in] p_obj Pointer to memory object.
* @param[in] p_data Pointer to the destination buffer.
* @param[in] len Amount of data to be read from the memory object.
* @param[in] offset Offset.
*/
void nrf_memobj_read(nrf_memobj_t * p_obj,
void * p_data,
size_t len,
size_t offset);
#ifdef __cplusplus
}
#endif
#endif //NRF_MEMOBJ_H
/** @} */