C++ PATCH: Plug memory leaks

Mark Mitchell mark@codesourcery.com
Wed Apr 5 17:46:00 GMT 2000


Here's the C++ follow-on to the previous patch.  There are a couple of
other minor fixes as well.  Notably, we weren't setting TREE_READONLY
for the `this' pointer, even though it's known to be `const'.  That's
not a bug, but it could pessimize some code.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

2000-04-05  Mark Mitchell  <mark@codesourcery.com>

	* call.c: Don't include obstack.h.  Include ggc.h.
	(obstack_chunk_alloc): Don't define.
	(obstack_chunk_free): Likewise.
	(add_candidate): Allocate the z_candidate with ggc_alloc_obj.
	* decl.c (push_switch): Use xmalloc to allocate the cp_switch.
	(pop_switch): Free it.

	* decl2.c (grokclassfn): Set TREE_READONLY for PARM_DECLs.

	* dump.c (dequeue_and_dump): Don't try to print the bit_position
	if we don't have a DECL_FIELD_OFFSET.

Index: cp/call.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/call.c,v
retrieving revision 1.205
diff -c -p -r1.205 call.c
*** call.c	2000/03/23 00:40:58	1.205
--- call.c	2000/04/06 00:37:18
*************** Boston, MA 02111-1307, USA.  */
*** 34,44 ****
  #include "toplev.h"
  #include "defaults.h"
  #include "expr.h"
  
- #include "obstack.h"
- #define obstack_chunk_alloc xmalloc
- #define obstack_chunk_free free
- 
  extern int inhibit_warnings;
  
  static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
--- 34,41 ----
  #include "toplev.h"
  #include "defaults.h"
  #include "expr.h"
+ #include "ggc.h"
  
  extern int inhibit_warnings;
  
  static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
*************** add_candidate (candidates, fn, convs, vi
*** 1232,1249 ****
       tree fn, convs;
       int viable;
  {
-   /* FIXME: This is a memory leak.  Presumably, we should use
-      ggc_alloc instead.  */
    struct z_candidate *cand
!     = (struct z_candidate *) expralloc (sizeof (struct z_candidate));
  
    cand->fn = fn;
    cand->convs = convs;
-   cand->second_conv = NULL_TREE;
    cand->viable = viable;
-   cand->basetype_path = NULL_TREE;
-   cand->template = NULL_TREE;
-   cand->warnings = NULL_TREE;
    cand->next = candidates;
  
    return cand;
--- 1229,1240 ----
       tree fn, convs;
       int viable;
  {
    struct z_candidate *cand
!     = (struct z_candidate *) ggc_alloc_obj (sizeof (struct z_candidate), 1);
  
    cand->fn = fn;
    cand->convs = convs;
    cand->viable = viable;
    cand->next = candidates;
  
    return cand;
Index: cp/decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl.c,v
retrieving revision 1.579
diff -c -p -r1.579 decl.c
*** decl.c	2000/03/27 01:26:17	1.579
--- decl.c	2000/04/06 00:37:30
*************** void
*** 4938,4944 ****
  push_switch ()
  {
    struct cp_switch *p
!     = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
    p->level = current_binding_level;
    p->next = switch_stack;
    switch_stack = p;
--- 4938,4944 ----
  push_switch ()
  {
    struct cp_switch *p
!     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
    p->level = current_binding_level;
    p->next = switch_stack;
    switch_stack = p;
*************** push_switch ()
*** 4947,4953 ****
--- 4947,4957 ----
  void
  pop_switch ()
  {
+   struct cp_switch *cs;
+   
+   cs = switch_stack;
    switch_stack = switch_stack->next;
+   free (cs);
  }
  
  /* Note that we've seen a definition of a case label, and complain if this
Index: cp/decl2.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl2.c,v
retrieving revision 1.323
diff -c -p -r1.323 decl2.c
*** decl2.c	2000/04/04 18:13:19	1.323
--- decl2.c	2000/04/06 00:37:33
*************** grokclassfn (ctype, function, flags, qua
*** 1009,1017 ****
        /* Right now we just make this a pointer.  But later
  	 we may wish to make it special.  */
        tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
  
!       tree parm = build_decl (PARM_DECL, this_identifier,
!                          cp_build_qualified_type (type, this_quals | TYPE_QUAL_CONST));
        /* Mark the artificial `this' parameter as "artificial".  */
        SET_DECL_ARTIFICIAL (parm);
        DECL_ARG_TYPE (parm) = type;
--- 1009,1024 ----
        /* Right now we just make this a pointer.  But later
  	 we may wish to make it special.  */
        tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
+       tree qual_type;
+       tree parm;
  
!       /* The `this' parameter is implicitly `const'; it cannot be
! 	 assigned to.  */
!       this_quals |= TYPE_QUAL_CONST;
!       qual_type = cp_build_qualified_type (type, this_quals);
!       parm = build_decl (PARM_DECL, this_identifier, qual_type);
!       c_apply_type_quals_to_decl (this_quals, parm);
! 
        /* Mark the artificial `this' parameter as "artificial".  */
        SET_DECL_ARTIFICIAL (parm);
        DECL_ARG_TYPE (parm) = type;
Index: cp/dump.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/dump.c,v
retrieving revision 1.40
diff -c -p -r1.40 dump.c
*** dump.c	2000/03/26 03:05:49	1.40
--- dump.c	2000/04/06 00:37:34
*************** dequeue_and_dump (di)
*** 550,556 ****
  	{
  	  if (DECL_C_BIT_FIELD (t))
  	    dump_string (di, "bitfield");
! 	  dump_child ("bpos", bit_position (t));
  	}
        break;
  
--- 550,557 ----
  	{
  	  if (DECL_C_BIT_FIELD (t))
  	    dump_string (di, "bitfield");
! 	  if (DECL_FIELD_OFFSET (t))
! 	    dump_child ("bpos", bit_position (t));
  	}
        break;
  


More information about the Gcc-patches mailing list