From 148a53fde47be05f875928dc050b989198c5425c Mon Sep 17 00:00:00 2001 From: H.G.Muller Date: Tue, 4 Mar 2014 10:40:50 +0100 Subject: [PATCH] Implement XBoard memory command The hash-table allocation in Initialize_data was moved to a separate routine AllocateTT, which is now also called in response to the XBoard memory command. --- gnushogi/commondsp.c | 13 ++++++- gnushogi/gnushogi.h | 1 + gnushogi/init-common.c | 92 ++++++++++++++++++++++++++++-------------------- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/gnushogi/commondsp.c b/gnushogi/commondsp.c index e35698d..4cdc459 100644 --- a/gnushogi/commondsp.c +++ b/gnushogi/commondsp.c @@ -1677,7 +1677,7 @@ InputCommand(char *command, int root) "shogi" #endif ); - printf("debug=1 setboard=0 sigint=0 done=1\n"); + printf("debug=1 setboard=0 sigint=0 memory=1 done=1\n"); } else if (strcmp(s, ".") == 0) { // periodic update request of analysis info: send stat01 info @@ -1711,6 +1711,17 @@ InputCommand(char *command, int root) { ok = true; } +#if ttblsz + else if (strcmp(s, "memory") == 0) + { + unsigned int mem, size, t = 1; + sscanf(sx, "memory %d", &mem); + if(mem > 2048) mem = 2048; /* prevent integer overflow for > 2GB hash */ + size = (mem << 20) / sizeof(struct hashentry) - rehash; + while(t <= size/4) t <<= 1; + AllocateTT(t); + } +#endif else if (strcmp(s, "go") == 0) { ok = true; diff --git a/gnushogi/gnushogi.h b/gnushogi/gnushogi.h index dd5c21e..d40c3ac 100644 --- a/gnushogi/gnushogi.h +++ b/gnushogi/gnushogi.h @@ -912,6 +912,7 @@ extern void Initialize_eval(void); /* eval.c */ extern void NewGame(void); extern void GetOpenings(void); extern int OpeningBook(unsigned short *hint); +extern int AllocateTT(int size); /* init-common.c */ typedef enum { diff --git a/gnushogi/init-common.c b/gnushogi/init-common.c index 1f71994..0d89dae 100644 --- a/gnushogi/init-common.c +++ b/gnushogi/init-common.c @@ -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,7 +196,6 @@ Initialize_data(void) size_t n; int i; char buffer[60]; - int doit = true; { small_short x = -1; @@ -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)); dsp->ShowMessage(buffer); - ttable[0] = ttable[1] = NULL; } #endif /* ttblsz */ -- 1.7.0.4