Commit b1e8c6fb by alokp@chromium.org

Getting ready to fix compile-errors on mac. This CL only contains cosmetic…

Getting ready to fix compile-errors on mac. This CL only contains cosmetic changes. Removed SGI_STL blocks. Removed tabs and fixed formatting. Review URL: http://codereview.appspot.com/1138043 git-svn-id: https://angleproject.googlecode.com/svn/trunk@272 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 4071e661
...@@ -152,11 +152,11 @@ const unsigned char TAllocation::guardBlockBeginVal = 0xfb; ...@@ -152,11 +152,11 @@ const unsigned char TAllocation::guardBlockBeginVal = 0xfb;
const unsigned char TAllocation::guardBlockEndVal = 0xfe; 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
// //
// Check a single guard block for damage // Check a single guard block for damage
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#define _POOLALLOC_INCLUDED_ #define _POOLALLOC_INCLUDED_
#ifdef _DEBUG #ifdef _DEBUG
# define GUARD_BLOCKS // define to enable guard block sanity checking #define GUARD_BLOCKS // define to enable guard block sanity checking
#endif #endif
// //
...@@ -41,63 +41,63 @@ ...@@ -41,63 +41,63 @@
class TAllocation { class TAllocation {
public: public:
TAllocation(size_t size, unsigned char* mem, TAllocation* prev = 0) : TAllocation(size_t size, unsigned char* mem, TAllocation* prev = 0) :
size(size), mem(mem), prevAlloc(prev) { size(size), mem(mem), prevAlloc(prev) {
// Allocations are bracketed: // Allocations are bracketed:
// [allocationHeader][initialGuardBlock][userData][finalGuardBlock] // [allocationHeader][initialGuardBlock][userData][finalGuardBlock]
// This would be cleaner with if (guardBlockSize)..., but that // This would be cleaner with if (guardBlockSize)..., but that
// makes the compiler print warnings about 0 length memsets, // makes the compiler print warnings about 0 length memsets,
// even with the if() protecting them. // even with the if() protecting them.
# ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
memset(preGuard(), guardBlockBeginVal, guardBlockSize); memset(preGuard(), guardBlockBeginVal, guardBlockSize);
memset(data(), userDataFill, size); memset(data(), userDataFill, size);
memset(postGuard(), guardBlockEndVal, guardBlockSize); memset(postGuard(), guardBlockEndVal, guardBlockSize);
# endif #endif
} }
void check() const { void check() const {
checkGuardBlock(preGuard(), guardBlockBeginVal, "before"); checkGuardBlock(preGuard(), guardBlockBeginVal, "before");
checkGuardBlock(postGuard(), guardBlockEndVal, "after"); checkGuardBlock(postGuard(), guardBlockEndVal, "after");
} }
void checkAllocList() const; void checkAllocList() const;
// Return total size needed to accomodate user buffer of 'size', // Return total size needed to accomodate user buffer of 'size',
// plus our tracking data. // plus our tracking data.
inline static size_t allocationSize(size_t size) { inline static size_t allocationSize(size_t size) {
return size + 2 * guardBlockSize + headerSize(); return size + 2 * guardBlockSize + headerSize();
} }
// Offset from surrounding buffer to get to user data buffer. // Offset from surrounding buffer to get to user data buffer.
inline static unsigned char* offsetAllocation(unsigned char* m) { inline static unsigned char* offsetAllocation(unsigned char* m) {
return m + guardBlockSize + headerSize(); return m + guardBlockSize + headerSize();
} }
private: private:
void checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const; void checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const;
// Find offsets to pre and post guard blocks, and user data buffer // Find offsets to pre and post guard blocks, and user data buffer
unsigned char* preGuard() const { return mem + headerSize(); } unsigned char* preGuard() const { return mem + headerSize(); }
unsigned char* data() const { return preGuard() + guardBlockSize; } unsigned char* data() const { return preGuard() + guardBlockSize; }
unsigned char* postGuard() const { return data() + size; } unsigned char* postGuard() const { return data() + size; }
size_t size; // size of the user data area size_t size; // size of the user data area
unsigned char* mem; // beginning of our allocation (pts to header) unsigned char* mem; // beginning of our allocation (pts to header)
TAllocation* prevAlloc; // prior allocation in the chain TAllocation* prevAlloc; // prior allocation in the chain
// Support MSVC++ 6.0 // Support MSVC++ 6.0
const static unsigned char guardBlockBeginVal; const static unsigned char guardBlockBeginVal;
const static unsigned char guardBlockEndVal; const static unsigned char guardBlockEndVal;
const static unsigned char userDataFill; const static unsigned char userDataFill;
const static size_t guardBlockSize; const static size_t guardBlockSize;
# ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
inline static size_t headerSize() { return sizeof(TAllocation); } inline static size_t headerSize() { return sizeof(TAllocation); }
# else #else
inline static size_t headerSize() { return 0; } inline static size_t headerSize() { return 0; }
# endif #endif
}; };
// //
// There are several stacks. One is to track the pushing and popping // There are several stacks. One is to track the pushing and popping
// of the user, and not yet implemented. The others are simply a // of the user, and not yet implemented. The others are simply a
...@@ -114,102 +114,102 @@ private: ...@@ -114,102 +114,102 @@ private:
// //
class TPoolAllocator { class TPoolAllocator {
public: public:
TPoolAllocator(bool global = false, int growthIncrement = 8*1024, int allocationAlignment = 16); TPoolAllocator(bool global = false, int growthIncrement = 8*1024, int allocationAlignment = 16);
// //
// Don't call the destructor just to free up the memory, call pop() // Don't call the destructor just to free up the memory, call pop()
// //
~TPoolAllocator(); ~TPoolAllocator();
// //
// Call push() to establish a new place to pop memory too. Does not // Call push() to establish a new place to pop memory too. Does not
// have to be called to get things started. // have to be called to get things started.
// //
void push(); void push();
// //
// Call pop() to free all memory allocated since the last call to push(), // Call pop() to free all memory allocated since the last call to push(),
// or if no last call to push, frees all memory since first allocation. // or if no last call to push, frees all memory since first allocation.
// //
void pop(); void pop();
// //
// Call popAll() to free all memory allocated. // Call popAll() to free all memory allocated.
// //
void popAll(); void popAll();
// //
// Call allocate() to actually acquire memory. Returns 0 if no memory // Call allocate() to actually acquire memory. Returns 0 if no memory
// available, otherwise a properly aligned pointer to 'numBytes' of memory. // available, otherwise a properly aligned pointer to 'numBytes' of memory.
// //
void* allocate(size_t numBytes); void* allocate(size_t numBytes);
// //
// There is no deallocate. The point of this class is that // There is no deallocate. The point of this class is that
// deallocation can be skipped by the user of it, as the model // deallocation can be skipped by the user of it, as the model
// of use is to simultaneously deallocate everything at once // of use is to simultaneously deallocate everything at once
// by calling pop(), and to not have to solve memory leak problems. // by calling pop(), and to not have to solve memory leak problems.
// //
protected: protected:
friend struct tHeader; friend struct tHeader;
struct tHeader { struct tHeader {
tHeader(tHeader* nextPage, size_t pageCount) : tHeader(tHeader* nextPage, size_t pageCount) :
#ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
lastAllocation(0), lastAllocation(0),
#endif #endif
nextPage(nextPage), pageCount(pageCount) { } nextPage(nextPage),
pageCount(pageCount) { }
~tHeader() { ~tHeader() {
#ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
if (lastAllocation) if (lastAllocation)
lastAllocation->checkAllocList(); lastAllocation->checkAllocList();
#endif #endif
} }
tHeader* nextPage; tHeader* nextPage;
size_t pageCount; size_t pageCount;
#ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
TAllocation* lastAllocation; TAllocation* lastAllocation;
#endif #endif
}; };
struct tAllocState { struct tAllocState {
size_t offset; size_t offset;
tHeader* page; tHeader* page;
}; };
typedef std::vector<tAllocState> tAllocStack; typedef std::vector<tAllocState> tAllocStack;
// Track allocations if and only if we're using guard blocks // Track allocations if and only if we're using guard blocks
void* initializeAllocation(tHeader* block, unsigned char* memory, size_t numBytes) { void* initializeAllocation(tHeader* block, unsigned char* memory, size_t numBytes) {
# ifdef GUARD_BLOCKS #ifdef GUARD_BLOCKS
new(memory) TAllocation(numBytes, memory, block->lastAllocation); new(memory) TAllocation(numBytes, memory, block->lastAllocation);
block->lastAllocation = reinterpret_cast<TAllocation*>(memory); block->lastAllocation = reinterpret_cast<TAllocation*>(memory);
# endif #endif
// This is optimized entirely away if GUARD_BLOCKS is not defined.
// This is optimized entirely away if GUARD_BLOCKS is not defined. return TAllocation::offsetAllocation(memory);
return TAllocation::offsetAllocation(memory); }
}
bool global; // should be true if this object is globally scoped
bool global; // should be true if this object is globally scoped size_t pageSize; // granularity of allocation from the OS
size_t pageSize; // granularity of allocation from the OS size_t alignment; // all returned allocations will be aligned at
size_t alignment; // all returned allocations will be aligned at // this granularity, which will be a power of 2
// this granularity, which will be a power of 2 size_t alignmentMask;
size_t alignmentMask; size_t headerSkip; // amount of memory to skip to make room for the
size_t headerSkip; // amount of memory to skip to make room for the // header (basically, size of header, rounded
// header (basically, size of header, rounded // up to make it aligned
// up to make it aligned size_t currentPageOffset; // next offset in top of inUseList to allocate from
size_t currentPageOffset; // next offset in top of inUseList to allocate from tHeader* freeList; // list of popped memory
tHeader* freeList; // list of popped memory tHeader* inUseList; // list of all memory currently being used
tHeader* inUseList; // list of all memory currently being used tAllocStack stack; // stack of where to allocate from, to partition pool
tAllocStack stack; // stack of where to allocate from, to partition pool
int numCalls; // just an interesting statistic
int numCalls; // just an interesting statistic size_t totalBytes; // just an interesting statistic
size_t totalBytes; // just an interesting statistic
private: private:
TPoolAllocator& operator=(const TPoolAllocator&); // dont allow assignment operator TPoolAllocator& operator=(const TPoolAllocator&); // dont allow assignment operator
TPoolAllocator(const TPoolAllocator&); // dont allow default copy constructor TPoolAllocator(const TPoolAllocator&); // dont allow default copy constructor
}; };
...@@ -225,7 +225,7 @@ extern TPoolAllocator& GetGlobalPoolAllocator(); ...@@ -225,7 +225,7 @@ extern TPoolAllocator& GetGlobalPoolAllocator();
struct TThreadGlobalPools struct TThreadGlobalPools
{ {
TPoolAllocator* globalPoolAllocator; TPoolAllocator* globalPoolAllocator;
}; };
void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator); void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator);
...@@ -240,82 +240,56 @@ void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator); ...@@ -240,82 +240,56 @@ void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator);
template<class T> template<class T>
class pool_allocator { class pool_allocator {
public: public:
typedef size_t size_type; typedef size_t size_type;
typedef ptrdiff_t difference_type; typedef ptrdiff_t difference_type;
typedef T *pointer; typedef T* pointer;
typedef const T *const_pointer; typedef const T* const_pointer;
typedef T& reference; typedef T& reference;
typedef const T& const_reference; typedef const T& const_reference;
typedef T value_type; typedef T value_type;
template<class Other>
struct rebind { template<class Other>
typedef pool_allocator<Other> other; struct rebind {
}; typedef pool_allocator<Other> other;
pointer address(reference x) const { return &x; } };
const_pointer address(const_reference x) const { return &x; } pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
#ifdef USING_SGI_STL
pool_allocator() { } pool_allocator() : allocator(GlobalPoolAllocator) { }
#else pool_allocator(TPoolAllocator& a) : allocator(a) { }
pool_allocator() : allocator(GlobalPoolAllocator) { } pool_allocator(const pool_allocator<T>& p) : allocator(p.allocator) { }
pool_allocator(TPoolAllocator& a) : allocator(a) { }
pool_allocator(const pool_allocator<T>& p) : allocator(p.allocator) { } template<class Other>
#endif pool_allocator(const pool_allocator<Other>& p) : allocator(p.getAllocator()) { }
#if defined(_MSC_VER) && _MSC_VER >= 1300 pointer allocate(size_type n) {
template<class Other> return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T)));
#ifdef USING_SGI_STL }
pool_allocator(const pool_allocator<Other>& p) /*: allocator(p.getAllocator())*/ { } pointer allocate(size_type n, const void*) {
#else return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T)));
pool_allocator(const pool_allocator<Other>& p) : allocator(p.getAllocator()) { } }
#endif
#endif void deallocate(void*, size_type) { }
void deallocate(pointer, size_type) { }
#ifndef _WIN32
template<class Other> pointer _Charalloc(size_t n) {
pool_allocator(const pool_allocator<Other>& p) : allocator(p.getAllocator()) { } return reinterpret_cast<pointer>(getAllocator().allocate(n));
#endif }
#ifdef USING_SGI_STL void construct(pointer p, const T& val) { new ((void *)p) T(val); }
static pointer allocate(size_type n) { void destroy(pointer p) { p->T::~T(); }
return reinterpret_cast<pointer>(getAllocator().allocate(n)); }
pointer allocate(size_type n, const void*) { bool operator==(const pool_allocator& rhs) const { return &getAllocator() == &rhs.getAllocator(); }
return reinterpret_cast<pointer>(getAllocator().allocate(n)); } bool operator!=(const pool_allocator& rhs) const { return &getAllocator() != &rhs.getAllocator(); }
static void deallocate(void*, size_type) { } size_type max_size() const { return static_cast<size_type>(-1) / sizeof(T); }
static void deallocate(pointer, size_type) { } size_type max_size(int size) const { return static_cast<size_type>(-1) / size; }
#else
pointer allocate(size_type n) { void setAllocator(TPoolAllocator* a) { allocator = *a; }
return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T))); } TPoolAllocator& getAllocator() const { return allocator; }
pointer allocate(size_type n, const void*) {
return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T))); }
void deallocate(void*, size_type) { }
void deallocate(pointer, size_type) { }
#endif
pointer _Charalloc(size_t n) {
return reinterpret_cast<pointer>(getAllocator().allocate(n)); }
void construct(pointer p, const T& val) { new ((void *)p) T(val); }
void destroy(pointer p) { p->T::~T(); }
bool operator==(const pool_allocator& rhs) const { return &getAllocator() == &rhs.getAllocator(); }
bool operator!=(const pool_allocator& rhs) const { return &getAllocator() != &rhs.getAllocator(); }
size_type max_size() const { return static_cast<size_type>(-1) / sizeof(T); }
size_type max_size(int size) const { return static_cast<size_type>(-1) / size; }
#ifdef USING_SGI_STL
//void setAllocator(TPoolAllocator* a) { allocator = a; }
static TPoolAllocator& getAllocator() { return GlobalPoolAllocator; }
#else
void setAllocator(TPoolAllocator* a) { allocator = *a; }
TPoolAllocator& getAllocator() const { return allocator; }
protected: protected:
TPoolAllocator& allocator; TPoolAllocator& allocator;
#endif
}; };
#endif // _POOLALLOC_INCLUDED_ #endif // _POOLALLOC_INCLUDED_
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