| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "RefCntMem.h" |
|
|
| CRefCntMem::CRefCntMem() |
| { |
| |
| m_ptr = 0; |
| } |
|
|
| CRefCntMem::~CRefCntMem() |
| { |
| |
| DecrementCount(); |
| m_ptr = 0; |
| } |
|
|
| void CRefCntMem::DecrementCount() |
| { |
| |
| if (m_ptr) |
| { |
| m_ptr->m_refCnt -= 1; |
| if (m_ptr->m_refCnt == 0) |
| { |
| if (m_ptr->m_deleteWhenDone) |
| { |
| if (m_ptr->m_delFn) |
| m_ptr->m_delFn(m_ptr->m_memory); |
| else |
| delete (double *) m_ptr->m_memory; |
| } |
| delete m_ptr; |
| } |
| } |
| } |
|
|
| void CRefCntMem::IncrementCount() |
| { |
| |
| if (m_ptr) |
| { |
| m_ptr->m_refCnt += 1; |
| } |
| } |
|
|
| CRefCntMem::CRefCntMem(const CRefCntMem& ref) |
| { |
| |
| m_ptr = 0; |
| (*this) = ref; |
| } |
|
|
| CRefCntMem& CRefCntMem::operator=(const CRefCntMem& ref) |
| { |
| |
| DecrementCount(); |
| m_ptr = ref.m_ptr; |
| IncrementCount(); |
| return *this; |
| } |
|
|
| void CRefCntMem::ReAllocate(int nBytes, void *memory, bool deleteWhenDone, |
| void (*deleteFunction)(void *ptr)) |
| { |
| |
| DecrementCount(); |
| if (memory) |
| { |
| m_ptr = new CRefCntMemPtr; |
| m_ptr->m_nBytes = nBytes; |
| m_ptr->m_memory = memory; |
| m_ptr->m_deleteWhenDone = deleteWhenDone; |
| m_ptr->m_refCnt = 1; |
| m_ptr->m_delFn = deleteFunction; |
| } |
| else |
| m_ptr = 0; |
| } |
|
|
| int CRefCntMem::NBytes() |
| { |
| |
| return (m_ptr) ? m_ptr->m_nBytes : 0; |
| } |
|
|
| bool CRefCntMem::InBounds(int index) |
| { |
| |
| return (m_ptr && 0 <= index && index < m_ptr->m_nBytes); |
| } |
|
|
| void* CRefCntMem::Memory() |
| { |
| |
| return (m_ptr) ? m_ptr->m_memory : 0; |
| } |
|
|