Skip to content
Snippets Groups Projects
HeapMemory.cpp 4.17 KiB
Newer Older
/*
 * HeapMemory.cpp
 *
 *  Created on: Aug 17, 2014
 *      Author: Pietro Incardona
 */

#include "HeapMemory.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
#include <cstring>
#include <iostream>
#include <cstdint>
static const int extra_pad = 512;

Pietro Incardona's avatar
Pietro Incardona committed
/*! \brief fill host and device memory with the selected byte
 *
 * \param byte to fill
 *
 */
void HeapMemory::fill(unsigned char c)
{
	memset(dm,c,size());
}

/*! \brief Allocate a chunk of memory
 *
 * \param sz size of the chunk of memory to allocate in byte
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed

bool HeapMemory::allocate(size_t sz)
{
	//! Allocate the device memory
	if (dm == NULL)
	{
		dmOrig = new byte[sz+alignement+extra_pad];
		#ifdef GARBAGE_INJECTOR
		memset(dmOrig,0xFF,sz+alignement+extra_pad);
		#endif
	}
		std::cerr << __FILE__ << ":" << __LINE__ << " error memory already allocated\n";
		return false;
	}
Pietro Incardona's avatar
Pietro Incardona committed
	dm = dmOrig;

	// align it, we do not know the size of the element we put 1
	// and we ignore the align check
	size_t sz_a = sz+alignement;
	align(alignement,1,(void *&)dm,sz_a);
Pietro Incardona's avatar
Pietro Incardona committed
/*! \brief set the memory block to be aligned by this number
 *
 */
void HeapMemory::setAlignment(size_t align)
{
	this->alignement = align;
}

Pietro Incardona's avatar
Pietro Incardona committed
/*! \brief Destroy the internal memory
Pietro Incardona's avatar
Pietro Incardona committed

	sz = 0;
	dm = NULL;
	dmOrig = NULL;
bool HeapMemory::copyFromPointer(const void * ptr,size_t sz)
Pietro Incardona's avatar
Pietro Incardona committed
	memcpy(dm,ptr,sz);

	return true;
}

/*! \brief copy from device to device
 *
 * copy a piece of memory from device to device
 *
 * \param m from where to copy
bool HeapMemory::copyDeviceToDevice(const HeapMemory & m)
{
	//! The source buffer is too big to copy it

	if (m.sz > sz)
	{
		std::cerr << "Error " << __LINE__ << __FILE__ << ": source buffer is too big to copy";
Pietro Incardona's avatar
Pietro Incardona committed
		return false;
Pietro Incardona's avatar
Pietro Incardona committed
	// Copy the memory from m
	memcpy(dm,m.dm,m.sz);
	return true;
Pietro Incardona's avatar
Pietro Incardona committed
/*! \brief copy the memory
bool HeapMemory::copy(const memory & m)
Pietro Incardona's avatar
Pietro Incardona committed
	//! Here we try to cast memory into HeapMemory
	const HeapMemory * ofpm = dynamic_cast<const HeapMemory *>(&m);

	//! if we fail we get the pointer and simply copy from the pointer

	if (ofpm == NULL)
	{
		// copy the memory from device to host and from host to device

Pietro Incardona's avatar
Pietro Incardona committed
		return copyFromPointer(m.getPointer(),m.size());
	}
	else
	{
		// they are the same memory type, use cuda/thrust buffer copy

		return copyDeviceToDevice(*ofpm);
	}
	return false;
}

/*! \brief Get the size of the allocated memory
 *
 * Get the size of the allocated memory
 *
 * \return the size of the allocated memory
 *
 */

size_t HeapMemory::size() const
{
	return sz;
}

/*! \brief Resize the allocated memory
 *
 * Resize the allocated memory, if request is smaller than the allocated memory
 * is not resized
 *
 * \param sz size
 * \return true if the resize operation complete correctly
 *
 */
bool HeapMemory::resize(size_t sz)
{
Pietro Incardona's avatar
Pietro Incardona committed
	// if the allocated memory is enough, do not resize
Pietro Incardona's avatar
Pietro Incardona committed
	if (sz <= HeapMemory::size())
Pietro Incardona's avatar
Pietro Incardona committed
		return true;

Pietro Incardona's avatar
Pietro Incardona committed
	if (HeapMemory::size() == 0)
		return HeapMemory::allocate(sz);

	//! Create a new buffer if sz is bigger than the actual size
Pietro Incardona's avatar
Pietro Incardona committed
	byte * tdm;
	byte * tdmOrig;
	tdmOrig = new byte[sz+alignement+extra_pad];
	#ifdef GARBAGE_INJECTOR
	memset(tdmOrig,0xFF,sz+alignement+extra_pad);
	#endif
Pietro Incardona's avatar
Pietro Incardona committed
	tdm = tdmOrig;

	//! size plus alignment
	size_t sz_a = sz+alignement;

	//! align it
	align(alignement,1,(void *&)tdm,sz_a);
Pietro Incardona's avatar
Pietro Incardona committed
	memcpy(tdm,dm,HeapMemory::size());
Pietro Incardona's avatar
Pietro Incardona committed
	HeapMemory::destroy();
Pietro Incardona's avatar
Pietro Incardona committed
	dmOrig = tdmOrig;
	this->sz = sz;
/*! \brief Return a readable pointer with your data
 *
 * Return a readable pointer with your data
 *
 */

void * HeapMemory::getDevicePointer()
{
	return dm;
}

/*! \brief Return a readable pointer with your data
 *
 * Return a readable pointer with your data
 *
 */
void * HeapMemory::getPointer()
{
	return dm;
}

/*! \brief Return a readable pointer with your data
 *
 * Return a readable pointer with your data
 *
 */

const void * HeapMemory::getPointer() const
{
	return dm;
}