Merge branch 'genesys-remove-manual-alloc' into 'master'

genesys: Remove manual allocations

See merge request sane-project/backends!112
merge-requests/114/head
Povilas Kanapickas 2019-08-09 10:23:15 +00:00
commit a3e492d6d5
4 zmienionych plików z 52 dodań i 50 usunięć

Wyświetl plik

@ -66,14 +66,22 @@
#include "../include/sane/sanei_magic.h" #include "../include/sane/sanei_magic.h"
#include "genesys_devices.cc" #include "genesys_devices.cc"
#include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <list> #include <list>
#include <exception> #include <exception>
#include <vector> #include <vector>
// Data that we allocate to back SANE_Device objects in s_sane_devices
struct SANE_Device_Data
{
std::string name;
};
StaticInit<std::list<Genesys_Scanner>> s_scanners; StaticInit<std::list<Genesys_Scanner>> s_scanners;
StaticInit<std::vector<SANE_Device>> s_sane_devices; StaticInit<std::vector<SANE_Device>> s_sane_devices;
StaticInit<std::vector<SANE_Device_Data>> s_sane_devices_data;
StaticInit<std::vector<SANE_Device*>> s_sane_devices_ptrs; StaticInit<std::vector<SANE_Device*>> s_sane_devices_ptrs;
StaticInit<std::list<Genesys_Device>> s_devices; StaticInit<std::list<Genesys_Device>> s_devices;
@ -4349,21 +4357,16 @@ SANE_Range *range=NULL;
* @param currdev current scanner device * @param currdev current scanner device
* @return an allocated string containing a file name * @return an allocated string containing a file name
*/ */
static char *calibration_filename(Genesys_Device *currdev) static std::string calibration_filename(Genesys_Device *currdev)
{ {
char *tmpstr; std::string ret;
ret.resize(PATH_MAX);
char *ptr; char *ptr;
char filename[80]; char filename[80];
unsigned int count; unsigned int count;
unsigned int i; unsigned int i;
/* allocate space for result */
tmpstr = (char*) malloc(PATH_MAX);
if(tmpstr==NULL)
{
return NULL;
}
/* first compute the DIR where we can store cache: /* first compute the DIR where we can store cache:
* 1 - home dir * 1 - home dir
* 2 - $TMPDIR * 2 - $TMPDIR
@ -4402,7 +4405,7 @@ static char *calibration_filename(Genesys_Device *currdev)
} }
if(count>1) if(count>1)
{ {
snprintf(filename,sizeof(filename),"%s.cal",currdev->file_name); std::snprintf(filename, sizeof(filename), "%s.cal", currdev->file_name.c_str());
for(i=0;i<strlen(filename);i++) for(i=0;i<strlen(filename);i++)
{ {
if(filename[i]==':'||filename[i]==PATH_SEP) if(filename[i]==':'||filename[i]==PATH_SEP)
@ -4417,23 +4420,29 @@ static char *calibration_filename(Genesys_Device *currdev)
} }
/* build final final name : store dir + filename */ /* build final final name : store dir + filename */
if (NULL == ptr) if (ptr == nullptr) {
{ int size = std::snprintf(&ret.front(), ret.size(), "%s", filename);
snprintf (tmpstr, PATH_MAX, "%s", filename); ret.resize(size);
} }
else else
{ {
int size = 0;
#ifdef HAVE_MKDIR #ifdef HAVE_MKDIR
/* make sure .sane directory exists in existing store dir */ /* make sure .sane directory exists in existing store dir */
snprintf (tmpstr, PATH_MAX, "%s%c.sane", ptr, PATH_SEP); size = std::snprintf(&ret.front(), ret.size(), "%s%c.sane", ptr, PATH_SEP);
mkdir(tmpstr,0700); ret.resize(size);
mkdir(ret.c_str(), 0700);
ret.resize(PATH_MAX);
#endif #endif
snprintf (tmpstr, PATH_MAX, "%s%c.sane%c%s", ptr, PATH_SEP, PATH_SEP, filename); size = std::snprintf(&ret.front(), ret.size(), "%s%c.sane%c%s",
ptr, PATH_SEP, PATH_SEP, filename);
ret.resize(size);
} }
DBG(DBG_info, "%s: calibration filename >%s<\n", __func__, tmpstr); DBG(DBG_info, "%s: calibration filename >%s<\n", __func__, ret.c_str());
return tmpstr; return ret;
} }
@ -5068,12 +5077,12 @@ attach (SANE_String_Const devname, Genesys_Device ** devp, SANE_Bool may_wait)
} }
for (auto& dev : *s_devices) { for (auto& dev : *s_devices) {
if (strcmp(dev.file_name, devname) == 0) { if (dev.file_name == devname) {
if (devp) if (devp)
*devp = &dev; *devp = &dev;
DBG(DBG_info, "%s: device `%s' was already in device list\n", __func__, devname); DBG(DBG_info, "%s: device `%s' was already in device list\n", __func__, devname);
return SANE_STATUS_GOOD; return SANE_STATUS_GOOD;
} }
} }
DBG(DBG_info, "%s: trying to open device `%s'\n", __func__, devname); DBG(DBG_info, "%s: trying to open device `%s'\n", __func__, devname);
@ -5114,14 +5123,9 @@ attach (SANE_String_Const devname, Genesys_Device ** devp, SANE_Bool may_wait)
return SANE_STATUS_INVAL; return SANE_STATUS_INVAL;
} }
char* new_devname = strdup (devname);
if (!new_devname) {
return SANE_STATUS_NO_MEM;
}
s_devices->emplace_back(); s_devices->emplace_back();
dev = &s_devices->back(); dev = &s_devices->back();
dev->file_name = new_devname; dev->file_name = devname;
dev->model = &found_usb_dev->model; dev->model = &found_usb_dev->model;
dev->vendorId = found_usb_dev->vendor; dev->vendorId = found_usb_dev->vendor;
@ -5129,8 +5133,8 @@ attach (SANE_String_Const devname, Genesys_Device ** devp, SANE_Bool may_wait)
dev->usb_mode = 0; /* i.e. unset */ dev->usb_mode = 0; /* i.e. unset */
dev->already_initialized = SANE_FALSE; dev->already_initialized = SANE_FALSE;
DBG(DBG_info, "%s: found %s flatbed scanner %s at %s\n", __func__, dev->model->vendor, DBG(DBG_info, "%s: found %s flatbed scanner %s at %s\n", __func__, dev->model->vendor,
dev->model->model, dev->file_name); dev->model->model, dev->file_name.c_str());
if (devp) { if (devp) {
*devp = dev; *devp = dev;
@ -5412,6 +5416,7 @@ sane_init_impl(SANE_Int * version_code, SANE_Auth_Callback authorize)
s_scanners.init(); s_scanners.init();
s_devices.init(); s_devices.init();
s_sane_devices.init(); s_sane_devices.init();
s_sane_devices_data.init();
s_sane_devices_ptrs.init(); s_sane_devices_ptrs.init();
genesys_init_sensor_tables(); genesys_init_sensor_tables();
genesys_init_frontend_tables(); genesys_init_frontend_tables();
@ -5464,8 +5469,10 @@ sane_get_devices_impl(const SANE_Device *** device_list, SANE_Bool local_only)
probe_genesys_devices (); probe_genesys_devices ();
s_sane_devices->clear(); s_sane_devices->clear();
s_sane_devices_data->clear();
s_sane_devices_ptrs->clear(); s_sane_devices_ptrs->clear();
s_sane_devices->reserve(s_devices->size()); s_sane_devices->reserve(s_devices->size());
s_sane_devices_data->reserve(s_devices->size());
s_sane_devices_ptrs->reserve(s_devices->size() + 1); s_sane_devices_ptrs->reserve(s_devices->size() + 1);
for (auto dev_it = s_devices->begin(); dev_it != s_devices->end();) { for (auto dev_it = s_devices->begin(); dev_it != s_devices->end();) {
@ -5473,8 +5480,11 @@ sane_get_devices_impl(const SANE_Device *** device_list, SANE_Bool local_only)
sanei_usb_find_devices(dev_it->vendorId, dev_it->productId, check_present); sanei_usb_find_devices(dev_it->vendorId, dev_it->productId, check_present);
if (present) { if (present) {
s_sane_devices->emplace_back(); s_sane_devices->emplace_back();
s_sane_devices_data->emplace_back();
auto& sane_device = s_sane_devices->back(); auto& sane_device = s_sane_devices->back();
sane_device.name = dev_it->file_name; auto& sane_device_data = s_sane_devices_data->back();
sane_device_data.name = dev_it->file_name;
sane_device.name = sane_device_data.name.c_str();
sane_device.vendor = dev_it->model->vendor; sane_device.vendor = dev_it->model->vendor;
sane_device.model = dev_it->model->model; sane_device.model = dev_it->model->model;
sane_device.type = "flatbed scanner"; sane_device.type = "flatbed scanner";
@ -5504,7 +5514,6 @@ sane_open_impl(SANE_String_Const devicename, SANE_Handle * handle)
{ {
DBG_HELPER_ARGS(dbg, "devicename = %s", devicename); DBG_HELPER_ARGS(dbg, "devicename = %s", devicename);
Genesys_Device *dev = nullptr; Genesys_Device *dev = nullptr;
char *tmpstr;
/* devicename="" or devicename="genesys" are default values that use /* devicename="" or devicename="genesys" are default values that use
* first available device * first available device
@ -5513,7 +5522,7 @@ sane_open_impl(SANE_String_Const devicename, SANE_Handle * handle)
{ {
/* search for the given devicename in the device list */ /* search for the given devicename in the device list */
for (auto& d : *s_devices) { for (auto& d : *s_devices) {
if (strcmp(d.file_name, devicename) == 0) { if (d.file_name == devicename) {
dev = &d; dev = &d;
break; break;
} }
@ -5536,9 +5545,8 @@ sane_open_impl(SANE_String_Const devicename, SANE_Handle * handle)
// empty devicename or "genesys" -> use first device // empty devicename or "genesys" -> use first device
if (!s_devices->empty()) { if (!s_devices->empty()) {
dev = &s_devices->front(); dev = &s_devices->front();
devicename = dev->file_name; DBG(DBG_info, "%s: empty devicename, trying `%s'\n", __func__, dev->file_name.c_str());
DBG(DBG_info, "%s: empty devicename, trying `%s'\n", __func__, devicename); }
}
} }
if (!dev) if (!dev)
@ -5554,8 +5562,8 @@ sane_open_impl(SANE_String_Const devicename, SANE_Handle * handle)
DBG(DBG_error0, " scanner and what does (not) work.\n"); DBG(DBG_error0, " scanner and what does (not) work.\n");
} }
dbg.vstatus("open device '%s'", dev->file_name); dbg.vstatus("open device '%s'", dev->file_name.c_str());
dev->usb_dev.open(dev->file_name); dev->usb_dev.open(dev->file_name.c_str());
dbg.clear(); dbg.clear();
@ -5592,12 +5600,11 @@ sane_open_impl(SANE_String_Const devicename, SANE_Handle * handle)
/* here is the place to fetch a stored calibration cache */ /* here is the place to fetch a stored calibration cache */
if (s->dev->force_calibration == 0) if (s->dev->force_calibration == 0)
{ {
tmpstr=calibration_filename(s->dev); auto path = calibration_filename(s->dev);
s->calibration_file = tmpstr; s->calibration_file = path;
s->dev->calib_file = tmpstr; s->dev->calib_file = path;
DBG(DBG_info, "%s: Calibration filename set to:\n", __func__); DBG(DBG_info, "%s: Calibration filename set to:\n", __func__);
DBG(DBG_info, "%s: >%s<\n", __func__, s->dev->calib_file.c_str()); DBG(DBG_info, "%s: >%s<\n", __func__, s->dev->calib_file.c_str());
free(tmpstr);
catch_all_exceptions(__func__, [&]() catch_all_exceptions(__func__, [&]()
{ {

Wyświetl plik

@ -50,9 +50,6 @@
Genesys_Device::~Genesys_Device() Genesys_Device::~Genesys_Device()
{ {
clear(); clear();
if (file_name != nullptr)
free(file_name);
} }
void Genesys_Device::clear() void Genesys_Device::clear()

Wyświetl plik

@ -214,7 +214,7 @@ struct Genesys_Device
// 2: USB 2.0 // 2: USB 2.0
SANE_Int usb_mode = 0; SANE_Int usb_mode = 0;
SANE_String file_name = nullptr; std::string file_name;
std::string calib_file; std::string calib_file;
// if enabled, no calibration data will be loaded or saved to files // if enabled, no calibration data will be loaded or saved to files

Wyświetl plik

@ -101,8 +101,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#define FREE_IFNOT_NULL(x) if(x!=NULL) { free(x); x=NULL;}
#define GENESYS_RED 0 #define GENESYS_RED 0
#define GENESYS_GREEN 1 #define GENESYS_GREEN 1
#define GENESYS_BLUE 2 #define GENESYS_BLUE 2