Use ssize_t for size types in CUtlMemory

This commit is contained in:
Kawe Mazidjatari 2022-08-04 11:51:38 +02:00
parent 6d92fdf15f
commit 68b3185a41

View File

@ -39,16 +39,16 @@
// The CUtlMemory class: // The CUtlMemory class:
// A growable memory class which doubles in size by default. // A growable memory class which doubles in size by default.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I = int64_t > template< class T, class I = ssize_t >
class CUtlMemory class CUtlMemory
{ {
template< class A, class B> friend class CUtlVector; template< class A, class B> friend class CUtlVector;
template< class A, size_t B> friend class CUtlVectorFixedGrowableCompat; template< class A, size_t B> friend class CUtlVectorFixedGrowableCompat;
public: public:
// constructor, destructor // constructor, destructor
CUtlMemory(int64_t nGrowSize = 0, int64_t nInitSize = 0); CUtlMemory(ssize_t nGrowSize = 0, ssize_t nInitSize = 0);
CUtlMemory(T* pMemory, int64_t numElements); CUtlMemory(T* pMemory, ssize_t numElements);
CUtlMemory(const T* pMemory, int64_t numElements); CUtlMemory(const T* pMemory, ssize_t numElements);
~CUtlMemory(); ~CUtlMemory();
CUtlMemory(const CUtlMemory&) = delete; CUtlMemory(const CUtlMemory&) = delete;
@ -58,7 +58,7 @@ public:
CUtlMemory& operator=(CUtlMemory&& moveFrom); CUtlMemory& operator=(CUtlMemory&& moveFrom);
// Set the size by which the memory grows // Set the size by which the memory grows
void Init(int64_t nGrowSize = 0, int64_t nInitSize = 0); void Init(ssize_t nGrowSize = 0, ssize_t nInitSize = 0);
class Iterator_t class Iterator_t
{ {
@ -94,9 +94,9 @@ public:
const T* Base() const; const T* Base() const;
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
void SetExternalBuffer(T* pMemory, int64_t numElements); void SetExternalBuffer(T* pMemory, ssize_t numElements);
void SetExternalBuffer(const T* pMemory, int64_t numElements); void SetExternalBuffer(const T* pMemory, ssize_t numElements);
void AssumeMemory(T* pMemory, int64_t nSize); void AssumeMemory(T* pMemory, ssize_t nSize);
T* Detach(); T* Detach();
void* DetachMemory(); void* DetachMemory();
@ -105,23 +105,23 @@ public:
// Switches the buffer from an external memory buffer to a reallocatable buffer // Switches the buffer from an external memory buffer to a reallocatable buffer
// Will copy the current contents of the external buffer to the reallocatable buffer // Will copy the current contents of the external buffer to the reallocatable buffer
void ConvertToGrowableMemory(int64_t nGrowSize); void ConvertToGrowableMemory(ssize_t nGrowSize);
// Size // Size
int64_t NumAllocated() const; ssize_t NumAllocated() const;
int64_t Count() const; ssize_t Count() const;
// Grows the memory, so that at least allocated + num elements are allocated // Grows the memory, so that at least allocated + num elements are allocated
void Grow(int64_t num = 1); void Grow(ssize_t num = 1);
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
void EnsureCapacity(int64_t num); void EnsureCapacity(ssize_t num);
// Memory deallocation // Memory deallocation
void Purge(); void Purge();
// Purge all but the given number of elements // Purge all but the given number of elements
void Purge(int64_t numElements); void Purge(ssize_t numElements);
// is the memory externally allocated? // is the memory externally allocated?
bool IsExternallyAllocated() const; bool IsExternallyAllocated() const;
@ -130,7 +130,7 @@ public:
bool IsReadOnly() const; bool IsReadOnly() const;
// Set the size by which the memory grows // Set the size by which the memory grows
void SetGrowSize(int64_t size); void SetGrowSize(ssize_t size);
protected: protected:
void ValidateGrowSize() void ValidateGrowSize()
@ -139,7 +139,7 @@ protected:
if (m_nGrowSize && m_nGrowSize != EXTERNAL_BUFFER_MARKER) if (m_nGrowSize && m_nGrowSize != EXTERNAL_BUFFER_MARKER)
{ {
// Max grow size at 128 bytes on XBOX // Max grow size at 128 bytes on XBOX
const int64_t MAX_GROW = 128; const ssize_t MAX_GROW = 128;
if (m_nGrowSize * sizeof(T) > MAX_GROW) if (m_nGrowSize * sizeof(T) > MAX_GROW)
{ {
m_nGrowSize = max(1, MAX_GROW / sizeof(T)); m_nGrowSize = max(1, MAX_GROW / sizeof(T));
@ -155,8 +155,8 @@ protected:
}; };
T* m_pMemory; T* m_pMemory;
int64_t m_nAllocationCount; ssize_t m_nAllocationCount;
int64_t m_nGrowSize; ssize_t m_nGrowSize;
}; };
@ -164,19 +164,19 @@ protected:
// The CUtlMemory class: // The CUtlMemory class:
// A growable memory class which doubles in size by default. // A growable memory class which doubles in size by default.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, size_t SIZE, class I = int64_t > template< class T, size_t SIZE, class I = ssize_t >
class CUtlMemoryFixedGrowable : public CUtlMemory< T, I > class CUtlMemoryFixedGrowable : public CUtlMemory< T, I >
{ {
typedef CUtlMemory< T, I > BaseClass; typedef CUtlMemory< T, I > BaseClass;
public: public:
CUtlMemoryFixedGrowable(int64_t nGrowSize = 0, int64_t nInitSize = SIZE) : BaseClass(m_pFixedMemory, SIZE) CUtlMemoryFixedGrowable(ssize_t nGrowSize = 0, ssize_t nInitSize = SIZE) : BaseClass(m_pFixedMemory, SIZE)
{ {
Assert(nInitSize == 0 || nInitSize == SIZE); Assert(nInitSize == 0 || nInitSize == SIZE);
m_nMallocGrowSize = nGrowSize; m_nMallocGrowSize = nGrowSize;
} }
void Grow(int64_t nCount = 1) void Grow(ssize_t nCount = 1)
{ {
if (this->IsExternallyAllocated()) if (this->IsExternallyAllocated())
{ {
@ -185,7 +185,7 @@ public:
BaseClass::Grow(nCount); BaseClass::Grow(nCount);
} }
void EnsureCapacity(int64_t num) void EnsureCapacity(ssize_t num)
{ {
if (CUtlMemory<T>::m_nAllocationCount >= num) if (CUtlMemory<T>::m_nAllocationCount >= num)
return; return;
@ -200,7 +200,7 @@ public:
} }
private: private:
int64_t m_nMallocGrowSize; ssize_t m_nMallocGrowSize;
T m_pFixedMemory[SIZE]; T m_pFixedMemory[SIZE];
}; };
@ -208,68 +208,68 @@ private:
// The CUtlMemoryFixed class: // The CUtlMemoryFixed class:
// A fixed memory class // A fixed memory class
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< typename T, size_t SIZE, int64_t nAlignment = 0 > template< typename T, size_t SIZE, ssize_t nAlignment = 0 >
class CUtlMemoryFixed class CUtlMemoryFixed
{ {
public: public:
// constructor, destructor // constructor, destructor
CUtlMemoryFixed(int64_t nGrowSize = 0, int64_t nInitSize = 0) { Assert(nInitSize == 0 || nInitSize == SIZE); } CUtlMemoryFixed(ssize_t nGrowSize = 0, ssize_t nInitSize = 0) { Assert(nInitSize == 0 || nInitSize == SIZE); }
CUtlMemoryFixed(T* pMemory, int64_t numElements) { Assert(0); } CUtlMemoryFixed(T* pMemory, ssize_t numElements) { Assert(0); }
// Can we use this index? // Can we use this index?
bool IsIdxValid(int64_t i) const { return (i >= 0) && (i < SIZE); } bool IsIdxValid(ssize_t i) const { return (i >= 0) && (i < SIZE); }
// Specify the invalid ('null') index that we'll only return on failure // Specify the invalid ('null') index that we'll only return on failure
static const int64_t INVALID_INDEX = -1; // For use with COMPILE_TIME_ASSERT static const ssize_t INVALID_INDEX = -1; // For use with COMPILE_TIME_ASSERT
static int64_t InvalidIndex() { return INVALID_INDEX; } static ssize_t InvalidIndex() { return INVALID_INDEX; }
// Gets the base address // Gets the base address
T* Base() { if (nAlignment == 0) return (T*)(&m_Memory[0]); else return (T*)AlignValue(&m_Memory[0], nAlignment); } T* Base() { if (nAlignment == 0) return (T*)(&m_Memory[0]); else return (T*)AlignValue(&m_Memory[0], nAlignment); }
const T* Base() const { if (nAlignment == 0) return (T*)(&m_Memory[0]); else return (T*)AlignValue(&m_Memory[0], nAlignment); } const T* Base() const { if (nAlignment == 0) return (T*)(&m_Memory[0]); else return (T*)AlignValue(&m_Memory[0], nAlignment); }
// element access // element access
T& operator[](int64_t i) { Assert(IsIdxValid(i)); return Base()[i]; } T& operator[](ssize_t i) { Assert(IsIdxValid(i)); return Base()[i]; }
const T& operator[](int64_t i) const { Assert(IsIdxValid(i)); return Base()[i]; } const T& operator[](ssize_t i) const { Assert(IsIdxValid(i)); return Base()[i]; }
T& Element(int64_t i) { Assert(IsIdxValid(i)); return Base()[i]; } T& Element(ssize_t i) { Assert(IsIdxValid(i)); return Base()[i]; }
const T& Element(int64_t i) const { Assert(IsIdxValid(i)); return Base()[i]; } const T& Element(ssize_t i) const { Assert(IsIdxValid(i)); return Base()[i]; }
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
void SetExternalBuffer(T* pMemory, int64_t numElements) { Assert(0); } void SetExternalBuffer(T* pMemory, ssize_t numElements) { Assert(0); }
// Size // Size
int64_t NumAllocated() const { return SIZE; } ssize_t NumAllocated() const { return SIZE; }
int64_t Count() const { return SIZE; } ssize_t Count() const { return SIZE; }
// Grows the memory, so that at least allocated + num elements are allocated // Grows the memory, so that at least allocated + num elements are allocated
void Grow(int64_t num = 1) { Assert(0); } void Grow(ssize_t num = 1) { Assert(0); }
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
void EnsureCapacity(int64_t num) { Assert(num <= SIZE); } void EnsureCapacity(ssize_t num) { Assert(num <= SIZE); }
// Memory deallocation // Memory deallocation
void Purge() {} void Purge() {}
// Purge all but the given number of elements (NOT IMPLEMENTED IN CUtlMemoryFixed) // Purge all but the given number of elements (NOT IMPLEMENTED IN CUtlMemoryFixed)
void Purge(int64_t numElements) { Assert(0); } void Purge(ssize_t numElements) { Assert(0); }
// is the memory externally allocated? // is the memory externally allocated?
bool IsExternallyAllocated() const { return false; } bool IsExternallyAllocated() const { return false; }
// Set the size by which the memory grows // Set the size by which the memory grows
void SetGrowSize(int64_t size) {} void SetGrowSize(ssize_t size) {}
class Iterator_t class Iterator_t
{ {
public: public:
Iterator_t(int64_t i) : index(i) {} Iterator_t(ssize_t i) : index(i) {}
int64_t index; ssize_t index;
bool operator==(const Iterator_t it) const { return index == it.index; } bool operator==(const Iterator_t it) const { return index == it.index; }
bool operator!=(const Iterator_t it) const { return index != it.index; } bool operator!=(const Iterator_t it) const { return index != it.index; }
}; };
Iterator_t First() const { return Iterator_t(IsIdxValid(0) ? 0 : InvalidIndex()); } Iterator_t First() const { return Iterator_t(IsIdxValid(0) ? 0 : InvalidIndex()); }
Iterator_t Next(const Iterator_t& it) const { return Iterator_t(IsIdxValid(it.index + 1) ? it.index + 1 : InvalidIndex()); } Iterator_t Next(const Iterator_t& it) const { return Iterator_t(IsIdxValid(it.index + 1) ? it.index + 1 : InvalidIndex()); }
int64_t GetIndex(const Iterator_t& it) const { return it.index; } ssize_t GetIndex(const Iterator_t& it) const { return it.index; }
bool IsIdxAfter(int64_t i, const Iterator_t& it) const { return i > it.index; } bool IsIdxAfter(ssize_t i, const Iterator_t& it) const { return i > it.index; }
bool IsValidIterator(const Iterator_t& it) const { return IsIdxValid(it.index); } bool IsValidIterator(const Iterator_t& it) const { return IsIdxValid(it.index); }
Iterator_t InvalidIterator() const { return Iterator_t(InvalidIndex()); } Iterator_t InvalidIterator() const { return Iterator_t(InvalidIndex()); }
@ -291,32 +291,32 @@ class CUtlMemoryConservative
public: public:
// constructor, destructor // constructor, destructor
CUtlMemoryConservative(int64_t nGrowSize = 0, int64_t nInitSize = 0) : m_pMemory(NULL) CUtlMemoryConservative(ssize_t nGrowSize = 0, ssize_t nInitSize = 0) : m_pMemory(NULL)
{ {
#ifdef REMEMBER_ALLOC_SIZE_FOR_VALGRIND #ifdef REMEMBER_ALLOC_SIZE_FOR_VALGRIND
m_nCurAllocSize = 0; m_nCurAllocSize = 0;
#endif #endif
} }
CUtlMemoryConservative(T* pMemory, int64_t numElements) { Assert(0); } CUtlMemoryConservative(T* pMemory, ssize_t numElements) { Assert(0); }
~CUtlMemoryConservative() { if (m_pMemory) free(m_pMemory); } ~CUtlMemoryConservative() { if (m_pMemory) free(m_pMemory); }
// Can we use this index? // Can we use this index?
bool IsIdxValid(int64_t i) const { return (IsDebug()) ? (i >= 0 && i < NumAllocated()) : (i >= 0); } bool IsIdxValid(ssize_t i) const { return (IsDebug()) ? (i >= 0 && i < NumAllocated()) : (i >= 0); }
static int64_t InvalidIndex() { return -1; } static ssize_t InvalidIndex() { return -1; }
// Gets the base address // Gets the base address
T* Base() { return m_pMemory; } T* Base() { return m_pMemory; }
const T* Base() const { return m_pMemory; } const T* Base() const { return m_pMemory; }
// element access // element access
T& operator[](int64_t i) { Assert(IsIdxValid(i)); return Base()[i]; } T& operator[](ssize_t i) { Assert(IsIdxValid(i)); return Base()[i]; }
const T& operator[](int64_t i) const { Assert(IsIdxValid(i)); return Base()[i]; } const T& operator[](ssize_t i) const { Assert(IsIdxValid(i)); return Base()[i]; }
T& Element(int64_t i) { Assert(IsIdxValid(i)); return Base()[i]; } T& Element(ssize_t i) { Assert(IsIdxValid(i)); return Base()[i]; }
const T& Element(int64_t i) const { Assert(IsIdxValid(i)); return Base()[i]; } const T& Element(ssize_t i) const { Assert(IsIdxValid(i)); return Base()[i]; }
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
void SetExternalBuffer(T* pMemory, int64_t numElements) { Assert(0); } void SetExternalBuffer(T* pMemory, ssize_t numElements) { Assert(0); }
// Size // Size
FORCEINLINE void RememberAllocSize(size_t sz) FORCEINLINE void RememberAllocSize(size_t sz)
@ -335,11 +335,11 @@ public:
#endif #endif
} }
int64_t NumAllocated() const ssize_t NumAllocated() const
{ {
return AllocSize() / sizeof(T); return AllocSize() / sizeof(T);
} }
int64_t Count() const ssize_t Count() const
{ {
return NumAllocated(); return NumAllocated();
} }
@ -350,14 +350,14 @@ public:
RememberAllocSize(sz); RememberAllocSize(sz);
} }
// Grows the memory, so that at least allocated + num elements are allocated // Grows the memory, so that at least allocated + num elements are allocated
void Grow(int64_t num = 1) void Grow(ssize_t num = 1)
{ {
int64_t nCurN = NumAllocated(); ssize_t nCurN = NumAllocated();
ReAlloc((nCurN + num) * sizeof(T)); ReAlloc((nCurN + num) * sizeof(T));
} }
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
void EnsureCapacity(int64_t num) void EnsureCapacity(ssize_t num)
{ {
size_t nSize = sizeof(T) * MAX(num, Count()); size_t nSize = sizeof(T) * MAX(num, Count());
ReAlloc(nSize); ReAlloc(nSize);
@ -372,27 +372,27 @@ public:
} }
// Purge all but the given number of elements // Purge all but the given number of elements
void Purge(int64_t numElements) { ReAlloc(numElements * sizeof(T)); } void Purge(ssize_t numElements) { ReAlloc(numElements * sizeof(T)); }
// is the memory externally allocated? // is the memory externally allocated?
bool IsExternallyAllocated() const { return false; } bool IsExternallyAllocated() const { return false; }
// Set the size by which the memory grows // Set the size by which the memory grows
void SetGrowSize(int64_t size) {} void SetGrowSize(ssize_t size) {}
class Iterator_t class Iterator_t
{ {
public: public:
Iterator_t(int64_t i, int64_t _limit) : index(i), limit(_limit) {} Iterator_t(ssize_t i, ssize_t _limit) : index(i), limit(_limit) {}
int64_t index; ssize_t index;
int64_t limit; ssize_t limit;
bool operator==(const Iterator_t it) const { return index == it.index; } bool operator==(const Iterator_t it) const { return index == it.index; }
bool operator!=(const Iterator_t it) const { return index != it.index; } bool operator!=(const Iterator_t it) const { return index != it.index; }
}; };
Iterator_t First() const { int64_t limit = NumAllocated(); return Iterator_t(limit ? 0 : InvalidIndex(), limit); } Iterator_t First() const { ssize_t limit = NumAllocated(); return Iterator_t(limit ? 0 : InvalidIndex(), limit); }
Iterator_t Next(const Iterator_t& it) const { return Iterator_t((it.index + 1 < it.limit) ? it.index + 1 : InvalidIndex(), it.limit); } Iterator_t Next(const Iterator_t& it) const { return Iterator_t((it.index + 1 < it.limit) ? it.index + 1 : InvalidIndex(), it.limit); }
int64_t GetIndex(const Iterator_t& it) const { return it.index; } ssize_t GetIndex(const Iterator_t& it) const { return it.index; }
bool IsIdxAfter(int64_t i, const Iterator_t& it) const { return i > it.index; } bool IsIdxAfter(ssize_t i, const Iterator_t& it) const { return i > it.index; }
bool IsValidIterator(const Iterator_t& it) const { return IsIdxValid(it.index) && (it.index < it.limit); } bool IsValidIterator(const Iterator_t& it) const { return IsIdxValid(it.index) && (it.index < it.limit); }
Iterator_t InvalidIterator() const { return Iterator_t(InvalidIndex(), 0); } Iterator_t InvalidIterator() const { return Iterator_t(InvalidIndex(), 0); }
@ -410,7 +410,7 @@ private:
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I > template< class T, class I >
CUtlMemory<T, I>::CUtlMemory(int64_t nGrowSize, int64_t nInitAllocationCount) : m_pMemory(0), CUtlMemory<T, I>::CUtlMemory(ssize_t nGrowSize, ssize_t nInitAllocationCount) : m_pMemory(0),
m_nAllocationCount(nInitAllocationCount), m_nGrowSize(nGrowSize) m_nAllocationCount(nInitAllocationCount), m_nGrowSize(nGrowSize)
{ {
ValidateGrowSize(); ValidateGrowSize();
@ -424,7 +424,7 @@ m_nAllocationCount(nInitAllocationCount), m_nGrowSize(nGrowSize)
} }
template< class T, class I > template< class T, class I >
CUtlMemory<T, I>::CUtlMemory(T* pMemory, int64_t numElements) : m_pMemory(pMemory), CUtlMemory<T, I>::CUtlMemory(T* pMemory, ssize_t numElements) : m_pMemory(pMemory),
m_nAllocationCount(numElements) m_nAllocationCount(numElements)
{ {
// Special marker indicating externally supplied modifyable memory // Special marker indicating externally supplied modifyable memory
@ -432,7 +432,7 @@ m_nAllocationCount(numElements)
} }
template< class T, class I > template< class T, class I >
CUtlMemory<T, I>::CUtlMemory(const T* pMemory, int64_t numElements) : m_pMemory((T*)pMemory), CUtlMemory<T, I>::CUtlMemory(const T* pMemory, ssize_t numElements) : m_pMemory((T*)pMemory),
m_nAllocationCount(numElements) m_nAllocationCount(numElements)
{ {
// Special marker indicating externally supplied modifyable memory // Special marker indicating externally supplied modifyable memory
@ -466,8 +466,8 @@ CUtlMemory<T, I>& CUtlMemory<T, I>::operator=(CUtlMemory&& moveFrom)
{ {
// Copy member variables to locals before purge to handle self-assignment // Copy member variables to locals before purge to handle self-assignment
T* pMemory = moveFrom.m_pMemory; T* pMemory = moveFrom.m_pMemory;
int64_t nAllocationCount = moveFrom.m_nAllocationCount; ssize_t nAllocationCount = moveFrom.m_nAllocationCount;
int64_t nGrowSize = moveFrom.m_nGrowSize; ssize_t nGrowSize = moveFrom.m_nGrowSize;
moveFrom.m_pMemory = nullptr; moveFrom.m_pMemory = nullptr;
moveFrom.m_nAllocationCount = 0; moveFrom.m_nAllocationCount = 0;
@ -484,7 +484,7 @@ CUtlMemory<T, I>& CUtlMemory<T, I>::operator=(CUtlMemory&& moveFrom)
} }
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::Init(int64_t nGrowSize /*= 0*/, int64_t nInitSize /*= 0*/) void CUtlMemory<T, I>::Init(ssize_t nGrowSize /*= 0*/, ssize_t nInitSize /*= 0*/)
{ {
Purge(); Purge();
@ -516,7 +516,7 @@ void CUtlMemory<T, I>::Swap(CUtlMemory<T, I>& mem)
// Switches the buffer from an external memory buffer to a reallocatable buffer // Switches the buffer from an external memory buffer to a reallocatable buffer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::ConvertToGrowableMemory(int64_t nGrowSize) void CUtlMemory<T, I>::ConvertToGrowableMemory(ssize_t nGrowSize)
{ {
if (!IsExternallyAllocated()) if (!IsExternallyAllocated())
return; return;
@ -527,7 +527,7 @@ void CUtlMemory<T, I>::ConvertToGrowableMemory(int64_t nGrowSize)
UTLMEMORY_TRACK_ALLOC(); UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS(); MEM_ALLOC_CREDIT_CLASS();
int64_t nNumBytes = m_nAllocationCount * sizeof(T); ssize_t nNumBytes = m_nAllocationCount * sizeof(T);
T* pMemory = MemAllocSingleton()->Alloc<T>(nNumBytes); T* pMemory = MemAllocSingleton()->Alloc<T>(nNumBytes);
memcpy(pMemory, m_pMemory, nNumBytes); memcpy(pMemory, m_pMemory, nNumBytes);
m_pMemory = pMemory; m_pMemory = pMemory;
@ -543,7 +543,7 @@ void CUtlMemory<T, I>::ConvertToGrowableMemory(int64_t nGrowSize)
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::SetExternalBuffer(T* pMemory, int64_t numElements) void CUtlMemory<T, I>::SetExternalBuffer(T* pMemory, ssize_t numElements)
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
@ -556,7 +556,7 @@ void CUtlMemory<T, I>::SetExternalBuffer(T* pMemory, int64_t numElements)
} }
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::SetExternalBuffer(const T* pMemory, int64_t numElements) void CUtlMemory<T, I>::SetExternalBuffer(const T* pMemory, ssize_t numElements)
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
@ -569,7 +569,7 @@ void CUtlMemory<T, I>::SetExternalBuffer(const T* pMemory, int64_t numElements)
} }
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::AssumeMemory(T* pMemory, int64_t numElements) void CUtlMemory<T, I>::AssumeMemory(T* pMemory, ssize_t numElements)
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
@ -653,7 +653,7 @@ bool CUtlMemory<T, I>::IsReadOnly() const
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::SetGrowSize(int64_t nSize) void CUtlMemory<T, I>::SetGrowSize(ssize_t nSize)
{ {
Assert(!IsExternallyAllocated()); Assert(!IsExternallyAllocated());
Assert(nSize >= 0); Assert(nSize >= 0);
@ -683,13 +683,13 @@ inline const T* CUtlMemory<T, I>::Base() const
// Size // Size
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I > template< class T, class I >
inline int64_t CUtlMemory<T, I>::NumAllocated() const inline ssize_t CUtlMemory<T, I>::NumAllocated() const
{ {
return m_nAllocationCount; return m_nAllocationCount;
} }
template< class T, class I > template< class T, class I >
inline int64_t CUtlMemory<T, I>::Count() const inline ssize_t CUtlMemory<T, I>::Count() const
{ {
return m_nAllocationCount; return m_nAllocationCount;
} }
@ -710,7 +710,7 @@ inline bool CUtlMemory<T, I>::IsIdxValid(I i) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Grows the memory // Grows the memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
inline int64_t UtlMemory_CalcNewAllocationCount(int64_t nAllocationCount, int64_t nGrowSize, int64_t nNewSize, int64_t nBytesItem) inline ssize_t UtlMemory_CalcNewAllocationCount(ssize_t nAllocationCount, ssize_t nGrowSize, ssize_t nNewSize, ssize_t nBytesItem)
{ {
if (nGrowSize) if (nGrowSize)
{ {
@ -734,7 +734,7 @@ inline int64_t UtlMemory_CalcNewAllocationCount(int64_t nAllocationCount, int64_
#ifndef _X360 #ifndef _X360
nAllocationCount *= 2; nAllocationCount *= 2;
#else #else
int64_t nNewAllocationCount = (nAllocationCount * 9) / 8; // 12.5 % ssize_t nNewAllocationCount = (nAllocationCount * 9) / 8; // 12.5 %
if (nNewAllocationCount > nAllocationCount) if (nNewAllocationCount > nAllocationCount)
nAllocationCount = nNewAllocationCount; nAllocationCount = nNewAllocationCount;
else else
@ -747,7 +747,7 @@ inline int64_t UtlMemory_CalcNewAllocationCount(int64_t nAllocationCount, int64_
} }
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::Grow(int64_t num) void CUtlMemory<T, I>::Grow(ssize_t num)
{ {
Assert(num > 0); Assert(num > 0);
@ -760,28 +760,28 @@ void CUtlMemory<T, I>::Grow(int64_t num)
// Make sure we have at least numallocated + num allocations. // Make sure we have at least numallocated + num allocations.
// Use the grow rules specified for this memory (in m_nGrowSize) // Use the grow rules specified for this memory (in m_nGrowSize)
int64_t nAllocationRequested = m_nAllocationCount + num; ssize_t nAllocationRequested = m_nAllocationCount + num;
UTLMEMORY_TRACK_FREE(); UTLMEMORY_TRACK_FREE();
int64_t nNewAllocationCount = UtlMemory_CalcNewAllocationCount(m_nAllocationCount, m_nGrowSize, nAllocationRequested, sizeof(T)); ssize_t nNewAllocationCount = UtlMemory_CalcNewAllocationCount(m_nAllocationCount, m_nGrowSize, nAllocationRequested, sizeof(T));
// if m_nAllocationRequested wraps index type I, recalculate // if m_nAllocationRequested wraps index type I, recalculate
if ((int64_t)(I)nNewAllocationCount < nAllocationRequested) if ((ssize_t)(I)nNewAllocationCount < nAllocationRequested)
{ {
if ((int64_t)(I)nNewAllocationCount == 0 && (int64_t)(I)(nNewAllocationCount - 1) >= nAllocationRequested) if ((ssize_t)(I)nNewAllocationCount == 0 && (ssize_t)(I)(nNewAllocationCount - 1) >= nAllocationRequested)
{ {
--nNewAllocationCount; // deal w/ the common case of m_nAllocationCount == MAX_USHORT + 1 --nNewAllocationCount; // deal w/ the common case of m_nAllocationCount == MAX_USHORT + 1
} }
else else
{ {
if ((int64_t)(I)nAllocationRequested != nAllocationRequested) if ((ssize_t)(I)nAllocationRequested != nAllocationRequested)
{ {
// we've been asked to grow memory to a size s.t. the index type can't address the requested amount of memory // we've been asked to grow memory to a size s.t. the index type can't address the requested amount of memory
Assert(0); Assert(0);
return; return;
} }
while ((int64_t)(I)nNewAllocationCount < nAllocationRequested) while ((ssize_t)(I)nNewAllocationCount < nAllocationRequested)
{ {
nNewAllocationCount = (nNewAllocationCount + nAllocationRequested) / 2; nNewAllocationCount = (nNewAllocationCount + nAllocationRequested) / 2;
} }
@ -811,7 +811,7 @@ void CUtlMemory<T, I>::Grow(int64_t num)
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, class I > template< class T, class I >
inline void CUtlMemory<T, I>::EnsureCapacity(int64_t num) inline void CUtlMemory<T, I>::EnsureCapacity(ssize_t num)
{ {
if (m_nAllocationCount >= num) if (m_nAllocationCount >= num)
return; return;
@ -861,7 +861,7 @@ void CUtlMemory<T, I>::Purge()
} }
template< class T, class I > template< class T, class I >
void CUtlMemory<T, I>::Purge(int64_t numElements) void CUtlMemory<T, I>::Purge(ssize_t numElements)
{ {
Assert(numElements >= 0); Assert(numElements >= 0);
@ -914,31 +914,31 @@ void CUtlMemory<T, I>::Purge(int64_t numElements)
// The CUtlMemory class: // The CUtlMemory class:
// A growable memory class which doubles in size by default. // A growable memory class which doubles in size by default.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
class CUtlMemoryAligned : public CUtlMemory<T> class CUtlMemoryAligned : public CUtlMemory<T>
{ {
public: public:
// constructor, destructor // constructor, destructor
CUtlMemoryAligned(int64_t nGrowSize = 0, int64_t nInitSize = 0); CUtlMemoryAligned(ssize_t nGrowSize = 0, ssize_t nInitSize = 0);
CUtlMemoryAligned(T* pMemory, int64_t numElements); CUtlMemoryAligned(T* pMemory, ssize_t numElements);
CUtlMemoryAligned(const T* pMemory, int64_t numElements); CUtlMemoryAligned(const T* pMemory, ssize_t numElements);
~CUtlMemoryAligned(); ~CUtlMemoryAligned();
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
void SetExternalBuffer(T* pMemory, int64_t numElements); void SetExternalBuffer(T* pMemory, ssize_t numElements);
void SetExternalBuffer(const T* pMemory, int64_t numElements); void SetExternalBuffer(const T* pMemory, ssize_t numElements);
// Grows the memory, so that at least allocated + num elements are allocated // Grows the memory, so that at least allocated + num elements are allocated
void Grow(int64_t num = 1); void Grow(ssize_t num = 1);
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
void EnsureCapacity(int64_t num); void EnsureCapacity(ssize_t num);
// Memory deallocation // Memory deallocation
void Purge(); void Purge();
// Purge all but the given number of elements (NOT IMPLEMENTED IN CUtlMemoryAligned) // Purge all but the given number of elements (NOT IMPLEMENTED IN CUtlMemoryAligned)
void Purge(int64_t numElements) { Assert(0); } void Purge(ssize_t numElements) { Assert(0); }
private: private:
void* Align(const void* pAddr); void* Align(const void* pAddr);
@ -948,7 +948,7 @@ private:
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Aligns a pointer // Aligns a pointer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
void* CUtlMemoryAligned<T, nAlignment>::Align(const void* pAddr) void* CUtlMemoryAligned<T, nAlignment>::Align(const void* pAddr)
{ {
size_t nAlignmentMask = nAlignment - 1; size_t nAlignmentMask = nAlignment - 1;
@ -959,8 +959,8 @@ void* CUtlMemoryAligned<T, nAlignment>::Align(const void* pAddr)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// constructor, destructor // constructor, destructor
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(int64_t nGrowSize, int64_t nInitAllocationCount) CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(ssize_t nGrowSize, ssize_t nInitAllocationCount)
{ {
CUtlMemory<T>::m_pMemory = 0; CUtlMemory<T>::m_pMemory = 0;
CUtlMemory<T>::m_nAllocationCount = nInitAllocationCount; CUtlMemory<T>::m_nAllocationCount = nInitAllocationCount;
@ -978,27 +978,27 @@ CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(int64_t nGrowSize, int64_t n
} }
} }
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(T* pMemory, int64_t numElements) CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(T* pMemory, ssize_t numElements)
{ {
// Special marker indicating externally supplied memory // Special marker indicating externally supplied memory
CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_BUFFER_MARKER; CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_BUFFER_MARKER;
CUtlMemory<T>::m_pMemory = (T*)Align(pMemory); CUtlMemory<T>::m_pMemory = (T*)Align(pMemory);
CUtlMemory<T>::m_nAllocationCount = ((int64_t)(pMemory + numElements) - (int64_t)CUtlMemory<T>::m_pMemory) / sizeof(T); CUtlMemory<T>::m_nAllocationCount = ((ssize_t)(pMemory + numElements) - (ssize_t)CUtlMemory<T>::m_pMemory) / sizeof(T);
} }
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(const T* pMemory, int64_t numElements) CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned(const T* pMemory, ssize_t numElements)
{ {
// Special marker indicating externally supplied memory // Special marker indicating externally supplied memory
CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_CONST_BUFFER_MARKER; CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_CONST_BUFFER_MARKER;
CUtlMemory<T>::m_pMemory = (T*)Align(pMemory); CUtlMemory<T>::m_pMemory = (T*)Align(pMemory);
CUtlMemory<T>::m_nAllocationCount = ((int64_t)(pMemory + numElements) - (int64_t)CUtlMemory<T>::m_pMemory) / sizeof(T); CUtlMemory<T>::m_nAllocationCount = ((ssize_t)(pMemory + numElements) - (ssize_t)CUtlMemory<T>::m_pMemory) / sizeof(T);
} }
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
CUtlMemoryAligned<T, nAlignment>::~CUtlMemoryAligned() CUtlMemoryAligned<T, nAlignment>::~CUtlMemoryAligned()
{ {
Purge(); Purge();
@ -1008,27 +1008,27 @@ CUtlMemoryAligned<T, nAlignment>::~CUtlMemoryAligned()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Attaches the buffer to external memory.... // Attaches the buffer to external memory....
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
void CUtlMemoryAligned<T, nAlignment>::SetExternalBuffer(T* pMemory, int64_t numElements) void CUtlMemoryAligned<T, nAlignment>::SetExternalBuffer(T* pMemory, ssize_t numElements)
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
CUtlMemory<T>::m_pMemory = (T*)Align(pMemory); CUtlMemory<T>::m_pMemory = (T*)Align(pMemory);
CUtlMemory<T>::m_nAllocationCount = ((int64_t)(pMemory + numElements) - (int64_t)CUtlMemory<T>::m_pMemory) / sizeof(T); CUtlMemory<T>::m_nAllocationCount = ((ssize_t)(pMemory + numElements) - (ssize_t)CUtlMemory<T>::m_pMemory) / sizeof(T);
// Indicate that we don't own the memory // Indicate that we don't own the memory
CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_BUFFER_MARKER; CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_BUFFER_MARKER;
} }
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
void CUtlMemoryAligned<T, nAlignment>::SetExternalBuffer(const T* pMemory, int64_t numElements) void CUtlMemoryAligned<T, nAlignment>::SetExternalBuffer(const T* pMemory, ssize_t numElements)
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
CUtlMemory<T>::m_pMemory = (T*)Align(pMemory); CUtlMemory<T>::m_pMemory = (T*)Align(pMemory);
CUtlMemory<T>::m_nAllocationCount = ((int64_t)(pMemory + numElements) - (int64_t)CUtlMemory<T>::m_pMemory) / sizeof(T); CUtlMemory<T>::m_nAllocationCount = ((ssize_t)(pMemory + numElements) - (ssize_t)CUtlMemory<T>::m_pMemory) / sizeof(T);
// Indicate that we don't own the memory // Indicate that we don't own the memory
CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_CONST_BUFFER_MARKER; CUtlMemory<T>::m_nGrowSize = CUtlMemory<T>::EXTERNAL_CONST_BUFFER_MARKER;
@ -1038,8 +1038,8 @@ void CUtlMemoryAligned<T, nAlignment>::SetExternalBuffer(const T* pMemory, int64
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Grows the memory // Grows the memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
void CUtlMemoryAligned<T, nAlignment>::Grow(int64_t num) void CUtlMemoryAligned<T, nAlignment>::Grow(ssize_t num)
{ {
Assert(num > 0); Assert(num > 0);
@ -1054,7 +1054,7 @@ void CUtlMemoryAligned<T, nAlignment>::Grow(int64_t num)
// Make sure we have at least numallocated + num allocations. // Make sure we have at least numallocated + num allocations.
// Use the grow rules specified for this memory (in m_nGrowSize) // Use the grow rules specified for this memory (in m_nGrowSize)
int64_t nAllocationRequested = CUtlMemory<T>::m_nAllocationCount + num; ssize_t nAllocationRequested = CUtlMemory<T>::m_nAllocationCount + num;
CUtlMemory<T>::m_nAllocationCount = UtlMemory_CalcNewAllocationCount(CUtlMemory<T>::m_nAllocationCount, CUtlMemory<T>::m_nGrowSize, nAllocationRequested, sizeof(T)); CUtlMemory<T>::m_nAllocationCount = UtlMemory_CalcNewAllocationCount(CUtlMemory<T>::m_nAllocationCount, CUtlMemory<T>::m_nGrowSize, nAllocationRequested, sizeof(T));
@ -1078,8 +1078,8 @@ void CUtlMemoryAligned<T, nAlignment>::Grow(int64_t num)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
inline void CUtlMemoryAligned<T, nAlignment>::EnsureCapacity(int64_t num) inline void CUtlMemoryAligned<T, nAlignment>::EnsureCapacity(ssize_t num)
{ {
if (CUtlMemory<T>::m_nAllocationCount >= num) if (CUtlMemory<T>::m_nAllocationCount >= num)
return; return;
@ -1113,7 +1113,7 @@ inline void CUtlMemoryAligned<T, nAlignment>::EnsureCapacity(int64_t num)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Memory deallocation // Memory deallocation
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T, int64_t nAlignment > template< class T, ssize_t nAlignment >
void CUtlMemoryAligned<T, nAlignment>::Purge() void CUtlMemoryAligned<T, nAlignment>::Purge()
{ {
if (!this->IsExternallyAllocated()) if (!this->IsExternallyAllocated())