對于先進(jìn)先出的內(nèi)存分配場景
分配內(nèi)存的策略可以簡單很多
例如消息隊列的內(nèi)存分配問題
思考消息隊列中的元素內(nèi)存大小差異巨大
無法預(yù)先分配確定的內(nèi)存塊來傳遞數(shù)據(jù)
在此提出一種基于計數(shù)的內(nèi)存分配算法
-> allocale a large block memory
-> set a atomic<int> count at the beginning of the memory
-> user ask for a <size> memory
-> count++ and last_index += size
-> return the memory to the user
-> user free the memory
-> count--
-> when the count equal to zero
-> reuse this block memory