--- freem/src/global_bltin.c 2025/04/11 16:23:18 1.18 +++ freem/src/global_bltin.c 2025/04/11 18:24:32 1.20 @@ -1,5 +1,5 @@ /* - * $Id: global_bltin.c,v 1.18 2025/04/11 16:23:18 snw Exp $ + * $Id: global_bltin.c,v 1.20 2025/04/11 18:24:32 snw Exp $ * freem database engine * * @@ -24,6 +24,12 @@ * along with FreeM. If not, see . * * $Log: global_bltin.c,v $ + * Revision 1.20 2025/04/11 18:24:32 snw + * Fix bug in memory cache + * + * Revision 1.19 2025/04/11 16:52:05 snw + * Fix indentation in global handler + * * Revision 1.18 2025/04/11 16:23:18 snw * Avoid re-reading the same block consecutively when possible * @@ -142,7 +148,7 @@ static void panic (void); #define DATA 8 #if !defined(__OpenBSD__) && !defined(_AIX) && !defined(__osf__) && !defined(MSDOS) && !defined(__vax__) && !defined(__OS2__) - long time (); +long time (); #endif inline long gbl_path(char *key, char *buf) @@ -496,9 +502,8 @@ int gbl_read_block(global_handle *g, uns g->use_count++; - fstat (g->fd, &gstat); if (!g->locked) gbl_lock (g, 1); - + fstat (g->fd, &gstat); if ((g->last_block == blocknum) && (g->have_cached_block) && @@ -506,7 +511,6 @@ int gbl_read_block(global_handle *g, uns (gstat.st_mtime < g->last_read_time)) { /* the global has not been modified since the last read; grab from memory */ g->memory_reads++; - g->last_read_time = time (0L); memcpy (block, g->last_block_accessed, g->header.block_size); } else { @@ -517,14 +521,14 @@ int gbl_read_block(global_handle *g, uns /* update cache */ memcpy (g->last_block_accessed, block, g->header.block_size); g->have_cached_block = TRUE; - g->last_read_time = time (0L); - g->cached_block_num = blocknum; - + g->cached_block_num = blocknum; g->last_block = blocknum; - g->use_count++; g->read_ops++; } + g->last_read_time = time (0L); + g->use_count++; + if (g->locked) gbl_unlock (g); return TRUE; @@ -736,7 +740,7 @@ void global_bltin (short action, char *k static char block[BLOCKLEN]; static int getnflag; /* flag 1=$ZO-variable 0=$Q-function */ static int tryfast; /* try fast access if get_sym on */ - /* previous global */ + /* previous global */ int iresult; @@ -1155,7 +1159,7 @@ reopen: if (action == get_sym) { - tfast0: +tfast0: gbl_lock (g, 3); if (g->fast_path > 0) goto tfast1; /* try again last block */ @@ -1165,7 +1169,7 @@ reopen: for (;;) { - tfast1: +tfast1: gbl_read_block (g, blknbr, block); /* temporarily disabled @@ -1259,15 +1263,15 @@ reopen: addr += UNSIGN (block[addr]) + 2; /* skip key */ if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == g->last_block) { - merr_raise (DBDGD); - goto quit; - } + merr_raise (DBDGD); + goto quit; + } - addr += PLEN; /* skip data */ - g->last_block = blknbr; - g->fast_path = 1; + addr += PLEN; /* skip data */ + g->last_block = blknbr; + g->fast_path = 1; - if (merr () == INRPT) goto quit; + if (merr () == INRPT) goto quit; } } /* end of get_sym */ @@ -1351,7 +1355,7 @@ k_again: /* entry point for repeated datal = stlen (data); offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); if (found != 2) { /* new entry */ @@ -1377,7 +1381,8 @@ k_again: /* entry point for repeated } -s10: { +s10: + { long len; /* insert key */ char key0[256]; @@ -1528,7 +1533,7 @@ s10: { goto splitd; } -s20: + s20: i = offset; k = addr + olddatal; @@ -1588,7 +1593,7 @@ s20: /* get following entry, eventually in the next blk */ offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); if (addr >= offset) { @@ -1723,7 +1728,7 @@ s20: while (i < keyl) if (compactkey[i++] & 01) - j1++; + j1++; i = 0; j = 0; k = 0; @@ -1756,8 +1761,8 @@ s20: while ((ch = UNSIGN (scratch[i++])) != g_EOL) { ch0 = (ch >= SP ? (ch >> 1) : /* 'string' chars */ - (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ - (ch >> 1) + SP)); /* '.' or '-' */ + (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ + (ch >> 1) + SP)); /* '.' or '-' */ if (ch0 == DEL) { @@ -1836,7 +1841,7 @@ s20: addr = 0; offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); } } @@ -1903,8 +1908,8 @@ s20: while ((ch = UNSIGN (key0[i++])) != g_EOL) { ch0 = (ch >= SP ? (ch >> 1) : /* 'string' chars */ - (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ - (ch >> 1) + SP)); /* '.' or '-' */ + (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ + (ch >> 1) + SP)); /* '.' or '-' */ if (ch0 == DEL) { @@ -1980,8 +1985,8 @@ s20: } ch0 = (ch >= SP ? (ch >> 1) : /* 'string' chars */ - (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ - (ch >> 1) + SP)); /* '.' or '-' */ + (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ + (ch >> 1) + SP)); /* '.' or '-' */ if (ch0 == DEL) { @@ -2025,7 +2030,7 @@ s20: case kill_sym: /* search and destroy */ -killo: /* entry from killone section */ + killo: /* entry from killone section */ offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); i = 0; @@ -2070,15 +2075,15 @@ killo: /* entry from killone section addr += UNSIGN (block[addr]); addr += (2 + PLEN); /* skip previous entry */ offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); traceadr[trx] = addr; if (addr < offset) break; traceadr[trx] = 0; traceblk[trx] = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); } @@ -2087,7 +2092,7 @@ killo: /* entry from killone section gbl_read_block (g, blknbr, block); offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); addr = 0; k = UNSIGN (block[0]); stcpy0 (key0, &block[2], k); @@ -2176,17 +2181,17 @@ killo: /* entry from killone section if ((begadr == 0) && (endadr == offset)) { /* block becomes empty */ long left, - right; + right; char block0[BLOCKLEN]; -p_empty: /* entry if pointer block goes empty */ + p_empty: /* entry if pointer block goes empty */ left = UNSIGN (block[LLPTR]) * 65536 + - UNSIGN (block[LLPTR + 1]) * 256 + - UNSIGN (block[LLPTR + 2]); + UNSIGN (block[LLPTR + 1]) * 256 + + UNSIGN (block[LLPTR + 2]); right = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); if (left) { @@ -2227,7 +2232,7 @@ p_empty: /* entry if pointer block goes gbl_read_block (g, blknbr, block); offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); freecnt = UNSIGN (block[addr]) + 2 + PLEN; /* delete key */ @@ -2300,19 +2305,19 @@ p_empty: /* entry if pointer block goes j = block[begadr + 1]; k = 0; if (begadr) - while (key0[k] == key1[k]) - k++; /* new key_offset */ + while (key0[k] == key1[k]) + k++; /* new key_offset */ if (k < j) { - ch = j - k; /* space requirement */ - block[begadr] = i + ch; /* new key_length */ - block[begadr + 1] = k; /* new key_offset */ - i = offset; - j = i + ch; - offset = j; - begadr++; - while (i > begadr) - block[j--] = block[i--]; - stcpy0 (&block[begadr + 1], &key0[k], ch); + ch = j - k; /* space requirement */ + block[begadr] = i + ch; /* new key_length */ + block[begadr + 1] = k; /* new key_offset */ + i = offset; + j = i + ch; + offset = j; + begadr++; + while (i > begadr) + block[j--] = block[i--]; + stcpy0 (&block[begadr + 1], &key0[k], ch); } } block[OFFS] = offset / 256; @@ -2330,7 +2335,7 @@ p_empty: /* entry if pointer block goes break; -zinv: + zinv: { long len; @@ -2348,7 +2353,7 @@ zinv: gbl_read_block (g, blknbr, block); addr = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); } @@ -2423,8 +2428,8 @@ zinv: while ((ch = UNSIGN (scratch[i++])) != g_EOL) { ch0 = (ch >= SP ? (ch >> 1) : /* 'string' chars */ - (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ - (ch >> 1) + SP)); /* '.' or '-' */ + (ch < 20 ? (ch >> 1) + '0' : /* 0...9 */ + (ch >> 1) + SP)); /* '.' or '-' */ if (ch0 == DEL) { @@ -2469,166 +2474,142 @@ zinv: case zdata: /* nonstandard data function */ - { - long counties[128]; - char key0[256]; - int icnt, icnt0, len; + { + long counties[128]; + char key0[256]; + int icnt, icnt0, len; - i = 0; + i = 0; - while (i < 128) counties[i++] = 0L; /* init count; */ + while (i < 128) counties[i++] = 0L; /* init count; */ - if (found == 2) { /* ... advance to next entry */ - addr += UNSIGN (block[addr]); - addr += 2; /* skip key */ - addr += UNSIGN (block[addr]); - addr++; /* skip data */ + if (found == 2) { /* ... advance to next entry */ + addr += UNSIGN (block[addr]); + addr += 2; /* skip key */ + addr += UNSIGN (block[addr]); + addr++; /* skip data */ - counties[0] = 1L; - } + counties[0] = 1L; + } - offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); - icnt = 0; - i = 0; + offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); + icnt = 0; + i = 0; - while ((ch = compactkey[i++]) != g_EOL) { - - if (ch & 01) { - icnt++; - } + while ((ch = compactkey[i++]) != g_EOL) { + if (ch & 01) { + icnt++; } - len = i - 1; - i = 0; + } - while (i < addr) { /* compute offset complete key */ + len = i - 1; + i = 0; + + while (i < addr) { /* compute offset complete key */ - icnt0 = UNSIGN (block[i++]); - icnt0 += (j = UNSIGN (block[i++])); + icnt0 = UNSIGN (block[i++]); + icnt0 += (j = UNSIGN (block[i++])); - while (j < icnt0) key0[j++] = block[i++]; + while (j < icnt0) key0[j++] = block[i++]; - key0[j] = g_EOL; - i += UNSIGN (block[i]); + key0[j] = g_EOL; + i += UNSIGN (block[i]); - i++; /* skip data */ + i++; /* skip data */ - } + } - for (;;) { /* is it still a descendant ??? */ + for (;;) { /* is it still a descendant ??? */ - if (addr >= offset) { /* look in next block */ + if (addr >= offset) { /* look in next block */ - if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2])) == 0) { - break; /* no next block */ - } + if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2])) == 0) { + break; /* no next block */ + } - gbl_read_block (g, blknbr, block); + gbl_read_block (g, blknbr, block); - addr = 0; - offset = UNSIGN (block[OFFS]) * 256 + + addr = 0; + offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); - } + } - i = UNSIGN (block[addr++]); - i += (j = UNSIGN (block[addr++])); + i = UNSIGN (block[addr++]); + i += (j = UNSIGN (block[addr++])); - while (j < i) key0[j++] = block[addr++]; + while (j < i) key0[j++] = block[addr++]; - addr += UNSIGN (block[addr]); - addr++; /* skip data */ - icnt0 = 0; - i = 0; + addr += UNSIGN (block[addr]); + addr++; /* skip data */ + icnt0 = 0; + i = 0; - while (i < j) if (key0[i++] & 01) + while (i < j) if (key0[i++] & 01) - icnt0++; + icnt0++; - if (icnt0 <= icnt) break; + if (icnt0 <= icnt) break; - i = 0; + i = 0; - while (i < len) { + while (i < len) { - if (key0[i] != compactkey[i]) break; + if (key0[i] != compactkey[i]) break; - i++; + i++; - } + } - if (i < len) break; + if (i < len) break; - counties[icnt0 - icnt]++; + counties[icnt0 - icnt]++; - } + } - i = 128; + i = 128; - while (counties[--i] == 0L); + while (counties[--i] == 0L); - lintstr (data, counties[0]); + lintstr (data, counties[0]); - j = 1; - tmp1[0] = ','; + j = 1; + tmp1[0] = ','; - while (j <= i) { - lintstr (&tmp1[1], counties[j++]); - stcat (data, tmp1); - } - + while (j <= i) { + lintstr (&tmp1[1], counties[j++]); + stcat (data, tmp1); } + + } - break; + break; case getinc: - { - int setopsav; + { + int setopsav; - setopsav = setop; - setop = '+'; - data[0] = '1'; - data[1] = EOL; + setopsav = setop; + setop = '+'; + data[0] = '1'; + data[1] = EOL; - global (set_sym, key, data); + global (set_sym, key, data); - setop = setopsav; + setop = setopsav; - return; - } + return; + } case killone: - - { - if (found == 2) goto killo; /* entry found use normal kill routine */ + { + if (found == 2) goto killo; /* entry found use normal kill routine */ - goto quit; - } - - case merge_sym: - - printf("MERGE NOT IMPLEMENTED FOR GLOBALS\n"); - -#ifdef DEBUG_GBL - - int loop; - - printf ("DEBUG MERGE: "); - printf ("[key] is ["); - - for (loop = 0; key[loop] != EOL; loop++) printf ("%c", (key[loop] == DELIM) ? '!' : key[loop]); - - printf ("]\r\n"); - printf ("[data] is ["); - - for(loop = 0; data[loop] != EOL; loop++) printf ("%c", (data[loop] == DELIM) ? '!' : data[loop]); - - printf("]\r\n"); - -#endif - return; + goto quit; + } default: @@ -2660,8 +2641,8 @@ splitd: /* split data block in two se if (addr >= offset) { long right, - right1, - right2; + right1, + right2; right = UNSIGN (block[RLPTR]); right1 = UNSIGN (block[RLPTR + 1]); @@ -2734,17 +2715,17 @@ splitd: /* split data block in two se } /* other is ***always*** zero !!! - * if (other=left*65536+left1*256+left2) up-date RL-PTR of LL-block - * { char block0[BLOCKLEN]; - * lseek(filedes,(long)other*(long)(BLOCKLEN),0); - * read(filedes,block0,BLOCKLEN); - * block0[RLPTR ]=blknbr/65536; - * block0[RLPTR+1]=blknbr%65536/256; - * block0[RLPTR+2]=blknbr%256; - * lseek(filedes,(long)other*(long)(BLOCKLEN),0); - * write(filedes,block0,BLOCKLEN); - * } - */ + * if (other=left*65536+left1*256+left2) up-date RL-PTR of LL-block + * { char block0[BLOCKLEN]; + * lseek(filedes,(long)other*(long)(BLOCKLEN),0); + * read(filedes,block0,BLOCKLEN); + * block0[RLPTR ]=blknbr/65536; + * block0[RLPTR+1]=blknbr%65536/256; + * block0[RLPTR+2]=blknbr%256; + * lseek(filedes,(long)other*(long)(BLOCKLEN),0); + * write(filedes,block0,BLOCKLEN); + * } + */ goto spltex; @@ -2826,8 +2807,8 @@ splitd: /* split data block in two se /* update rightlink and leftlink pointers */ other = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; block0[RLPTR + 2] = block[RLPTR + 2]; @@ -2863,8 +2844,8 @@ splitd: /* split data block in two se /* save old block away and make new block the current block */ /* update rightlink and leftlink pointers */ other = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; block0[RLPTR + 2] = block[RLPTR + 2]; @@ -2957,8 +2938,8 @@ static void splitp (global_handle *g, ch /* update number of blocks ! */ i = UNSIGN (block0[NRBLK]) * 65536 + - UNSIGN (block0[NRBLK + 1]) * 256 + - UNSIGN (block0[NRBLK + 2]) + 1; + UNSIGN (block0[NRBLK + 1]) * 256 + + UNSIGN (block0[NRBLK + 2]) + 1; block0[NRBLK] = i / 65536; block0[NRBLK + 1] = i % 65536 / 256; @@ -3024,8 +3005,8 @@ static void splitp (global_handle *g, ch /* update rightlink and leftlink pointers */ other = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; block0[RLPTR + 2] = block[RLPTR + 2]; @@ -3060,8 +3041,8 @@ static void splitp (global_handle *g, ch /* update rightlink and leftlink pointers */ other = UNSIGN (block[RLPTR]) * 65536 + - UNSIGN (block[RLPTR + 1]) * 256 + - UNSIGN (block[RLPTR + 2]); + UNSIGN (block[RLPTR + 1]) * 256 + + UNSIGN (block[RLPTR + 2]); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; @@ -3134,7 +3115,7 @@ static void update (global_handle *g, ch j = oldkeyl - keyl; offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); if (j > 0) { /* surplus space */ @@ -3208,7 +3189,7 @@ static void insert (global_handle *g, ch gbl_read_block (g, blk, block); offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); if (traceadr[trx + 1] != (-1)) { addr += UNSIGN (block[addr]); @@ -3278,8 +3259,8 @@ static void b_free (global_handle *g, un gbl_read_block (g, free, block0); other = UNSIGN (block0[RLPTR]) * 65536 + - UNSIGN (block0[RLPTR + 1]) * 256 + - UNSIGN (block0[RLPTR + 2]); + UNSIGN (block0[RLPTR + 1]) * 256 + + UNSIGN (block0[RLPTR + 2]); if (other == 0) break; @@ -3295,8 +3276,8 @@ static void b_free (global_handle *g, un offset -= PLEN; other = UNSIGN (block0[offset]) * 65536 + - UNSIGN (block0[offset + 1]) * 256 + - UNSIGN (block0[offset + 2]); + UNSIGN (block0[offset + 1]) * 256 + + UNSIGN (block0[offset + 2]); block0[offset] = 0; block0[offset + 1] = 0; @@ -3420,7 +3401,7 @@ static void scandblk (char *block, long char key0[256]; offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); + UNSIGN (block[OFFS + 1]); while (i < offset) {