对于先进先出的内存分配场景
分配内存的策略可以简单很多
例如消息队列的内存分配问题
思考消息队列中的元素内存大小差异巨大
无法预先分配确定的内存块来传递数据
在此提出一种基于计数的内存分配算法
-> 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
网友评论