MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / QueueItem

Class QueueItem

practice/itemPool.cpp:52–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50private:
51 // 产生一个 QueueItem 的对象池(10000 个 QueueItem 节点)
52 struct QueueItem
53 {
54 QueueItem(T data = T()) : _data(data), _next(nullptr) {}
55
56 // 给 QueueItem 提供自定义的内存管理
57 void *operator new(size_t size)
58 {
59 if (_itemPool == nullptr)
60 {
61 _itemPool = (QueueItem*) new char[POOL_ITEM_SIZE * sizeof(QueueItem)];
62 QueueItem *p = _itemPool;
63 for (; p < _itemPool + POOL_ITEM_SIZE - 1; ++ p) {
64 p->_next = p + 1;
65 }
66 p->_next = nullptr;
67 }
68
69 QueueItem *p = _itemPool;
70 _itemPool = _itemPool->_next;
71 return p;
72 }
73
74 void operator delete(void *ptr)
75 {
76 QueueItem *p = (QueueItem*)ptr;
77 p->_next = _itemPool; // 相当于接到链表头
78 _itemPool = p;
79 }
80
81 T _data;
82 QueueItem *_next;
83 static QueueItem *_itemPool; // 指向对象池首个未分配的节点
84 static const int POOL_ITEM_SIZE = 10000;
85 };
86
87 QueueItem *_front; // 指向头节点的前一个?
88 QueueItem *_rear; // 指向队尾

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected