一个类似于Java的GC功能的指针.

ookook 2003-12-12 04:26:05
今日编了一个类似于Java的GC功能的指针.与大家分享:如下
...全文
17 15 打赏 收藏 举报
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Darkay_Lee 2004-03-14
开个站点好好研究讨论一下吧,这里不方便讨论
  • 打赏
  • 举报
回复
Hmilu 2004-03-08
Mark/
learn it later
  • 打赏
  • 举报
回复
Tommy 2004-01-19
和boost::shared_ptr有什么区别吗?看起来是通过引用计数起作用的,并不是真正的GC。

通过引用计数的话,如楼上说的,会有对象环的问题,就是A对象中有指向B对象的指针,B对象中也有指向A对象的指针,这时候,两个对象互相引用,所以大家的计数都是1,都不会释放。在boost中,是引用了一个弱指针weak_ptr来解决这个问题的。
  • 打赏
  • 举报
回复
lubin59 2004-01-19
好长的一段,
  • 打赏
  • 举报
回复
dfwid 2004-01-17
mark
  • 打赏
  • 举报
回复
oyd 2004-01-08
假的
  • 打赏
  • 举报
回复
zengpan_panpan 2004-01-07
只要有引用计数就不是GC,对象环无法释放。
  • 打赏
  • 举报
回复
ookook 2003-12-23
共两部分
一个类似于Java的GC功能的指针.(一)
http://expert.csdn.net/Expert/topic/2557/2557623.xml?temp=.2687494
一个类似于Java的GC功能的指针.(二)
http://expert.csdn.net/Expert/topic/2557/2557679.xml?temp=.4401514
  • 打赏
  • 举报
回复
qhgary 2003-12-19
mark
  • 打赏
  • 举报
回复
Jinhao 2003-12-19
先Mark,再研究
  • 打赏
  • 举报
回复
i_jianyong 2003-12-19
mark
  • 打赏
  • 举报
回复
ckacka 2003-12-19
mark
  • 打赏
  • 举报
回复
ookook 2003-12-12
//GarbageCollect.h文件(第三部分)

_GC_Y<_C>& operator ++ ()
{
#ifdef _DEBUG
if (NULL != m_pBaseClass && ++m_uiOffset == m_pBaseClass->m_uiSize)
{
--m_uiOffset;
_UpperLimit_ErrMessage();
}
#else
NULL != m_pBaseClass && ++m_uiOffset == m_pBaseClass->m_uiSize && --m_uiOffset;
#endif
return *this;
}
_GC_Y<_C> operator ++ (int)
{
_GC_Y<_C> Temp = *this;
#ifdef _DEBUG
if (NULL != m_pBaseClass && ++m_uiOffset == m_pBaseClass->m_uiSize)
{
m_uiOffset--;
_UpperLimit_ErrMessage();
}
#else
NULL != m_pBaseClass && ++m_uiOffset == m_pBaseClass->m_uiSize && m_uiOffset--;
#endif
return Temp;
}
_GC_Y<_C>& operator -- ()
{
#ifdef _DEBUG
if (NULL != m_pBaseClass && 0 == m_uiOffset--)
{
++m_uiOffset;
_LowerLimit_ErrMessage();
}
#else
NULL != m_pBaseClass && (0 == m_uiOffset || --m_uiOffset);
#endif
return *this;
}
_GC_Y<_C> operator -- (int)
{
_GC_Y<_C> Temp = *this;
#ifdef _DEBUG
if (NULL != m_pBaseClass && 0 == m_uiOffset--)
{
m_uiOffset++;
_LowerLimit_ErrMessage();
}
#else
NULL != m_pBaseClass && (0 == m_uiOffset || m_uiOffset--);
#endif
return Temp;
}
_GC_Y<_C>& operator += (const int iIncrement)
{
if (NULL != m_pBaseClass)
if (iIncrement >0)
if (m_pBaseClass->m_uiSize-1 - m_uiOffset >= (unsigned int)iIncrement)
m_uiOffset += iIncrement;
else
{
m_uiOffset = m_pBaseClass->m_uiSize-1;
_UpperLimit_ErrMessage();
}
else
if (m_uiOffset >= (unsigned int)-iIncrement)
m_uiOffset += iIncrement;
else
{
m_uiOffset = 0;
_LowerLimit_ErrMessage();
}
return *this;
}
/*
_GC_Y<_C>& operator -= (int iDecrement)
{
if (NULL != m_pBaseClass)
if (iDecrement > 0)
if (m_uiOffset >= (unsigned int)iDecrement)
m_uiOffset -= iDecrement;
else
{
m_uiOffset = 0;
_LowerLimit_ErrMessage();
}
else
if (m_pBaseClass->m_uiSize-1 - m_uiOffset >= (unsigned int)-iDecrement)
m_uiOffset -= iDecrement;
else
{
m_uiOffset = m_pBaseClass->m_uiSize-1;
_UpperLimit_ErrMessage();
}
return *this;
}
*/
_GC_Y<_C>& operator -= (const int iDecrement)
{
return *this += -iDecrement;
}
_C& operator * ()
{
//这个地方在Release版本中内存分配失败时存在问题(不过发生几率很小,约为%0.01)
_GC_ASSERT(NULL != m_pBaseClass);
return *(m_pBaseClass->m_pBuf + m_uiOffset);
}
_C& operator [] (int iOffset)
{
//这个地方在Release版本中内存分配失败时存在问题(不过发生几率很小,约为%0.01)
_GC_ASSERT(NULL != m_pBaseClass);
if (iOffset >0)
if (m_pBaseClass->m_uiSize-1 - m_uiOffset >= (unsigned int)iOffset)
iOffset = m_uiOffset + iOffset;
else
{
iOffset = m_pBaseClass->m_uiSize-1;
_UpperLimit_ErrMessage();
}
else
if (m_uiOffset >= (unsigned int)-iOffset)
iOffset = m_uiOffset + iOffset;
else
{
iOffset = 0;
_LowerLimit_ErrMessage();
}
return *(m_pBaseClass->m_pBuf + iOffset);
}
_C* operator -> ()
{
//这个地方在Release版本中内存分配失败时存在问题(不过发生几率很小,约为%0.01)
_GC_ASSERT(NULL != m_pBaseClass);
return m_pBaseClass->m_pBuf + m_uiOffset;
}
_GC_Y<_C> operator + (int iOffset)
{
_GC_Y<_C> Temp = *this;
if (NULL != m_pBaseClass)
{
if (iOffset >0)
if (m_pBaseClass->m_uiSize-1 - m_uiOffset >= (unsigned int)iOffset)
iOffset = m_uiOffset + iOffset;
else
{
iOffset = m_pBaseClass->m_uiSize-1;
_UpperLimit_ErrMessage();
}
else
if (m_uiOffset >= (unsigned int)-iOffset)
iOffset = m_uiOffset + iOffset;
else
{
iOffset = 0;
_LowerLimit_ErrMessage();
}
Temp.m_uiOffset = (unsigned int)iOffset;
}
return Temp;
}
_GC_Y<_C> operator - (int iOffset)
{
return this->operator + (-iOffset);
}
operator _C* ()
{
//这个地方在Release版本中内存分配失败时存在问题(不过发生几率很小,约为%0.01)
_GC_ASSERT(NULL != m_pBaseClass);
return m_pBaseClass->m_pBuf + m_uiOffset;
}
unsigned int GetSize() const
{
if (NULL != m_pBaseClass)
return m_pBaseClass->m_uiSize;
return 0;
}
unsigned int GetCurrent() const
{
return m_uiOffset;
}
private:
unsigned int m_uiOffset;
_GC_X<_C>* m_pBaseClass;
};

  • 打赏
  • 举报
回复
ookook 2003-12-12
//GarbageCollect.h文件(第二部分)


template <typename _C>
class _GC_Y
{
public:
_GC_Y()
{
m_uiOffset = 0;
m_pBaseClass = NULL;
}
explicit _GC_Y(_C* pBuf)
{
m_uiOffset = 0;
m_pBaseClass = new _GC_XY<_C>(pBuf);
}
explicit _GC_Y(const unsigned int uiSize)
{
m_uiOffset = 0;
m_pBaseClass = new _GC_XX<_C>(uiSize);
}
_GC_Y(const _GC_Y<_C>& Other)
{
m_uiOffset = Other.m_uiOffset;
m_pBaseClass = Other.m_pBaseClass;
NULL != m_pBaseClass && InterlockedIncrement((long*)&(m_pBaseClass->m_uiCounter));
}
~_GC_Y()
{
if (NULL != m_pBaseClass && 0 == InterlockedDecrement((long*)&m_pBaseClass->m_uiCounter))
delete m_pBaseClass;
}
_GC_Y<_C>& operator = (const _GC_Y<_C>& Other)
{
if (this != &Other)
{
if (NULL != m_pBaseClass && 0 == InterlockedDecrement((long*)&m_pBaseClass->m_uiCounter))
delete m_pBaseClass;
m_uiOffset = Other.m_uiOffset;
m_pBaseClass = Other.m_pBaseClass;
NULL != m_pBaseClass && InterlockedIncrement((long*)&m_pBaseClass->m_uiCounter);
}
return *this;
}
_GC_Y<_C>& operator = (const int Other)
{
_GC_ASSERT(0 == Other);
if (NULL != m_pBaseClass && 0 == InterlockedDecrement((long*)&m_pBaseClass->m_uiCounter))
delete m_pBaseClass;
m_uiOffset = 0;
m_pBaseClass = NULL;
return *this;
}
/*
_GC_Y<_C>& operator = (const void* Other)
{
_GC_ASSERT(0 == (int)Other);
if (NULL != m_pBaseClass && 0 == InterlockedDecrement((long*)&m_pBaseClass->m_uiCounter))
delete m_pBaseClass;
m_uiOffset = 0;
m_pBaseClass = NULL;
return *this;
}
*/
bool operator == (const _GC_Y<_C>& Other) const
{
if (m_pBaseClass == Other.m_pBaseClass)
return true;
return false;
}
bool operator == (const int Other) const
{
if (0 != Other)
return false;
if (NULL == m_pBaseClass || NULL == m_pBaseClass->m_pBuf)
return true;
return false;
}
/*
bool operator == (const void* Other)
{
if (0 != (int) Other)
return false;
if (NULL == m_pBaseClass || NULL == m_pBaseClass->m_pBuf)
return true;
return false;
}
*/
bool operator != (const _GC_Y<_C>& Other) const
{
if (m_pBaseClass != Other.m_pBaseClass)
return true;
return false;
}
bool operator != (const int Other) const
{
if (0 != Other)
return true;
if (NULL != m_pBaseClass && NULL != m_pBaseClass->m_pBuf)
return true;
return false;
}
/*
bool operator != (const void* Other)
{
if (0 != (int) Other)
return true;
if (NULL != m_pBaseClass && NULL != m_pBaseClass->m_pBuf)
return true;
return false;
}
*/
bool CompleteEqual (const _GC_Y<_C>& Other)
{
if (m_pBaseClass == Other.m_pBaseClass && m_uiOffset == Other.m_uiOffset)
return true;
return false;
}
  • 打赏
  • 举报
回复
ookook 2003-12-12
//GarbageCollect.h文件(第一部分)

/****************************************************************************
* *
* GARBAGECOLLECT.h -- Garbage Memory Auto Collect *
* *
* Version 1.0, Bug report to MAIL: ok-ok-ok-ok@163.com. *
* *
* Copyright (c) 2003-2004 by Mike Cao. All rights reserved. *
* *
****************************************************************************/
#ifndef _GARBAGE_COLLECT_H
#define _GARBAGE_COLLECT_H

#ifndef __cplusplus
#error C++ compiler required.
#endif
#ifndef _MSC_VER
#error Microsoft C++ compiler required.
#endif

#include <Windows.h>
#pragma warning(disable: 4284)
#pragma message("/****************************************************************************")
#pragma message("* *")
#pragma message("* GARBAGECOLLECT.h -- Garbage Memory Auto Collect *")
#pragma message("* *")
#pragma message("* Copyright (c) 2003-2004 by Mike Cao. All rights reserved. *")
#pragma message("* *")
#pragma message("****************************************************************************/")

#ifndef NULL
#define NULL 0
//#define NULL ((void*)0)
#endif

#ifdef _DEBUG
//如果要判断的表达式很长,扩大_GC_ERR_BUF的值
#define _GC_ERR_BUF 1024
#define _GC_ASSERT(_X) ((void)( (_X) || (_GC_ErrMessage("_GC_ASSERT( "#_X" )", __FILE__, __LINE__), 0) ))
void _GC_ErrMessage(const char* _X, const char* _F, const int _L)
{
TCHAR pBuf[_GC_ERR_BUF];
wsprintf(pBuf, TEXT("Application Runtime Occur Error.\n\n")
TEXT("Reason: %hs\n")
TEXT("File: %hs\n")
TEXT("Line: %d\n")
TEXT("\nFor information on how you program can cause this error, see \n")
TEXT("you code on this line.\n\n")
TEXT("(Press Yes to exit the application, No to continue.)"), _X, _F, _L);
if (IDYES == MessageBox(NULL, pBuf, TEXT("GC Debug Error"), MB_YESNO | MB_ICONERROR))
ExitProcess(unsigned int(-1));
}
#define _UpperLimit_ErrMessage() _GC_ErrMessage("Access memory beyond upper limit.", __FILE__, __LINE__)
#define _LowerLimit_ErrMessage() _GC_ErrMessage("Access Memory under lower limit.", __FILE__, __LINE__)
#else
#define _GC_ASSERT(_X) ((void)0)
#define _UpperLimit_ErrMessage() ((void)0)
#define _LowerLimit_ErrMessage() ((void)0)
#endif

template <typename>
class _GC_Y;

template <typename _C>
class _GC_X
{
protected:
friend class _GC_Y<_C>;
_GC_X(){};
virtual ~_GC_X(){};
unsigned int m_uiCounter;
_C* m_pBuf;
unsigned int m_uiSize;
};

template <typename _C>
class _GC_XX :public _GC_X<_C>
{
protected:
friend class _GC_Y<_C>;
_GC_XX(const unsigned int uiSize)
{
m_uiSize = uiSize;
m_uiCounter = 1;
m_pBuf = new _C[uiSize];
}
~_GC_XX()
{
delete []m_pBuf;
}
};

template <typename _C>
class _GC_XY :public _GC_X<_C>
{
protected:
friend class _GC_Y<_C>;
_GC_XY(_C* pBuf)
{
m_uiSize = 1;
m_uiCounter = 1;
m_pBuf = pBuf;
}
~_GC_XY()
{
delete m_pBuf;
}
};
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.1w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2003-12-12 04:26
社区公告
暂无公告