Files
0ad/source/lib/file/io/io_buf.cpp
T
janwas 317f98a6c0 WIP of new file code
archive: pretty much done except for trace and archive_builder
IO: io_manager still needs to be fixed/revised (especially WRT buffer
and alignment management)
posix: done
vfs: need to complete vfs_mount and tie it in to vfs.cpp
dir_util: needs further revisions (VFS interface changed since then)

This was SVN commit r5475.
2007-11-20 18:51:07 +00:00

51 lines
1010 B
C++

#include "precompiled.h"
#include "io_buf.h"
#include "lib/allocators/allocators.h" // AllocatorChecker
#include "lib/bits.h" // round_up
#include "block_cache.h" // BLOCK_SIZE
#ifndef NDEBUG
static AllocatorChecker allocatorChecker;
#endif
class IoBufDeleter
{
public:
IoBufDeleter(size_t paddedSize)
: m_paddedSize(paddedSize)
{
debug_assert(m_paddedSize != 0);
}
void operator()(const u8* mem)
{
debug_assert(m_paddedSize != 0);
#ifndef NDEBUG
allocatorChecker.notify_free((void*)mem, m_paddedSize);
#endif
page_aligned_free((void*)mem, m_paddedSize);
m_paddedSize = 0;
}
private:
size_t m_paddedSize;
};
IoBuf io_buf_Allocate(size_t size)
{
const size_t paddedSize = (size_t)round_up(size, BLOCK_SIZE);
const u8* mem = (const u8*)page_aligned_alloc(paddedSize);
if(!mem)
throw std::bad_alloc();
#ifndef NDEBUG
allocatorChecker.notify_alloc((void*)mem, paddedSize);
#endif
return IoBuf(mem, IoBufDeleter(paddedSize));
}