From 4b7e7087aa5f37fc5621b50b6c4048508018e688 Mon Sep 17 00:00:00 2001 From: Stanislav Shwartsman Date: Tue, 30 May 2006 17:01:27 +0000 Subject: [PATCH] Handle more fields memory management insie the bx_param_c. Remove more strdups --- bochs/config.cc | 55 ++++++-------------- bochs/cpu/init.cc | 4 +- bochs/gui/siminterface.cc | 107 ++++++++++++++++++++++++-------------- bochs/gui/siminterface.h | 52 +++++++++--------- bochs/logio.cc | 11 ++-- 5 files changed, 118 insertions(+), 111 deletions(-) diff --git a/bochs/config.cc b/bochs/config.cc index 022b73f8d..bf31b57b2 100755 --- a/bochs/config.cc +++ b/bochs/config.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// $Id: config.cc,v 1.105 2006-05-29 22:33:38 sshwarts Exp $ +// $Id: config.cc,v 1.106 2006-05-30 17:01:27 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // Copyright (C) 2002 MandrakeSoft S.A. @@ -461,7 +461,7 @@ void bx_init_options() path = new bx_param_filename_c(optnum1, "path", "Path", - strdup(descr), + descr, "", BX_PATHNAME_LEN); sprintf(label, "Name of optional ROM image #%d", i+1); strcat(label, " : %s"); @@ -470,7 +470,7 @@ void bx_init_options() optaddr = new bx_param_num_c(optnum1, "addr", "Address", - strdup(descr), + descr, 0, BX_MAX_BIT32U, 0); optaddr->set_base(16); @@ -490,7 +490,7 @@ void bx_init_options() path = new bx_param_filename_c(optnum2, "path", "Path", - strdup(descr), + descr, "", BX_PATHNAME_LEN); sprintf(label, "Name of optional RAM image #%d", i+1); strcat(label, " : %s"); @@ -499,7 +499,7 @@ void bx_init_options() optaddr = new bx_param_num_c(optnum2, "addr", "Address", - strdup(descr), + descr, 0, BX_MAX_BIT32U, 0); optaddr->set_base(16); @@ -585,8 +585,8 @@ void bx_init_options() sprintf (label, "PCI slot #%d device", i+1); bx_param_string_c *devname = new bx_param_string_c(slot, name, - strdup(label), - strdup(descr), + label, + descr, "", BX_PATHNAME_LEN); // add to deplist *pci_deps_ptr++ = devname; @@ -838,8 +838,8 @@ void bx_init_options() sprintf(descr, "Name of drive #%d in boot sequence (A, C or CD)", i+1); bx_param_enum_c *bootdrive = new bx_param_enum_c(boot_params, name, - strdup(label), - strdup(descr), + label, + descr, &bochs_bootdisk_names[(i==0)?BX_BOOT_FLOPPYA:BX_BOOT_NONE], (i==0)?BX_BOOT_FLOPPYA:BX_BOOT_NONE, (i==0)?BX_BOOT_FLOPPYA:BX_BOOT_NONE); @@ -1233,17 +1233,11 @@ void bx_init_options() menu->get_options()->set(bx_list_c::SERIES_ASK); sprintf(label, "Enable parallel port #%d", i+1); sprintf(descr, "Controls whether parallel port #%d is installed or not", i+1); - enabled = new bx_param_bool_c(menu, - "enabled", - strdup(label), - strdup(descr), + enabled = new bx_param_bool_c(menu, "enabled", label, descr, (i==0)? 1 : 0); // only enable #1 by default sprintf(label, "Parallel port #%d output file", i+1); sprintf(descr, "Data written to parport#%d by the guest OS is written to this file", i+1); - path = new bx_param_filename_c(menu, - "outfile", - strdup(label), - strdup(descr), + path = new bx_param_filename_c(menu, "outfile", label, descr, "", BX_PATHNAME_LEN); deplist = new bx_list_c(NULL, 1); deplist->add(path); @@ -1270,27 +1264,16 @@ void bx_init_options() menu->get_options()->set(bx_list_c::SERIES_ASK); sprintf(label, "Enable serial port #%d (COM%d)", i+1, i+1); sprintf(descr, "Controls whether COM%d is installed or not", i+1); - enabled = new bx_param_bool_c(menu, - "enabled", - strdup(label), - strdup(descr), + enabled = new bx_param_bool_c(menu, "enabled", label, descr, (i==0)?1 : 0); // only enable the first by default sprintf(label, "I/O mode of the serial device for COM%d", i+1); sprintf(descr, "The mode can be one these: 'null', 'file', 'term', 'raw', 'mouse', 'socket'"); - mode = new bx_param_enum_c(menu, - "mode", - strdup(label), - strdup(descr), - serial_mode_list, - 0, - 0); + mode = new bx_param_enum_c(menu, "mode", label, descr, + serial_mode_list, 0, 0); mode->set_ask_format("Choose I/O mode of the serial device [%s] "); sprintf(label, "Pathname of the serial device for COM%d", i+1); sprintf(descr, "The path can be a real serial device or a pty (X/Unix only)"); - path = new bx_param_filename_c(menu, - "dev", - strdup(label), - strdup(descr), + path = new bx_param_filename_c(menu, "dev", label, descr, "", BX_PATHNAME_LEN); deplist = new bx_list_c(NULL, 2); deplist->add(mode); @@ -1311,11 +1294,7 @@ void bx_init_options() menu->set_enabled(BX_SUPPORT_PCIUSB); sprintf(label, "Enable usb hub #%d", i+1); sprintf(descr, "Controls whether %s is installed or not", group); - enabled = new bx_param_bool_c(menu, - "enabled", - strdup(label), - strdup(descr), - 0); + enabled = new bx_param_bool_c(menu, "enabled", label, descr, 0); enabled->set_enabled(BX_SUPPORT_PCIUSB); port = new bx_param_string_c(menu, "port1", @@ -1747,7 +1726,7 @@ char *bx_find_bochsrc () } assert (fd != NULL && rcfile[0] != 0); fclose (fd); - return strdup (rcfile); + return strdup(rcfile); } static int parse_bochsrc(const char *rcfile) diff --git a/bochs/cpu/init.cc b/bochs/cpu/init.cc index 74a3dd77b..45e0fcbad 100644 --- a/bochs/cpu/init.cc +++ b/bochs/cpu/init.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// $Id: init.cc,v 1.110 2006-05-29 22:33:38 sshwarts Exp $ +// $Id: init.cc,v 1.111 2006-05-30 17:01:27 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // Copyright (C) 2001 MandrakeSoft S.A. @@ -392,7 +392,7 @@ void BX_CPU_C::register_state(void) BXRS_PARAM_SPECIAL32(list, cpu_version, param_save_handler, param_restore_handler); BXRS_PARAM_SPECIAL32(list, cpuid_std, param_save_handler, param_restore_handler); BXRS_PARAM_SPECIAL32(list, cpuid_ext, param_save_handler, param_restore_handler); - BXRS_HEX_PARAM_SIMPLE(list, cpu_mode); + BXRS_DEC_PARAM_SIMPLE(list, cpu_mode); BXRS_HEX_PARAM_SIMPLE(list, inhibit_mask); #if BX_SUPPORT_X86_64 BXRS_HEX_PARAM_SIMPLE(list, RAX); diff --git a/bochs/gui/siminterface.cc b/bochs/gui/siminterface.cc index 7fa17d5eb..810231dd7 100644 --- a/bochs/gui/siminterface.cc +++ b/bochs/gui/siminterface.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// $Id: siminterface.cc,v 1.155 2006-05-30 16:05:50 vruppert Exp $ +// $Id: siminterface.cc,v 1.156 2006-05-30 17:01:27 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // See siminterface.h for description of the siminterface concept. @@ -1176,30 +1176,39 @@ void bx_real_sim_c::save_sr_param(FILE *fp, bx_param_c *node, const char *sr_pat // define methods of bx_param_* and family ///////////////////////////////////////////////////////////////////////// -bx_object_c::bx_object_c(Bit32u id) -{ - this->id = id; - this->type = BXT_OBJECT; -} - -void bx_object_c::set_type(bx_objtype type) -{ - this->type = type; -} - const char* bx_param_c::default_text_format = NULL; -bx_param_c::bx_param_c(Bit32u id, const char *param_name, char *description) +bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_desc) : bx_object_c(id) { set_type(BXT_PARAM); this->name = new char[strlen(param_name)+1]; strcpy(this->name, param_name); - this->description = description; + this->description = NULL; + set_description(param_desc); + this->label = NULL; this->text_format = default_text_format; this->long_text_format = default_text_format; this->ask_format = NULL; + this->group_name = NULL; + this->runtime_param = 0; + this->enabled = 1; + this->parent = NULL; +} + +bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_label, const char *param_desc) + : bx_object_c(id) +{ + set_type(BXT_PARAM); + this->name = new char[strlen(param_name)+1]; + strcpy(this->name, param_name); + this->description = NULL; + set_description(param_desc); this->label = NULL; + set_label(param_label); + this->text_format = default_text_format; + this->long_text_format = default_text_format; + this->ask_format = NULL; this->group_name = NULL; this->runtime_param = 0; this->enabled = 1; @@ -1209,6 +1218,30 @@ bx_param_c::bx_param_c(Bit32u id, const char *param_name, char *description) bx_param_c::~bx_param_c() { delete [] name; + delete [] label; + delete [] description; +} + +void bx_param_c::set_description(const char *text) +{ + delete [] this->description; + if (text) { + this->description = new char[strlen(text)+1]; + strcpy(this->description, text); + } else { + this->description = NULL; + } +} + +void bx_param_c::set_label(const char *text) +{ + delete [] this->label; + if (text) { + this->label = new char[strlen(text)+1]; + strcpy(this->label, text); + } else { + this->label = NULL; + } } int bx_param_c::get_param_path(char *path_out, int maxlen) @@ -1236,14 +1269,13 @@ const char* bx_param_c::set_default_format(const char *f) bx_param_num_c::bx_param_num_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, Bit64s min, Bit64s max, Bit64s initial_val, bx_bool is_shadow) - : bx_param_c(SIM->gen_param_id(), name, description) + : bx_param_c(SIM->gen_param_id(), name, label, description) { set_type(BXT_PARAM_NUM); - this->label = label; this->min = min; this->max = max; this->initial_val = initial_val; @@ -1594,8 +1626,8 @@ void bx_shadow_num_c::reset() bx_param_bool_c::bx_param_bool_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, Bit64s initial_val, bx_bool is_shadow) : bx_param_num_c(parent, name, label, description, 0, 1, initial_val, is_shadow) @@ -1605,7 +1637,7 @@ bx_param_bool_c::bx_param_bool_c(bx_param_c *parent, bx_shadow_bool_c::bx_shadow_bool_c(bx_param_c *parent, const char *name, - char *label, + const char *label, bx_bool *ptr_to_real_val, Bit8u bitnum) : bx_param_bool_c(parent, name, label, NULL, (Bit64s) *ptr_to_real_val, 1) @@ -1650,8 +1682,8 @@ void bx_shadow_bool_c::set(Bit64s newval) bx_param_enum_c::bx_param_enum_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, char **choices, Bit64s initial_val, Bit64s value_base) @@ -1689,14 +1721,13 @@ bx_bool bx_param_enum_c::set_by_name(const char *string) bx_param_string_c::bx_param_string_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, const char *initial_val, int maxsize) - : bx_param_c(SIM->gen_param_id(), name, description) + : bx_param_c(SIM->gen_param_id(), name, label, description) { set_type(BXT_PARAM_STRING); - this->label = label; if (maxsize < 0) maxsize = strlen(initial_val) + 1; this->val = new char[maxsize]; @@ -1718,9 +1749,9 @@ bx_param_string_c::bx_param_string_c(bx_param_c *parent, bx_param_filename_c::bx_param_filename_c(bx_param_c *parent, const char *name, - char *label, - char *description, - char *initial_val, + const char *label, + const char *description, + const char *initial_val, int maxsize) : bx_param_string_c(parent, name, label, description, initial_val, maxsize) { @@ -1729,14 +1760,14 @@ bx_param_filename_c::bx_param_filename_c(bx_param_c *parent, bx_param_string_c::~bx_param_string_c() { - if (this->val != NULL) delete [] this->val; - if (this->initial_val != NULL) delete [] this->initial_val; - if (this->options != NULL) delete this->options; + if (val != NULL) delete [] val; + if (initial_val != NULL) delete [] initial_val; + if (options != NULL) delete options; } void bx_param_string_c::reset() { - strncpy(this->val, this->initial_val, maxsize); + strncpy(val, initial_val, maxsize); } void bx_param_string_c::set_handler(param_string_event_handler handler) @@ -1894,10 +1925,10 @@ bx_list_c::bx_list_c(bx_param_c *parent, const char *name, char *title, bx_param bx_list_c::~bx_list_c() { - if (this->list) delete [] this->list; - if (this->title != NULL) delete this->title; - if (this->options != NULL) delete this->options; - if (this->choice != NULL) delete this->choice; + if (list != NULL) delete [] list; + if (title != NULL) delete title; + if (options != NULL) delete options; + if (choice != NULL) delete choice; } void bx_list_c::init(const char *list_title) diff --git a/bochs/gui/siminterface.h b/bochs/gui/siminterface.h index c2a8d38fa..940544e26 100644 --- a/bochs/gui/siminterface.h +++ b/bochs/gui/siminterface.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// $Id: siminterface.h,v 1.193 2006-05-29 22:33:38 sshwarts Exp $ +// $Id: siminterface.h,v 1.194 2006-05-30 17:01:27 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // Intro to siminterface by Bryce Denney: @@ -578,12 +578,12 @@ private: Bit32u id; bx_objtype type; protected: - void set_type(bx_objtype type); + void set_type(bx_objtype _type) { type = _type; } public: - bx_object_c(Bit32u id); + bx_object_c(Bit32u _id): id(_id), type(BXT_OBJECT) {} virtual ~bx_object_c() {} - Bit32u get_id() { return id; } - Bit8u get_type() { return type; } + Bit32u get_id() const { return id; } + Bit8u get_type() const { return type; } }; #define BASE_DEC 10 @@ -603,7 +603,8 @@ protected: int runtime_param; int enabled; public: - bx_param_c(Bit32u id, const char *name, char *description); + bx_param_c(Bit32u id, const char *name, const char *description); + bx_param_c(Bit32u id, const char *name, const char *label, const char *description); virtual ~bx_param_c(); bx_param_c *get_parent() { return (bx_param_c *) parent; } int get_param_path(char *path_out, int maxlen); @@ -613,14 +614,15 @@ public: const char *get_long_format() const {return long_text_format;} void set_ask_format(char *format) {ask_format = format; } char *get_ask_format() {return ask_format;} - void set_label(char *text) {label = text;} - char *get_label() {return label;} + void set_label(const char *text); + void set_description(const char *text); + const char *get_label() const {return label;} void set_runtime_param(int val) { runtime_param = val; } int get_runtime_param() { return runtime_param; } void set_group(char *group) {group_name = group;} char *get_group() {return group_name;} const char *get_name() const { return name; } - char *get_description() { return description; } + const char *get_description() const { return description; } int get_enabled() const { return enabled; } virtual void set_enabled(int enabled) { this->enabled = enabled; } virtual void reset() {} @@ -676,8 +678,8 @@ public: } bx_numopt_bits; bx_param_num_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, Bit64s min, Bit64s max, Bit64s initial_val, bx_bool is_shadow = 0); virtual void reset(); @@ -694,14 +696,14 @@ public: virtual void set(Bit64s val); void set_base(int base) { this->base = base; } void set_initial_val(Bit64s initial_val); - int get_base() { return base; } + int get_base() const { return base; } void set_range(Bit64u min, Bit64u max); Bit64s get_min() { return min; } Bit64s get_max() { return max; } static Bit32u set_default_base(Bit32u val); static Bit32u get_default_base() { return default_base; } void set_options(Bit32u options) { this->options = options; } - Bit32u get_options() { return options; } + Bit32u get_options() const { return options; } #if BX_USE_TEXTCONFIG virtual void text_print(FILE *fp); virtual int text_ask(FILE *fpin, FILE *fpout); @@ -778,8 +780,8 @@ class BOCHSAPI bx_param_bool_c : public bx_param_num_c { public: bx_param_bool_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, Bit64s initial_val, bx_bool is_shadow = 0); #if BX_USE_TEXTCONFIG @@ -796,7 +798,7 @@ class BOCHSAPI bx_shadow_bool_c : public bx_param_bool_c { public: bx_shadow_bool_c(bx_param_c *parent, const char *name, - char *label, + const char *label, bx_bool *ptr_to_real_val, Bit8u bitnum = 0); bx_shadow_bool_c(bx_param_c *parent, @@ -813,8 +815,8 @@ class BOCHSAPI bx_param_enum_c : public bx_param_num_c { public: bx_param_enum_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, char **choices, Bit64s initial_val, Bit64s value_base = 0); @@ -847,8 +849,8 @@ public: } bx_string_opt_bits; bx_param_string_c(bx_param_c *parent, const char *name, - char *label, - char *description, + const char *label, + const char *description, const char *initial_val, int maxsize=-1); virtual ~bx_param_string_c(); @@ -878,9 +880,9 @@ class BOCHSAPI bx_param_filename_c : public bx_param_string_c { public: bx_param_filename_c(bx_param_c *parent, const char *name, - char *label, - char *description, - char *initial_val, + const char *label, + const char *description, + const char *initial_val, int maxsize=-1); }; @@ -894,7 +896,7 @@ public: Bit8u *ptr_to_data, Bit32u data_size); Bit8u *getptr() {return data_ptr;} - Bit32u get_size() {return data_size;} + Bit32u get_size() const {return data_size;} }; #endif @@ -950,7 +952,7 @@ public: void add(bx_param_c *param); bx_param_c *get(int index); bx_param_c *get_by_name(const char *name); - int get_size() { return size; } + int get_size() const { return size; } bx_param_num_c *get_options() { return options; } bx_param_num_c *get_choice() { return choice; } bx_param_string_c *get_title() { return title; } diff --git a/bochs/logio.cc b/bochs/logio.cc index 819b6b7ec..1f3d2b70e 100644 --- a/bochs/logio.cc +++ b/bochs/logio.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// $Id: logio.cc,v 1.53 2006-03-06 22:32:02 sshwarts Exp $ +// $Id: logio.cc,v 1.54 2006-05-30 17:01:27 sshwarts Exp $ ///////////////////////////////////////////////////////////////////////// // // Copyright (C) 2001 MandrakeSoft S.A. @@ -281,11 +281,7 @@ logfunctions::logfunctions(iofunc_t *iofunc) logfunctions::~logfunctions(void) { - if ( this->prefix ) - { - free(this->prefix); - this->prefix = NULL; - } + if (this->prefix) free(this->prefix); } void logfunctions::setio(iofunc_t *i) @@ -600,11 +596,10 @@ logfunc_t *genlog = NULL; void bx_center_print (FILE *file, char *line, int maxwidth) { - int imax; int len = strlen(line); if (len > maxwidth) BX_PANIC (("bx_center_print: line is too long: '%s'", line)); - imax = (maxwidth - len) >> 1; + int imax = (maxwidth - len) >> 1; for (int i=0; i