mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-06 21:58:57 -04:00

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8094 b45a01b8-16f6-495d-af2f-9b41ad6348cc
49 lines
889 B
C++
49 lines
889 B
C++
#pragma once
|
|
|
|
#include <stdlib.h>
|
|
|
|
// This is a scope guard to release the memory block when going of of the current scope.
|
|
// Can be very useful to auto-delete some memory on quit without the need to call free each time.
|
|
//
|
|
// Usage:
|
|
//
|
|
// {
|
|
// TemporaryMemoryHolder mem(size) ;
|
|
//
|
|
// if(mem != NULL)
|
|
// [ do something ] ;
|
|
//
|
|
// memcopy(mem, some_other_memory, size) ;
|
|
//
|
|
// [do something]
|
|
//
|
|
// } // mem gets freed automatically
|
|
//
|
|
class RsTemporaryMemory
|
|
{
|
|
public:
|
|
RsTemporaryMemory(size_t s)
|
|
{
|
|
_mem = (unsigned char *)malloc(s) ;
|
|
}
|
|
|
|
operator unsigned char *() { return _mem ; }
|
|
|
|
~RsTemporaryMemory()
|
|
{
|
|
if(_mem != NULL)
|
|
{
|
|
free(_mem) ;
|
|
_mem = NULL ;
|
|
}
|
|
}
|
|
|
|
private:
|
|
unsigned char *_mem ;
|
|
|
|
// make it noncopyable
|
|
RsTemporaryMemory& operator=(const RsTemporaryMemory&) { return *this ;}
|
|
RsTemporaryMemory(const RsTemporaryMemory&) {}
|
|
};
|
|
|
|
|