source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/march/ChunkAllocator.cc @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 2.4 KB
Line 
1/*
2
3Brian Curless
4
5Computer Graphics Laboratory
6Stanford University
7
8---------------------------------------------------------------------
9
10Copyright (1997) The Board of Trustees of the Leland Stanford Junior
11University. Except for commercial resale, lease, license or other
12commercial transactions, permission is hereby given to use, copy,
13modify this software for academic purposes only.  No part of this
14software or any derivatives thereof may be used in the production of
15computer models for resale or for use in a commercial
16product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND
17CONCERNING THIS SOFTWARE.  No support is implied or provided.
18
19*/
20
21
22#include "ChunkAllocator.h"
23
24ChunkAllocator::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
34ChunkAllocator::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
47uchar *
48ChunkAllocator::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
58void
59ChunkAllocator::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
73void
74ChunkAllocator::reset()
75{
76    this->currentChunk = this->chunkList;
77    this->currentAddr = this->chunkList->chunk;
78    this->countInChunk = 0;
79}
80
81
82ChunkAllocator::~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
94
95uchar *
96ChunkAllocator::nextElem(int size)
97{
98    uchar *addr;
99
100    if (size+this->countInChunk > this->chunkSize) {
101        this->currentChunk = this->currentChunk->next;
102        this->countInChunk = 0;
103        this->currentAddr = this->currentChunk->chunk;
104    }
105   
106    addr = this->currentAddr;
107    this->currentAddr += size;
108    this->countInChunk += size;
109   
110    return addr;   
111}
112
Note: See TracBrowser for help on using the repository browser.