Fix typeMask
[gnushogi.git] / gnushogi / init-common.c
index 061c7f9..0d89dae 100644 (file)
@@ -136,6 +136,59 @@ const small_short side_of_ptype[NO_PTYPE_PIECES] =
     white, white
 };
 
+int
+AllocateTT(int size)
+{
+    int n = sizeof(struct hashentry)*(size + rehash);
+    static int oldSize;
+    int doit = true;
+
+    if(oldSize == size) return use_ttable;
+    oldSize = ttblsize = size;
+
+    if(ttable[0]) free(ttable[0]);
+    if(ttable[1]) free(ttable[1]);
+
+    while (doit && ttblsize > MINTTABLE)
+    {
+        ttable[0] = malloc(n);  /* FIXME: cast to the correct type. */
+        ttable[1] = ttable[0] ? malloc(n) : NULL;
+
+        if (!ttable[0] || !ttable[1])
+        {
+            if (!ttable[0])
+                free(ttable[0]);
+
+            if (!ttable[1])
+                free(ttable[1]);
+
+            ttblsize = ttblsize >> 1;
+            n = sizeof(struct hashentry) * (ttblsize + rehash);
+        }
+        else
+        {
+            doit = false;
+        }
+    }
+
+    if (ttblsize <= MINTTABLE)
+    {
+        use_ttable = false;
+    }
+
+    if (use_ttable)
+    {
+        /* CHECKME: is the precedence here correct? */
+        /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
+        ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
+    }
+    else
+    {
+        ttable[0] = ttable[1] = NULL;
+    }
+
+    return use_ttable;
+}
 
 int
 Initialize_data(void)
@@ -143,14 +196,13 @@ Initialize_data(void)
     size_t n;
     int i;
     char buffer[60];
-    int doit = true;
 
     {
         small_short x = -1;
 
         if (x >= 0)
         {
-            ShowMessage("datatype 'small_short' is unsigned; "
+            dsp->ShowMessage("datatype 'small_short' is unsigned; "
                         "check gnushogi.h\n");
             return 1;
         }
@@ -163,7 +215,7 @@ Initialize_data(void)
     {
         sprintf(buffer, "Cannot allocate %ld bytes for search tree",
                 (long)n);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
         return 1;
     }
 
@@ -173,7 +225,7 @@ Initialize_data(void)
     if (!hashcode)
     {
         sprintf(buffer, "Cannot allocate %ld bytes for hashcode", (long)n);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
         return 1;
     }
 
@@ -185,7 +237,7 @@ Initialize_data(void)
         sprintf(buffer,
                 "Cannot allocate %ld bytes for drop_hashcode",
                 (long)n);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
         return 1;
     }
 
@@ -197,7 +249,7 @@ Initialize_data(void)
         sprintf(buffer,
                 "Cannot allocate %ld bytes for game record",
                 (long)n);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
         return 1;
     }
 
@@ -214,7 +266,7 @@ Initialize_data(void)
             {
                 sprintf(buffer, "cannot allocate %ld space for nextdir %d",
                         (long)(n), i);
-                ShowMessage(buffer);
+                dsp->ShowMessage(buffer);
             }
 
             nextdir[i] = NULL;
@@ -229,7 +281,7 @@ Initialize_data(void)
             {
                 sprintf(buffer, "cannot allocate %ld space for nextpos %d",
                         (long)(n), i);
-                ShowMessage(buffer);
+                dsp->ShowMessage(buffer);
             }
 
             use_nextpos = false;
@@ -247,7 +299,7 @@ Initialize_data(void)
 
     if (!value)
     {
-        ShowMessage("cannot allocate value space");
+        dsp->ShowMessage("cannot allocate value space");
         return 1;
     }
 
@@ -256,7 +308,7 @@ Initialize_data(void)
 
     if (!fscore)
     {
-        ShowMessage("cannot allocate fscore space");
+        dsp->ShowMessage("cannot allocate fscore space");
         return 1;
     }
 
@@ -268,7 +320,7 @@ Initialize_data(void)
     {
         sprintf(buffer, "Cannot allocate %ld bytes for history table",
                 (long)sizeof_history);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
         use_history = false;
     }
 #endif
@@ -284,7 +336,7 @@ Initialize_data(void)
         {
             sprintf(buffer, "Cannot allocate %ld bytes for cache table %ld",
                     (long)n, (long)i);
-            ShowMessage(buffer);
+            dsp->ShowMessage(buffer);
             use_etable = false;
         }
     }
@@ -295,47 +347,11 @@ Initialize_data(void)
     if (rehash < 0)
         rehash = MAXrehash;
 
-    n = sizeof(struct hashentry)*(ttblsize + rehash);
-
-    while (doit && ttblsize > MINTTABLE)
-    {
-        ttable[0] = malloc(n);  /* FIXME: cast to the correct type. */
-        ttable[1] = ttable[0] ? malloc(n) : NULL;
-
-        if (!ttable[0] || !ttable[1])
-        {
-            if (!ttable[0])
-                free(ttable[0]);
-
-            if (!ttable[1])
-                free(ttable[1]);
-
-            ttblsize = ttblsize >> 1;
-            n = sizeof(struct hashentry) * (ttblsize + rehash);
-        }
-        else
-        {
-            doit = false;
-        }
-    }
-
-    if (ttblsize <= MINTTABLE)
-    {
-        use_ttable = false;
-    }
-
-    if (use_ttable)
-    {
-        /* CHECKME: is the precedence here correct? */
-        /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
-        ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
-    }
-    else
+    if (!AllocateTT(ttblsize))
     {
         sprintf(buffer, "Cannot allocate %ld bytes for transposition table",
                 (long)(2 * n));
-        ShowMessage(buffer);
-        ttable[0] = ttable[1] = NULL;
+        dsp->ShowMessage(buffer);
     }
 #endif /* ttblsz */
 
@@ -345,7 +361,7 @@ Initialize_data(void)
 
     if (!distdata)
     {
-        ShowMessage("cannot allocate distdata space...");
+        dsp->ShowMessage("cannot allocate distdata space...");
         use_distdata = false;
     }
 #endif