--- freem/src/global_bltin.c 2025/04/08 20:00:56 1.10 +++ freem/src/global_bltin.c 2025/04/11 14:21:03 1.17 @@ -1,5 +1,5 @@ /* - * $Id: global_bltin.c,v 1.10 2025/04/08 20:00:56 snw Exp $ + * $Id: global_bltin.c,v 1.17 2025/04/11 14:21:03 snw Exp $ * freem database engine * * @@ -24,6 +24,27 @@ * along with FreeM. If not, see . * * $Log: global_bltin.c,v $ + * Revision 1.17 2025/04/11 14:21:03 snw + * Make all but one of the read/write calls in global_bltin use gbl_read_block or gbl_write_block + * + * Revision 1.16 2025/04/11 00:52:40 snw + * Replace all lseek/read calls in global handler to use gbl_read_block function + * + * Revision 1.15 2025/04/10 01:24:38 snw + * Remove C++ style comments + * + * Revision 1.14 2025/04/09 19:52:02 snw + * Eliminate as many warnings as possible while building with -Wall + * + * Revision 1.13 2025/04/09 14:34:30 snw + * Further work on global_bltin.c refactor + * + * Revision 1.12 2025/04/09 00:43:07 snw + * Exit with fatal error if a header mismatch found + * + * Revision 1.11 2025/04/08 21:41:13 snw + * Make insert, update, and splitp global handler functions take a ptr to a global_handle instead of a file descriptor + * * Revision 1.10 2025/04/08 20:00:56 snw * Global handler now uses a header file and maintains the last journaling transaction ID * @@ -63,16 +84,17 @@ #include "global_bltin.h" global_handle *global_handles_head; +unsigned long gbl_cache_misses = 0; +unsigned long gbl_cache_hits = 0; -static void b_free (short filedes, unsigned long blknbr); -static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr); -static void update (short filedes, char *ins_key, long keyl); -static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr); +static void b_free (global_handle *g, unsigned long blknbr); +static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr); +static void update (global_handle *g, char *ins_key, long keyl); +static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr); static void scanpblk (char *block, long *adr, long *fnd); static void scandblk (char *block, long *adr, long *fnd); -static void getnewblk (int filedes, unsigned long *blknbr); +static void getnewblk (global_handle *g, unsigned long *blknbr); static short int g_collate (char *t); -//static short int g_numeric (char *str); short g_numeric (char *str); void close_all_globals(void); static void panic (void); @@ -123,19 +145,14 @@ static void panic (void); inline long gbl_path(char *key, char *buf) { long savj; - long savch; /* saved j and ch for multiple pathes */ register long int i; register long int j; register long int k; register long int ch; - long pathscan; /* flag for repeated scan of pathlist setting an undef global */ /* construct full UNIX filename */ savj = 0; - savch = ch = EOL; - pathscan = TRUE; -nextpath: k = 0; j = savj; @@ -159,8 +176,6 @@ nextpath: } } - - if (savj == 0 && ch == EOL) pathscan = FALSE; /* one path only: inhibit search */ if (k > 0) { @@ -173,7 +188,6 @@ nextpath: } - savch = ch; savj = j; i = 0; j = 0; @@ -206,6 +220,19 @@ nextpath: return i; } /* gbl_path() */ +void gbl_cache_hit(global_handle *g) +{ + g->cache_hits++; + gbl_cache_hits++; +} /* gbl_cache_hit() */ + +void gbl_cache_miss(global_handle *g) +{ + g->fast_path = 0; + g->cache_misses++; + gbl_cache_misses++; +} /* gbl_cache_miss() */ + int gbl_lock(global_handle *g, int type) { if (g->locked == TRUE || lonelyflag == TRUE) { @@ -214,6 +241,8 @@ int gbl_lock(global_handle *g, int type) locking (g->fd, type, 0L); g->locked = TRUE; + + return TRUE; } /* gbl_lock() */ int gbl_unlock(global_handle *g) @@ -224,6 +253,8 @@ int gbl_unlock(global_handle *g) locking (g->fd, 0, 0L); g->locked = FALSE; + + return TRUE; } /* gbl_unlock() */ void gbl_close(global_handle *g) @@ -292,7 +323,7 @@ int gbl_write_header(global_handle *g, g return FALSE; } - gbl_lock (g, 1); + if (g->locked == FALSE) gbl_lock (g, 1); old_position = lseek (g->fd, 0, SEEK_CUR); lseek (g->fd, 0, SEEK_SET); @@ -302,7 +333,9 @@ int gbl_write_header(global_handle *g, g } lseek (g->fd, old_position, SEEK_SET); - gbl_unlock (g); + if (g->locked == TRUE) gbl_unlock (g); + + gbl_read_header (g, &g->header); return TRUE; } /* gbl_write_header() */ @@ -371,7 +404,8 @@ int gbl_create(global_handle *g) g->age = time (0L); g->last_block = 0; g->use_count = 1; - + g->fast_path = 0; + gbl_write_initial_header (g); return OK; @@ -380,9 +414,9 @@ int gbl_create(global_handle *g) short gbl_open(global_handle *g, short action) { int result; - global_header h; if (g->opened == FALSE) { + gbl_cache_miss (g); while (1) { errno = 0; g->fd = open (g->global_path, 2); @@ -411,13 +445,29 @@ short gbl_open(global_handle *g, short a } else { g->opened = TRUE; - result = gbl_read_header (g, &h); - + result = gbl_read_header (g, &g->header); + if (result == GBL_HDR_OK) { g->opened = TRUE; } - else { - gbl_close (g); + else { + gbl_close (g); + set_io (UNIX); + + switch (result) { + + case GBL_HDR_BADMAGIC: + fprintf (stderr, "gbl_open: bad magic value in %s [FATAL]\n", g->global_name); + exit (1); + break; + + case GBL_HDR_BADVERSION: + fprintf (stderr, "gbl_open: global version is %d in %s (must be %d) [FATAL]\n", g->header.format_version, g->global_name, GBL_FORMAT_VERSION); + exit (1); + break; + + } + return FALSE; } } @@ -427,13 +477,71 @@ short gbl_open(global_handle *g, short a } /* gbl_open() */ +int gbl_read_block(global_handle *g, unsigned long blocknum, char *block) +{ + unsigned long hdr_offset; + hdr_offset = sizeof (global_header); + + if (g->opened == FALSE) { + return FALSE; + } + + gbl_lock (g, 1); + lseek (g->fd, hdr_offset + ((long) blocknum * (long) (g->header.block_size)), SEEK_SET); + read (g->fd, block, g->header.block_size); + g->last_block = blocknum; + g->use_count++; + g->read_ops++; + gbl_unlock (g); + + return TRUE; +} /* gbl_read_block() */ + +int gbl_write_block(global_handle *g, unsigned long blocknum, char *block) +{ + int errsav; + unsigned long hdr_offset; + hdr_offset = sizeof (global_header); + + if (g->opened == FALSE) { + return FALSE; + } + + if (!g->locked) { + gbl_lock (g, 1); + } + + for (;;) { + + errno = 0; + + lseek (g->fd, hdr_offset + (blocknum * g->header.block_size), SEEK_SET); + write (g->fd, block, BLOCKLEN); + errsav = errno; + g->last_block = blocknum; + g->use_count++; + g->write_ops++; + + if (errsav == 0) break; + + panic (); + + } + + gbl_update_tid (g); + + if (g->locked) { + gbl_unlock (g); + } + + return TRUE; +} + global_handle *gbl_handle(char *key) { global_handle *g; char global_name[256]; int i; - long path_len; - char block[BLOCKLEN]; struct stat dinf; i = 0; @@ -474,6 +582,10 @@ global_handle *gbl_handle(char *key) g->opened = FALSE; g->fd = 0; g->fast_path = -1; + g->cache_misses = 0; + g->cache_hits = 0; + g->read_ops = 0; + g->write_ops = 0; strcpy (g->global_name, global_name); gbl_path (key, g->global_path); @@ -568,7 +680,6 @@ void global_bltin (short action, char *k unsigned long hdr_offset; /* these must be static variables */ - static short filedes; /* filedescr for global access */ static char filnam[256]; /* name of global/unix file */ /* the following vars may be */ @@ -597,16 +708,13 @@ void global_bltin (short action, char *k int iresult; - struct stat dinf; /* get modification date */ - - long savj; - long savch; /* saved j and ch for multiple pathes */ register long int i; register long int j; register long int k; register long int ch; - long pathscan; /* flag for repeated scan of pathlist setting an undef global */ + j = 0; + hdr_offset = sizeof (global_header); /* process optional limitations */ @@ -971,19 +1079,9 @@ reopen: merr_raise (iresult); return; } - - for (;;) { - - errno = 0; - - lseek (g->fd, hdr_offset + (ROOT * BLOCKLEN), SEEK_SET); - write (g->fd, block, BLOCKLEN); - - if (errno == 0) break; - - panic (); - } + gbl_write_block (g, ROOT, block); + block[NRBLK] = 0; block[NRBLK + 1] = 0; block[NRBLK + 2] = ROOT; /* clear */ @@ -1019,132 +1117,123 @@ reopen: /* odd numbered actions get read access (get_sym,data,fra_order) 3 */ /* even ones read/write access (set_sym,kill_sym) 1 */ -lock: +/* temporarily disabled + lock: +*/ if (action == get_sym) { -tfast0: + tfast0: gbl_lock (g, 3); - if (tryfast) goto tfast1; /* try again last block */ + + if (g->fast_path > 0) goto tfast1; /* try again last block */ blknbr = g->last_block = ROOT; /* start with ROOT block */ for (;;) { -tfast1: - - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); - - -tfast2: - - if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */ + tfast1: + gbl_read_block (g, blknbr, block); - offset = UNSIGN (block[OFFS]) * 256 + - UNSIGN (block[OFFS + 1]); - j = UNSIGN (block[0]); - i = 0; - - stcpy0 (key1, &block[2], j); /* get first key */ - - ch = keyl; /* ch is a register! */ - - while (i < offset) { - - j = UNSIGN (block[i++]); /* length of key - offset */ - k = UNSIGN (block[i++]); /* offset into previous entry */ - -#ifdef VERSNEW +/* temporarily disabled + tfast2: +*/ + if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */ - stcpy0 (&key0[k], &block[i], j); - i += j; - j += k; - -#else + offset = UNSIGN (block[OFFS]) * 256 + + UNSIGN (block[OFFS + 1]); + j = UNSIGN (block[0]); + i = 0; - j += k; + stcpy0 (key1, &block[2], j); /* get first key */ - while (k < j) key1[k++] = block[i++]; /* get key */ - -#endif /* VERSNEW */ - - if (j != ch) { /* keys have different length */ + ch = keyl; /* ch is a register! */ + + while (i < offset) { + + j = UNSIGN (block[i++]); /* length of key - offset */ + k = UNSIGN (block[i++]); /* offset into previous entry */ + + j += k; + + while (k < j) key1[k++] = block[i++]; /* get key */ + + if (j != ch) { /* keys have different length */ + + i += UNSIGN (block[i]); + i++; + + continue; + + } + + j = ch; + + do { + j--; + } while (compactkey[j] == key1[j]); /* compare keys */ + + + if (j < 0) { + + k = UNSIGN (block[i++]); + stcpy0 (data, &block[i], k); /* get value */ + data[k] = EOL1; /* append EOL */ + + goto quit; + + } i += UNSIGN (block[i]); - i++; - - continue; - + i++; /* skip data */ + + } + + /* fast access failed. try normal path */ + if (tryfast) { + gbl_cache_miss (g); + goto tfast0; } - j = ch; + merr_raise (M7); + data[0] = EOL; - do { - j--; - } while (compactkey[j] == key1[j]); /* compare keys */ + goto quit; /* variable not found */ + } + else { - - if (j < 0) { + if (g->fast_path > 0) { + gbl_cache_miss (g); + goto tfast0; + } + + if (typ == EMPTY) { - k = UNSIGN (block[i++]); - stcpy0 (data, &block[i], k); /* get value */ - data[k] = EOL1; /* append EOL */ - + if (blknbr == ROOT) { + gbl_close (g); + goto reopen; + } + + merr_raise (DBDGD); goto quit; - + } - - i += UNSIGN (block[i]); - i++; /* skip data */ - - } - - /* fast access failed. try normal path */ - if (tryfast) { - tryfast = FALSE; - goto tfast0; - } - - merr_raise (M7); - data[0] = EOL; - - goto quit; /* variable not found */ - } - else { - - if (tryfast) { - tryfast = FALSE; - goto tfast0; - } - - if (typ == EMPTY) { - if (blknbr == ROOT) { - //close (filedes); - gbl_close (g); - goto reopen; - } - - merr_raise (DBDGD); - goto quit; - } - - } - - scanpblk (block, &addr, &found); - - 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) { + + scanpblk (block, &addr, &found); + + 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; } addr += PLEN; /* skip data */ g->last_block = blknbr; + g->fast_path = 1; if (merr () == INRPT) goto quit; @@ -1156,14 +1245,12 @@ tfast2: /* a KILL on an unsubscripted global deletes the entire file */ if (action == kill_sym && compactkey[0] == g_EOL) { - lseek (g->fd, hdr_offset + ROOT, SEEK_SET); - - /* note : UNIX does not tell other */ - block[BTYP] = EMPTY; /* jobs that a file has been unlinked */ - + /* note : UNIX does not tell other jobs that a file has been unlinked */ /* as long as they keep it open. */ /* so we mark this global as EMPTY */ - write (g->fd, block, BLOCKLEN); + block[BTYP] = EMPTY; + + gbl_write_block (g, ROOT, block); gbl_unlock (g); gbl_close (g); @@ -1188,10 +1275,8 @@ k_again: /* entry point for repeated traceblk[trx] = blknbr; traceadr[trx] = 0; - - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); - + + gbl_read_block (g, blknbr, block); typ = block[BTYP]; if (typ == DATA) { @@ -1223,6 +1308,7 @@ k_again: /* entry point for repeated addr += PLEN; /* skip data */ g->last_block = blknbr; + g->fast_path = 1; } traceadr[trx] = addr; @@ -1357,10 +1443,9 @@ s10: { } - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); + gbl_write_block (g, blknbr, block); - if (traceadr[trx] == 0) update (g->fd, compactkey, keyl); + if (traceadr[trx] == 0) update (g, compactkey, keyl); break; } @@ -1444,10 +1529,8 @@ s20: } stcpy0 (&block[++addr], data, (long) datal); - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); - gbl_update_tid (g); + gbl_write_block (g, blknbr, block); break; @@ -1478,9 +1561,8 @@ s20: if (addr >= offset) { if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2]))) { - - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + + gbl_read_block (g, blknbr, block); j1 = UNSIGN (block[0]); i = 0; @@ -1561,9 +1643,8 @@ s20: goto quit; } /* no next block */ - - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + + gbl_read_block (g, blknbr, block); scandblk (block, &addr, &found); } @@ -1719,8 +1800,7 @@ s20: goto quit; /* no next block */ } - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + gbl_read_block (g, blknbr, block); addr = 0; offset = UNSIGN (block[OFFS]) * 256 + @@ -1743,8 +1823,7 @@ s20: goto quit; /* no next block */ } - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + gbl_read_block (g, blknbr, block); addr = 0; } @@ -1953,9 +2032,8 @@ killo: /* entry from killone section other = traceblk[--trx]; addr = traceadr[trx]; - - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + + gbl_read_block (g, other, block); addr += UNSIGN (block[addr]); addr += (2 + PLEN); /* skip previous entry */ @@ -1974,8 +2052,7 @@ killo: /* entry from killone section trx = trxsav; - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + gbl_read_block (g, blknbr, block); offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); @@ -2081,33 +2158,29 @@ p_empty: /* entry if pointer block goes if (left) { - lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block0, BLOCKLEN); + gbl_read_block (g, left, block0); block0[RLPTR] = block[RLPTR]; block0[RLPTR + 1] = block[RLPTR + 1]; block0[RLPTR + 2] = block[RLPTR + 2]; - - lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); + + gbl_write_block (g, left, block0); } if (right) { - - lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block0, BLOCKLEN); + + gbl_read_block (g, right, block0); block0[LLPTR] = block[LLPTR]; block0[LLPTR + 1] = block[LLPTR + 1]; block0[LLPTR + 2] = block[LLPTR + 2]; - - lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); + + gbl_write_block (g, right, block0); } - b_free (g->fd, blknbr); /* modify free list */ + b_free (g, blknbr); /* modify free list */ /* delete pointer */ /**************************/ @@ -2120,8 +2193,7 @@ p_empty: /* entry if pointer block goes blknbr = traceblk[--trx]; addr = traceadr[trx]; - lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + gbl_read_block (g, blknbr, block); offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); freecnt = UNSIGN (block[addr]) + 2 + PLEN; @@ -2133,14 +2205,15 @@ p_empty: /* entry if pointer block goes if (blknbr == ROOT) { /* global went empty */ - lseek (g->fd, hdr_offset + 0L, SEEK_SET); + /* note : UNIX does not tell other */ block[BTYP] = EMPTY; /* jobs that a file has been unlinked */ /* as long as they keep it open. */ /* so we mark this global as EMPTY */ - write (g->fd, block, BLOCKLEN); + gbl_write_block (g, 0L, block); + gbl_close (g); unlink (filnam); @@ -2165,13 +2238,12 @@ p_empty: /* entry if pointer block goes for (i = offset; i < offset + freecnt; block[i++] = 0); - lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); + gbl_write_block (g, blknbr, block); if (addr == 0) { /* update of pointer */ traceadr[trx] = 0; - update (g->fd, &block[2], (long) UNSIGN (block[0])); + update (g, &block[2], (long) UNSIGN (block[0])); } trx = trxsav; @@ -2213,11 +2285,12 @@ p_empty: /* entry if pointer block goes } block[OFFS] = offset / 256; block[OFFS + 1] = offset % 256; - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); + + gbl_write_block (g, blknbr, block); + if (addr < 3) { /* update of pointer */ - traceadr[trx] = 0; - update (g->fd, &block[2], (long) UNSIGN (block[0])); + traceadr[trx] = 0; + update (g, &block[2], (long) UNSIGN (block[0])); } } @@ -2239,9 +2312,8 @@ zinv: data[0] = EOL1; goto quit; } /* no previous block */ - - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + + gbl_read_block (g, blknbr, block); addr = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); @@ -2420,8 +2492,7 @@ zinv: break; /* no next block */ } - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block, BLOCKLEN); + gbl_read_block (g, blknbr, block); addr = 0; offset = UNSIGN (block[OFFS]) * 256 + @@ -2549,7 +2620,7 @@ splitd: /* split data block in two se /* 'addr' there I would like to insert, if possible (which is not) */ /* 'offset' filled up to this limit */ - getnewblk (g->fd, &newblk); /* get a new block */ + getnewblk (g, &newblk); /* get a new block */ /* if we have to insert at the begin or end of a block */ /* we don't split - we just start a new block */ @@ -2567,8 +2638,7 @@ splitd: /* split data block in two se block[RLPTR + 1] = newblk % 65536 / 256; block[RLPTR + 2] = newblk % 256; - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); + gbl_write_block (g, blknbr, block); block[RLPTR] = right; block[RLPTR + 1] = right1; @@ -2580,23 +2650,21 @@ splitd: /* split data block in two se addr = 0; blknbr = newblk; - insert (g->fd, compactkey, keyl, newblk); + insert (g, compactkey, keyl, newblk); /* up-date LL-PTR of RL-block */ if ((other = right * 65536 + right1 * 256 + right2)) { char block0[BLOCKLEN]; - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block0, BLOCKLEN); + gbl_read_block (g, other, block0); block0[LLPTR] = blknbr / 65536; block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); - + gbl_write_block (g, other, block0); + } goto spltex; @@ -2613,9 +2681,8 @@ splitd: /* split data block in two se block[LLPTR + 1] = newblk % 65536 / 256; block[LLPTR + 2] = newblk % 256; - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); - + gbl_write_block (g, blknbr, block); + block[LLPTR] = left; block[LLPTR + 1] = left1; block[LLPTR + 2] = left2; @@ -2626,12 +2693,12 @@ splitd: /* split data block in two se blknbr = newblk; traceadr[trx] = (-1); /* inhibit second update of pointers */ - insert (g->fd, compactkey, keyl, newblk); + insert (g, compactkey, keyl, newblk); if (addr < 3) { /* update of pointer */ traceadr[trx] = 0; - update (g->fd, compactkey, keyl); + update (g, compactkey, keyl); } /* other is ***always*** zero !!! @@ -2739,26 +2806,23 @@ splitd: /* split data block in two se block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); + gbl_write_block (g, newblk, block0); offset = limit; /* insert new block in pointer structure */ - insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk); + insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk); /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block0, BLOCKLEN); + gbl_read_block (g, other, block0); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); + + gbl_write_block (g, other, block0); } @@ -2779,28 +2843,26 @@ splitd: /* split data block in two se block0[LLPTR + 1] = blknbr % 65536 / 256; block0[LLPTR + 2] = blknbr % 256; - lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block, BLOCKLEN); + gbl_write_block (g, blknbr, block); + stcpy0 (block, block0, (long) BLOCKLEN); traceadr[trx] = (addr -= limit); traceblk[trx] = (blknbr = newblk); /* insert new block in pointer structure */ - insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk); + insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk); /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (g->fd, block0, BLOCKLEN); + gbl_read_block (g, other, block0); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - - lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - write (g->fd, block0, BLOCKLEN); + + gbl_write_block (g, other, block0); } @@ -2827,7 +2889,7 @@ spltex: * 'addr' there I would like to insert, if possible (which is not) * 'offset' filled up to this limit */ -static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr) +static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr) { char block0[BLOCKLEN]; @@ -2835,12 +2897,8 @@ static void splitp (short filedes, char unsigned long newblk; unsigned long other; register int i, j; - - long hdr_offset; - - hdr_offset = sizeof (global_header); - getnewblk (filedes, &newblk); /* get a new block */ + getnewblk (g, &newblk); /* get a new block */ if (*blknbr == ROOT) { /* ROOT overflow is special */ @@ -2875,8 +2933,7 @@ static void splitp (short filedes, char block0[NRBLK + 2] = i % 256; block0[BTYP] = POINTER; - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - write (filedes, block0, BLOCKLEN); + gbl_write_block (g, ROOT, block0); /* shift trace_stack */ j = trx++; @@ -2893,7 +2950,7 @@ static void splitp (short filedes, char traceblk[1] = newblk; *blknbr = newblk; - getnewblk (filedes, &newblk); /* get a new block */ + getnewblk (g, &newblk); /* get a new block */ } @@ -2947,25 +3004,22 @@ static void splitp (short filedes, char block0[LLPTR + 1] = (*blknbr) % 65536 / 256; block0[LLPTR + 2] = (*blknbr) % 256; - lseek (filedes, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block0, BLOCKLEN); + gbl_write_block (g, newblk, block0); (*offs) = limit; - insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk); + insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk); /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (filedes, block0, BLOCKLEN); + gbl_read_block (g, other, block0); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - - lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block0, BLOCKLEN); + + gbl_write_block (g, other, block0); } @@ -2989,27 +3043,25 @@ static void splitp (short filedes, char (*addr) -= limit; (*offs) -= limit; + + gbl_write_block (g, *blknbr, block); - lseek (filedes, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block, BLOCKLEN); stcpy0 (block, block0, (long) BLOCKLEN); (*blknbr) = newblk; - insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk); + insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk); /* up-date LL-PTR of RL-block */ if (other != 0) { - lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - read (filedes, block0, BLOCKLEN); + gbl_read_block (g, other, block0); block0[LLPTR] = newblk / 65536; block0[LLPTR + 1] = newblk % 65536 / 256; block0[LLPTR + 2] = newblk % 256; - - lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block0, BLOCKLEN); + + gbl_write_block (g, other, block0); } @@ -3024,16 +3076,13 @@ static void splitp (short filedes, char * ins_key: key to be inserted * keyl: length of that key */ -static void update (short filedes, char *ins_key, long keyl) +static void update (global_handle *g, char *ins_key, long keyl) { long offset; long addr; unsigned long blknbr; char block[BLOCKLEN]; long i, j, k; - long hdr_offset; - - hdr_offset = sizeof (global_header); while (traceadr[trx] == 0) { /* update of pointer blocks necessary */ @@ -3041,9 +3090,8 @@ static void update (short filedes, char blknbr = traceblk[trx]; addr = traceadr[trx]; - - lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (filedes, block, BLOCKLEN); + + gbl_read_block (g, blknbr, block); { long oldkeyl; @@ -3070,7 +3118,7 @@ static void update (short filedes, char else if (j < 0) { /* we need more space */ /* block too small */ - if ((offset - j) > DATALIM) splitp (filedes, block, &addr, &offset, &blknbr); + if ((offset - j) > DATALIM) splitp (g, block, &addr, &offset, &blknbr); i = offset; offset -= j; @@ -3093,13 +3141,10 @@ static void update (short filedes, char while (i < keyl) block[j++] = ins_key[i++]; /* block pointed to remains the same */ - lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block, BLOCKLEN); - + gbl_write_block (g, blknbr, block); } - - lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET); - read (filedes, block, BLOCKLEN); + + gbl_read_block (g, blknbr, block); } @@ -3114,7 +3159,7 @@ static void update (short filedes, char * key_len: length of that key * blknbr: key points to this block */ -static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr) /* insert pointer */ +static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr) /* insert pointer */ { unsigned long blk; char block[BLOCKLEN]; @@ -3123,16 +3168,12 @@ static void insert (int filedes, char *i long needed; long addr; register int i, k; - long hdr_offset; - - hdr_offset = sizeof (global_header); trxsav = trx--; blk = traceblk[trx]; addr = traceadr[trx]; - lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET); - read (filedes, block, BLOCKLEN); + gbl_read_block (g, blk, block); offset = UNSIGN (block[OFFS]) * 256 + UNSIGN (block[OFFS + 1]); @@ -3144,7 +3185,7 @@ static void insert (int filedes, char *i needed = key_len + 2 + PLEN; - if ((offset + needed) > DATALIM) splitp (filedes, block, &addr, &offset, &blk); + if ((offset + needed) > DATALIM) splitp (g, block, &addr, &offset, &blk); /* insert key */ i = (offset += needed); @@ -3168,8 +3209,7 @@ static void insert (int filedes, char *i block[i++] = blknbr % 65536 / 256; block[i] = blknbr % 256; - lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET); - write (filedes, block, BLOCKLEN); + gbl_write_block (g, blk, block); trx = trxsav; @@ -3181,36 +3221,29 @@ static void insert (int filedes, char *i * filedes: global file descriptor * blknbr: block to be freed */ -static void b_free (short filedes, unsigned long blknbr) +static void b_free (global_handle *g, unsigned long blknbr) { char block0[BLOCKLEN]; unsigned long free; unsigned long other; long i; long offset; - long hdr_offset; - - hdr_offset = sizeof (global_header); /* mark block as empty */ - lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET); - read (filedes, block0, BLOCKLEN); - - block0[BTYP] = EMPTY; + gbl_read_block (g, blknbr, block0); - lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET); - write (filedes, block0, BLOCKLEN); + block0[BTYP] = EMPTY; + + gbl_write_block (g, blknbr, block0); /* do we have a list of free blocks? */ - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - read (filedes, block0, BLOCKLEN); + gbl_read_block (g, ROOT, block0); if ((free = UNSIGN (block0[FREE]) * 65536 + UNSIGN (block0[FREE + 1]) * 256 + UNSIGN (block0[FREE + 2]))) { for (;;) { - - lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); - read (filedes, block0, BLOCKLEN); + + gbl_read_block (g, free, block0); other = UNSIGN (block0[RLPTR]) * 65536 + UNSIGN (block0[RLPTR + 1]) * 256 + @@ -3241,9 +3274,8 @@ static void b_free (short filedes, unsig block0[RLPTR] = other / 65536; block0[RLPTR + 1] = other % 65536 / 256; block0[RLPTR + 2] = other % 256; - - lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); - write (filedes, block0, BLOCKLEN); + + gbl_write_block (g, free, block0); for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */ @@ -3259,19 +3291,16 @@ static void b_free (short filedes, unsig } else { - - getnewblk (filedes, &free); + getnewblk (g, &free); /* set FBLK free blocks pointer */ - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - read (filedes, block0, BLOCKLEN); + gbl_read_block (g, ROOT, block0); block0[FREE] = free / 65536; block0[FREE + 1] = free % 65536 / 256; block0[FREE + 2] = free % 256; - - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - write (filedes, block0, BLOCKLEN); + + gbl_write_block (g, ROOT, block0); for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */ @@ -3286,8 +3315,7 @@ static void b_free (short filedes, unsig block0[OFFS] = offset / 256; block0[OFFS + 1] = offset % 256; - lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET); - write (filedes, block0, BLOCKLEN); + gbl_write_block (g, free, block0); return; } /* end of b_free() */ @@ -3407,26 +3435,21 @@ static void scandblk (char *block, long * filedes: global file descriptor * blknbr: number of new block */ -static void getnewblk (int filedes, unsigned long *blknbr) +static void getnewblk (global_handle *g, unsigned long *blknbr) { char nblock[BLOCKLEN]; unsigned long freeblks, no_of_blks; long other; long offset; - long hdr_offset; - hdr_offset = sizeof (global_header); - - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - read (filedes, nblock, BLOCKLEN); + gbl_read_block (g, ROOT, nblock); freeblks = UNSIGN (nblock[FREE]) * 65536 + UNSIGN (nblock[FREE + 1]) * 256 + UNSIGN (nblock[FREE + 2]); no_of_blks = UNSIGN (nblock[NRBLK]) * 65536 + UNSIGN (nblock[NRBLK + 1]) * 256 + UNSIGN (nblock[NRBLK + 2]); if (freeblks) { - - lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET); - read (filedes, nblock, BLOCKLEN); + + gbl_read_block (g, freeblks, nblock); offset = UNSIGN (nblock[OFFS]) * 256 + UNSIGN (nblock[OFFS + 1]); @@ -3438,28 +3461,24 @@ static void getnewblk (int filedes, unsi /* update RL-block, if any */ if (other) { - lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET); - read (filedes, nblock, BLOCKLEN); + gbl_read_block (g, other, nblock); nblock[LLPTR] = 0; nblock[LLPTR + 1] = 0; nblock[LLPTR + 2] = 0; - - lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET); - write (filedes, nblock, BLOCKLEN); + + gbl_write_block (g, other, nblock); } /* update ROOT block */ - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - read (filedes, nblock, BLOCKLEN); + gbl_read_block (g, ROOT, nblock); nblock[FREE] = other / 65536; nblock[FREE + 1] = other % 65536 / 256; nblock[FREE + 2] = other % 256; - - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - write (filedes, nblock, BLOCKLEN); + + gbl_write_block (g, ROOT, nblock); return; @@ -3471,9 +3490,8 @@ static void getnewblk (int filedes, unsi nblock[offset + 1] = 0; nblock[OFFS] = offset / 256; nblock[OFFS + 1] = offset % 256; - - lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET); - write (filedes, nblock, BLOCKLEN); + + gbl_write_block (g, freeblks, nblock); return; @@ -3484,24 +3502,12 @@ static void getnewblk (int filedes, unsi nblock[NRBLK] = no_of_blks / 65536; nblock[NRBLK + 1] = no_of_blks % 65536 / 256; nblock[NRBLK + 2] = no_of_blks % 256; - - lseek (filedes, hdr_offset + ROOT, SEEK_SET); - write (filedes, nblock, BLOCKLEN); + + gbl_write_block (g, ROOT, nblock); *blknbr = no_of_blks; - - for (;;) { - errno = 0; - - lseek (filedes, hdr_offset + ((long) (no_of_blks) * BLOCKLEN), SEEK_SET); - write (filedes, nblock, BLOCKLEN); - - if (errno == 0) break; - - panic (); - - } + gbl_write_block (g, no_of_blks, nblock); return; @@ -3630,7 +3636,6 @@ static short int g_collate (char *t) /* * test whether 'str' is canonical */ -//static short int g_numeric (char *str) short g_numeric (char *str) { register int ptr = 0, ch; @@ -3706,14 +3711,39 @@ static void panic (void) void gbl_dump_stat(void) { global_handle *g; + int ct; + int miss_pct; + int hit_pct; + unsigned long access_total; + unsigned long hit_total; - printf ("FreeM Global Statistics [PID %d]\r\n", pid); + printf ("\r\nFreeM Global Statistics [PID %d]\r\n\r\n", pid); - printf ("%-20s%-10s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "AGE", "LAST BLK", "FILE"); - printf ("%-20s%-10s%-20s%-10s%s\r\n", "======", "=====", "===", "========", "===="); - + printf ("%-20s%-10s%-10s%-10s%-12s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "READS", "WRITES", "SLOW PTHCT", "AGE", "LAST BLK", "FILE"); + printf ("%-20s%-10s%-10s%-10s%-12s%-20s%-10s%s\r\n", "======", "=====", "=====", "======", "==========", "===", "========", "===="); + + access_total = 0; + ct = 0; for (g = global_handles_head; g != NULL; g = g->next) { - printf ("%-20s%-10d%-20d%-10d%s\r\n", g->global_name, g->use_count, g->age, g->last_block, g->global_path); - } - + printf ("%-20s%-10ld%-10ld%-10ld%-12ld%-20ld%-10ld%s\r\n", + g->global_name, + g->use_count, + g->read_ops, + g->write_ops, + g->cache_misses, + g->age, + g->last_block, + g->global_path); + ct++; + access_total += g->use_count; + } + if (!ct) printf ("\r\n"); + + hit_total = access_total - gbl_cache_misses; + miss_pct = (gbl_cache_misses * 100) / access_total; + hit_pct = (hit_total * 100) / access_total; + + printf ("\r\nTotal accesses: %ld\r\n", access_total); + printf ("Fast path hits %ld\t(%d%%)\r\n", hit_total, hit_pct); + printf ("Fast path misses: %ld\t(%d%%)\r\n", gbl_cache_misses, miss_pct); }