diff libinterp/parse-tree/lex.h @ 22858:a183a0929653

eliminate size limit for token cache; provide destructor for class * lex.h (token_cache::~token_cache): New destructor. Clear cache so that tokens are properly deleted when the token cache is deleted. (token_cache::sz): Delete data member and all uses.
author John W. Eaton <jwe@octave.org>
date Fri, 02 Dec 2016 14:35:57 -0500
parents d90e0c79aa45
children 89756f2f085b
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.h	Fri Dec 02 14:16:43 2016 -0500
+++ b/libinterp/parse-tree/lex.h	Fri Dec 02 14:35:57 2016 -0500
@@ -182,15 +182,23 @@
     public:
 
       // Store an "unlimited" number of tokens.
-      token_cache (size_t sz_arg = std::numeric_limits<size_t>::max ())
-        : buffer (), sz (sz_arg)
-      { }
+
+      // Tokens are allocated with new.  Delete them when they are
+      // removed from the cache.
+      //
+      // One of the reasons for using this class instead of std::deque
+      // directly is that we can ensure that memory is cleaned up
+      // properly.  It's more tedious to do that with deque since the
+      // deque destructor and clear method don't call delete on the
+      // elements that it stores.  Another reason is that it makes it
+      // easier to change the implementation later if needed.
+
+      token_cache (void) : buffer () { }
+
+      ~token_cache (void) { clear (); }
 
       void push (token *tok)
       {
-        if (buffer.size () == sz)
-          pop ();
-
         buffer.push_front (tok);
       }
 
@@ -235,7 +243,7 @@
         return empty () ? 0 : buffer.back ();
       }
 
-      // Number of elements currently in the buffer, max of sz.
+      // Number of elements currently in the buffer.
       size_t size (void) const { return buffer.size (); }
 
       bool empty (void) const { return buffer.empty (); }
@@ -250,8 +258,6 @@
 
       std::deque<token *> buffer;
 
-      size_t sz;
-
       // No copying!
 
       token_cache (const token_cache&);