1
0
forked from 0ad/0ad

shared_ptr: use Philip's idea of making the Deleter's operator() a template instead of its enclosing class. in code, use DummySharedPtr() instead of passing a DummyDeleter

h_mgr: compile fix for windows memory tracker (see forum thread)
timer: add assert to catch incorrect behaviour as reported by philip

This was SVN commit r5544.
This commit is contained in:
janwas 2008-01-10 19:29:52 +00:00
parent 271e892618
commit 8e341c2d6b
6 changed files with 16 additions and 10 deletions

View File

@ -22,10 +22,10 @@ void PageAlignedDeleter::operator()(u8* p)
static AllocatorChecker s_allocatorChecker;
#endif
class CheckedDeleter
class CheckedArrayDeleter
{
public:
CheckedDeleter(size_t size)
CheckedArrayDeleter(size_t size)
: m_size(size)
{
}
@ -53,5 +53,5 @@ shared_ptr<u8> Allocate(size_t size)
s_allocatorChecker.OnAllocate(p, size);
#endif
return shared_ptr<u8>(p, CheckedDeleter(size));
return shared_ptr<u8>(p, CheckedArrayDeleter(size));
}

View File

@ -12,9 +12,9 @@ private:
size_t m_size;
};
template<class T>
struct DummyDeleter
{
template<class T>
void operator()(T*)
{
}
@ -23,19 +23,19 @@ struct DummyDeleter
template<class T>
shared_ptr<T> DummySharedPtr(T* ptr)
{
return shared_ptr<T>(ptr, DummyDeleter<T>());
return shared_ptr<T>(ptr, DummyDeleter());
}
struct ArrayDeleter
{
template <typename T>
template<class T>
void operator()(T* p)
{
delete[] p;
}
};
// (note: uses CheckedArrayDeleter)
LIB_API shared_ptr<u8> Allocate(size_t size);
#endif // #ifndef INCLUDED_SHARED_PTR

View File

@ -23,7 +23,7 @@ class TestTex : public CxxTest::TestSuite
memset(&t, 0, sizeof(t));
// decode from file format
shared_ptr<u8> ptr(da.base, DummyDeleter<u8>());
shared_ptr<u8> ptr = DummySharedPtr(da.base);
TS_ASSERT_OK(tex_decode(ptr, da.cur_size, &t));
// make sure pixel format gets converted completely to plain
@ -104,7 +104,7 @@ public:
void test_mipmap_create()
{
static u8 imgData[] = { 0x10,0x20,0x30, 0x40,0x60,0x80, 0xA0,0xA4,0xA8, 0xC0,0xC1,0xC2 };
shared_ptr<u8> img(imgData, DummyDeleter<u8>());
shared_ptr<u8> img = DummySharedPtr(imgData);
// assumes 2x2 box filter algorithm with rounding
static const u8 mipmap[] = { 0x6C,0x79,0x87 };
Tex t;

View File

@ -190,9 +190,12 @@ static HDATA* h_data_from_idx(const i32 idx)
page = (HDATA*)calloc(1, PAGE_SIZE);
if(!page)
return 0;
#include "lib/nommgr.h" // placement new
// Initialise all the VfsPath members
for(uint i = 0; i < hdata_per_page; ++i)
new (&page[i].pathname) VfsPath;
#include "lib/mmgr.h"
}
// note: VC7.1 optimizes the divides to shift and mask.
@ -617,7 +620,9 @@ static LibError h_free_idx(i32 idx, HDATA* hd)
hd->pathname.~VfsPath(); // FIXME: ugly hack, but necessary to reclaim std::string memory
memset(hd, 0, sizeof(*hd));
#include "lib/nommgr.h" // placement new
new (&hd->pathname) VfsPath; // FIXME too: necessary because otherwise it'll break if we reuse this page
#include "lib/mmgr.h"
free_idx(idx);

View File

@ -273,6 +273,7 @@ void timer_DisplayClientTotals()
num_clients--;
const double sum = tc->sum.Seconds();
debug_assert(sum >= 0.0);
// determine scale factor for pretty display
double scale = 1e6;

View File

@ -181,7 +181,7 @@ static LibError tex_write(Tex* t, const VfsPath& filename)
LibError ret = INFO::OK;
{
(void)da_set_size(&da, round_up(da.cur_size, BLOCK_SIZE));
shared_ptr<u8> file(da.base, DummyDeleter<u8>());
shared_ptr<u8> file = DummySharedPtr(da.base);
const ssize_t bytes_written = g_VFS->CreateFile(filename, file, da.pos);
if(bytes_written > 0)
debug_assert(bytes_written == (ssize_t)da.pos);