2017年11月1日 星期三

[ThreadX, 5] memory

20171031
下午 03:22
Byte pool:
  • 範例:
/* Create a memory pool whose total size is 2000 bytes
starting at address 0x500000. */
status = tx_byte_pool_create(&my_pool, "my_pool", (VOID *) 0x500000, 2000);

/* Allocate a 112 byte memory area from my_pool. Assume
that the byte pool has already been created with a call to tx_byte_pool_create. */
status = tx_byte_allocate(&my_pool, (VOID **) &memory_ptr, 112, TX_WAIT_FOREVER);



Block pool:
  • 範例(create 時除了定義pool大小外還需要定義每一個block的大小,allocate時則不用):
/* Create a memory pool whose total size is 1000 bytes starting at address 0x100000. Each block in this
pool is defined to be 50 bytes long. */
status = tx_block_pool_create(&my_pool, "my_pool_name", 50, (VOID *) 0x100000, 1000);

status = tx_block_allocate(&my_pool, (VOID **) &memory_ptr, TX_WAIT_FOREVER);



  • byte/block pool 都是利用 link list做管理當有記憶體問題時可以dump出此list來做檢查(CB是否被破壞)



  • 比較兩者,Byte較為靈活但有碎片以及效率(搜尋可用大小,整理碎片)的問題;嵌入式系統中建議使用block pool,透過create不同的block pool來增加靈活度,減輕其不足的部分。

沒有留言:

張貼留言