1
1
forked from 0ad/0ad

# fix vtbl_magic compile error on VC8; fix incorrect type in u32_from_larger; explanation+cleanup

- move AllocatorChecker to allocators.h since usefulness is not limited
to CacheAllocator
- file: add note on casting File.opaque
- vfs_redirector: was missing u32 (fails on VC8)
- zip: fix u32_from_larger (was using incorrect type)

This was SVN commit r3786.
This commit is contained in:
janwas 2006-04-20 04:25:14 +00:00
parent fd08924348
commit 6792e95517
5 changed files with 63 additions and 52 deletions

View File

@ -407,4 +407,46 @@ public:
}
};
//
// allocator test rig
//
// call for each allocator operation to sanity-check them.
// should only be used during debug mode due to serious overhead.
class AllocatorChecker
{
public:
void notify_alloc(void* p, size_t size)
{
const Allocs::value_type item = std::make_pair(p, size);
std::pair<Allocs::iterator, bool> ret = allocs.insert(item);
TEST(ret.second == true); // wasn't already in map
}
void notify_free(void* p, size_t size)
{
Allocs::iterator it = allocs.find(p);
if(it == allocs.end())
debug_warn("AllocatorChecker: freeing invalid pointer");
else
{
// size must match what was passed to notify_alloc
const size_t allocated_size = it->second;
TEST(size == allocated_size);
allocs.erase(it);
}
}
void notify_clear()
{
allocs.clear();
}
private:
typedef std::map<void*, size_t> Allocs;
Allocs allocs;
};
#endif // #ifndef ALLOCATORS_H__

View File

@ -226,6 +226,14 @@ struct File
// the file provider's vtbl.
const FileProvider_VTbl* type;
// storage for the provider-specific fields.
// the implementations cast this to their e.g. PosixFile struct.
//
// note: when doing so, there's no need to verify type - if
// vfs_io dispatches to afile_read, then the File.type must obviously
// have been "archive".
// if users call the e.g. archive.h methods directly, we assume they
// know what they're doing and don't check that.
u8 opaque[FILE_OPAQUE_SIZE];
};

View File

@ -243,48 +243,6 @@ void block_cache_release(BlockId id)
//-----------------------------------------------------------------------------
#ifndef NDEBUG
// tracks and sanity-checks all operations made by allocator.
// active only during debug mode due to overhead.
class AllocatorChecker
{
public:
void notify_alloc(void* p, size_t size)
{
// debug_printf("a %p %d\n", p, size);
const Allocs::value_type item = std::make_pair(p, size);
std::pair<Allocs::iterator, bool> ret = allocs.insert(item);
TEST(ret.second == true); // wasn't already in map
}
void notify_free(void* p, size_t size)
{
// debug_printf("f %p %d\n", p, size);
Allocs::iterator it = allocs.find(p);
if(it == allocs.end())
debug_warn("AllocatorChecker: freeing invalid pointer");
else
{
// size must match what was passed to notify_alloc
const size_t allocated_size = it->second;
TEST(size == allocated_size);
allocs.erase(it);
}
}
void notify_clear()
{
allocs.clear();
}
private:
typedef std::map<void*, size_t> Allocs;
Allocs allocs;
};
static AllocatorChecker alloc_checker;
#endif
// >= file_sector_size or else waio will have to realign.
// chosen as exactly 1 page: this allows write-protecting file buffers
// without worrying about their (non-page-aligned) borders.
@ -447,6 +405,10 @@ success:
}
private:
#ifndef NDEBUG
AllocatorChecker alloc_checker;
#endif
Pool pool;
//-------------------------------------------------------------------------
@ -1364,15 +1326,14 @@ static void test_file_cache()
// actually put it in the atom_fn storage (permanently clutters it).
// just increment this pointer (evil but works since it's not used).
// const char* atom_fn = (const char*)1;
// give to file_cache
// file_cache_add((FileIOBuf)p, size, atom_fn++);
file_cache_reset();
TEST(file_cache.empty());
// (even though everything has now been freed,
// the freelists may be a bit scattered already).
// note: even though everything has now been freed,
// the freelists may be a bit scattered already.
}
static void self_test()

View File

@ -25,7 +25,7 @@
#include "file_internal.h"
#include "byte_order.h" // FOURCC
static const vtbl_magic = FOURCC('F','P','V','T');
static const u32 vtbl_magic = FOURCC('F','P','V','T');
// HACK: these thunks and the vtbls are implemented here,

View File

@ -45,15 +45,15 @@
template<typename T> u32 u32_from_larger(T x)
{
const T max = std::numeric_limits<T>::max();
debug_assert(x <= max);
const u32 max = std::numeric_limits<u32>::max();
debug_assert((u64)x <= (u64)max);
return (u32)(x & max);
}
template<typename T> u16 u16_from_larger(T x)
{
const T max = std::numeric_limits<T>::max();
debug_assert(x <= max);
const u16 max = std::numeric_limits<u16>::max();
debug_assert((u64)x <= (u64)max);
return (u16)(x & max);
}
@ -116,7 +116,7 @@ enum ZipCompressionMethod
ZIP_CM_DEFLATE = 8
};
// translate ArchiveEntry's method to zip_method.
// translate ArchiveEntry.method to zip_method.
static ZipCompressionMethod zip_method_for(CompressionMethod method)
{
switch(method)
@ -445,7 +445,7 @@ have_ecdr:
// because it'd be slow.
// - do not warn - the corrupt archive will be deleted on next
// successful archive builder run anyway.
return ERR_CORRUPTED;
return ERR_CORRUPTED; // NOWARN
}