1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-27 23:41:18 +01:00

Merge pull request #14930 from bergzand/pr/memarray/calloc

memarray: Add memarray_calloc
This commit is contained in:
Koen Zandberg 2020-09-02 19:48:54 +02:00 committed by GitHub
commit b584a2dadf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

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

View File

@ -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));

View File

@ -58,12 +58,10 @@ static cn_cbor_context ct =
static void *cbor_calloc(size_t count, size_t size, void *memblock)
{
(void)count;
(void)size;
expect(count == 1); /* Count is always 1 with cn-cbor */
void *block = memarray_alloc(memblock);
if (block) {
memset(block, 0, size);
}
return block;
expect(size == sizeof(cn_cbor));
return memarray_calloc(memblock);
}
static void cbor_free(void *ptr, void *memblock)