lib: os: mem_blocks: add alloc/free event notifications
This adds event notification for mem_blocks using the new heap listener interface. Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
parent
a5d55ddcf2
commit
f3856d0b47
2 changed files with 25 additions and 0 deletions
|
@ -117,4 +117,12 @@ config SYS_MEM_BLOCKS
|
|||
the buffer to reside in memory regions where these can be
|
||||
powered down to conserve energy.
|
||||
|
||||
config SYS_MEM_BLOCKS_LISTENER
|
||||
bool "Enable Memory Blocks Allocator event notifications"
|
||||
depends on SYS_MEM_BLOCKS
|
||||
select HEAP_LISTENER
|
||||
help
|
||||
This allows application to listen for memory blocks allocator
|
||||
events, such as memory allocation and de-allocation.
|
||||
|
||||
endmenu
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <zephyr.h>
|
||||
#include <sys/__assert.h>
|
||||
#include <sys/check.h>
|
||||
#include <sys/heap_listener.h>
|
||||
#include <sys/mem_blocks.h>
|
||||
#include <sys/util.h>
|
||||
|
||||
|
@ -91,6 +92,11 @@ int sys_mem_blocks_alloc(sys_mem_blocks_t *mem_block, size_t count,
|
|||
}
|
||||
|
||||
out_blocks[i] = ptr;
|
||||
|
||||
#ifdef CONFIG_SYS_MEM_BLOCKS_LISTENER
|
||||
heap_listener_notify_alloc(HEAP_ID_FROM_POINTER(mem_block),
|
||||
ptr, BIT(mem_block->blk_sz_shift));
|
||||
#endif
|
||||
}
|
||||
|
||||
/* If error, free already allocated blocks. */
|
||||
|
@ -137,6 +143,17 @@ int sys_mem_blocks_free(sys_mem_blocks_t *mem_block, size_t count,
|
|||
if (r != 0) {
|
||||
ret = r;
|
||||
}
|
||||
#ifdef CONFIG_SYS_MEM_BLOCKS_LISTENER
|
||||
else {
|
||||
/*
|
||||
* Since we do not keep track of failed free ops,
|
||||
* we need to notify free one-by-one, instead of
|
||||
* notifying at the end of function.
|
||||
*/
|
||||
heap_listener_notify_free(HEAP_ID_FROM_POINTER(mem_block),
|
||||
ptr, BIT(mem_block->blk_sz_shift));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
out:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue