support buffers already in RAM

git-svn-id: svn://anubis/misc/FileLoader@255 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2010-10-26 20:09:16 +00:00
parent ab0b8157f4
commit 8abf3ff02b

View File

@ -39,13 +39,24 @@ class FileLoader
data = new char[size]; data = new char[size];
_refcnt = new int; _refcnt = new int;
*_refcnt = 1; *_refcnt = 1;
m_alloced = true;
}
Buffer(char * data, int sz)
{
size = sz;
this->data = data;
m_alloced = false;
} }
void copy(const Buffer & other) void copy(const Buffer & other)
{ {
data = other.data; data = other.data;
size = other.size; size = other.size;
_refcnt = other._refcnt; m_alloced = other.m_alloced;
(*_refcnt)++; if (m_alloced)
{
_refcnt = other._refcnt;
(*_refcnt)++;
}
} }
Buffer(const Buffer & other) { copy(other); } Buffer(const Buffer & other) { copy(other); }
Buffer & operator=(const Buffer & other) Buffer & operator=(const Buffer & other)
@ -55,15 +66,19 @@ class FileLoader
} }
~Buffer() ~Buffer()
{ {
(*_refcnt)--; if (m_alloced)
if (*_refcnt < 1)
{ {
delete _refcnt; (*_refcnt)--;
delete data; if (*_refcnt < 1)
{
delete _refcnt;
delete data;
}
} }
} }
protected: protected:
int * _refcnt; int * _refcnt;
bool m_alloced;
}; };
virtual int getSize(const Path & path) = 0; virtual int getSize(const Path & path) = 0;