Commit 95e0fccc authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

Improve decompress error handling by using automatic variables

* Replace auto_ptr by unique_ptr
* Add HeapPtr<T>, a unique_ptr with a Deleter set to ::free
parent a90f92e4
......@@ -27,6 +27,13 @@
namespace eigerapi
{
template <typename T>
struct HeapDeleter {
void operator()(T *p) { ::free(p); };
};
template <typename T>
using HeapPtr = std::unique_ptr<T, HeapDeleter<T>>;
class Requests
{
public:
......@@ -116,7 +123,7 @@ namespace eigerapi
bool m_delete_after_transfer;
long m_download_size;
FILE* m_target_file;
void* m_buffer;
HeapPtr<void> m_buffer;
};
typedef std::shared_ptr<CurlLoop::FutureRequest> CurlReq;
......
......@@ -389,7 +389,7 @@ struct StatusChangedCallback {
std::string error;
void fire() { cbk->status_changed(status, error); }
};
typedef std::auto_ptr<StatusChangedCallback> StatusChangedCallbackPtr;
typedef std::unique_ptr<StatusChangedCallback> StatusChangedCallbackPtr;
void CurlLoop::FutureRequest::_status_changed()
{
......
......@@ -753,19 +753,20 @@ Requests::Transfer::Transfer(Requests& requests,
m_delete_after_transfer(delete_after_transfer),
m_download_size(0)
{
if(posix_memalign(&m_buffer,4*1024,buffer_write_size))
void *ptr;
if(posix_memalign(&ptr,4*1024,buffer_write_size))
THROW_EIGER_EXCEPTION("Can't allocate write buffer memory","");
m_buffer.reset(ptr);
m_target_file = fopen(target_path.c_str(),"w+");
if(!m_target_file)
{
free(m_buffer);
char str_errno[1024];
strerror_r(errno,str_errno,sizeof(str_errno));
std::ostringstream error_buffer;
error_buffer << "Can't open destination file " << target_path;
THROW_EIGER_EXCEPTION(error_buffer.str().c_str(), str_errno);
}
setbuffer(m_target_file,(char*)m_buffer,buffer_write_size);
setbuffer(m_target_file,(char*)m_buffer.get(),buffer_write_size);
curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, _write);
curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, this);
}
......@@ -773,7 +774,6 @@ Requests::Transfer::~Transfer()
{
if(m_target_file)
fclose(m_target_file);
free(m_buffer);
}
size_t
......
......@@ -25,11 +25,14 @@
#include "EigerDecompress.h"
#include "EigerStream.h"
#include <eigerapi/Requests.h>
#include "processlib/LinkTask.h"
#include "processlib/ProcessExceptions.h"
using namespace lima;
using namespace lima::Eiger;
using namespace eigerapi;
class _DecompressTask : public LinkTask
{
......@@ -58,23 +61,24 @@ void _expand(void *src,Data& dst)
Data _DecompressTask::process(Data& out)
{
void *lima_buffer = out.data();
Stream::ImageData img_data;
if(!m_stream.get_msg(lima_buffer,img_data))
throw ProcessException("_DecompressTask: can't find compressed message");
Stream::ImageData img_data = m_stream.get_msg(lima_buffer);
void *msg_data;
size_t msg_size;
img_data.getMsgDataNSize(msg_data, msg_size);
const size_t& depth = img_data.depth;
const int& depth = img_data.depth;
const Camera::CompressionType& type = img_data.comp_type;
bool expand_16_to_32bit = ((out.depth() == 4) && (depth == 2));
int size = out.size() / (expand_16_to_32bit ? 2 : 1);
bool decompress = (type != Camera::NoCompression);
void *aux_buffer = NULL;
if(expand_16_to_32bit && decompress)
if(posix_memalign(&aux_buffer,16,size))
HeapPtr<void> aux_buffer;
if(expand_16_to_32bit && decompress) {
void *ptr;
if(posix_memalign(&ptr,16,size))
throw ProcessException("Can't allocate temporary memory");
aux_buffer.reset(ptr);
}
void *decompress_out = expand_16_to_32bit ? aux_buffer : lima_buffer;
void *decompress_out = aux_buffer ? aux_buffer.get() : lima_buffer;
int return_code = 0;
if (type == Camera::LZ4) {
return_code = LZ4_decompress_fast((const char*)msg_data,
......@@ -95,8 +99,6 @@ Data _DecompressTask::process(Data& out)
}
if(return_code < 0)
{
if(aux_buffer) free(aux_buffer);
char ErrorBuff[1024];
snprintf(ErrorBuff,sizeof(ErrorBuff),
"_DecompressTask: decompression failed, (error code: %d) (data size %d)",
......@@ -110,11 +112,6 @@ Data _DecompressTask::process(Data& out)
} else if(!decompress)
memcpy(lima_buffer, msg_data, size);
if(aux_buffer)
free(aux_buffer);
m_stream.release_msg(lima_buffer);
return out;
}
......
......@@ -542,7 +542,7 @@ void Stream::getLastStreamInfo(StreamInfo& last_info)
DEB_RETURN() << DEB_VAR1(last_info);
}
bool Stream::get_msg(void* aDataBuffer,ImageData& img_data)
Stream::ImageData Stream::get_msg(void* aDataBuffer)
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(aDataBuffer);
......@@ -550,25 +550,13 @@ bool Stream::get_msg(void* aDataBuffer,ImageData& img_data)
AutoMutex lock(m_cond.mutex());
Data2Message::iterator it = m_data_2_msg.find(aDataBuffer);
if(it == m_data_2_msg.end())
return false;
img_data = it->second;
THROW_HW_ERROR(Error) << "Can't find image_data message";
ImageData img_data = it->second;
m_data_2_msg.erase(it);
lock.unlock();
if (DEB_CHECK_ANY(DebTypeReturn))
DEB_RETURN() << DEB_VAR1(img_data);
return true;
}
void Stream::release_msg(void* aDataBuffer)
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(aDataBuffer);
AutoMutex lock(m_cond.mutex());
Data2Message::iterator it = m_data_2_msg.find(aDataBuffer);
if(it == m_data_2_msg.end())
THROW_HW_ERROR(Error) << "Internal error: releasing buffer not in list";
m_data_2_msg.erase(it);
return img_data;
}
void Stream::release_all_msgs()
......
......@@ -64,8 +64,7 @@ namespace lima
HwBufferCtrlObj* getBufferCtrlObj();
bool get_msg(void* aDataBuffer,ImageData& img_data);
void release_msg(void* aDataBuffer);
ImageData get_msg(void* aDataBuffer);
void release_all_msgs();
void getLastStreamInfo(StreamInfo& info);
......@@ -73,6 +72,7 @@ namespace lima
private:
class _BufferCtrlObj;
friend class _BufferCtrlObj;
friend class ImageDataPtr;
typedef std::map<void*,ImageData> Data2Message;
......@@ -103,7 +103,7 @@ namespace lima
TrigMode m_trigger_mode;
CompressionType m_comp_type;
std::auto_ptr<_BufferCtrlObj> m_buffer_ctrl_obj;
std::unique_ptr<_BufferCtrlObj> m_buffer_ctrl_obj;
StdBufferCbMgr* m_buffer_mgr;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment