Message ID | 1442877416-16659-1-git-send-email-simon.marchi@ericsson.com |
---|---|
State | New, archived |
Headers |
Received: (qmail 118636 invoked by alias); 21 Sep 2015 23:17:04 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: <gdb-patches.sourceware.org> List-Unsubscribe: <mailto:gdb-patches-unsubscribe-##L=##H@sourceware.org> List-Subscribe: <mailto:gdb-patches-subscribe@sourceware.org> List-Archive: <http://sourceware.org/ml/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-help@sourceware.org>, <http://sourceware.org/ml/#faqs> Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 118625 invoked by uid 89); 21 Sep 2015 23:17:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: usevmg21.ericsson.net Received: from usevmg21.ericsson.net (HELO usevmg21.ericsson.net) (198.24.6.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Mon, 21 Sep 2015 23:17:02 +0000 Received: from EUSAAHC001.ericsson.se (Unknown_Domain [147.117.188.75]) by usevmg21.ericsson.net (Symantec Mail Security) with SMTP id 7D.B4.26730.9B420065; Mon, 21 Sep 2015 17:39:37 +0200 (CEST) Received: from elxcz23q12-y4.dyn.mo.ca.am.ericsson.se (147.117.188.8) by smtps-am.internal.ericsson.com (147.117.188.75) with Microsoft SMTP Server (TLS) id 14.3.248.2; Mon, 21 Sep 2015 19:16:59 -0400 From: Simon Marchi <simon.marchi@ericsson.com> To: <gdb-patches@sourceware.org> CC: Simon Marchi <simon.marchi@ericsson.com> Subject: [PATCH] tui: Simplify tui_alloc_content Date: Mon, 21 Sep 2015 19:16:56 -0400 Message-ID: <1442877416-16659-1-git-send-email-simon.marchi@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain X-IsSubscribed: yes |
Commit Message
Simon Marchi
Sept. 21, 2015, 11:16 p.m. UTC
I stumbled upon this while doing some cxx-conversion work. Since the x-family alloc functions throw on failure, it is useless to test their result for failure. The else branch of != NULL is basically dead code. I changed the type of element_block_ptr to struct tui_win_element, which seems obvious (this is actually what raised the flag, casting the result of xmalloc to struct tui_win_element* wouldn't work). gdb/ChangeLog: * tui/tui-data.c (tui_alloc_content): Don't check xmalloc result. Change type of element_block_ptr. Change allocation to use XNEWVEC. --- gdb/tui/tui-data.c | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-)
Comments
* Simon Marchi <simon.marchi@ericsson.com> [2015-09-21 19:16:56 -0400]: > > gdb/ChangeLog: > > * tui/tui-data.c (tui_alloc_content): Don't check xmalloc > result. Change type of element_block_ptr. Change allocation to > use XNEWVEC. > --- > gdb/tui/tui-data.c | 41 ++++++++++++++--------------------------- > 1 file changed, 14 insertions(+), 27 deletions(-) > > diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c > index 2fcd547..ca7502d 100644 > --- a/gdb/tui/tui-data.c > +++ b/gdb/tui/tui-data.c > @@ -573,40 +573,27 @@ tui_win_content > tui_alloc_content (int num_elements, enum tui_win_type type) > { > tui_win_content content; > - char *element_block_ptr; > + struct tui_win_element *element_block_ptr; > int i; > > content = XNEWVEC (struct tui_win_element *, num_elements); > - if (content != NULL) > + > + /* > + * All windows, except the data window, can allocate the > + * elements in a chunk. The data window cannot because items > + * can be added/removed from the data display by the user at any > + * time. > + */ > + if (type != DATA_WIN) > { > - /* > - * All windows, except the data window, can allocate the > - * elements in a chunk. The data window cannot because items > - * can be added/removed from the data display by the user at any > - * time. > - */ > - if (type != DATA_WIN) > + element_block_ptr = XNEWVEC (struct tui_win_element, num_elements); > + for (i = 0; i < num_elements; i++) > { > - element_block_ptr = > - xmalloc (sizeof (struct tui_win_element) * num_elements); > - if (element_block_ptr != NULL) > - { > - for (i = 0; i < num_elements; i++) > - { > - content[i] = (struct tui_win_element *) element_block_ptr; > - init_content_element (content[i], type); > - element_block_ptr += sizeof (struct tui_win_element); > - } > - } > - else > - { > - xfree (content); > - content = (tui_win_content) NULL; > - } > + content[i] = element_block_ptr; > + init_content_element (content[i], type); > + element_block_ptr++; > } > } > - > - return content; > } Doesn't that leave the function without a return? Thanks, Andrew
On 15-09-22 03:50 AM, Andrew Burgess wrote: > * Simon Marchi <simon.marchi@ericsson.com> [2015-09-21 19:16:56 -0400]: > >> >> gdb/ChangeLog: >> >> * tui/tui-data.c (tui_alloc_content): Don't check xmalloc >> result. Change type of element_block_ptr. Change allocation to >> use XNEWVEC. >> --- >> gdb/tui/tui-data.c | 41 ++++++++++++++--------------------------- >> 1 file changed, 14 insertions(+), 27 deletions(-) >> >> diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c >> index 2fcd547..ca7502d 100644 >> --- a/gdb/tui/tui-data.c >> +++ b/gdb/tui/tui-data.c >> @@ -573,40 +573,27 @@ tui_win_content >> tui_alloc_content (int num_elements, enum tui_win_type type) >> { >> tui_win_content content; >> - char *element_block_ptr; >> + struct tui_win_element *element_block_ptr; >> int i; >> >> content = XNEWVEC (struct tui_win_element *, num_elements); >> - if (content != NULL) >> + >> + /* >> + * All windows, except the data window, can allocate the >> + * elements in a chunk. The data window cannot because items >> + * can be added/removed from the data display by the user at any >> + * time. >> + */ >> + if (type != DATA_WIN) >> { >> - /* >> - * All windows, except the data window, can allocate the >> - * elements in a chunk. The data window cannot because items >> - * can be added/removed from the data display by the user at any >> - * time. >> - */ >> - if (type != DATA_WIN) >> + element_block_ptr = XNEWVEC (struct tui_win_element, num_elements); >> + for (i = 0; i < num_elements; i++) >> { >> - element_block_ptr = >> - xmalloc (sizeof (struct tui_win_element) * num_elements); >> - if (element_block_ptr != NULL) >> - { >> - for (i = 0; i < num_elements; i++) >> - { >> - content[i] = (struct tui_win_element *) element_block_ptr; >> - init_content_element (content[i], type); >> - element_block_ptr += sizeof (struct tui_win_element); >> - } >> - } >> - else >> - { >> - xfree (content); >> - content = (tui_win_content) NULL; >> - } >> + content[i] = element_block_ptr; >> + init_content_element (content[i], type); >> + element_block_ptr++; >> } >> } >> - >> - return content; >> } > > Doesn't that leave the function without a return? > > Thanks, > Andrew Oops, of course the return needs to stay, otherwise it doesn't even build. I have it fixed locally but forgot to amend the commit. Thanks!
On 15-09-21 07:16 PM, Simon Marchi wrote: > I stumbled upon this while doing some cxx-conversion work. Since the > x-family alloc functions throw on failure, it is useless to test their > result for failure. The else branch of != NULL is basically dead code. > > I changed the type of element_block_ptr to struct tui_win_element, which > seems obvious (this is actually what raised the flag, casting the result > of xmalloc to struct tui_win_element* wouldn't work). > > gdb/ChangeLog: > > * tui/tui-data.c (tui_alloc_content): Don't check xmalloc > result. Change type of element_block_ptr. Change allocation to > use XNEWVEC. > --- > gdb/tui/tui-data.c | 41 ++++++++++++++--------------------------- > 1 file changed, 14 insertions(+), 27 deletions(-) > > diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c > index 2fcd547..ca7502d 100644 > --- a/gdb/tui/tui-data.c > +++ b/gdb/tui/tui-data.c > @@ -573,40 +573,27 @@ tui_win_content > tui_alloc_content (int num_elements, enum tui_win_type type) > { > tui_win_content content; > - char *element_block_ptr; > + struct tui_win_element *element_block_ptr; > int i; > > content = XNEWVEC (struct tui_win_element *, num_elements); > - if (content != NULL) > + > + /* > + * All windows, except the data window, can allocate the > + * elements in a chunk. The data window cannot because items > + * can be added/removed from the data display by the user at any > + * time. > + */ > + if (type != DATA_WIN) > { > - /* > - * All windows, except the data window, can allocate the > - * elements in a chunk. The data window cannot because items > - * can be added/removed from the data display by the user at any > - * time. > - */ > - if (type != DATA_WIN) > + element_block_ptr = XNEWVEC (struct tui_win_element, num_elements); > + for (i = 0; i < num_elements; i++) > { > - element_block_ptr = > - xmalloc (sizeof (struct tui_win_element) * num_elements); > - if (element_block_ptr != NULL) > - { > - for (i = 0; i < num_elements; i++) > - { > - content[i] = (struct tui_win_element *) element_block_ptr; > - init_content_element (content[i], type); > - element_block_ptr += sizeof (struct tui_win_element); > - } > - } > - else > - { > - xfree (content); > - content = (tui_win_content) NULL; > - } > + content[i] = element_block_ptr; > + init_content_element (content[i], type); > + element_block_ptr++; > } > } > - > - return content; > } Ping.
On 10/26/2015 02:11 PM, Simon Marchi wrote: > On 15-09-21 07:16 PM, Simon Marchi wrote: >> I stumbled upon this while doing some cxx-conversion work. Since the >> x-family alloc functions throw on failure, it is useless to test their >> result for failure. The else branch of != NULL is basically dead code. >> >> I changed the type of element_block_ptr to struct tui_win_element, which >> seems obvious (this is actually what raised the flag, casting the result >> of xmalloc to struct tui_win_element* wouldn't work). >> >> gdb/ChangeLog: >> >> * tui/tui-data.c (tui_alloc_content): Don't check xmalloc >> result. Change type of element_block_ptr. Change allocation to >> use XNEWVEC. >> --- >> gdb/tui/tui-data.c | 41 ++++++++++++++--------------------------- >> 1 file changed, 14 insertions(+), 27 deletions(-) >> >> diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c >> index 2fcd547..ca7502d 100644 >> --- a/gdb/tui/tui-data.c >> +++ b/gdb/tui/tui-data.c >> @@ -573,40 +573,27 @@ tui_win_content >> tui_alloc_content (int num_elements, enum tui_win_type type) >> { >> tui_win_content content; >> - char *element_block_ptr; >> + struct tui_win_element *element_block_ptr; >> int i; >> >> content = XNEWVEC (struct tui_win_element *, num_elements); >> - if (content != NULL) >> + >> + /* >> + * All windows, except the data window, can allocate the >> + * elements in a chunk. The data window cannot because items >> + * can be added/removed from the data display by the user at any >> + * time. >> + */ >> + if (type != DATA_WIN) >> { >> - /* >> - * All windows, except the data window, can allocate the >> - * elements in a chunk. The data window cannot because items >> - * can be added/removed from the data display by the user at any >> - * time. >> - */ >> - if (type != DATA_WIN) >> + element_block_ptr = XNEWVEC (struct tui_win_element, num_elements); >> + for (i = 0; i < num_elements; i++) >> { >> - element_block_ptr = >> - xmalloc (sizeof (struct tui_win_element) * num_elements); >> - if (element_block_ptr != NULL) >> - { >> - for (i = 0; i < num_elements; i++) >> - { >> - content[i] = (struct tui_win_element *) element_block_ptr; >> - init_content_element (content[i], type); >> - element_block_ptr += sizeof (struct tui_win_element); >> - } >> - } >> - else >> - { >> - xfree (content); >> - content = (tui_win_content) NULL; >> - } >> + content[i] = element_block_ptr; >> + init_content_element (content[i], type); >> + element_block_ptr++; >> } >> } >> - >> - return content; >> } > > Ping. > OK with the return statement kept. Thanks, Pedro Alves
On 15-10-26 12:33 PM, Pedro Alves wrote: > OK with the return statement kept. > > Thanks, > Pedro Alves Pushed with the return statement kept, thanks.
diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c index 2fcd547..ca7502d 100644 --- a/gdb/tui/tui-data.c +++ b/gdb/tui/tui-data.c @@ -573,40 +573,27 @@ tui_win_content tui_alloc_content (int num_elements, enum tui_win_type type) { tui_win_content content; - char *element_block_ptr; + struct tui_win_element *element_block_ptr; int i; content = XNEWVEC (struct tui_win_element *, num_elements); - if (content != NULL) + + /* + * All windows, except the data window, can allocate the + * elements in a chunk. The data window cannot because items + * can be added/removed from the data display by the user at any + * time. + */ + if (type != DATA_WIN) { - /* - * All windows, except the data window, can allocate the - * elements in a chunk. The data window cannot because items - * can be added/removed from the data display by the user at any - * time. - */ - if (type != DATA_WIN) + element_block_ptr = XNEWVEC (struct tui_win_element, num_elements); + for (i = 0; i < num_elements; i++) { - element_block_ptr = - xmalloc (sizeof (struct tui_win_element) * num_elements); - if (element_block_ptr != NULL) - { - for (i = 0; i < num_elements; i++) - { - content[i] = (struct tui_win_element *) element_block_ptr; - init_content_element (content[i], type); - element_block_ptr += sizeof (struct tui_win_element); - } - } - else - { - xfree (content); - content = (tui_win_content) NULL; - } + content[i] = element_block_ptr; + init_content_element (content[i], type); + element_block_ptr++; } } - - return content; }