home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d170 / dis6502.lha / Dis6502 / ref.c < prev    next >
C/C++ Source or Header  |  1988-11-22  |  1KB  |  91 lines

  1. #include "dis.h"
  2.  
  3. #define HTSIZE 0x1000            /* Power of 2 */
  4. #define HTMASK (HTSIZE-1)
  5.  
  6. struct hashslot {
  7.     int addr;            /* The key */
  8.     struct ref_chain *ref;        /* Who references it */
  9.     char *name;            /* The symbolic name (if it has one) */
  10. };
  11.  
  12. struct   hashslot hashtbl[HTSIZE];    /* the hash table */
  13.  
  14. struct hashslot *
  15. hash(loc, allocate)
  16. int loc;
  17. int allocate;
  18. {
  19.     int probes;
  20.     register struct hashslot *hp;
  21.  
  22.     hp = &hashtbl[loc & HTMASK];
  23.     probes = 0;
  24.  
  25.     while (probes< HTSIZE) {
  26.         if (hp->addr == loc)
  27.             return(hp);
  28.         if (hp->name == NULL && hp->ref == NULL) {
  29.             if (allocate) {
  30.                 hp->addr = loc;
  31.                 return(hp);
  32.             } else {
  33.                 return(NULL);
  34.             }
  35.         }
  36.         hp++;
  37.         if (hp == &hashtbl[HTSIZE])
  38.             hp = &hashtbl[0];
  39.         probes++;
  40.     }
  41.  
  42.     crash("Hash table full");
  43.     /*NOTREACHED*/
  44. }
  45.  
  46. save_ref(refer, refee) 
  47. int refer;
  48. int refee;
  49. {
  50.     struct ref_chain *rc;
  51.     struct hashslot *hp;
  52.  
  53.     rc = (struct ref_chain *)emalloc(sizeof(*rc));
  54.     rc->who = refer;
  55.     hp = hash(refee, 1);
  56.     rc->next = hp->ref;
  57.     hp->ref = rc;
  58. }
  59.  
  60. save_name(loc, name)
  61. int loc;
  62. char *name;
  63. {
  64.     struct hashslot *hp;
  65.  
  66.     hp = hash(loc, 1);
  67.     hp->name = name;
  68. }
  69.  
  70. struct ref_chain *
  71. get_ref(loc)
  72. {
  73.     struct hashslot *hp;
  74.  
  75.     hp = hash(loc, 0);
  76.     if (!hp) 
  77.         return(NULL);
  78.     return(hp->ref);
  79. }
  80.  
  81. char *
  82. get_name(loc)
  83. {
  84.     struct hashslot *hp;
  85.  
  86.     hp = hash(loc, 0);
  87.     if (!hp) 
  88.         return(NULL);
  89.     return(hp->name);
  90. }
  91.