1
0
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:
Koen Zandberg 2020-09-02 19:16:30 +02:00
parent 5c3f7bde0c
commit 232237796a
No known key found for this signature in database
GPG Key ID: 0895A893E6D2985B
2 changed files with 23 additions and 0 deletions

View File

@ -54,6 +54,8 @@ void memarray_init(memarray_t *mem, void *data, size_t size, size_t num);
* *
* @pre `mem != NULL` * @pre `mem != NULL`
* *
* @note Allocated structure is not cleared before returned
*
* @param[in,out] mem memarray pool to allocate block in * @param[in,out] mem memarray pool to allocate block in
* *
* @return pointer to allocated structure, if enough memory was available * @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); 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 * @brief Free memory chunk in memarray pool
* *

View File

@ -44,6 +44,15 @@ void *memarray_alloc(memarray_t *mem)
return free; 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) void memarray_free(memarray_t *mem, void *ptr)
{ {
assert((mem != NULL) && (ptr != NULL)); assert((mem != NULL) && (ptr != NULL));