| 33 | } |
| 34 | |
| 35 | void* LinearAllocator::alloc(size_t size) { |
| 36 | Chunk* chunk = _tail; |
| 37 | |
| 38 | do { |
| 39 | // Fast path: bump a pointer with CAS |
| 40 | for (size_t offs = chunk->offs; offs + size <= _chunk_size; offs = chunk->offs) { |
| 41 | if (__sync_bool_compare_and_swap(&chunk->offs, offs, offs + size)) { |
| 42 | if (_chunk_size / 2 - offs < size) { |
| 43 | // Stepped over a middle of the chunk - it's time to prepare a new one |
| 44 | reserveChunk(chunk); |
| 45 | } |
| 46 | return (char*)chunk + offs; |
| 47 | } |
| 48 | } |
| 49 | } while ((chunk = getNextChunk(chunk)) != NULL); |
| 50 | |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | Chunk* LinearAllocator::allocateChunk(Chunk* current) { |
| 55 | Chunk* chunk = (Chunk*)OS::safeAlloc(_chunk_size); |