~ubuntu-branches/ubuntu/quantal/mesa/quantal

« back to all changes in this revision

Viewing changes to src/mesa/shader/slang/MachineIndependent/PoolAlloc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-02-21 12:44:07 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20070221124407-rgcacs32mycrtadl
ImportĀ upstreamĀ versionĀ 6.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3
 
//All rights reserved.
4
 
//
5
 
//Redistribution and use in source and binary forms, with or without
6
 
//modification, are permitted provided that the following conditions
7
 
//are met:
8
 
//
9
 
//    Redistributions of source code must retain the above copyright
10
 
//    notice, this list of conditions and the following disclaimer.
11
 
//
12
 
//    Redistributions in binary form must reproduce the above
13
 
//    copyright notice, this list of conditions and the following
14
 
//    disclaimer in the documentation and/or other materials provided
15
 
//    with the distribution.
16
 
//
17
 
//    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18
 
//    contributors may be used to endorse or promote products derived
19
 
//    from this software without specific prior written permission.
20
 
//
21
 
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
 
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
 
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
 
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
 
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
 
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 
//POSSIBILITY OF SUCH DAMAGE.
33
 
//
34
 
 
35
 
#include "../Include/PoolAlloc.h"
36
 
#include "../Include/Common.h"
37
 
 
38
 
#include "Include/InitializeGlobals.h"
39
 
#include "osinclude.h"
40
 
 
41
 
OS_TLSIndex PoolIndex;
42
 
 
43
 
void InitializeGlobalPools()
44
 
{
45
 
    TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));    
46
 
    if (globalPools)
47
 
        return;
48
 
 
49
 
    TPoolAllocator *globalPoolAllocator = new TPoolAllocator(true);
50
 
 
51
 
    TThreadGlobalPools* threadData = new TThreadGlobalPools();
52
 
    
53
 
    threadData->globalPoolAllocator = globalPoolAllocator;
54
 
        
55
 
    OS_SetTLSValue(PoolIndex, threadData);     
56
 
        globalPoolAllocator->push();
57
 
}
58
 
 
59
 
void FreeGlobalPools()
60
 
{
61
 
    // Release the allocated memory for this thread.
62
 
    TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));    
63
 
    if (!globalPools)
64
 
        return;
65
 
        
66
 
    GlobalPoolAllocator.popAll();
67
 
    delete &GlobalPoolAllocator;       
68
 
    delete globalPools;
69
 
}
70
 
 
71
 
bool InitializePoolIndex()
72
 
{
73
 
    // Allocate a TLS index.
74
 
    if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
75
 
        return false;
76
 
 
77
 
    return true;
78
 
}
79
 
 
80
 
void FreePoolIndex()
81
 
{
82
 
    // Release the TLS index.
83
 
    OS_FreeTLSIndex(PoolIndex);
84
 
}
85
 
 
86
 
TPoolAllocator& GetGlobalPoolAllocator()
87
 
{
88
 
    TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
89
 
 
90
 
    return *threadData->globalPoolAllocator;
91
 
}
92
 
 
93
 
void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator)
94
 
{
95
 
    TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
96
 
 
97
 
    threadData->globalPoolAllocator = poolAllocator;
98
 
}
99
 
 
100
 
//
101
 
// Implement the functionality of the TPoolAllocator class, which
102
 
// is documented in PoolAlloc.h.
103
 
//
104
 
TPoolAllocator::TPoolAllocator(bool g, int growthIncrement, int allocationAlignment) : 
105
 
    global(g),
106
 
    pageSize(growthIncrement),
107
 
    alignment(allocationAlignment),
108
 
    freeList(0),
109
 
    inUseList(0),
110
 
    numCalls(0)
111
 
{
112
 
    //
113
 
    // Don't allow page sizes we know are smaller than all common
114
 
    // OS page sizes.
115
 
    //
116
 
    if (pageSize < 4*1024)
117
 
        pageSize = 4*1024;
118
 
 
119
 
    //
120
 
    // A large currentPageOffset indicates a new page needs to
121
 
    // be obtained to allocate memory.
122
 
    //
123
 
    currentPageOffset = pageSize;
124
 
 
125
 
    //
126
 
    // Adjust alignment to be at least pointer aligned and
127
 
    // power of 2.
128
 
    //
129
 
    size_t minAlign = sizeof(void*);
130
 
    alignment &= ~(minAlign - 1);
131
 
    if (alignment < minAlign)
132
 
        alignment = minAlign;
133
 
    size_t a = 1;
134
 
    while (a < alignment)
135
 
        a <<= 1;
136
 
    alignment = a;
137
 
    alignmentMask = a - 1;
138
 
 
139
 
    //
140
 
    // Align header skip
141
 
    //
142
 
    headerSkip = minAlign;
143
 
    if (headerSkip < sizeof(tHeader)) {
144
 
        headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask;
145
 
    }
146
 
 
147
 
    //
148
 
    // Put a marker at the beginning of the stack.  We won't 
149
 
    // pop() past this.
150
 
    //
151
 
    tAllocState start = { currentPageOffset, 0 };
152
 
    stack.push_back(start);
153
 
}
154
 
 
155
 
TPoolAllocator::~TPoolAllocator()
156
 
{
157
 
    if (!global) {
158
 
        //
159
 
        // Then we know that this object is not being 
160
 
        // allocated after other, globally scoped objects
161
 
        // that depend on it.  So we can delete the "in use" memory.
162
 
        //
163
 
            while (inUseList) {
164
 
                tHeader* next = inUseList->nextPage;
165
 
            inUseList->~tHeader();
166
 
            delete [] reinterpret_cast<char*>(inUseList);
167
 
                inUseList = next;
168
 
            }
169
 
    }
170
 
 
171
 
    //
172
 
    // Always delete the free list memory - it can't be being
173
 
    // (correctly) referenced, whether the pool allocator was
174
 
    // global or not.  We should not check the guard blocks
175
 
    // here, because we did it already when the block was
176
 
    // placed into the free list.
177
 
    //
178
 
    while (freeList) {
179
 
        tHeader* next = freeList->nextPage;
180
 
        delete [] reinterpret_cast<char*>(freeList);
181
 
        freeList = next;
182
 
    }
183
 
}
184
 
 
185
 
// Support MSVC++ 6.0
186
 
const unsigned char TAllocation::guardBlockBeginVal = 0xfb;
187
 
const unsigned char TAllocation::guardBlockEndVal   = 0xfe;
188
 
const unsigned char TAllocation::userDataFill       = 0xcd;
189
 
 
190
 
#   ifdef GUARD_BLOCKS
191
 
    const size_t TAllocation::guardBlockSize = 16;
192
 
#   else
193
 
    const size_t TAllocation::guardBlockSize = 0;
194
 
#   endif
195
 
 
196
 
//
197
 
// Check a single guard block for damage
198
 
//
199
 
void TAllocation::checkGuardBlock(unsigned char* blockMem, unsigned char val, char* locText) const
200
 
{
201
 
    for (int x = 0; x < guardBlockSize; x++) {
202
 
        if (blockMem[x] != val) {
203
 
            char assertMsg[80];
204
 
 
205
 
            // We don't print the assert message.  It's here just to be helpful.
206
 
            sprintf(assertMsg, "PoolAlloc: Damage %s %u byte allocation at 0x%p\n",
207
 
                    locText, size, data());
208
 
            assert(0 && "PoolAlloc: Damage in guard block");
209
 
        }
210
 
    }
211
 
}
212
 
 
213
 
 
214
 
void TPoolAllocator::push()
215
 
{
216
 
    tAllocState state = { currentPageOffset, inUseList };
217
 
 
218
 
    stack.push_back(state);
219
 
        
220
 
    //
221
 
    // Indicate there is no current page to allocate from.
222
 
    //
223
 
    currentPageOffset = pageSize;
224
 
}
225
 
 
226
 
//
227
 
// Do a mass-deallocation of all the individual allocations
228
 
// that have occurred since the last push(), or since the
229
 
// last pop(), or since the object's creation.
230
 
//
231
 
// The deallocated pages are saved for future allocations.
232
 
//
233
 
void TPoolAllocator::pop()
234
 
{
235
 
    if (stack.size() < 1)
236
 
        return;
237
 
 
238
 
    tHeader* page = stack.back().page;
239
 
    currentPageOffset = stack.back().offset;
240
 
 
241
 
    while (inUseList != page) {
242
 
        // invoke destructor to free allocation list
243
 
        inUseList->~tHeader();
244
 
        
245
 
        tHeader* nextInUse = inUseList->nextPage;
246
 
        if (inUseList->pageCount > 1)
247
 
            delete [] reinterpret_cast<char*>(inUseList);
248
 
        else {
249
 
            inUseList->nextPage = freeList;
250
 
            freeList = inUseList;
251
 
        }
252
 
        inUseList = nextInUse;
253
 
    }
254
 
 
255
 
    stack.pop_back();
256
 
}
257
 
 
258
 
//
259
 
// Do a mass-deallocation of all the individual allocations
260
 
// that have occurred.
261
 
//
262
 
void TPoolAllocator::popAll()
263
 
{
264
 
    while (stack.size() > 0)
265
 
        pop();
266
 
}
267
 
 
268
 
void* TPoolAllocator::allocate(size_t numBytes)
269
 
{
270
 
    // If we are using guard blocks, all allocations are bracketed by
271
 
    // them: [guardblock][allocation][guardblock].  numBytes is how
272
 
    // much memory the caller asked for.  allocationSize is the total
273
 
    // size including guard blocks.  In release build,
274
 
    // guardBlockSize=0 and this all gets optimized away.
275
 
    size_t allocationSize = TAllocation::allocationSize(numBytes);
276
 
    
277
 
    //
278
 
    // Just keep some interesting statistics.
279
 
    //
280
 
    ++numCalls;
281
 
    totalBytes += numBytes;
282
 
 
283
 
    //
284
 
    // Do the allocation, most likely case first, for efficiency.
285
 
    // This step could be moved to be inline sometime.
286
 
    //
287
 
    if (currentPageOffset + allocationSize <= pageSize) {
288
 
        //
289
 
        // Safe to allocate from currentPageOffset.
290
 
        //
291
 
        unsigned char* memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset;
292
 
        currentPageOffset += allocationSize;
293
 
        currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask;
294
 
 
295
 
        return initializeAllocation(inUseList, memory, numBytes);
296
 
    }
297
 
 
298
 
    if (allocationSize + headerSkip > pageSize) {
299
 
        //
300
 
        // Do a multi-page allocation.  Don't mix these with the others.
301
 
        // The OS is efficient and allocating and free-ing multiple pages.
302
 
        //
303
 
        size_t numBytesToAlloc = allocationSize + headerSkip;
304
 
        tHeader* memory = reinterpret_cast<tHeader*>(::new char[numBytesToAlloc]);
305
 
        if (memory == 0)
306
 
            return 0;
307
 
 
308
 
        // Use placement-new to initialize header
309
 
        new(memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize);
310
 
        inUseList = memory;
311
 
 
312
 
        currentPageOffset = pageSize;  // make next allocation come from a new page
313
 
 
314
 
        // No guard blocks for multi-page allocations (yet)
315
 
        return reinterpret_cast<void*>(reinterpret_cast<UINT_PTR>(memory) + headerSkip);
316
 
    }
317
 
 
318
 
    //
319
 
    // Need a simple page to allocate from.
320
 
    //
321
 
    tHeader* memory;
322
 
    if (freeList) {
323
 
        memory = freeList;
324
 
        freeList = freeList->nextPage;
325
 
    } else {
326
 
        memory = reinterpret_cast<tHeader*>(::new char[pageSize]);
327
 
        if (memory == 0)
328
 
            return 0;
329
 
    }
330
 
 
331
 
    // Use placement-new to initialize header
332
 
    new(memory) tHeader(inUseList, 1);
333
 
    inUseList = memory;
334
 
    
335
 
    unsigned char* ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip;
336
 
    currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask;
337
 
 
338
 
    return initializeAllocation(inUseList, ret, numBytes);
339
 
}
340
 
 
341
 
 
342
 
//
343
 
// Check all allocations in a list for damage by calling check on each.
344
 
//
345
 
void TAllocation::checkAllocList() const
346
 
{
347
 
    for (const TAllocation* alloc = this; alloc != 0; alloc = alloc->prevAlloc)
348
 
        alloc->check();
349
 
}