| 104 | } |
| 105 | |
| 106 | void SimpleDataPool::Return(void* data) { |
| 107 | if (data == NULL) { |
| 108 | return; |
| 109 | } |
| 110 | if (!_factory->ResetData(data)) { |
| 111 | return _factory->DestroyData(data); |
| 112 | } |
| 113 | std::unique_lock<butil::Mutex> mu(_mutex); |
| 114 | if (_capacity == _size) { |
| 115 | const unsigned new_cap = (_capacity <= 1 ? 128 : (_capacity * 3 / 2)); |
| 116 | void** new_pool = (void**)malloc(new_cap * sizeof(void*)); |
| 117 | if (NULL == new_pool) { |
| 118 | mu.unlock(); |
| 119 | return _factory->DestroyData(data); |
| 120 | } |
| 121 | if (_pool) { |
| 122 | memcpy(new_pool, _pool, _capacity * sizeof(void*)); |
| 123 | free(_pool); |
| 124 | } |
| 125 | _capacity = new_cap; |
| 126 | _pool = new_pool; |
| 127 | } |
| 128 | _pool[_size++] = data; |
| 129 | } |
| 130 | |
| 131 | SimpleDataPool::Stat SimpleDataPool::stat() const { |
| 132 | Stat s = { _size, _ncreated.load(butil::memory_order_relaxed) }; |
no test coverage detected