Diff for /freem/src/global_bltin.c between versions 1.12 and 1.16

version 1.12, 2025/04/09 00:43:07 version 1.16, 2025/04/11 00:52:40
Line 24 Line 24
  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.   *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
  *   *
  *   $Log$   *   $Log$
    *   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   *   Revision 1.12  2025/04/09 00:43:07  snw
  *   Exit with fatal error if a header mismatch found   *   Exit with fatal error if a header mismatch found
  *   *
Line 69 Line 81
 #include "global_bltin.h"  #include "global_bltin.h"
   
 global_handle *global_handles_head;  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 b_free (short filedes, unsigned long blknbr);
 static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);  static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);
Line 78  static void scanpblk (char *block, long Line 92  static void scanpblk (char *block, long
 static void scandblk (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 (int filedes, unsigned long *blknbr);
 static short int g_collate (char *t);  static short int g_collate (char *t);
 //static short int g_numeric (char *str);  
 short g_numeric (char *str);  short g_numeric (char *str);
 void close_all_globals(void);  void close_all_globals(void);
 static void panic (void);  static void panic (void);
Line 129  static void panic (void); Line 142  static void panic (void);
 inline long gbl_path(char *key, char *buf)  inline long gbl_path(char *key, char *buf)
 {  {
     long savj;      long savj;
     long savch;                 /* saved j and ch for multiple pathes */  
   
     register long int i;      register long int i;
     register long int j;      register long int j;
     register long int k;      register long int k;
     register long int ch;      register long int ch;
     long pathscan;                      /* flag for repeated scan of pathlist setting an undef global */  
         
     /* construct full UNIX filename */      /* construct full UNIX filename */
     savj = 0;      savj = 0;
     savch = ch = EOL;  
     pathscan = TRUE;  
 nextpath:  
     k = 0;      k = 0;
     j = savj;      j = savj;
   
Line 165  nextpath: Line 173  nextpath:
         }          }
   
     }      }
   
     if (savj == 0 && ch == EOL) pathscan = FALSE;               /* one path only: inhibit search */  
           
     if (k > 0) {      if (k > 0) {
   
Line 179  nextpath: Line 185  nextpath:
   
     }      }
   
     savch = ch;  
     savj = j;      savj = j;
     i = 0;      i = 0;
     j = 0;      j = 0;
Line 212  nextpath: Line 217  nextpath:
     return i;      return i;
 } /* gbl_path() */  } /* 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)  int gbl_lock(global_handle *g, int type)
 {  {
     if (g->locked == TRUE || lonelyflag == TRUE) {      if (g->locked == TRUE || lonelyflag == TRUE) {
Line 220  int gbl_lock(global_handle *g, int type) Line 238  int gbl_lock(global_handle *g, int type)
   
     locking (g->fd, type, 0L);      locking (g->fd, type, 0L);
     g->locked = TRUE;      g->locked = TRUE;
   
       return TRUE;
 } /* gbl_lock() */  } /* gbl_lock() */
   
 int gbl_unlock(global_handle *g)  int gbl_unlock(global_handle *g)
Line 230  int gbl_unlock(global_handle *g) Line 250  int gbl_unlock(global_handle *g)
   
     locking (g->fd, 0, 0L);      locking (g->fd, 0, 0L);
     g->locked = FALSE;      g->locked = FALSE;
   
       return TRUE;
 } /* gbl_unlock() */  } /* gbl_unlock() */
   
 void gbl_close(global_handle *g)  void gbl_close(global_handle *g)
Line 379  int gbl_create(global_handle *g) Line 401  int gbl_create(global_handle *g)
     g->age = time (0L);      g->age = time (0L);
     g->last_block = 0;      g->last_block = 0;
     g->use_count = 1;      g->use_count = 1;
       g->fast_path = 0;
       
     gbl_write_initial_header (g);      gbl_write_initial_header (g);
           
     return OK;      return OK;
Line 390  short gbl_open(global_handle *g, short a Line 413  short gbl_open(global_handle *g, short a
     int result;      int result;
           
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
           gbl_cache_miss (g);
         while (1) {          while (1) {
             errno = 0;              errno = 0;
             g->fd = open (g->global_path, 2);              g->fd = open (g->global_path, 2);
Line 450  short gbl_open(global_handle *g, short a Line 474  short gbl_open(global_handle *g, short a
           
 } /* gbl_open() */  } /* 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;    
   }
   
 global_handle *gbl_handle(char *key)  global_handle *gbl_handle(char *key)
 {  {
     global_handle *g;      global_handle *g;
     char global_name[256];      char global_name[256];
     int i;      int i;
     long path_len;  
     char block[BLOCKLEN];  
     struct stat dinf;                  struct stat dinf;            
           
     i = 0;      i = 0;
Line 497  global_handle *gbl_handle(char *key) Line 539  global_handle *gbl_handle(char *key)
     g->opened = FALSE;      g->opened = FALSE;
     g->fd = 0;      g->fd = 0;
     g->fast_path = -1;      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);          strcpy (g->global_name, global_name);    
     gbl_path (key, g->global_path);      gbl_path (key, g->global_path);
Line 591  void global_bltin (short action, char *k Line 637  void global_bltin (short action, char *k
     unsigned long hdr_offset;      unsigned long hdr_offset;
           
     /* these must be static variables */      /* these must be static variables */
     static short filedes;               /* filedescr for global access */  
     static char filnam[256];            /* name of global/unix file */      static char filnam[256];            /* name of global/unix file */
   
     /* the following vars may be */      /* the following vars may be */
Line 620  void global_bltin (short action, char *k Line 665  void global_bltin (short action, char *k
   
     int iresult;      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 i;
     register long int j;      register long int j;
     register long int k;      register long int k;
     register long int ch;      register long int ch;
     long pathscan;                      /* flag for repeated scan of pathlist setting an undef global */  
   
       j = 0;
       
     hdr_offset = sizeof (global_header);      hdr_offset = sizeof (global_header);
           
     /* process optional limitations */      /* process optional limitations */
Line 1042  reopen: Line 1084  reopen:
     /* odd numbered actions get read access (get_sym,data,fra_order) 3 */      /* odd numbered actions get read access (get_sym,data,fra_order) 3 */
     /* even ones read/write access          (set_sym,kill_sym)   1 */      /* even ones read/write access          (set_sym,kill_sym)   1 */
   
   /* temporarily disabled    
 lock:  lock:
   */
   
     if (action == get_sym) {      if (action == get_sym) {
   
 tfast0:  tfast0:
         gbl_lock (g, 3);          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 */          blknbr = g->last_block = ROOT;          /* start with ROOT block */
                   
Line 1056  tfast0: Line 1101  tfast0:
   
   
 tfast1:  tfast1:
               gbl_read_block (g, blknbr, block);
   
             lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);  /* temporarily disabled
             read (g->fd, block, BLOCKLEN);  
   
   
 tfast2:  tfast2:
   */
         if ((typ = block[BTYP]) == DATA) {      /* scan data block: here we test for equality only */          if ((typ = block[BTYP]) == DATA) {      /* scan data block: here we test for equality only */
   
             offset = UNSIGN (block[OFFS]) * 256 +                          offset = UNSIGN (block[OFFS]) * 256 +            
Line 1079  tfast2: Line 1122  tfast2:
                 j = UNSIGN (block[i++]);        /* length of key - offset */                  j = UNSIGN (block[i++]);        /* length of key - offset */
                 k = UNSIGN (block[i++]);        /* offset into previous entry */                  k = UNSIGN (block[i++]);        /* offset into previous entry */
   
 #ifdef VERSNEW  
   
                 stcpy0 (&key0[k], &block[i], j);  
                 i += j;  
                 j += k;  
   
 #else  
   
                 j += k;                  j += k;
                           
                 while (k < j) key1[k++] = block[i++];           /* get key */                  while (k < j) key1[k++] = block[i++];           /* get key */
   
 #endif /* VERSNEW */  
   
                 if (j != ch) {  /* keys have different length */                  if (j != ch) {  /* keys have different length */
                                           
                     i += UNSIGN (block[i]);                      i += UNSIGN (block[i]);
Line 1126  tfast2: Line 1159  tfast2:
   
             /* fast access failed. try normal path */              /* fast access failed. try normal path */
             if (tryfast) {              if (tryfast) {
                 tryfast = FALSE;                  gbl_cache_miss (g);
                 goto tfast0;                  goto tfast0;
             }              }
   
Line 1137  tfast2: Line 1170  tfast2:
         }           } 
         else {          else {
                   
             if (tryfast) {              if (g->fast_path > 0) {
                 tryfast = FALSE;                  gbl_cache_miss (g);
                 goto tfast0;                  goto tfast0;
             }              }
   
             if (typ == EMPTY) {              if (typ == EMPTY) {
                                   
                 if (blknbr == ROOT) {                  if (blknbr == ROOT) {
                     //close (filedes);  
                     gbl_close (g);                      gbl_close (g);
                     goto reopen;                      goto reopen;
                 }                  }
Line 1168  tfast2: Line 1200  tfast2:
   
         addr += PLEN;           /* skip data */          addr += PLEN;           /* skip data */
         g->last_block = blknbr;          g->last_block = blknbr;
           g->fast_path = 1;
                   
         if (merr () == INRPT) goto quit;          if (merr () == INRPT) goto quit;
   
Line 1211  k_again:    /* entry point for repeated Line 1244  k_again:    /* entry point for repeated
   
         traceblk[trx] = blknbr;          traceblk[trx] = blknbr;
         traceadr[trx] = 0;          traceadr[trx] = 0;
           
         lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);          gbl_read_block (g, blknbr, block);
         read (g->fd, block, BLOCKLEN);  
           
         typ = block[BTYP];          typ = block[BTYP];
                   
         if (typ == DATA) {          if (typ == DATA) {
Line 1246  k_again:    /* entry point for repeated Line 1277  k_again:    /* entry point for repeated
   
         addr += PLEN;                   /* skip data */          addr += PLEN;                   /* skip data */
         g->last_block = blknbr;          g->last_block = blknbr;
           g->fast_path = 1;
     }      }
   
     traceadr[trx] = addr;      traceadr[trx] = addr;
Line 1501  s20: Line 1533  s20:
                 if (addr >= offset) {                  if (addr >= offset) {
   
                     if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2]))) {                      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);                          gbl_read_block (g, blknbr, block);
                         read (g->fd, block, BLOCKLEN);  
                         j1 = UNSIGN (block[0]);                          j1 = UNSIGN (block[0]);
                                           
                         i = 0;                          i = 0;
Line 1584  s20: Line 1615  s20:
                                   
                     goto quit;                      goto quit;
                 }       /* no next block */                  }       /* no next block */
                   
                 lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                  gbl_read_block (g, blknbr, block);
                 read (g->fd, block, BLOCKLEN);  
                 scandblk (block, &addr, &found);                  scandblk (block, &addr, &found);
   
             }               } 
Line 1742  s20: Line 1772  s20:
                         goto quit;              /* no next block */                          goto quit;              /* no next block */
                     }                      }
   
                     lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                      gbl_read_block (g, blknbr, block);
                     read (g->fd, block, BLOCKLEN);  
   
                     addr = 0;                      addr = 0;
                     offset = UNSIGN (block[OFFS]) * 256 +                      offset = UNSIGN (block[OFFS]) * 256 +
Line 1766  s20: Line 1795  s20:
                     goto quit;          /* no next block */                      goto quit;          /* no next block */
                 }                  }
   
                 lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                  gbl_read_block (g, blknbr, block);
                 read (g->fd, block, BLOCKLEN);  
                                   
                 addr = 0;                  addr = 0;
             }               } 
Line 1976  killo:    /* entry from killone section Line 2004  killo:    /* entry from killone section
   
                         other = traceblk[--trx];                          other = traceblk[--trx];
                         addr = traceadr[trx];                          addr = traceadr[trx];
                           
                         lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);                          gbl_read_block (g, other, block);
                         read (g->fd, block, BLOCKLEN);  
                                                   
                         addr += UNSIGN (block[addr]);                          addr += UNSIGN (block[addr]);
                         addr += (2 + PLEN);     /* skip previous entry */                          addr += (2 + PLEN);     /* skip previous entry */
Line 1997  killo:    /* entry from killone section Line 2024  killo:    /* entry from killone section
   
                     trx = trxsav;                      trx = trxsav;
   
                     lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                      gbl_read_block (g, blknbr, block);
                     read (g->fd, block, BLOCKLEN);  
   
                     offset = UNSIGN (block[OFFS]) * 256 +                      offset = UNSIGN (block[OFFS]) * 256 +
                     UNSIGN (block[OFFS + 1]);                      UNSIGN (block[OFFS + 1]);
Line 2104  p_empty:  /* entry if pointer block goes Line 2130  p_empty:  /* entry if pointer block goes
   
                     if (left) {                      if (left) {
   
                         lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET);                          gbl_read_block (g, left, block0);
                         read (g->fd, block0, BLOCKLEN);  
                                                   
                         block0[RLPTR] = block[RLPTR];                          block0[RLPTR] = block[RLPTR];
                         block0[RLPTR + 1] = block[RLPTR + 1];                          block0[RLPTR + 1] = block[RLPTR + 1];
Line 2117  p_empty:  /* entry if pointer block goes Line 2142  p_empty:  /* entry if pointer block goes
                     }                      }
   
                     if (right) {                      if (right) {
                           
                         lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET);                          gbl_read_block (g, right, block0);
                         read (g->fd, block0, BLOCKLEN);  
                                                   
                         block0[LLPTR] = block[LLPTR];                          block0[LLPTR] = block[LLPTR];
                         block0[LLPTR + 1] = block[LLPTR + 1];                          block0[LLPTR + 1] = block[LLPTR + 1];
Line 2143  p_empty:  /* entry if pointer block goes Line 2167  p_empty:  /* entry if pointer block goes
                         blknbr = traceblk[--trx];                          blknbr = traceblk[--trx];
                         addr = traceadr[trx];                          addr = traceadr[trx];
   
                         lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET);                          gbl_read_block (g, blknbr, block);
                         read (g->fd, block, BLOCKLEN);  
                         offset = UNSIGN (block[OFFS]) * 256 +                          offset = UNSIGN (block[OFFS]) * 256 +
                         UNSIGN (block[OFFS + 1]);                          UNSIGN (block[OFFS + 1]);
                         freecnt = UNSIGN (block[addr]) + 2 + PLEN;                          freecnt = UNSIGN (block[addr]) + 2 + PLEN;
Line 2262  zinv: Line 2285  zinv:
                         data[0] = EOL1;                          data[0] = EOL1;
                         goto quit;                          goto quit;
                     }                   /* no previous block */                      }                   /* no previous block */
                       
                     lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                      gbl_read_block (g, blknbr, block);
                     read (g->fd, block, BLOCKLEN);  
                                           
                     addr = UNSIGN (block[OFFS]) * 256 +                      addr = UNSIGN (block[OFFS]) * 256 +
                     UNSIGN (block[OFFS + 1]);                      UNSIGN (block[OFFS + 1]);
Line 2443  zinv: Line 2465  zinv:
                             break;              /* no next block */                              break;              /* no next block */
                         }                          }
   
                         lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);                          gbl_read_block (g, blknbr, block);
                         read (g->fd, block, BLOCKLEN);  
                                                   
                         addr = 0;                          addr = 0;
                         offset = UNSIGN (block[OFFS]) * 256 +                          offset = UNSIGN (block[OFFS]) * 256 +
Line 2610  splitd:    /* split data block in two se Line 2631  splitd:    /* split data block in two se
                   
             char    block0[BLOCKLEN];              char    block0[BLOCKLEN];
   
             lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);              gbl_read_block (g, other, block0);
             read (g->fd, block0, BLOCKLEN);  
                   
             block0[LLPTR] = blknbr / 65536;              block0[LLPTR] = blknbr / 65536;
             block0[LLPTR + 1] = blknbr % 65536 / 256;              block0[LLPTR + 1] = blknbr % 65536 / 256;
Line 2773  splitd:    /* split data block in two se Line 2793  splitd:    /* split data block in two se
             /* up-date LL-PTR of RL-block */              /* up-date LL-PTR of RL-block */
             if (other != 0) {              if (other != 0) {
   
                 lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);                  gbl_read_block (g, other, block0);
                 read (g->fd, block0, BLOCKLEN);  
   
                 block0[LLPTR] = newblk / 65536;                  block0[LLPTR] = newblk / 65536;
                 block0[LLPTR + 1] = newblk % 65536 / 256;                  block0[LLPTR + 1] = newblk % 65536 / 256;
Line 2815  splitd:    /* split data block in two se Line 2834  splitd:    /* split data block in two se
             /* up-date LL-PTR of RL-block */              /* up-date LL-PTR of RL-block */
             if (other != 0) {              if (other != 0) {
   
                 lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);                  gbl_read_block (g, other, block0);
                 read (g->fd, block0, BLOCKLEN);  
                                   
                 block0[LLPTR] = newblk / 65536;                  block0[LLPTR] = newblk / 65536;
                 block0[LLPTR + 1] = newblk % 65536 / 256;                  block0[LLPTR + 1] = newblk % 65536 / 256;
Line 3653  static short int g_collate (char *t) Line 3671  static short int g_collate (char *t)
 /*  /*
  * test whether 'str' is canonical   * test whether 'str' is canonical
  */   */
 //static short int g_numeric (char *str)  
 short g_numeric (char *str)  short g_numeric (char *str)
 {  {
     register int ptr = 0, ch;      register int ptr = 0, ch;
Line 3729  static void panic (void) Line 3746  static void panic (void)
 void gbl_dump_stat(void)  void gbl_dump_stat(void)
 {  {
     global_handle *g;      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%-12s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "SLOW PTHCT", "AGE", "LAST BLK", "FILE");
     printf ("%-20s%-10s%-20s%-10s%s\r\n", "======", "=====", "===", "========", "====");      printf ("%-20s%-10s%-12s%-20s%-10s%s\r\n", "======", "=====", "==========", "===", "========", "====");
       
       access_total = 0;
       ct = 0;
     for (g = global_handles_head; g != NULL; g = g->next) {      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%-12ld%-20ld%-10ld%s\r\n",
     }                  g->global_name,
                       g->use_count,
                   g->cache_misses,
                   g->age,
                   g->last_block,
                   g->global_path);
           ct++;
           access_total += g->use_count;      
       }
       if (!ct) printf ("<no globals opened in this pid>\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);    
 }  }

Removed from v.1.12  
changed lines
  Added in v.1.16


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>