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,14 +39,25 @@ 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;
m_alloced = other.m_alloced;
if (m_alloced)
{
_refcnt = other._refcnt; _refcnt = other._refcnt;
(*_refcnt)++; (*_refcnt)++;
} }
}
Buffer(const Buffer & other) { copy(other); } Buffer(const Buffer & other) { copy(other); }
Buffer & operator=(const Buffer & other) Buffer & operator=(const Buffer & other)
{ {
@ -54,6 +65,8 @@ class FileLoader
return *this; return *this;
} }
~Buffer() ~Buffer()
{
if (m_alloced)
{ {
(*_refcnt)--; (*_refcnt)--;
if (*_refcnt < 1) if (*_refcnt < 1)
@ -62,8 +75,10 @@ class FileLoader
delete data; 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;