mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
memarray: Add memarray_calloc
The memarray_alloc can be error prone to use as the block returned can contain data from previous uses. The memarray_calloc call added in this commit always zeroes the block before returning it to the user.
This commit is contained in:
parent
5c3f7bde0c
commit
232237796a
@ -54,6 +54,8 @@ void memarray_init(memarray_t *mem, void *data, size_t size, size_t num);
|
||||
*
|
||||
* @pre `mem != NULL`
|
||||
*
|
||||
* @note Allocated structure is not cleared before returned
|
||||
*
|
||||
* @param[in,out] mem memarray pool to allocate block in
|
||||
*
|
||||
* @return pointer to allocated structure, if enough memory was available
|
||||
@ -61,6 +63,18 @@ void memarray_init(memarray_t *mem, void *data, size_t size, size_t num);
|
||||
*/
|
||||
void *memarray_alloc(memarray_t *mem);
|
||||
|
||||
/**
|
||||
* @brief Allocate and clear memory chunk in memarray pool
|
||||
*
|
||||
* @pre `mem != NULL`
|
||||
*
|
||||
* @param[in,out] mem memarray pool to allocate block in
|
||||
*
|
||||
* @return pointer to allocated structure, if enough memory was available
|
||||
* @return NULL, on failure
|
||||
*/
|
||||
void *memarray_calloc(memarray_t *mem);
|
||||
|
||||
/**
|
||||
* @brief Free memory chunk in memarray pool
|
||||
*
|
||||
|
@ -44,6 +44,15 @@ void *memarray_alloc(memarray_t *mem)
|
||||
return free;
|
||||
}
|
||||
|
||||
void *memarray_calloc(memarray_t *mem)
|
||||
{
|
||||
void *new = memarray_alloc(mem);
|
||||
if (new) {
|
||||
memset(new, 0, mem->size);
|
||||
}
|
||||
return new;
|
||||
}
|
||||
|
||||
void memarray_free(memarray_t *mem, void *ptr)
|
||||
{
|
||||
assert((mem != NULL) && (ptr != NULL));
|
||||
|
Loading…
Reference in New Issue
Block a user