add dyn_mem_shared class; increase start scanning timeout to 20s temporarily

This commit is contained in:
gb 2024-01-03 17:38:26 +08:00
parent 51ba9c21df
commit b30660fc49
3 changed files with 51 additions and 7 deletions

View File

@ -476,7 +476,7 @@ int scanner_handler::scan_start(void)
{
auto call = [&](cmd_result* cmd) -> int
{
cmd->set_timeout(4000);
cmd->set_timeout(20 * 1000);
return usb_->scan_start(cmd->get_id());
};
auto clean = [&](cmd_result* cmd) -> int
@ -489,7 +489,7 @@ int scanner_handler::scan_start(void)
*used = sizeof(PACK_BASE);
*more = nullptr;
status_ = SCANNER_STATUS_START_SCANNING;
status_ = pack->data == 0 ? SCANNER_STATUS_START_SCANNING : SCANNER_STATUS_READY;
cmd->trigger();
return nullptr;

View File

@ -322,22 +322,28 @@ data_source::~data_source()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// dyn_mem
#ifdef STAT_MEM
uint64_t dyn_mem::mem_used_bytes_ = 0;
MUTEX dyn_mem::mem_lock_;
#endif
dyn_mem::dyn_mem(size_t size) : buf_(nullptr), len_(0), space_(ALIGN_TO(size, 16))
{
buf_ = (uint8_t*)malloc(space_);
if (buf_)
{
SIMPLE_LOCK(dyn_mem::mem_lock_);
dyn_mem::mem_used_bytes_ += space_;
#ifdef STAT_MEM
{
SIMPLE_LOCK(dyn_mem::mem_lock_);
dyn_mem::mem_used_bytes_ += space_;
}
#endif
memset(buf_, 0, space_);
}
}
dyn_mem::dyn_mem(void* buf, size_t size) : buf_((uint8_t*)buf), space_(size), len_(size)
dyn_mem::dyn_mem(void* buf, size_t size)
: buf_((uint8_t*)buf), space_(size), len_(size)
{
}
dyn_mem::~dyn_mem()
@ -345,17 +351,22 @@ dyn_mem::~dyn_mem()
if (buf_)
{
free(buf_);
#ifdef STAT_MEM
{
SIMPLE_LOCK(dyn_mem::mem_lock_);
dyn_mem::mem_used_bytes_ -= space_;
}
#endif
}
}
#ifdef STAT_MEM
uint64_t dyn_mem::mem_used(void)
{
return dyn_mem::mem_used_bytes_;
}
#endif
dyn_mem_ptr dyn_mem::memory(size_t size)
{
return new dyn_mem(size);
@ -424,10 +435,12 @@ dyn_mem& dyn_mem::operator+=(dyn_mem& r)
memcpy(buf, buf_, len_);
free(buf_);
buf_ = buf;
#ifdef STAT_MEM
{
SIMPLE_LOCK(dyn_mem::mem_lock_);
dyn_mem::mem_used_bytes_ += size - space_;
}
#endif
space_ = size;
}
memcpy(buf_ + len_, r.buf_, r.get_rest());
@ -475,6 +488,17 @@ int dyn_mem::fetch_data(void* buf, uint32_t* size)
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
dyn_mem_shared::dyn_mem_shared(void* buf, size_t size, BEFORE_DESTROY_FUNC destroy, void* param)
: dyn_mem(buf, size), destroy_(destroy), param_(param)
{}
dyn_mem_shared::~dyn_mem_shared()
{
if(destroy_)
destroy_(this, param_);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
file_reader::file_reader() : len_(0), src_(nullptr), path_(""), consume_(0)

View File

@ -205,6 +205,10 @@ public:
virtual int fetch_data(void* buf, uint32_t* size) = 0;
};
// #define STAT_MEM
#define BEFORE_DESTROY_RET void
#define BEFORE_DESTROY_PARAM dyn_mem* mem, void* param
#define BEFORE_DESTROY_FUNC std::function<BEFORE_DESTROY_RET(BEFORE_DESTROY_PARAM)>
class dyn_mem : public data_source
{
@ -212,18 +216,22 @@ class dyn_mem : public data_source
size_t space_; // occupy space in bytes
size_t len_; // data length in bytes
#ifdef STAT_MEM
static MUTEX mem_lock_;
static uint64_t mem_used_bytes_;
#endif
public:
dyn_mem(size_t size);
dyn_mem(void* buf, size_t size);
#ifdef STAT_MEM
static uint64_t mem_used(void);
#endif
static dyn_mem* memory(size_t size);
protected:
~dyn_mem();
virtual ~dyn_mem();
public:
uint32_t space(void);
@ -246,6 +254,17 @@ public:
// following API valid when is_memory_block() return false
virtual int fetch_data(void* buf, uint32_t* size) override;
};
class dyn_mem_shared : public dyn_mem
{
BEFORE_DESTROY_FUNC destroy_ = BEFORE_DESTROY_FUNC();
void* param_ = nullptr;
public:
dyn_mem_shared(void* buf, size_t size, BEFORE_DESTROY_FUNC destroy = BEFORE_DESTROY_FUNC(), void* param = nullptr);
protected:
~dyn_mem_shared();
};
class file_reader : public data_source
{
@ -284,6 +303,7 @@ CLS_PTR(data_holder);
CLS_PTR(mem_holder);
CLS_PTR(data_source);
CLS_PTR(dyn_mem);
CLS_PTR(dyn_mem_shared);
CLS_PTR(file_reader);