]> granicus.if.org Git - re2c/commitdiff
- Provide memory management template helper class free_list
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 5 Jun 2006 22:27:32 +0000 (22:27 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Mon, 5 Jun 2006 22:27:32 +0000 (22:27 +0000)
- Fix memleaks (for RegExp using free_list)
- Fix memleaks (for Range using free_list)

re2c/main.cc
re2c/re.h

index 3f46db617e68bed6d1d4fb5e83f92260317ca729..b801c58a4e5a06b25bdf784ee3cb94c5c44c3a2c 100644 (file)
@@ -58,6 +58,9 @@ uint next_fill_index = 0;
 uint last_fill_index = 0;
 std::set<uint> vUsedLabels;
 
+free_list<RegExp*> RegExp::vFreeList;
+free_list<Range*>  Range::vFreeList;
+
 using namespace std;
 
 static char *opt_arg = NULL;
index b436375447194919e2bbf852c050539af77daa22..7b48570d51a5ec0a842ab3fc2a978dca0e392560 100644 (file)
--- a/re2c/re.h
+++ b/re2c/re.h
@@ -3,6 +3,7 @@
 #define _re_h
 
 #include <iostream>
+#include <set>
 #include "token.h"
 #include "ins.h"
 #include "globals.h"
 namespace re2c
 {
 
+template<class _Ty>
+class free_list: protected std::set<_Ty>
+{
+public:
+       typedef typename std::set<_Ty>::iterator   iterator;
+       typedef typename std::set<_Ty>::size_type  size_type;
+       typedef typename std::set<_Ty>::key_type   key_type;
+       
+       free_list(): in_clear(false)
+       {
+       }
+       
+       using std::set<_Ty>::insert;
+
+       size_type erase(const key_type& key)
+       {
+               if (!in_clear)
+               {
+                       return std::set<_Ty>::erase(key);
+               }
+               return 0;
+       }
+       
+       void clear()
+       {
+               in_clear = true;
+
+               for(iterator it = this->begin(); it != this->end(); ++it)
+               {
+                       delete *it;
+               }
+               std::set<_Ty>::clear();
+               
+               in_clear = false;
+       }
+
+       ~free_list()
+       {
+               clear();
+       }
+
+protected:
+       bool in_clear;
+};
+
 typedef struct extop
 {
        char op;
@@ -46,12 +92,23 @@ public:
        Range   *next;
        uint    lb, ub;         // [lb,ub)
 
+       static free_list<Range*> vFreeList;
+
 public:
        Range(uint l, uint u) : next(NULL), lb(l), ub(u)
-       { }
+       {
+               vFreeList.insert(this);
+       }
 
        Range(Range &r) : next(NULL), lb(r.lb), ub(r.ub)
-       { }
+       {
+               vFreeList.insert(this);
+       }
+
+       ~Range()
+       {
+               vFreeList.erase(this);
+       }
 
        friend std::ostream& operator<<(std::ostream&, const Range&);
        friend std::ostream& operator<<(std::ostream&, const Range*);
@@ -67,13 +124,20 @@ class RegExp
 
 public:
        uint    size;
+       
+       static free_list<RegExp*> vFreeList;
 
 public:
        RegExp() : size(0)
        {
+               vFreeList.insert(this);
+       }
+
+       virtual ~RegExp()
+       {
+               vFreeList.erase(this);
        }
 
-       virtual ~RegExp() {}
        virtual const char *typeOf() = 0;
        RegExp *isA(const char *t)
        {