http://stxxl.sourceforge.net
<beckmann@cs.uni-frankfurt.de>
http://www.boost.org/LICENSE_1_0.txt
#include <cstring>
#include <stxxl/bits/io/mem_file.h>
#include <stxxl/bits/io/iostats.h>
__STXXL_BEGIN_NAMESPACE
void mem_file::serve(const request * req) throw (io_error)
{
assert(req->get_file() == this);
offset_type offset = req->get_offset();
void * buffer = req->get_buffer();
size_type bytes = req->get_size();
request::request_type type = req->get_type();
if (type == request::READ)
{
stats::scoped_read_timer read_timer(bytes);
memcpy(buffer, ptr + offset, bytes);
}
else
{
stats::scoped_write_timer write_timer(bytes);
memcpy(ptr + offset, buffer, bytes);
}
}
const char * mem_file::io_type() const
{
return "memory";
}
mem_file::~mem_file()
{
delete[] ptr;
ptr = NULL;
}
void mem_file::lock()
{
}
file::offset_type mem_file::size()
{
return sz;
}
void mem_file::set_size(offset_type newsize)
{
assert(ptr == NULL);
if (ptr == NULL)
ptr = new char[sz = newsize];
}
void mem_file::discard(offset_type offset, offset_type size)
{
#ifndef STXXL_MEMFILE_DONT_CLEAR_FREED_MEMORY
STXXL_VERBOSE("discard at " << offset << " len " << size);
void * uninitialized = malloc(BLOCK_ALIGN);
while (size >= BLOCK_ALIGN) {
memcpy(ptr + offset, uninitialized, BLOCK_ALIGN);
offset += BLOCK_ALIGN;
size -= BLOCK_ALIGN;
}
if (size > 0)
memcpy(ptr + offset, uninitialized, size);
free(uninitialized);
#else
STXXL_UNUSED(offset);
STXXL_UNUSED(size);
#endif
}
__STXXL_END_NAMESPACE