2018-05-30 21:19:13 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/util: rsmemory.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2012-2012 by Cyril Soler <csoler@users.sourceforge.net> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2015-03-28 17:23:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-01-12 21:10:11 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <util/stacktrace.h>
|
|
|
|
|
2016-01-12 21:43:04 -05:00
|
|
|
void *rs_malloc(size_t size) ;
|
2015-03-28 17:23:52 +00:00
|
|
|
|
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// {
|
2015-03-28 18:07:01 +00:00
|
|
|
// TemporaryMemoryHolder mem(size) ;
|
|
|
|
//
|
|
|
|
// if(mem != NULL)
|
|
|
|
// [ do something ] ;
|
|
|
|
//
|
|
|
|
// memcopy(mem, some_other_memory, size) ;
|
2015-03-28 17:23:52 +00:00
|
|
|
//
|
|
|
|
// [do something]
|
2015-03-28 18:07:01 +00:00
|
|
|
//
|
2015-03-28 17:23:52 +00:00
|
|
|
// } // mem gets freed automatically
|
|
|
|
//
|
2015-03-28 18:07:01 +00:00
|
|
|
class RsTemporaryMemory
|
2015-03-28 17:23:52 +00:00
|
|
|
{
|
2015-10-07 21:24:31 -04:00
|
|
|
public:
|
|
|
|
RsTemporaryMemory(size_t s)
|
|
|
|
{
|
2016-01-12 21:43:04 -05:00
|
|
|
_mem = (unsigned char *)rs_malloc(s) ;
|
2015-10-07 21:24:31 -04:00
|
|
|
|
|
|
|
if(_mem)
|
|
|
|
_size = s ;
|
|
|
|
else
|
|
|
|
_size = 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator unsigned char *() { return _mem ; }
|
|
|
|
|
|
|
|
size_t size() const { return _size ; }
|
|
|
|
|
|
|
|
~RsTemporaryMemory()
|
|
|
|
{
|
|
|
|
if(_mem != NULL)
|
|
|
|
{
|
|
|
|
free(_mem) ;
|
|
|
|
_mem = NULL ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned char *_mem ;
|
|
|
|
size_t _size ;
|
|
|
|
|
|
|
|
// make it noncopyable
|
|
|
|
RsTemporaryMemory& operator=(const RsTemporaryMemory&) { return *this ;}
|
|
|
|
RsTemporaryMemory(const RsTemporaryMemory&) {}
|
2015-03-28 17:23:52 +00:00
|
|
|
};
|