shithub: trueawk

Download patch

ref: 25baaf87ddcd93aa8c5f9f0afa8abdd318d6273b
parent: fe72348b892774d05e32e53bc42621d37e73362f
author: ozan yigit <ozan.yigit@gmail.com>
date: Sun Oct 15 07:27:40 EDT 2023

gototab reallocation pulled into resize_gototab

--- a/b.c
+++ b/b.c
@@ -596,6 +596,16 @@
 	return(0);
 }
 
+static void resize_gototab(fa *f, int state)
+{
+	size_t new_size = f->gototab[state].allocated * 2;
+	gtte *p = (gtte *) realloc(f->gototab[state].entries, new_size * sizeof(gtte));
+	if (p == NULL)
+		overflo(__func__);
+	f->gototab[state].allocated = new_size;
+	f->gototab[state].entries = p;
+}
+
 static int get_gototab(fa *f, int state, int ch) /* hide gototab inplementation */
 {
 	gtte key;
@@ -632,16 +642,10 @@
 		return val;
 	} else if (ch > f->gototab[state].entries[f->gototab[state].inuse-1].ch) {
 		// not seen yet, insert and return
-		// FIXME: (Oz, hint, hint): Resizing should be pulled out into a function...
 		gtt *tab = & f->gototab[state];
-		if (tab->inuse + 1 >= tab->allocated) {
-			size_t new_size = tab->allocated * 2;
-			gtte *p = (gtte *) realloc(f->gototab[state].entries, new_size * sizeof(gtte));
-			if (p == NULL)
-				overflo(__func__);
-			f->gototab[state].allocated = new_size;
-			f->gototab[state].entries = p;
-		}
+		if (tab->inuse + 1 >= tab->allocated)
+			resize_gototab(f, state);
+
 		f->gototab[state].entries[f->gototab[state].inuse-1].ch = ch;
 		f->gototab[state].entries[f->gototab[state].inuse-1].state = val;
 		f->gototab[state].inuse++;
@@ -666,14 +670,8 @@
 	}
 
 	gtt *tab = & f->gototab[state];
-	if (tab->inuse + 1 >= tab->allocated) {
-		size_t new_size = tab->allocated * 2;
-		gtte *p = (gtte *) realloc(f->gototab[state].entries, new_size * sizeof(gtte));
-		if (p == NULL)
-			overflo(__func__);
-		f->gototab[state].allocated = new_size;
-		f->gototab[state].entries = p;
-	}
+	if (tab->inuse + 1 >= tab->allocated)
+		resize_gototab(f, state);
 	++tab->inuse;
 	f->gototab[state].entries[tab->inuse].ch = ch;
 	f->gototab[state].entries[tab->inuse].state = val;
--