Remove limit on number of sync points
authorH. Peter Anvin <hpa@zytor.com>
Thu, 20 Sep 2007 04:41:02 +0000 (21:41 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Thu, 20 Sep 2007 04:41:02 +0000 (21:41 -0700)
Make it possible for ndisasm to allocate more memory for sync points
as needed.

sync.c

diff --git a/sync.c b/sync.c
index 88d882a..562c59d 100644 (file)
--- a/sync.c
+++ b/sync.c
 #include <limits.h>
 #include <inttypes.h>
 
+#include "nasmlib.h"
 #include "sync.h"
 
-#define SYNC_MAX 4096           /* max # of sync points */
+#define SYNC_MAX 4096           /* max # of sync points (initial) */
 
 /*
  * This lot manages the current set of sync points by means of a
@@ -24,29 +25,12 @@ static struct Sync {
     uint32_t pos;
     uint32_t length;
 } *synx;
-static int nsynx;
+static int max_synx, nsynx;
 
 void init_sync(void)
 {
-    /*
-     * I'd like to allocate an array of size SYNC_MAX, then write
-     * `synx--' which would allow numbering the array from one
-     * instead of zero without wasting memory. Sadly I don't trust
-     * this to work in 16-bit Large model, so it's staying the way
-     * it is. Btw, we don't care about freeing this array, since it
-     * has to last for the duration of the program and will then be
-     * auto-freed on exit. And I'm lazy ;-)
-     * 
-     * Speaking of 16-bit Large model, that's also the reason I'm
-     * not declaring this array statically - by doing it
-     * dynamically I avoid problems with the total size of DGROUP
-     * in Borland C.
-     */
-    synx = malloc((SYNC_MAX + 1) * sizeof(*synx));
-    if (!synx) {
-        fprintf(stderr, "ndisasm: not enough memory for sync array\n");
-        exit(1);
-    }
+    max_synx = SYNC_MAX-1;
+    synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
     nsynx = 0;
 }
 
@@ -54,8 +38,10 @@ void add_sync(uint32_t pos, uint32_t length)
 {
     int i;
 
-    if (nsynx == SYNC_MAX)
-        return;                 /* can't do anything - overflow */
+    if (nsynx >= max_synx) {
+       max_synx = (max_synx << 1)+1;
+       synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
+    }
 
     nsynx++;
     synx[nsynx].pos = pos;