[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Brian Curless |
---|
| 4 | |
---|
| 5 | Computer Graphics Laboratory |
---|
| 6 | Stanford University |
---|
| 7 | |
---|
| 8 | --------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
| 11 | University. Except for commercial resale, lease, license or other |
---|
| 12 | commercial transactions, permission is hereby given to use, copy, |
---|
| 13 | modify this software for academic purposes only. No part of this |
---|
| 14 | software or any derivatives thereof may be used in the production of |
---|
| 15 | computer models for resale or for use in a commercial |
---|
| 16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
| 17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | #include "ChunkAllocator.h" |
---|
| 23 | |
---|
| 24 | ChunkAllocator::ChunkAllocator() |
---|
| 25 | { |
---|
| 26 | this->chunkSize = 0; |
---|
| 27 | this->chunkList = NULL; |
---|
| 28 | this->currentChunk = NULL; |
---|
| 29 | this->currentAddr = NULL; |
---|
| 30 | this->countInChunk = 0; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | ChunkAllocator::ChunkAllocator(int chunkSize) |
---|
| 35 | { |
---|
| 36 | this->chunkSize = chunkSize; |
---|
| 37 | |
---|
| 38 | this->chunkList = new ChunkLink(chunkSize); |
---|
| 39 | //printf("\nAllocating new chunk.\n"); |
---|
| 40 | |
---|
| 41 | this->currentChunk = this->chunkList; |
---|
| 42 | this->currentAddr = this->chunkList->chunk; |
---|
| 43 | this->countInChunk = 0; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | uchar * |
---|
| 48 | ChunkAllocator::alloc(int num) |
---|
| 49 | { |
---|
| 50 | this->newChunkIfNeeded(num); |
---|
| 51 | uchar *addr = this->currentAddr; |
---|
| 52 | this->currentAddr += num; |
---|
| 53 | this->countInChunk += num; |
---|
| 54 | return addr; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | void |
---|
| 59 | ChunkAllocator::newChunkIfNeeded(int num) |
---|
| 60 | { |
---|
| 61 | if (num+this->countInChunk > this->chunkSize) { |
---|
| 62 | if (this->currentChunk->next == NULL) { |
---|
| 63 | //printf("\nAllocating new chunk.\n"); |
---|
| 64 | this->currentChunk->next = new ChunkLink(chunkSize); |
---|
| 65 | } |
---|
| 66 | this->currentChunk = this->currentChunk->next; |
---|
| 67 | this->countInChunk = 0; |
---|
| 68 | this->currentAddr = this->currentChunk->chunk; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | void |
---|
| 74 | ChunkAllocator::reset() |
---|
| 75 | { |
---|
| 76 | this->currentChunk = this->chunkList; |
---|
| 77 | this->currentAddr = this->chunkList->chunk; |
---|
| 78 | this->countInChunk = 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | ChunkAllocator::~ChunkAllocator() |
---|
| 83 | { |
---|
| 84 | ChunkLink *chunk, *nextChunk; |
---|
| 85 | |
---|
| 86 | chunk = this->chunkList; |
---|
| 87 | while (chunk != NULL) { |
---|
| 88 | nextChunk = chunk->next; |
---|
| 89 | delete chunk; |
---|
| 90 | chunk = nextChunk; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|