Skip to content
Snippets Groups Projects
PtrMemory.hpp 2.82 KiB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
/*
 * PtrMemory.hpp
 *
 *  Created on: Apr 15, 2015
 *      Author: Pietro Incardona
 */

#ifndef PTRMEMORY_HPP_
#define PTRMEMORY_HPP_


/**
 * \brief This class give memory from a preallocated memory, memory destruction is not performed
 *
 * Useful to shape pieces of memory
 *
 * Usage:
 *
 * void * ptr = new int[1000]
 *
 * PtrMemory m = new PtrMemory(ptr,1000);
 *
 * m.allocate();
 * int * ptr = m.getPointer();
 * *ptr[999] = 1000;
 * ....
 *
 * delete[] ptr;
 *
 */

#include "config.h"
#include "memory.hpp"
#include <cstddef>
#include <cstdint>
#include <iostream>


class PtrMemory : public memory
{
	//! Size of the pointed memory
	size_t spm;

	//! Pointed memory
	void * dm;

	//! Reference counter
	long int ref_cnt;

	//! copy from Pointer to Heap
	bool copyFromPointer(const void * ptr, size_t sz);
Pietro Incardona's avatar
Pietro Incardona committed

	//! Set alignment the memory will be aligned with this number
	void setAlignment(size_t align);

public:

Pietro Incardona's avatar
Pietro Incardona committed
	//! copy from same Heap to Heap
	bool copyDeviceToDevice(const PtrMemory & m);

Pietro Incardona's avatar
Pietro Incardona committed
	//! flush the memory
	virtual bool flush() {return true;};
Pietro Incardona's avatar
Pietro Incardona committed
	//! allocate memory
	virtual bool allocate(size_t sz);
	//! destroy memory
	virtual void destroy();
	//! copy memory
	virtual bool copy(const memory & m);
Pietro Incardona's avatar
Pietro Incardona committed
	//! the the size of the allocated memory
	virtual size_t size() const;
Pietro Incardona's avatar
Pietro Incardona committed
	//! resize the memory allocated
	virtual bool resize(size_t sz);
	//! get a readable pointer with the data
	virtual void * getPointer();

	//! get a readable pointer with the data
	virtual const void * getPointer() const;

	//! get a readable pointer with the data
	virtual void * getDevicePointer();


	//! Do nothing
	virtual void deviceToHost(){};

	//! Do nothing
	virtual void hostToDevice(){};

Pietro Incardona's avatar
Pietro Incardona committed
	//! Do nothing
	virtual void deviceToHost(size_t start, size_t stop) {};

	//! Do nothing
	virtual void hostToDevice(size_t start, size_t top) {};

Pietro Incardona's avatar
Pietro Incardona committed
	/*! \brief fill memory with the selected byte
	 *
	 *
	 */
	virtual void fill(unsigned char c);

Pietro Incardona's avatar
Pietro Incardona committed
	//! Increment the reference counter
	virtual void incRef()
	{ref_cnt++;}

	//! Decrement the reference counter
	virtual void decRef()
	{ref_cnt--;}

	//! Return the reference counter
	virtual long int ref()
	{
		return ref_cnt;
	}

Pietro Incardona's avatar
Pietro Incardona committed
	/*! \brief Return true if the device and the host pointer are the same
	 *
	 * \return true if they are the same
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
	constexpr static bool isDeviceHostSame()
Pietro Incardona's avatar
Pietro Incardona committed
	{
		return true;
	}

	/*! \brief Allocated Memory is already initialized
	 *
	 * \return true
	 *
	 */
	bool isInitialized()
	{
		return true;
	}

Pietro Incardona's avatar
Pietro Incardona committed
	// Default constructor
	PtrMemory():spm(0),dm(NULL),ref_cnt(0)
Pietro Incardona's avatar
Pietro Incardona committed
	{
	};

	//! Constructor, we choose a default alignment of 32 for avx
	PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),ref_cnt(0)
Pietro Incardona's avatar
Pietro Incardona committed
	{
	};

	~PtrMemory()
	{
		if(ref_cnt == 0)
			destroy();
		else
			std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
	};
};


#endif /* PTRMEMORY_HPP_ */