| 65 | PoolAllocatorImpl(Allocator *a): allocator(a), buffers(), used() {} |
| 66 | |
| 67 | Buffer *allocate(size_t size) |
| 68 | { |
| 69 | unique_lock guard(used_lock); |
| 70 | while (used[0] && used[1]) |
| 71 | WAIT_CONDITION(available_cond, used_lock, guard); |
| 72 | |
| 73 | if (!used[0]) { |
| 74 | if (buffers[0] == NULL) |
| 75 | buffers[0] = allocator->allocate(size); |
| 76 | buffers[0]->length = 0; |
| 77 | buffers[0]->allocator = this; |
| 78 | used[0] = true; |
| 79 | return buffers[0]; |
| 80 | } else if (!used[1]) { |
| 81 | if (buffers[1] == NULL) |
| 82 | buffers[1] = allocator->allocate(size); |
| 83 | buffers[1]->length = 0; |
| 84 | buffers[1]->allocator = this; |
| 85 | used[1] = true; |
| 86 | return buffers[1]; |
| 87 | } else { |
| 88 | // should never happen |
| 89 | return NULL; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void free(Buffer *b) |
| 94 | { |