len3d

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
写出来大概是这个样子,完全没有必要像我写的MemMan那样复杂!

class Resource {
public:
 Resource()
 {
  ResourceManager.attach( this );
 }

 virtual ~Resource()
 {
  ResourceManager.detach( this );
 }

 virtual void streamIn() = 0;
 virtual UINT streamOut() = 0;

public:
 UINT last_use_time;
};

class ResourceManager {
public:
 void attach( Resource *res );
 void detach( Resource *res );
 UINT get_current_time()
 {
  return ++current_time;
 }
 UINT pop()
 {
  Resource *old_res;

  for( UINT i = 0; i < reslist.size(); ++i )
  {
   Resource *res = reslist[i];

   // LRU...
   if( res->last_use_time < old_res->last_use_time )
    old_res = res;
  }

  return old_res->steamOut();
 }
private:
 std::vector<Resource*> reslist;
 UINT     current_time;
};

class MemoryAllocator {
public:
 void *alloc( UINT size )
 {
  if( memoryAvailable > 0 )
   return new char[size];
  else
  {
   UINT got_size = 0;
   do {
    got_size += ResourceManager.pop();
   } while( got_size < size );
   return new char[size];
  }
 }
};

class RayObject : public Resource {
public:
 void streamIn()
 {
  if( compressed )
  {
   prilist = new RayTriangle [prisize];
   compressed = false;
  }

  last_use_time = ResourceManager.get_current_time();
 }

 UINT streamOut()
 {
  if( !compressed )
  {
   delete [] prilist;
   compressed = true;
  }

  return size;
 }

private:
 RayTriangle  prilist;
 bool   compressed;
};

posted on 2007-11-06 17:55  Len3d  阅读(446)  评论(4编辑  收藏  举报