PennMUSH Community
Show
Ignore:
Timestamp:
06/12/07 15:21:47 (1 year ago)
Author:
shawnw
Message:

1.8.3p3

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 1.8.3/trunk/src/chunk.c

    r846 r919  
    238238 
    239239#ifdef WIN32 
    240 #pragma warning( disable : 4761)   /* disable warning re conversion */ 
     240#pragma warning( disable : 4761)        /* disable warning re conversion */ 
    241241#endif 
    242242 
     
    265265#ifdef CHUNK_DEBUG 
    266266#define ASSERT(x) assert(x) 
    267 #else              /* CHUNK_DEBUG */ 
    268 static int ignore; /**< Used to shut up compiler warnings when not asserting */ 
     267#else                           /* CHUNK_DEBUG */ 
     268static int ignore;      /**< Used to shut up compiler warnings when not asserting */ 
    269269#define ASSERT(x) ignore++ 
    270 #endif             /* CHUNK_DEBUG */ 
     270#endif                          /* CHUNK_DEBUG */ 
    271271 
    272272/* 
     
    531531 */ 
    532532typedef struct region_header { 
    533   uint16_t region_id;      /**< will be INVALID_REGION_ID if not in use */ 
    534   uint16_t first_free;     /**< offset of 1st free chunk */ 
    535   struct region_header *prev;  /**< linked list prev for LRU cache */ 
    536   struct region_header *next;  /**< linked list next for LRU cache */ 
     533  uint16_t region_id;           /**< will be INVALID_REGION_ID if not in use */ 
     534  uint16_t first_free;          /**< offset of 1st free chunk */ 
     535  struct region_header *prev;   /**< linked list prev for LRU cache */ 
     536  struct region_header *next;   /**< linked list next for LRU cache */ 
    537537} RegionHeader; 
    538538 
     
    541541/** In-memory (never paged) region info.  */ 
    542542typedef struct region { 
    543   uint16_t used_count;     /**< number of used chunks */ 
    544   uint16_t free_count;     /**< number of free chunks */ 
    545   uint16_t free_bytes;     /**< number of free bytes (with headers) */ 
    546   uint16_t largest_free_chunk; /**< largest single free chunk */ 
    547   uint32_t total_derefs;   /**< total of all used chunk derefs */ 
    548   uint32_t period_last_touched;    /**< "this" period, for deref counts; 
     543  uint16_t used_count;          /**< number of used chunks */ 
     544  uint16_t free_count;          /**< number of free chunks */ 
     545  uint16_t free_bytes;          /**< number of free bytes (with headers) */ 
     546  uint16_t largest_free_chunk;  /**< largest single free chunk */ 
     547  uint32_t total_derefs;        /**< total of all used chunk derefs */ 
     548  uint32_t period_last_touched; /**< "this" period, for deref counts; 
    549549                                           we don't page in regions to update 
    550550                                           counts on period change! */ 
    551   RegionHeader *in_memory; /**< cache entry; NULL if paged out */ 
    552   uint16_t oddballs[NUM_ODDBALLS]; /**< chunk offsets with odd derefs */ 
     551  RegionHeader *in_memory;      /**< cache entry; NULL if paged out */ 
     552  uint16_t oddballs[NUM_ODDBALLS];      /**< chunk offsets with odd derefs */ 
    553553} Region; 
    554554 
     
    588588 * Info about all regions 
    589589 */ 
    590 static uint32_t region_count;  /**< regions in use */ 
    591 static uint32_t region_array_len;  /**< length of regions array */ 
    592 static Region *regions;        /**< regions array, realloced as (rarely) needed */ 
     590static uint32_t region_count;   /**< regions in use */ 
     591static uint32_t region_array_len;       /**< length of regions array */ 
     592static Region *regions;         /**< regions array, realloced as (rarely) needed */ 
    593593 
    594594/* 
    595595 * regions presently in memory 
    596596 */ 
    597 static uint32_t cached_region_count;   /**< number of regions in cache */ 
    598 static RegionHeader *cache_head;   /**< most recently used region */ 
    599 static RegionHeader *cache_tail;   /**< least recently used region */ 
     597static uint32_t cached_region_count;    /**< number of regions in cache */ 
     598static RegionHeader *cache_head;        /**< most recently used region */ 
     599static RegionHeader *cache_tail;        /**< least recently used region */ 
    600600 
    601601/* 
    602602 * statistics 
    603603 */ 
    604 static int stat_used_short_count;  /**< How many short chunks? */ 
    605 static int stat_used_short_bytes;  /**< How much space in short chunks? */ 
    606 static int stat_used_medium_count; /**< How many medium chunks? */ 
    607 static int stat_used_medium_bytes; /**< How much space in medium chunks? */ 
    608 static int stat_used_long_count;   /**< How many long chunks? */ 
    609 static int stat_used_long_bytes;   /**< How much space in long chunks? */ 
    610 static int stat_deref_count;       /**< Dereferences this period */ 
    611 static int stat_deref_maxxed;      /**< Number of chunks with max derefs */ 
     604static int stat_used_short_count;       /**< How many short chunks? */ 
     605static int stat_used_short_bytes;       /**< How much space in short chunks? */ 
     606static int stat_used_medium_count;      /**< How many medium chunks? */ 
     607static int stat_used_medium_bytes;      /**< How much space in medium chunks? */ 
     608static int stat_used_long_count;        /**< How many long chunks? */ 
     609static int stat_used_long_bytes;        /**< How much space in long chunks? */ 
     610static int stat_deref_count;            /**< Dereferences this period */ 
     611static int stat_deref_maxxed;           /**< Number of chunks with max derefs */ 
    612612/** histogram for average derefs of regions being paged in/out */ 
    613613static int stat_paging_histogram[CHUNK_DEREF_MAX + 1]; 
    614 static int stat_page_out;      /**< Number of page-outs */ 
    615 static int stat_page_in;       /**< Number of page-ins */ 
    616 static int stat_migrate_slide;     /**< Number of slide migrations */ 
    617 static int stat_migrate_move;      /**< Number of move migrations */ 
    618 static int stat_migrate_away;      /**< Number of chunk evictions */ 
    619 static int stat_create;            /**< Number of chunk creations */ 
    620 static int stat_delete;            /**< Number of chunk deletions */ 
     614static int stat_page_out;               /**< Number of page-outs */ 
     615static int stat_page_in;                /**< Number of page-ins */ 
     616static int stat_migrate_slide;          /**< Number of slide migrations */ 
     617static int stat_migrate_move;           /**< Number of move migrations */ 
     618static int stat_migrate_away;           /**< Number of chunk evictions */ 
     619static int stat_create;                 /**< Number of chunk creations */ 
     620static int stat_delete;                 /**< Number of chunk deletions */ 
    621621 
    622622 
     
    625625 * migration globals that are used for holding relevant data... 
    626626 */ 
    627 static int m_count;        /**< The used length for the arrays. */ 
     627static int m_count;             /**< The used length for the arrays. */ 
    628628static chunk_reference_t **m_references; /**< The passed-in references array. */ 
    629629 
     
    662662#else 
    663663  if (format) 
    664     return;            /* shut up the compiler warning */ 
     664    return;                     /* shut up the compiler warning */ 
    665665#endif 
    666666} 
     
    717717 
    718718  fprintf(fp, "region: id:%04x period:%-8x deref:%-8x (%-2x per chunk)\n", 
    719      region, rp->period_last_touched, rp->total_derefs, 
    720      RegionDerefs(region)); 
     719          region, rp->period_last_touched, rp->total_derefs, 
     720          RegionDerefs(region)); 
    721721  fprintf(fp, "        #used:%-4x #free:%-4x fbytes:%-4x hole:%-4x ", 
    722      rp->used_count, rp->free_count, rp->free_bytes, 
    723      rp->largest_free_chunk); 
     722          rp->used_count, rp->free_count, rp->free_bytes, 
     723          rp->largest_free_chunk); 
    724724  if (rhp) 
    725725    fprintf(fp, "first:%-4x h_id:%-4x\n", rhp->first_free, rhp->region_id); 
     
    730730  if (rhp) { 
    731731    for (offset = FIRST_CHUNK_OFFSET_IN_REGION; 
    732     offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
     732        offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
    733733      fprintf(fp, "chunk:%c%4s %-6s off:%04x full:%04x ", 
    734          migratable(region, offset) ? '*' : ' ', 
    735          ChunkIsFree(region, offset) ? "FREE" : "", 
    736          ChunkIsShort(region, offset) ? "SHORT" : 
    737          (ChunkIsMedium(region, offset) ? "MEDIUM" : "LONG"), 
    738          offset, ChunkFullLen(region, offset)); 
     734              migratable(region, offset) ? '*' : ' ', 
     735              ChunkIsFree(region, offset) ? "FREE" : "", 
     736              ChunkIsShort(region, offset) ? "SHORT" : 
     737              (ChunkIsMedium(region, offset) ? "MEDIUM" : "LONG"), 
     738              offset, ChunkFullLen(region, offset)); 
    739739      if (ChunkIsFree(region, offset)) { 
    740    fprintf(fp, "next:%04x\n", ChunkNextFree(region, offset)); 
     740        fprintf(fp, "next:%04x\n", ChunkNextFree(region, offset)); 
    741741      } else { 
    742    fprintf(fp, "doff:%04x len:%04x ", 
    743        ChunkDataPtr(region, offset) - (unsigned char *) rhp, 
    744        ChunkLen(region, offset)); 
    745    count = ChunkDerefs(region, offset); 
    746    if (count == 0xFF) { 
    747      fprintf(fp, "deref:many\n"); 
    748    } else { 
    749      fprintf(fp, "deref:%04x\n", count); 
    750    
     742        fprintf(fp, "doff:%04x len:%04x ", 
     743                ChunkDataPtr(region, offset) - (unsigned char *) rhp, 
     744                ChunkLen(region, offset)); 
     745        count = ChunkDerefs(region, offset); 
     746        if (count == 0xFF) { 
     747          fprintf(fp, "deref:many\n"); 
     748        } else { 
     749          fprintf(fp, "deref:%04x\n", count); 
     750       
    751751      } 
    752752    } 
     
    770770    if (pos == offset) { 
    771771      if (ChunkIsFree(region, pos)) 
    772    mush_panic("Invalid reference to free chunk as used"); 
     772        mush_panic("Invalid reference to free chunk as used"); 
    773773      return; 
    774774    } 
     
    803803  if (region >= region_count) { 
    804804    do_rawlog(LT_ERR, "region 0x%04x is not valid: region_count is 0x%04x", 
    805          region, region_count); 
     805              region, region_count); 
    806806    return 0; 
    807807  } 
     
    811811  if (rp->used_count > REGION_SIZE / MIN_REMNANT_LEN) { 
    812812    do_rawlog(LT_ERR, 
    813          "region 0x%04x is not valid: chunk count is ludicrous: 0x%04x", 
    814          region, rp->used_count); 
     813              "region 0x%04x is not valid: chunk count is ludicrous: 0x%04x", 
     814              region, rp->used_count); 
    815815    result = 0; 
    816816  } 
    817817  if (rp->free_count > REGION_SIZE / MIN_REMNANT_LEN) { 
    818818    do_rawlog(LT_ERR, 
    819          "region 0x%04x is not valid: free count is ludicrous: 0x%04x", 
    820          region, rp->free_count); 
     819              "region 0x%04x is not valid: free count is ludicrous: 0x%04x", 
     820              region, rp->free_count); 
    821821    result = 0; 
    822822  } 
    823823  if (rp->largest_free_chunk > rp->free_bytes) { 
    824824    do_rawlog(LT_ERR, 
    825          "region 0x%04x is not valid: largest free chunk > free bytes:" 
    826          " 0x%04x > 0x%04x", 
    827          region, rp->largest_free_chunk, rp->free_bytes); 
     825              "region 0x%04x is not valid: largest free chunk > free bytes:" 
     826              " 0x%04x > 0x%04x", 
     827              region, rp->largest_free_chunk, rp->free_bytes); 
    828828    result = 0; 
    829829  } 
     
    835835  if (rhp->region_id != region) { 
    836836    do_rawlog(LT_ERR, "region 0x%04x is not valid: region in cache is 0x%04x", 
    837          region, rhp->region_id); 
     837              region, rhp->region_id); 
    838838    result = 0; 
    839839  } 
     
    850850    if (was_free && ChunkIsFree(region, offset)) { 
    851851      do_rawlog(LT_ERR, 
    852        "region 0x%04x is not valid: uncoalesced free chunk:" 
    853        " 0x%04x (see map)", region, offset); 
     852                "region 0x%04x is not valid: uncoalesced free chunk:" 
     853                " 0x%04x (see map)", region, offset); 
    854854      result = 0; 
    855855      dump = 1; 
     
    861861      free_bytes += len; 
    862862      if (largest_free < len) 
    863    largest_free = len; 
     863        largest_free = len; 
    864864      if (next_free != offset) { 
    865    do_rawlog(LT_ERR, 
    866          "region 0x%04x is not valid: free chain broken:" 
    867          " 0x%04x, expecting 0x%04x (see map)", 
    868          region, offset, next_free); 
    869    result = 0; 
    870    dump = 1; 
     865        do_rawlog(LT_ERR, 
     866                  "region 0x%04x is not valid: free chain broken:" 
     867                  " 0x%04x, expecting 0x%04x (see map)", 
     868                  region, offset, next_free); 
     869        result = 0; 
     870        dump = 1; 
    871871      } 
    872872      next_free = ChunkNextFree(region, offset); 
     
    875875      total_derefs += ChunkDerefs(region, offset); 
    876876      if (ChunkIsMedium(region, offset) && 
    877      ChunkLen(region, offset) <= MAX_SHORT_CHUNK_LEN) { 
    878    do_rawlog(LT_ERR, 
    879          "region 0x%04x is not valid: medium chunk too small:" 
    880          " 0x%04x (see map)", region, offset); 
    881    result = 0; 
    882    dump = 1; 
     877          ChunkLen(region, offset) <= MAX_SHORT_CHUNK_LEN) { 
     878        do_rawlog(LT_ERR, 
     879                  "region 0x%04x is not valid: medium chunk too small:" 
     880                  " 0x%04x (see map)", region, offset); 
     881        result = 0; 
     882        dump = 1; 
    883883      } 
    884884      if (ChunkIsLong(region, offset) && 
    885      ChunkLen(region, offset) <= MAX_MEDIUM_CHUNK_LEN) { 
    886    do_rawlog(LT_ERR, 
    887          "region 0x%04x is not valid: long chunk too small:" 
    888          " 0x%04x (see map)", region, offset); 
    889    result = 0; 
    890    dump = 1; 
     885          ChunkLen(region, offset) <= MAX_MEDIUM_CHUNK_LEN) { 
     886        do_rawlog(LT_ERR, 
     887                  "region 0x%04x is not valid: long chunk too small:" 
     888                  " 0x%04x (see map)", region, offset); 
     889        result = 0; 
     890        dump = 1; 
    891891      } 
    892892    } 
     
    894894  if (offset != REGION_SIZE) { 
    895895    do_rawlog(LT_ERR, 
    896          "region 0x%04x is not valid: last chunk past bounds (see map)", 
    897          region); 
     896              "region 0x%04x is not valid: last chunk past bounds (see map)", 
     897              region); 
    898898    result = 0; 
    899899  } 
    900900  if (next_free != 0) { 
    901901    do_rawlog(LT_ERR, 
    902          "region 0x%04x is not valid: free chain unterminated:" 
    903          " expecting 0x%04x (see map)", region, next_free); 
     902              "region 0x%04x is not valid: free chain unterminated:" 
     903              " expecting 0x%04x (see map)", region, next_free); 
    904904    result = 0; 
    905905    dump = 1; 
     
    907907  if (rp->used_count != used_count) { 
    908908    do_rawlog(LT_ERR, 
    909          "region 0x%04x is not valid: used count is wrong:" 
    910          " 0x%04x should be 0x%04x", region, rp->used_count, used_count); 
     909              "region 0x%04x is not valid: used count is wrong:" 
     910              " 0x%04x should be 0x%04x", region, rp->used_count, used_count); 
    911911    result = 0; 
    912912  } 
    913913  if (rp->total_derefs != total_derefs) { 
    914914    do_rawlog(LT_ERR, 
    915          "region 0x%04x is not valid: total derefs is wrong:" 
    916          " 0x%04x should be 0x%04x", 
    917          region, rp->total_derefs, total_derefs); 
     915              "region 0x%04x is not valid: total derefs is wrong:" 
     916              " 0x%04x should be 0x%04x", 
     917              region, rp->total_derefs, total_derefs); 
    918918    result = 0; 
    919919  } 
    920920  if (rp->free_count != free_count) { 
    921921    do_rawlog(LT_ERR, 
    922          "region 0x%04x is not valid: free count is wrong:" 
    923          " 0x%04x should be 0x%04x", region, rp->free_count, free_count); 
     922              "region 0x%04x is not valid: free count is wrong:" 
     923              " 0x%04x should be 0x%04x", region, rp->free_count, free_count); 
    924924    result = 0; 
    925925  } 
    926926  if (rp->free_bytes != free_bytes) { 
    927927    do_rawlog(LT_ERR, 
    928          "region 0x%04x is not valid: free bytes is wrong:" 
    929          " 0x%04x should be 0x%04x", region, rp->free_bytes, free_bytes); 
     928              "region 0x%04x is not valid: free bytes is wrong:" 
     929              " 0x%04x should be 0x%04x", region, rp->free_bytes, free_bytes); 
    930930    result = 0; 
    931931  } 
    932932  if (rp->largest_free_chunk != largest_free) { 
    933933    do_rawlog(LT_ERR, 
    934          "region 0x%04x is not valid: largest free is wrong:" 
    935          " 0x%04x should be 0x%04x", 
    936          region, rp->largest_free_chunk, largest_free); 
     934              "region 0x%04x is not valid: largest free is wrong:" 
     935              " 0x%04x should be 0x%04x", 
     936              region, rp->largest_free_chunk, largest_free); 
    937937    result = 0; 
    938938  } 
     
    958958static void 
    959959write_used_chunk(uint16_t region, uint16_t offset, uint16_t full_len, 
    960         unsigned char const *data, uint16_t data_len, uint8_t derefs) 
     960                unsigned char const *data, uint16_t data_len, uint8_t derefs) 
    961961{ 
    962962  unsigned char *cptr = ChunkPointer(region, offset); 
     
    993993static void 
    994994write_free_chunk(uint16_t region, uint16_t offset, uint16_t full_len, 
    995         uint16_t next) 
     995                uint16_t next) 
    996996{ 
    997997  unsigned char *cptr = ChunkPointer(region, offset); 
     
    11781178      uint16_t hole; 
    11791179      for (hole = rp->in_memory->first_free; 
    1180       hole; hole = ChunkNextFree(region, hole)) 
    1181    if (ChunkNextFree(region, hole) == offset) 
    1182      break; 
     1180           hole; hole = ChunkNextFree(region, hole)) 
     1181        if (ChunkNextFree(region, hole) == offset) 
     1182          break; 
    11831183      ASSERT(hole); 
    11841184      write_next_free(region, hole, ChunkNextFree(region, offset)); 
     
    11991199    rp->free_bytes -= full_len; 
    12001200    write_free_chunk(region, offset + full_len, 
    1201             hole_len - full_len, ChunkNextFree(region, offset)); 
     1201                     hole_len - full_len, ChunkNextFree(region, offset)); 
    12021202    if (rp->in_memory->first_free == offset) 
    12031203      rp->in_memory->first_free += full_len; 
     
    12051205      uint16_t hole; 
    12061206      for (hole = rp->in_memory->first_free; 
    1207       hole; hole = ChunkNextFree(region, hole)) 
    1208    if (ChunkNextFree(region, hole) == offset) 
    1209      break; 
     1207           hole; hole = ChunkNextFree(region, hole)) 
     1208        if (ChunkNextFree(region, hole) == offset) 
     1209          break; 
    12101210      ASSERT(hole); 
    12111211      write_next_free(region, hole, offset + full_len); 
     
    12171217    rp->free_bytes -= full_len; 
    12181218    write_free_chunk(region, offset, 
    1219             hole_len - full_len, ChunkNextFree(region, offset)); 
     1219                     hole_len - full_len, ChunkNextFree(region, offset)); 
    12201220    if (rp->largest_free_chunk == hole_len) 
    12211221      rp->largest_free_chunk = largest_hole(region); 
     
    12751275      pos += done; 
    12761276      if (!remaining) 
    1277    return; 
     1277        return; 
    12781278    } 
    12791279#ifndef WIN32 
     
    12841284#ifdef WIN32 
    12851285  mush_panicf("chunk swap file read, %lu remaining, GetLastError %d", 
    1286          (unsigned long) remaining, GetLastError()); 
     1286              (unsigned long) remaining, GetLastError()); 
    12871287#else 
    12881288  mush_panicf("chunk swap file read, %lu remaining, errno %d: %s", 
    1289          (unsigned long) remaining, errno, strerror(errno)); 
     1289              (unsigned long) remaining, errno, strerror(errno)); 
    12901290#endif 
    12911291} 
     
    13401340      pos += done; 
    13411341      if (!remaining) 
    1342    return; 
     1342        return; 
    13431343    } 
    13441344#ifndef WIN32 
     
    13491349#ifdef WIN32 
    13501350  mush_panicf("chunk swap file write, %lu remaining, GetLastError %d", 
    1351          (unsigned long) remaining, GetLastError()); 
     1351              (unsigned long) remaining, GetLastError()); 
    13521352#else 
    13531353  mush_panicf("chunk swap file write, %lu remaining, errno %d: %s", 
    1354          (unsigned long) remaining, errno, strerror(errno)); 
     1354              (unsigned long) remaining, errno, strerror(errno)); 
    13551355#endif 
    13561356} 
     
    14171417#ifdef DEBUG_CHUNK_PAGING 
    14181418  do_rawlog(LT_TRACE, "CHUNK: Paging out region %04x (offset %08x)", 
    1419        rhp->region_id, (unsigned) file_offset); 
     1419            rhp->region_id, (unsigned) file_offset); 
    14201420#endif 
    14211421  write_cache_region(swap_fd, rhp, rhp->region_id); 
     
    14591459#ifdef DEBUG_CHUNK_PAGING 
    14601460  do_rawlog(LT_TRACE, "CHUNK: Paging in region %04x (offset %08x)", 
    1461        region, (unsigned) file_offset); 
     1461            region, (unsigned) file_offset); 
    14621462#endif 
    14631463  read_cache_region(swap_fd, rhp, region); 
     
    14761476      rp->total_derefs = 0; 
    14771477      for (offset = FIRST_CHUNK_OFFSET_IN_REGION; 
    1478       offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
    1479    ChunkDerefs(region, offset) = 0; 
     1478           offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
     1479        ChunkDerefs(region, offset) = 0; 
    14801480      } 
    14811481    } else { 
    14821482      rp->total_derefs = 0; 
    14831483      for (offset = FIRST_CHUNK_OFFSET_IN_REGION; 
    1484       offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
    1485    if (ChunkIsFree(region, offset)) 
    1486      continue; 
    1487    ChunkDerefs(region, offset) >>= shift; 
    1488    rp->total_derefs += ChunkDerefs(region, offset); 
     1484           offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
     1485        if (ChunkIsFree(region, offset)) 
     1486          continue; 
     1487        ChunkDerefs(region, offset) >>= shift; 
     1488        rp->total_derefs += ChunkDerefs(region, offset); 
    14891489      } 
    14901490    } 
     
    15211521      regions = (Region *) realloc(regions, region_array_len * sizeof(Region)); 
    15221522      if (!regions) 
    1523    mush_panic("chunk: region array realloc failure"); 
     1523        mush_panic("chunk: region array realloc failure"); 
    15241524    } 
    15251525    region = region_count; 
     
    15391539  regions[region].in_memory->first_free = FIRST_CHUNK_OFFSET_IN_REGION; 
    15401540  write_free_chunk(region, FIRST_CHUNK_OFFSET_IN_REGION, 
    1541           regions[region].free_bytes, 0); 
     1541                   regions[region].free_bytes, 0); 
    15421542 
    15431543  touch_cache_region(regions[region].in_memory); 
     
    15721572    while (j--) { 
    15731573      if (!rp->oddballs[j]) 
    1574    continue; 
     1574        continue; 
    15751575      d2 = abs(mean - ChunkDerefs(region, rp->oddballs[j])); 
    15761576      if (d1 < d2) 
    1577    break; 
     1577        break; 
    15781578      if (j < NUM_ODDBALLS - 1) 
    1579    rp->oddballs[j + 1] = rp->oddballs[j]; 
     1579        rp->oddballs[j + 1] = rp->oddballs[j]; 
    15801580    } 
    15811581    j++; 
     
    16121612    free_bytes += rp->free_bytes; 
    16131613    if (!FitsInSpace(full_len, rp->largest_free_chunk) && 
    1614    !(rp->free_count == 2 && 
    1615      rp->free_bytes - rp->largest_free_chunk == full_len)) 
     1614        !(rp->free_count == 2 && 
     1615          rp->free_bytes - rp->largest_free_chunk == full_len)) 
    16161616      continue; 
    16171617 
     
    16391639    best_region = create_region(); 
    16401640  } else if (best_score > (1 << LONLINESS_LIMIT) + IN_MEMORY_BIAS && 
    1641         (free_bytes * 100 / (REGION_CAPACITY * region_count)) < 
    1642         FREE_PERCENT_LIMIT) { 
     1641             (free_bytes * 100 / (REGION_CAPACITY * region_count)) < 
     1642             FREE_PERCENT_LIMIT) { 
    16431643#ifdef DEBUG_CHUNK_REGION_CREATE 
    16441644    do_rawlog(LT_TRACE, "find_best_region chose to create region %04x", region); 
     
    16571657static uint16_t 
    16581658find_best_offset(uint16_t full_len, uint16_t region, 
    1659         uint16_t old_region, uint16_t old_offset) 
     1659                uint16_t old_region, uint16_t old_offset) 
    16601660{ 
    16611661  uint16_t fits, offset; 
     
    16681668    if (region == old_region) { 
    16691669      if (offset > old_offset) 
    1670    break; 
     1670        break; 
    16711671      if (offset + ChunkFullLen(region, offset) == old_offset) 
    1672    return fits ? fits : offset; 
     1672        return fits ? fits : offset; 
    16731673    } 
    16741674    if (ChunkFullLen(region, offset) == full_len) 
     
    17591759    stat_used_long_count * CHUNK_LONG_DATA_OFFSET; 
    17601760  STAT_OUT(tprintf 
    1761       ("Chunks:    %10d allocated (%10d bytes, %10d (%2d%%) overhead)", 
    1762        used_count, used_bytes, overhead, 
    1763        used_bytes ? overhead * 100 / used_bytes : 0)); 
     1761           ("Chunks:    %10d allocated (%10d bytes, %10d (%2d%%) overhead)", 
     1762            used_count, used_bytes, overhead, 
     1763            used_bytes ? overhead * 100 / used_bytes : 0)); 
    17641764  overhead = stat_used_short_count * CHUNK_SHORT_DATA_OFFSET; 
    17651765  STAT_OUT(tprintf 
    1766       ("             %10d short     (%10d bytes, %10d (%2d%%) overhead)", 
    1767        stat_used_short_count, stat_used_short_bytes, overhead, 
    1768        stat_used_short_bytes ? overhead * 100 / 
    1769        stat_used_short_bytes : 0)); 
     1766           ("             %10d short     (%10d bytes, %10d (%2d%%) overhead)", 
     1767            stat_used_short_count, stat_used_short_bytes, overhead, 
     1768            stat_used_short_bytes ? overhead * 100 / 
     1769            stat_used_short_bytes : 0)); 
    17701770  overhead = stat_used_medium_count * CHUNK_MEDIUM_DATA_OFFSET; 
    17711771  STAT_OUT(tprintf 
    1772       ("             %10d medium    (%10d bytes, %10d (%2d%%) overhead)", 
    1773        stat_used_medium_count, stat_used_medium_bytes, overhead, 
    1774        stat_used_medium_bytes ? overhead * 100 / 
    1775        stat_used_medium_bytes : 0)); 
     1772           ("             %10d medium    (%10d bytes, %10d (%2d%%) overhead)", 
     1773            stat_used_medium_count, stat_used_medium_bytes, overhead, 
     1774            stat_used_medium_bytes ? overhead * 100 / 
     1775            stat_used_medium_bytes : 0)); 
    17761776  overhead = stat_used_long_count * CHUNK_LONG_DATA_OFFSET; 
    17771777  STAT_OUT(tprintf 
    1778       ("             %10d long      (%10d bytes, %10d (%2d%%) overhead)", 
    1779        stat_used_long_count, stat_used_long_bytes, overhead, 
    1780        stat_used_long_bytes ? overhead * 100 / stat_used_long_bytes : 0)); 
     1778           ("             %10d long      (%10d bytes, %10d (%2d%%) overhead)", 
     1779            stat_used_long_count, stat_used_long_bytes, overhead, 
     1780            stat_used_long_bytes ? overhead * 100 / stat_used_long_bytes : 0)); 
    17811781  STAT_OUT(tprintf 
    1782       ("           %10d free      (%10d bytes, %10d (%2d%%) fragmented)", 
    1783        free_count, free_bytes, free_bytes - free_large, 
    1784        free_bytes ? (free_bytes - free_large) * 100 / free_bytes : 0)); 
     1782           ("           %10d free      (%10d bytes, %10d (%2d%%) fragmented)", 
     1783            free_count, free_bytes, free_bytes - free_large, 
     1784            free_bytes ? (free_bytes - free_large) * 100 / free_bytes : 0)); 
    17851785  overhead = region_count * REGION_SIZE + region_array_len * sizeof(Region); 
    17861786  STAT_OUT(tprintf("Storage:   %10d total (%2d%% saturation)", 
    1787           overhead, used_bytes * 100 / overhead)); 
     1787                   overhead, used_bytes * 100 / overhead)); 
    17881788  STAT_OUT(tprintf("Regions:   %10d total, %8d cached", 
    1789           region_count, cached_region_count)); 
     1789                   region_count, cached_region_count)); 
    17901790  STAT_OUT(tprintf("Paging:    %10d out, %10d in", 
    1791           stat_page_out, stat_page_in)); 
     1791                   stat_page_out, stat_page_in)); 
    17921792  STAT_OUT(" "); 
    17931793  STAT_OUT(tprintf("Period:    %10d (%10d accesses so far, %10d chunks at max)", 
    1794           curr_period, stat_deref_count, stat_deref_maxxed)); 
     1794                   curr_period, stat_deref_count, stat_deref_maxxed)); 
    17951795  STAT_OUT(tprintf("Activity:  %10d creates, %10d deletes this period", 
    1796           stat_create, stat_delete)); 
     1796                   stat_create, stat_delete)); 
    17971797  STAT_OUT(tprintf("Migration: %10d moves this period", 
    1798           stat_migrate_slide + stat_migrate_move)); 
     1798                   stat_migrate_slide + stat_migrate_move)); 
    17991799  STAT_OUT(tprintf("             %10d slide    %10d move", 
    1800           stat_migrate_slide, stat_migrate_move)); 
     1800                   stat_migrate_slide, stat_migrate_move)); 
    18011801  STAT_OUT(tprintf("             %10d in region%10d out of region", 
    1802           stat_migrate_slide + stat_migrate_move - stat_migrate_away, 
    1803           stat_migrate_away)); 
     1802                   stat_migrate_slide + stat_migrate_move - stat_migrate_away, 
     1803                   stat_migrate_away)); 
    18041804} 
    18051805 
     
    18121812  const char *s; 
    18131813  STAT_OUT(tprintf("Paging:    %10d out, %10d in", 
    1814           stat_page_out, stat_page_in)); 
     1814                   stat_page_out, stat_page_in)); 
    18151815} 
    18161816 
     
    18291829  for (rid = 0; rid < region_count; rid++) { 
    18301830    STAT_OUT(tprintf 
    1831         ("region:%4d  #used:%5d  #free:%5d  " 
    1832          "fbytes:%04x  largest:%04x  deref:%3d", 
    1833          rid, regions[rid].used_count, regions[rid].free_count, 
    1834          regions[rid].free_bytes, regions[rid].largest_free_chunk, 
    1835          RegionDerefs(rid))); 
     1831             ("region:%4d  #used:%5d  #free:%5d  " 
     1832              "fbytes:%04x  largest:%04x  deref:%3d", 
     1833              rid, regions[rid].used_count, regions[rid].free_count, 
     1834              regions[rid].free_bytes, regions[rid].largest_free_chunk, 
     1835              RegionDerefs(rid))); 
    18361836  } 
    18371837} 
     
    18941894      sprintf(num, "(%d)", k); 
    18951895      if (j < 32) { 
    1896    if (j < pen) 
    1897      ante = 18; 
    1898    else 
    1899      ante = 19; 
    1900    memcpy(buffer[ante] + j + 1, num, strlen(num)); 
    1901    pen = j + strlen(num) + 1; 
     1896        if (j < pen) 
     1897          ante = 18; 
     1898        else 
     1899          ante = 19; 
     1900        memcpy(buffer[ante] + j + 1, num, strlen(num)); 
     1901        pen = j + strlen(num) + 1; 
    19021902      } else { 
    1903    if (j - (int) strlen(num) < pen) 
    1904      ante = 18; 
    1905    else 
    1906      ante = 19; 
    1907    memcpy(buffer[ante] + j - strlen(num), num, strlen(num)); 
    1908    pen = j; 
     1903        if (j - (int) strlen(num) < pen) 
     1904          ante = 18; 
     1905        else 
     1906          ante = 19; 
     1907        memcpy(buffer[ante] + j - strlen(num), num, strlen(num)); 
     1908        pen = j; 
    19091909      } 
    19101910    } 
     
    19401940    for (k = j; k--;) { 
    19411941      if (m_references[k][0] < t[0]) 
    1942    break; 
     1942        break; 
    19431943      m_references[k + 1] = m_references[k]; 
    19441944    } 
     
    19591959 
    19601960  debug_log("migrate_slide %d (%08x) to %04x%04x", 
    1961        which, m_references[which][0], region, offset); 
     1961            which, m_references[which][0], region, offset); 
    19621962 
    19631963  bring_in_region(region); 
     
    19741974#ifdef DEBUG_CHUNK_MIGRATE 
    19751975    do_rawlog(LT_TRACE, "CHUNK: Sliding chunk %08x to %04x%04x", 
    1976          m_references[which][0], region, offset); 
     1976              m_references[which][0], region, offset); 
    19771977#endif 
    19781978    m_references[which][0] = ChunkReference(region, offset); 
     
    19831983#ifdef DEBUG_CHUNK_MIGRATE 
    19841984    do_rawlog(LT_TRACE, "CHUNK: Sliding chunk %08x to %04x%04x", 
    1985          m_references[which][0], region, prev); 
     1985              m_references[which][0], region, prev); 
    19861986#endif 
    19871987    m_references[which][0] = ChunkReference(region, prev); 
     
    20052005    do_rawlog(LT_TRACE, "Invalid region after migrate_slide!"); 
    20062006    do_rawlog(LT_TRACE, "Was moving %04x%04x to %04x%04x (became %08x)...", 
    2007          region, o_oth, region, o_off, m_references[which][0]); 
     2007              region, o_oth, region, o_off, m_references[which][0]); 
    20082008    do_rawlog(LT_TRACE, "Chunk length %04x into hole length %04x", o_len, len); 
    20092009    debug_dump_region(region, tracelog_fp); 
     
    20282028 
    20292029  debug_log("migrate_move %d (%08x) to %04x%04x, alignment %d", 
    2030        which, m_references[which][0], region, offset, align); 
     2030            which, m_references[which][0], region, offset, align); 
    20312031 
    20322032  s_reg = ChunkReferenceToRegion(m_references[which][0]); 
     
    20522052    dump_debug_log(tracelog_fp); 
    20532053    mush_panicf("Trying to migrate into too small a hole: %04x into %04x!", 
    2054        s_len, length); 
     2054                s_len, length); 
    20552055  } 
    20562056#endif 
     
    20612061#ifdef DEBUG_CHUNK_MIGRATE 
    20622062  do_rawlog(LT_TRACE, "CHUNK: moving chunk %08x to %04x%04x", 
    2063        m_references[which][0], region, offset); 
     2063            m_references[which][0], region, offset); 
    20642064#endif 
    20652065  m_references[which][0] = ChunkReference(region, offset); 
     
    20732073    do_rawlog(LT_TRACE, "Invalid region after migrate_move!"); 
    20742074    do_rawlog(LT_TRACE, "Was moving %04x%04x to %04x%04x (became %04x%04x)...", 
    2075          s_reg, s_off, region, o_off, region, offset); 
     2075              s_reg, s_off, region, o_off, region, offset); 
    20762076    do_rawlog(LT_TRACE, "Chunk length %04x into hole length %04x, alignment %d", 
    2077          s_len, length, align); 
     2077              s_len, length, align); 
    20782078    debug_dump_region(region, tracelog_fp); 
    20792079    mush_panic("Invalid region after migrate_move!"); 
     
    21022102      best_offset = find_best_offset(length, best_region, region, offset); 
    21032103      if (best_offset) 
    2104    migrate_move(best_region, best_offset, j, 1); 
     2104        migrate_move(best_region, best_offset, j, 1); 
    21052105      if (best_region != region) 
    2106    stat_migrate_away++; 
     2106        stat_migrate_away++; 
    21072107    } 
    21082108  } 
     
    21862186uint16_t 
    21872187chunk_fetch(chunk_reference_t reference, 
    2188        unsigned char *buffer, uint16_t buffer_len) 
     2188            unsigned char *buffer, uint16_t buffer_len) 
    21892189{ 
    21902190  uint16_t region, offset, len; 
     
    22762276    for (k = 0; k < m_count; k++) 
    22772277      if (ChunkReferenceToRegion(m_references[k][0]) == region) 
    2278    break; 
     2278        break; 
    22792279    if (k >= m_count) 
    22802280      continue; 
     
    22832283      /* If not in memory, see if we've got an oddball. */ 
    22842284      while (k < m_count) { 
    2285    if (ChunkReferenceToRegion(m_references[k][0]) != region) 
    2286      break; 
    2287    offset = ChunkReferenceToOffset(m_references[k][0]); 
    2288    for (l = 0; l < NUM_ODDBALLS; l++) { 
    2289      if (regions[region].oddballs[l] == offset) { 
    2290        /* Yup, have an oddball... that's worth bringing it in. */ 
    2291        bring_in_region(region); 
    2292        goto do_migrate; 
    2293      } 
    2294    
    2295    k++; 
     2285        if (ChunkReferenceToRegion(m_references[k][0]) != region) 
     2286          break; 
     2287        offset = ChunkReferenceToOffset(m_references[k][0]); 
     2288        for (l = 0; l < NUM_ODDBALLS; l++) { 
     2289          if (regions[region].oddballs[l] == offset) { 
     2290            /* Yup, have an oddball... that's worth bringing it in. */ 
     2291            bring_in_region(region); 
     2292            goto do_migrate; 
     2293          } 
     2294       
     2295        k++; 
    22962296      } 
    22972297    } else { 
     
    23382338#ifdef WIN32 
    23392339  swap_fd = CreateFile(CHUNK_SWAP_FILE, GENERIC_READ | GENERIC_WRITE, 
    2340               0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); 
     2340                       0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); 
    23412341  if (swap_fd == INVALID_HANDLE_VALUE) 
    23422342    mush_panicf("Cannot open swap file: %d", GetLastError()); 
     
    23572357  do_rawlog(LT_TRACE, "CHUNK: malloc()ing initial region array"); 
    23582358#endif 
    2359   regions = mush_malloc(region_array_len * sizeof(Region), "chunk region list"); 
     2359  regions = mush_calloc(region_array_len, sizeof(Region), "chunk region list"); 
    23602360  if (!regions) 
    23612361    mush_panic("cannot malloc space for chunk region list"); 
     
    23822382  case CSTATS_REGIONG: 
    23832383    chunk_histogram(player, chunk_regionhist(), 
    2384            "Chart number of regions (y) vs. references (x)"); 
     2384                    "Chart number of regions (y) vs. references (x)"); 
    23852385    break; 
    23862386  case CSTATS_PAGINGG: 
    23872387    chunk_histogram(player, stat_paging_histogram, 
    2388            "Chart pages in/out (y) vs. references (x)"); 
     2388                    "Chart pages in/out (y) vs. references (x)"); 
    23892389    break; 
    23902390  case CSTATS_FREESPACEG: 
    23912391    chunk_histogram(player, chunk_freehist(), 
    2392            "Chart region free space (y) vs. references (x)"); 
     2392                    "Chart region free space (y) vs. references (x)"); 
    23932393    break; 
    23942394  case CSTATS_REGION: 
     
    24402440      rp->total_derefs = 0; 
    24412441      for (offset = FIRST_CHUNK_OFFSET_IN_REGION; 
    2442       offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
    2443    ChunkDerefs(region, offset) = 0; 
     2442           offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
     2443        ChunkDerefs(region, offset) = 0; 
    24442444      } 
    24452445    } else { 
    24462446      rp->total_derefs = 0; 
    24472447      for (offset = FIRST_CHUNK_OFFSET_IN_REGION; 
    2448       offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
    2449    if (ChunkIsFree(region, offset)) 
    2450      continue; 
    2451    ChunkDerefs(region, offset) >>= shift; 
    2452    rp->total_derefs += ChunkDerefs(region, offset); 
     2448           offset < REGION_SIZE; offset += ChunkFullLen(region, offset)) { 
     2449        if (ChunkIsFree(region, offset)) 
     2450          continue; 
     2451        ChunkDerefs(region, offset) >>= shift; 
     2452        rp->total_derefs += ChunkDerefs(region, offset); 
    24532453      } 
    24542454    } 
     
    25382538  swap_fd_child = -1; 
    25392539} 
    2540 #endif             /* !WIN32 */ 
     2540#endif                          /* !WIN32 */