Commit 4e89d233 by alokp@chromium.org

Cleaned up Common.h. Removed unnecessary includes and typedefs. Removed tabs…

Cleaned up Common.h. Removed unnecessary includes and typedefs. Removed tabs from PoolAlloc.h and replaced UINT_PTR with uintptr_t. Review URL: http://codereview.appspot.com/1221041 git-svn-id: https://angleproject.googlecode.com/svn/trunk@289 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent cff1aff5
...@@ -7,46 +7,17 @@ ...@@ -7,46 +7,17 @@
#ifndef _COMMON_INCLUDED_ #ifndef _COMMON_INCLUDED_
#define _COMMON_INCLUDED_ #define _COMMON_INCLUDED_
#ifdef _WIN32 #include <assert.h>
#include <basetsd.h> #include <stdio.h>
#elif defined (solaris)
#include <sys/int_types.h>
#define UINT_PTR uintptr_t
#else
#include <stdint.h>
#define UINT_PTR uintptr_t
#endif
/* windows only pragma */
#ifdef _MSC_VER
#pragma warning(disable : 4786) // Don't warn about too long identifiers
#pragma warning(disable : 4514) // unused inline method
#pragma warning(disable : 4201) // nameless union
#endif
// #include <map>
// Doing the push and pop below for warnings does not leave the warning state #include <string>
// the way it was. This seems like a defect in the compiler. We would like #include <vector>
// to do this, but since it does not work correctly right now, it is turned
// off.
//
//??#pragma warning(push, 3)
#include <set> #include "compiler/PoolAlloc.h"
#include <vector>
#include <map>
#include <list>
#include <string>
#include <stdio.h>
//??#pragma warning(pop)
typedef int TSourceLoc; typedef int TSourceLoc;
#include <assert.h>
#include "compiler/PoolAlloc.h"
// //
// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme. // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
// //
...@@ -82,20 +53,6 @@ public: ...@@ -82,20 +53,6 @@ public:
TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {} TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
}; };
template <class T> class TList : public std::list<T, pool_allocator<T> > {
public:
typedef typename std::list<T, pool_allocator<T> >::size_type size_type;
TList() : std::list<T, pool_allocator<T> >() {}
TList(const pool_allocator<T>& a) : std::list<T, pool_allocator<T> >(a) {}
TList(size_type i): std::list<T, pool_allocator<T> >(i) {}
};
// This is called TStlSet, because TSet is taken by an existing compiler class.
template <class T, class CMP> class TStlSet : public std::set<T, CMP, pool_allocator<T> > {
// No pool allocator versions of constructors in std::set.
};
template <class K, class D, class CMP = std::less<K> > template <class K, class D, class CMP = std::less<K> >
class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > { class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
public: public:
...@@ -113,12 +70,6 @@ public: ...@@ -113,12 +70,6 @@ public:
typedef std::basic_string<char> TPersistString; typedef std::basic_string<char> TPersistString;
// //
// templatized min and max functions.
//
template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
//
// Create a TString object from an integer. // Create a TString object from an integer.
// //
inline const TString String(const int i, const int base = 10) inline const TString String(const int i, const int base = 10)
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#ifndef CROSSCOMPILERGLSL_OUTPUTGLSL_H_ #ifndef CROSSCOMPILERGLSL_OUTPUTGLSL_H_
#define CROSSCOMPILERGLSL_OUTPUTGLSL_H_ #define CROSSCOMPILERGLSL_OUTPUTGLSL_H_
#include <set>
#include "compiler/intermediate.h" #include "compiler/intermediate.h"
#include "compiler/ParseHelper.h" #include "compiler/ParseHelper.h"
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#ifndef COMPILER_OUTPUTHLSL_H_ #ifndef COMPILER_OUTPUTHLSL_H_
#define COMPILER_OUTPUTHLSL_H_ #define COMPILER_OUTPUTHLSL_H_
#include <list>
#include <set>
#include "compiler/intermediate.h" #include "compiler/intermediate.h"
#include "compiler/ParseHelper.h" #include "compiler/ParseHelper.h"
......
...@@ -6,67 +6,67 @@ ...@@ -6,67 +6,67 @@
#include "compiler/PoolAlloc.h" #include "compiler/PoolAlloc.h"
#include "compiler/osinclude.h"
#include "compiler/Common.h" #include "compiler/Common.h"
#include "compiler/InitializeGlobals.h" #include "compiler/InitializeGlobals.h"
#include "compiler/osinclude.h"
OS_TLSIndex PoolIndex = OS_INVALID_TLS_INDEX; OS_TLSIndex PoolIndex = OS_INVALID_TLS_INDEX;
void InitializeGlobalPools() void InitializeGlobalPools()
{ {
TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
if (globalPools) if (globalPools)
return; return;
TPoolAllocator *globalPoolAllocator = new TPoolAllocator(true); TPoolAllocator *globalPoolAllocator = new TPoolAllocator(true);
TThreadGlobalPools* threadData = new TThreadGlobalPools(); TThreadGlobalPools* threadData = new TThreadGlobalPools();
threadData->globalPoolAllocator = globalPoolAllocator; threadData->globalPoolAllocator = globalPoolAllocator;
OS_SetTLSValue(PoolIndex, threadData); OS_SetTLSValue(PoolIndex, threadData);
globalPoolAllocator->push(); globalPoolAllocator->push();
} }
void FreeGlobalPools() void FreeGlobalPools()
{ {
// Release the allocated memory for this thread. // Release the allocated memory for this thread.
TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
if (!globalPools) if (!globalPools)
return; return;
GlobalPoolAllocator.popAll(); GlobalPoolAllocator.popAll();
delete &GlobalPoolAllocator; delete &GlobalPoolAllocator;
delete globalPools; delete globalPools;
} }
bool InitializePoolIndex() bool InitializePoolIndex()
{ {
// Allocate a TLS index. // Allocate a TLS index.
if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX) if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
return false; return false;
return true; return true;
} }
void FreePoolIndex() void FreePoolIndex()
{ {
// Release the TLS index. // Release the TLS index.
OS_FreeTLSIndex(PoolIndex); OS_FreeTLSIndex(PoolIndex);
} }
TPoolAllocator& GetGlobalPoolAllocator() TPoolAllocator& GetGlobalPoolAllocator()
{ {
TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
return *threadData->globalPoolAllocator; return *threadData->globalPoolAllocator;
} }
void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator) void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator)
{ {
TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex));
threadData->globalPoolAllocator = poolAllocator; threadData->globalPoolAllocator = poolAllocator;
} }
// //
...@@ -74,77 +74,77 @@ void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator) ...@@ -74,77 +74,77 @@ void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator)
// is documented in PoolAlloc.h. // is documented in PoolAlloc.h.
// //
TPoolAllocator::TPoolAllocator(bool g, int growthIncrement, int allocationAlignment) : TPoolAllocator::TPoolAllocator(bool g, int growthIncrement, int allocationAlignment) :
global(g), global(g),
pageSize(growthIncrement), pageSize(growthIncrement),
alignment(allocationAlignment), alignment(allocationAlignment),
freeList(0), freeList(0),
inUseList(0), inUseList(0),
numCalls(0) numCalls(0)
{ {
// //
// Don't allow page sizes we know are smaller than all common // Don't allow page sizes we know are smaller than all common
// OS page sizes. // OS page sizes.
// //
if (pageSize < 4*1024) if (pageSize < 4*1024)
pageSize = 4*1024; pageSize = 4*1024;
// //
// A large currentPageOffset indicates a new page needs to // A large currentPageOffset indicates a new page needs to
// be obtained to allocate memory. // be obtained to allocate memory.
// //
currentPageOffset = pageSize; currentPageOffset = pageSize;
// //
// Adjust alignment to be at least pointer aligned and // Adjust alignment to be at least pointer aligned and
// power of 2. // power of 2.
// //
size_t minAlign = sizeof(void*); size_t minAlign = sizeof(void*);
alignment &= ~(minAlign - 1); alignment &= ~(minAlign - 1);
if (alignment < minAlign) if (alignment < minAlign)
alignment = minAlign; alignment = minAlign;
size_t a = 1; size_t a = 1;
while (a < alignment) while (a < alignment)
a <<= 1; a <<= 1;
alignment = a; alignment = a;
alignmentMask = a - 1; alignmentMask = a - 1;
// //
// Align header skip // Align header skip
// //
headerSkip = minAlign; headerSkip = minAlign;
if (headerSkip < sizeof(tHeader)) { if (headerSkip < sizeof(tHeader)) {
headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask; headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask;
} }
} }
TPoolAllocator::~TPoolAllocator() TPoolAllocator::~TPoolAllocator()
{ {
if (!global) { if (!global) {
// //
// Then we know that this object is not being // Then we know that this object is not being
// allocated after other, globally scoped objects // allocated after other, globally scoped objects
// that depend on it. So we can delete the "in use" memory. // that depend on it. So we can delete the "in use" memory.
// //
while (inUseList) { while (inUseList) {
tHeader* next = inUseList->nextPage; tHeader* next = inUseList->nextPage;
inUseList->~tHeader(); inUseList->~tHeader();
delete [] reinterpret_cast<char*>(inUseList); delete [] reinterpret_cast<char*>(inUseList);
inUseList = next; inUseList = next;
} }
} }
// //
// Always delete the free list memory - it can't be being // Always delete the free list memory - it can't be being
// (correctly) referenced, whether the pool allocator was // (correctly) referenced, whether the pool allocator was
// global or not. We should not check the guard blocks // global or not. We should not check the guard blocks
// here, because we did it already when the block was // here, because we did it already when the block was
// placed into the free list. // placed into the free list.
// //
while (freeList) { while (freeList) {
tHeader* next = freeList->nextPage; tHeader* next = freeList->nextPage;
delete [] reinterpret_cast<char*>(freeList); delete [] reinterpret_cast<char*>(freeList);
freeList = next; freeList = next;
} }
} }
// Support MSVC++ 6.0 // Support MSVC++ 6.0
...@@ -153,9 +153,9 @@ const unsigned char TAllocation::guardBlockEndVal = 0xfe; ...@@ -153,9 +153,9 @@ const unsigned char TAllocation::guardBlockEndVal = 0xfe;
const unsigned char TAllocation::userDataFill = 0xcd; const unsigned char TAllocation::userDataFill = 0xcd;
#ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
const size_t TAllocation::guardBlockSize = 16; const size_t TAllocation::guardBlockSize = 16;
#else #else
const size_t TAllocation::guardBlockSize = 0; const size_t TAllocation::guardBlockSize = 0;
#endif #endif
// //
...@@ -163,29 +163,29 @@ const unsigned char TAllocation::userDataFill = 0xcd; ...@@ -163,29 +163,29 @@ const unsigned char TAllocation::userDataFill = 0xcd;
// //
void TAllocation::checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const void TAllocation::checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const
{ {
for (size_t x = 0; x < guardBlockSize; x++) { for (size_t x = 0; x < guardBlockSize; x++) {
if (blockMem[x] != val) { if (blockMem[x] != val) {
char assertMsg[80]; char assertMsg[80];
// We don't print the assert message. It's here just to be helpful. // We don't print the assert message. It's here just to be helpful.
sprintf(assertMsg, "PoolAlloc: Damage %s %lu byte allocation at 0x%p\n", sprintf(assertMsg, "PoolAlloc: Damage %s %lu byte allocation at 0x%p\n",
locText, size, data()); locText, size, data());
assert(0 && "PoolAlloc: Damage in guard block"); assert(0 && "PoolAlloc: Damage in guard block");
} }
} }
} }
void TPoolAllocator::push() void TPoolAllocator::push()
{ {
tAllocState state = { currentPageOffset, inUseList }; tAllocState state = { currentPageOffset, inUseList };
stack.push_back(state); stack.push_back(state);
// //
// Indicate there is no current page to allocate from. // Indicate there is no current page to allocate from.
// //
currentPageOffset = pageSize; currentPageOffset = pageSize;
} }
// //
...@@ -197,27 +197,27 @@ void TPoolAllocator::push() ...@@ -197,27 +197,27 @@ void TPoolAllocator::push()
// //
void TPoolAllocator::pop() void TPoolAllocator::pop()
{ {
if (stack.size() < 1) if (stack.size() < 1)
return; return;
tHeader* page = stack.back().page; tHeader* page = stack.back().page;
currentPageOffset = stack.back().offset; currentPageOffset = stack.back().offset;
while (inUseList != page) { while (inUseList != page) {
// invoke destructor to free allocation list // invoke destructor to free allocation list
inUseList->~tHeader(); inUseList->~tHeader();
tHeader* nextInUse = inUseList->nextPage; tHeader* nextInUse = inUseList->nextPage;
if (inUseList->pageCount > 1) if (inUseList->pageCount > 1)
delete [] reinterpret_cast<char*>(inUseList); delete [] reinterpret_cast<char*>(inUseList);
else { else {
inUseList->nextPage = freeList; inUseList->nextPage = freeList;
freeList = inUseList; freeList = inUseList;
} }
inUseList = nextInUse; inUseList = nextInUse;
} }
stack.pop_back(); stack.pop_back();
} }
// //
...@@ -226,81 +226,81 @@ void TPoolAllocator::pop() ...@@ -226,81 +226,81 @@ void TPoolAllocator::pop()
// //
void TPoolAllocator::popAll() void TPoolAllocator::popAll()
{ {
while (stack.size() > 0) while (stack.size() > 0)
pop(); pop();
} }
void* TPoolAllocator::allocate(size_t numBytes) void* TPoolAllocator::allocate(size_t numBytes)
{ {
// If we are using guard blocks, all allocations are bracketed by // If we are using guard blocks, all allocations are bracketed by
// them: [guardblock][allocation][guardblock]. numBytes is how // them: [guardblock][allocation][guardblock]. numBytes is how
// much memory the caller asked for. allocationSize is the total // much memory the caller asked for. allocationSize is the total
// size including guard blocks. In release build, // size including guard blocks. In release build,
// guardBlockSize=0 and this all gets optimized away. // guardBlockSize=0 and this all gets optimized away.
size_t allocationSize = TAllocation::allocationSize(numBytes); size_t allocationSize = TAllocation::allocationSize(numBytes);
// //
// Just keep some interesting statistics. // Just keep some interesting statistics.
// //
++numCalls; ++numCalls;
totalBytes += numBytes; totalBytes += numBytes;
// //
// Do the allocation, most likely case first, for efficiency. // Do the allocation, most likely case first, for efficiency.
// This step could be moved to be inline sometime. // This step could be moved to be inline sometime.
// //
if (currentPageOffset + allocationSize <= pageSize) { if (currentPageOffset + allocationSize <= pageSize) {
// //
// Safe to allocate from currentPageOffset. // Safe to allocate from currentPageOffset.
// //
unsigned char* memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset; unsigned char* memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset;
currentPageOffset += allocationSize; currentPageOffset += allocationSize;
currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask; currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask;
return initializeAllocation(inUseList, memory, numBytes); return initializeAllocation(inUseList, memory, numBytes);
} }
if (allocationSize + headerSkip > pageSize) { if (allocationSize + headerSkip > pageSize) {
// //
// Do a multi-page allocation. Don't mix these with the others. // Do a multi-page allocation. Don't mix these with the others.
// The OS is efficient and allocating and free-ing multiple pages. // The OS is efficient and allocating and free-ing multiple pages.
// //
size_t numBytesToAlloc = allocationSize + headerSkip; size_t numBytesToAlloc = allocationSize + headerSkip;
tHeader* memory = reinterpret_cast<tHeader*>(::new char[numBytesToAlloc]); tHeader* memory = reinterpret_cast<tHeader*>(::new char[numBytesToAlloc]);
if (memory == 0) if (memory == 0)
return 0; return 0;
// Use placement-new to initialize header // Use placement-new to initialize header
new(memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize); new(memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize);
inUseList = memory; inUseList = memory;
currentPageOffset = pageSize; // make next allocation come from a new page currentPageOffset = pageSize; // make next allocation come from a new page
// No guard blocks for multi-page allocations (yet) // No guard blocks for multi-page allocations (yet)
return reinterpret_cast<void*>(reinterpret_cast<UINT_PTR>(memory) + headerSkip); return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(memory) + headerSkip);
} }
// //
// Need a simple page to allocate from. // Need a simple page to allocate from.
// //
tHeader* memory; tHeader* memory;
if (freeList) { if (freeList) {
memory = freeList; memory = freeList;
freeList = freeList->nextPage; freeList = freeList->nextPage;
} else { } else {
memory = reinterpret_cast<tHeader*>(::new char[pageSize]); memory = reinterpret_cast<tHeader*>(::new char[pageSize]);
if (memory == 0) if (memory == 0)
return 0; return 0;
} }
// Use placement-new to initialize header // Use placement-new to initialize header
new(memory) tHeader(inUseList, 1); new(memory) tHeader(inUseList, 1);
inUseList = memory; inUseList = memory;
unsigned char* ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip; unsigned char* ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip;
currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask; currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask;
return initializeAllocation(inUseList, ret, numBytes); return initializeAllocation(inUseList, ret, numBytes);
} }
...@@ -309,6 +309,6 @@ void* TPoolAllocator::allocate(size_t numBytes) ...@@ -309,6 +309,6 @@ void* TPoolAllocator::allocate(size_t numBytes)
// //
void TAllocation::checkAllocList() const void TAllocation::checkAllocList() const
{ {
for (const TAllocation* alloc = this; alloc != 0; alloc = alloc->prevAlloc) for (const TAllocation* alloc = this; alloc != 0; alloc = alloc->prevAlloc)
alloc->check(); alloc->check();
} }
...@@ -244,7 +244,7 @@ public: ...@@ -244,7 +244,7 @@ public:
totalSize = size; totalSize = size;
if (isArray()) if (isArray())
totalSize *= Max(getArraySize(), getMaxArraySize()); totalSize *= std::max(getArraySize(), getMaxArraySize());
return totalSize; return totalSize;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment