Skip to content

Commit

Permalink
Refactor: Moved LumpCache class to new source file lumpcache.h
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Sep 27, 2012
1 parent be27111 commit 3f7dc55
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 121 deletions.
1 change: 1 addition & 0 deletions doomsday/engine/engine.pro
Expand Up @@ -236,6 +236,7 @@ DENG_HEADERS += \
portable/include/keycode.h \
portable/include/library.h \
portable/include/linedef.h \
portable/include/lumpcache.h \
portable/include/lumpdirectory.h \
portable/include/lumpfile.h \
portable/include/lumpinfo.h \
Expand Down
122 changes: 1 addition & 121 deletions doomsday/engine/portable/src/wadfile.cpp
Expand Up @@ -25,6 +25,7 @@
#include "de_base.h"
#include "de_filesys.h"

#include "lumpcache.h"
#include "lumpdirectory.h"
#include "pathdirectory.h"
#include "wadfile.h"
Expand All @@ -34,129 +35,8 @@
#include <de/ByteOrder>
#include <de/Error>
#include <de/Log>
#include <de/memory.h>
#include <de/memoryzone.h>

class LumpCache
{
public:
LumpCache(uint size) : size_(size)
{
if(size_ > 1)
{
lumps = (void**) M_Calloc(size_ * sizeof(*lumps));
}
}

~LumpCache()
{
clear();
if(size_ > 1 && lumps)
{
M_Free(lumps);
}
}

uint size() const { return size_; }

bool isValidIndex(uint idx) const { return idx < size_; }

uint8_t const* data(uint lumpIdx) const
{
void** cacheAdr = address(lumpIdx);
return reinterpret_cast<uint8_t*>(cacheAdr? *cacheAdr : 0);
}

LumpCache& insert(uint lumpIdx, uint8_t* data)
{
LOG_AS("LumpCache::insert");
if(!isValidIndex(lumpIdx)) throw de::Error("LumpCache::insert", QString("Invalid index %1").arg(lumpIdx));

remove(lumpIdx);

void** cacheAdr = address(lumpIdx);
*cacheAdr = data;
Z_ChangeUser(*cacheAdr, cacheAdr);
return *this;
}

LumpCache& insertAndLock(uint lumpIdx, uint8_t* data)
{
return insert(lumpIdx, data).lock(lumpIdx);
}

LumpCache& lock(uint /*lumpIdx*/)
{
/// @todo Implement a proper thread-safe locking mechanism.
return *this;
}

LumpCache& unlock(uint lumpIdx)
{
/// @todo Implement a proper thread-safe locking mechanism.
void** cacheAdr = address(lumpIdx);
bool isCached = cacheAdr && *cacheAdr;
if(isCached)
{
Z_ChangeTag2(*cacheAdr, PU_PURGELEVEL);
}
return *this;
}

LumpCache& remove(uint lumpIdx, bool* retRemoved = 0)
{
void** cacheAdr = address(lumpIdx);
bool isCached = (cacheAdr && *cacheAdr);
if(isCached)
{
/// @todo Implement a proper thread-safe locking mechanism.

// Elevate the cached data to purge level so it will be explicitly
// free'd by the Zone the next time the rover passes it.
if(Z_GetTag(*cacheAdr) != PU_PURGELEVEL)
{
Z_ChangeTag2(*cacheAdr, PU_PURGELEVEL);
}
// Mark the data as unowned.
Z_ChangeUser(*cacheAdr, (void*) 0x2);
}

if(retRemoved) *retRemoved = isCached;
return *this;
}

LumpCache& clear()
{
for(uint i = 0; i < size_; ++i)
{
remove(i);
}
return *this;
}

private:
void** address(uint lumpIdx) const
{
if(!lumps) return 0;
if(!isValidIndex(lumpIdx)) return 0;
if(size_ > 1)
{
return &lumps[lumpIdx];
}
else
{
return (void**)&lumps;
}
}

private:
/// Number of data lumps which can be stored in the cache.
uint size_;

/// Lump data pointers.
void** lumps;
};

/// The following structures are used to read data directly from WAD files.
#pragma pack(1)
typedef struct {
Expand Down

0 comments on commit 3f7dc55

Please sign in to comment.