diff --git a/backend/genesys.cc b/backend/genesys.cc index d7ef08c3c..0551363d8 100644 --- a/backend/genesys.cc +++ b/backend/genesys.cc @@ -5920,9 +5920,7 @@ attach (SANE_String_Const devname, Genesys_Device ** devp, SANE_Bool may_wait) if (vendor == genesys_usb_device_list[i].vendor && product == genesys_usb_device_list[i].product) { - dev = (Genesys_Device*) malloc(sizeof (*dev)); - if (!dev) - return SANE_STATUS_NO_MEM; + dev = new Genesys_Device; break; } } @@ -5934,12 +5932,10 @@ attach (SANE_String_Const devname, Genesys_Device ** devp, SANE_Bool may_wait) return SANE_STATUS_INVAL; } - memset(dev, 0x00, sizeof(Genesys_Device)); - dev->file_name = strdup (devname); if (!dev->file_name) { - free(dev); + delete dev; return SANE_STATUS_NO_MEM; } @@ -6437,11 +6433,8 @@ sane_exit_impl(void) DBGSTART; for (dev = first_dev; dev; dev = next) { - /* sane_close() free many fields, not much things left to - * do here */ next = dev->next; - free (dev->file_name); - free (dev); + delete dev; } first_dev = 0; first_handle = 0; @@ -6514,7 +6507,7 @@ sane_get_devices_impl(const SANE_Device *** device_list, SANE_Bool local_only) if (dev->next == NULL) { /* empty the whole list */ - free (dev); + delete dev; first_dev = NULL; num_devices = 0; dev = NULL; @@ -6524,7 +6517,7 @@ sane_get_devices_impl(const SANE_Device *** device_list, SANE_Bool local_only) /* assign new start */ first_dev = dev->next; num_devices--; - free (dev); + delete dev; dev = first_dev; } } @@ -6533,7 +6526,7 @@ sane_get_devices_impl(const SANE_Device *** device_list, SANE_Bool local_only) { /* link previous dev to next dev */ prev->next = dev->next; - free (dev); + delete dev; num_devices--; /* next loop */ @@ -6696,7 +6689,6 @@ void sane_close_impl(SANE_Handle handle) { Genesys_Scanner *prev, *s; - Genesys_Calibration_Cache *cache, *next_cache; SANE_Status status; DBGSTART; @@ -6747,24 +6739,6 @@ sane_close_impl(SANE_Handle handle) if (s->dev->force_calibration == 0) write_calibration (s->dev); - for (cache = s->dev->calibration_cache; cache; cache = next_cache) - { - next_cache = cache->next; - free (cache->dark_average_data); - free (cache->white_average_data); - free (cache); - } - - sanei_genesys_buffer_free (&(s->dev->read_buffer)); - sanei_genesys_buffer_free (&(s->dev->lines_buffer)); - sanei_genesys_buffer_free (&(s->dev->shrink_buffer)); - sanei_genesys_buffer_free (&(s->dev->out_buffer)); - sanei_genesys_buffer_free (&(s->dev->binarize_buffer)); - sanei_genesys_buffer_free (&(s->dev->local_buffer)); - FREE_IFNOT_NULL (s->dev->white_average_data); - FREE_IFNOT_NULL (s->dev->dark_average_data); - FREE_IFNOT_NULL (s->dev->calib_file); - /* free allocated gamma tables */ FREE_IFNOT_NULL (s->dev->sensor.gamma_table[0]); FREE_IFNOT_NULL (s->dev->sensor.gamma_table[1]); @@ -6785,6 +6759,8 @@ sane_close_impl(SANE_Handle handle) else first_handle = s->next; + s->dev->clear(); + /* LAMP OFF : same register across all the ASICs */ sanei_genesys_write_register (s->dev, 0x03, 0x00); @@ -6796,6 +6772,7 @@ sane_close_impl(SANE_Handle handle) sanei_usb_reset (s->dev->dn); sanei_usb_close (s->dev->dn); + // not freeing s->dev because it's in the dev list free (s); DBGCOMPLETED; diff --git a/backend/genesys_low.cc b/backend/genesys_low.cc index f5af01440..be9384f7d 100644 --- a/backend/genesys_low.cc +++ b/backend/genesys_low.cc @@ -49,6 +49,37 @@ #include + +Genesys_Device::~Genesys_Device() +{ + clear(); + + if (file_name != nullptr) + free(file_name); +} + +void Genesys_Device::clear() +{ + sanei_genesys_buffer_free(&(read_buffer)); + sanei_genesys_buffer_free(&(lines_buffer)); + sanei_genesys_buffer_free(&(shrink_buffer)); + sanei_genesys_buffer_free(&(out_buffer)); + sanei_genesys_buffer_free(&(binarize_buffer)); + sanei_genesys_buffer_free(&(local_buffer)); + + FREE_IFNOT_NULL(white_average_data); + FREE_IFNOT_NULL(dark_average_data); + FREE_IFNOT_NULL(calib_file); + + Genesys_Calibration_Cache* next_cache = nullptr; + for (Genesys_Calibration_Cache* cache = calibration_cache; cache; cache = next_cache) { + next_cache = cache->next; + free(cache->dark_average_data); + free(cache->white_average_data); + free(cache); + } +} + /* ------------------------------------------------------------------------ */ /* functions calling ASIC specific functions */ /* ------------------------------------------------------------------------ */ diff --git a/backend/genesys_low.h b/backend/genesys_low.h index 2382f8890..663c4e7a3 100644 --- a/backend/genesys_low.h +++ b/backend/genesys_low.h @@ -83,6 +83,7 @@ #include #include +#include #include #include #include @@ -875,93 +876,130 @@ struct Genesys_Calibration_Cache */ struct Genesys_Device { - SANE_Int dn; - SANE_Word vendorId; /**< USB vendor identifier */ - SANE_Word productId; /**< USB product identifier */ + Genesys_Device() = default; + ~Genesys_Device(); - // USB mode: - // 0: not set - // 1: USB 1.1 - // 2: USB 2.0 - SANE_Int usb_mode; + // frees commonly used data + void clear(); - SANE_String file_name; - SANE_String calib_file; - SANE_Int force_calibration; // if enabled, no calibration data will be loaded - // or saved to files - Genesys_Model *model; + SANE_Int dn = 0; + SANE_Word vendorId = 0; /**< USB vendor identifier */ + SANE_Word productId = 0; /**< USB product identifier */ - Genesys_Register_Set reg[256]; - Genesys_Register_Set calib_reg[256]; - Genesys_Settings settings; - Genesys_Frontend frontend; - Genesys_Sensor sensor; - Genesys_Gpo gpo; - Genesys_Motor motor; - uint16_t slope_table0[256]; - uint16_t slope_table1[256]; - uint8_t control[6]; - time_t init_date; + // USB mode: + // 0: not set + // 1: USB 1.1 + // 2: USB 2.0 + SANE_Int usb_mode = 0; - size_t average_size; - size_t calib_pixels; /**< number of pixels used during shading calibration */ - size_t calib_lines; /**< number of lines used during shading calibration */ - size_t calib_channels; - size_t calib_resolution; - uint8_t *white_average_data; - uint8_t *dark_average_data; - uint16_t dark[3]; + SANE_String file_name = nullptr; + SANE_String calib_file = nullptr; - SANE_Bool already_initialized; - SANE_Int scanhead_position_in_steps; - SANE_Int lamp_off_time; + // if enabled, no calibration data will be loaded or saved to files + SANE_Int force_calibration = 0; + Genesys_Model *model = nullptr; - SANE_Bool read_active; - SANE_Bool parking; /**< signal wether the park command has been issued */ - SANE_Bool document; /**< for sheetfed scanner's, is TRUE when there - is a document in the scanner */ + Genesys_Register_Set reg[256] = {}; + Genesys_Register_Set calib_reg[256] = {}; + Genesys_Settings settings = {}; + Genesys_Frontend frontend = {}; + Genesys_Sensor sensor; + Genesys_Gpo gpo = {}; + Genesys_Motor motor = {}; + uint16_t slope_table0[256] = {}; + uint16_t slope_table1[256] = {}; + uint8_t control[6] = {}; + time_t init_date = 0; - Genesys_Buffer read_buffer; - Genesys_Buffer lines_buffer; - Genesys_Buffer shrink_buffer; - Genesys_Buffer out_buffer; - Genesys_Buffer binarize_buffer; /**< buffer for digital lineart from gray data */ - Genesys_Buffer local_buffer; /**< local buffer for gray data during dynamix lineart */ + size_t average_size = 0; + // number of pixels used during shading calibration + size_t calib_pixels = 0; + // number of lines used during shading calibration + size_t calib_lines = 0; + size_t calib_channels = 0; + size_t calib_resolution = 0; + uint8_t *white_average_data = nullptr; + uint8_t *dark_average_data = nullptr; + uint16_t dark[3] = {}; - size_t read_bytes_left; /**< bytes to read from scanner */ + SANE_Bool already_initialized = 0; + SANE_Int scanhead_position_in_steps = 0; + SANE_Int lamp_off_time = 0; - size_t total_bytes_read; /**< total bytes read sent to frontend */ - size_t total_bytes_to_read; /**< total bytes read to be sent to frontend */ - size_t wpl; /**< asic's word per line */ + SANE_Bool read_active = 0; + // signal wether the park command has been issued + SANE_Bool parking = 0; - Genesys_Current_Setup current_setup; /* contains the real used values */ + // for sheetfed scanner's, is TRUE when there is a document in the scanner + SANE_Bool document = 0; - /**< look up table used in dynamic rasterization */ - unsigned char lineart_lut[256]; + Genesys_Buffer read_buffer = {}; + Genesys_Buffer lines_buffer = {}; + Genesys_Buffer shrink_buffer = {}; + Genesys_Buffer out_buffer = {}; - Genesys_Calibration_Cache *calibration_cache; + // buffer for digital lineart from gray data + Genesys_Buffer binarize_buffer = {}; + // local buffer for gray data during dynamix lineart + Genesys_Buffer local_buffer = {}; - struct Genesys_Device *next; + // bytes to read from scanner + size_t read_bytes_left = 0; - SANE_Int ld_shift_r; /**< used red line-distance shift*/ - SANE_Int ld_shift_g; /**< used green line-distance shift*/ - SANE_Int ld_shift_b; /**< used blue line-distance shift*/ - int segnb; /**< number of segments composing the sensor */ - int line_interp; /**< number of lines used in line interpolation */ - int line_count; /**< number of scan lines used during scan */ - size_t bpl; /**< bytes per full scan widthline */ - size_t dist; /**< bytes distance between an odd and an even pixel */ - size_t len; /**< number of even pixels */ - size_t cur; /**< current pixel position within sub window */ - size_t skip; /**< number of bytes to skip at start of line */ - size_t *order; /**< array describing the order of the sub-segments of the sensor */ - Genesys_Buffer oe_buffer; /**< buffer to handle even/odd data */ + // total bytes read sent to frontend + size_t total_bytes_read = 0; + // total bytes read to be sent to frontend + size_t total_bytes_to_read = 0; + // asic's word per line + size_t wpl = 0; - SANE_Bool buffer_image; /**< when true the scanned picture is first buffered - * to allow software image enhancements */ - SANE_Byte *img_buffer; /**< image buffer where the scanned picture is stored */ + // contains the real used values + Genesys_Current_Setup current_setup = {}; - FILE *binary; /**< binary logger file */ + // look up table used in dynamic rasterization + unsigned char lineart_lut[256] = {}; + + Genesys_Calibration_Cache* calibration_cache = nullptr; + + Genesys_Device* next = nullptr; + + // used red line-distance shift + SANE_Int ld_shift_r = 0; + // used green line-distance shift + SANE_Int ld_shift_g = 0; + // used blue line-distance shift + SANE_Int ld_shift_b = 0; + // number of segments composing the sensor + int segnb = 0; + // number of lines used in line interpolation + int line_interp = 0; + // number of scan lines used during scan + int line_count = 0; + // bytes per full scan widthline + size_t bpl = 0; + // bytes distance between an odd and an even pixel + size_t dist = 0; + // number of even pixels + size_t len = 0; + // current pixel position within sub window + size_t cur = 0; + // number of bytes to skip at start of line + size_t skip = 0; + + // array describing the order of the sub-segments of the sensor + size_t* order = nullptr; + + // buffer to handle even/odd data + Genesys_Buffer oe_buffer = {}; + + // when true the scanned picture is first buffered to allow software image enhancements + SANE_Bool buffer_image = 0; + + // image buffer where the scanned picture is stored + SANE_Byte *img_buffer = nullptr; + + // binary logger file + FILE *binary = nullptr; }; typedef struct Genesys_USB_Device_Entry