From 033827b434c950fa8532cba60aea475da6e08e4b Mon Sep 17 00:00:00 2001 From: helly Date: Mon, 5 Jun 2006 22:27:32 +0000 Subject: [PATCH] - Provide memory management template helper class free_list - Fix memleaks (for RegExp using free_list) - Fix memleaks (for Range using free_list) --- re2c/main.cc | 3 +++ re2c/re.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/re2c/main.cc b/re2c/main.cc index 3f46db61..b801c58a 100644 --- a/re2c/main.cc +++ b/re2c/main.cc @@ -58,6 +58,9 @@ uint next_fill_index = 0; uint last_fill_index = 0; std::set vUsedLabels; +free_list RegExp::vFreeList; +free_list Range::vFreeList; + using namespace std; static char *opt_arg = NULL; diff --git a/re2c/re.h b/re2c/re.h index b4363754..7b48570d 100644 --- a/re2c/re.h +++ b/re2c/re.h @@ -3,6 +3,7 @@ #define _re_h #include +#include #include "token.h" #include "ins.h" #include "globals.h" @@ -10,6 +11,51 @@ namespace re2c { +template +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 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 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) { -- 2.50.1