Ignore:
Timestamp:
Mar 18, 2008, 12:05:08 AM (12 years ago)
Author:
Sam Hocevar
Message:
  • Cleaned up cache.cpp.
  • Renamed stupid "cash" variable into "cache".
  • Rename cache_list and crc_manager into CacheList? and CrcManager?.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/cache.cpp

    r86 r123  
    3636                   if ((x)->last_access<0) { normalize(); (x)->last_access=1; } }
    3737
    38 crc_manager crc_man;
     38CrcManager crc_manager;
    3939
    4040int past_startup=0;
     
    4242int crc_man_write_crc_file(char const *filename)
    4343{
    44   return crc_man.write_crc_file(filename);
    45 }
    46 
    47 int crc_manager::write_crc_file(char const *filename)  // return 0 on failure
     44  return crc_manager.write_crc_file(filename);
     45}
     46
     47int CrcManager::write_crc_file(char const *filename)  // return 0 on failure
    4848{
    4949  char msg[100];
     
    9898}
    9999
    100 int crc_manager::load_crc_file(char const *filename)
     100int CrcManager::load_crc_file(char const *filename)
    101101{
    102102  bFILE *fp=open_file(filename,"rb");
     
    122122}
    123123
    124 void crc_manager::clean_up()
     124void CrcManager::clean_up()
    125125{
    126126  for (int i=0;i<total_files;i++)
     
    132132}
    133133
    134 crced_file::~crced_file()
     134CrcedFile::~CrcedFile()
    135135{
    136136  jfree(filename);
    137137}
    138138
    139 crced_file::crced_file(char const *name)
     139CrcedFile::CrcedFile(char const *name)
    140140{
    141141  filename=strcpy((char *)jmalloc(strlen(name)+1,"crc_file"),name);
     
    143143}
    144144
    145 crc_manager::crc_manager()
     145CrcManager::CrcManager()
    146146{
    147147  total_files=0;
     
    149149}
    150150
    151 int crc_manager::get_filenumber(char const *filename)
     151int CrcManager::get_filenumber(char const *filename)
    152152{
    153153  for (int i=0;i<total_files;i++)
    154154    if (!strcmp(filename,files[i]->filename)) return i;
    155155  total_files++;
    156   files=(crced_file **)jrealloc(files,total_files*sizeof(crced_file *),"crc_file_list");
    157   files[total_files-1]=new crced_file(filename);
     156  files=(CrcedFile **)jrealloc(files,total_files*sizeof(CrcedFile *),"crc_file_list");
     157  files[total_files-1]=new CrcedFile(filename);
    158158  return total_files-1;
    159159}
    160160
    161 char *crc_manager::get_filename(int32_t filenumber)
     161char *CrcManager::get_filename(int32_t filenumber)
    162162{
    163163  CHECK(filenumber>=0 && filenumber<total_files);
     
    165165}
    166166
    167 uint32_t crc_manager::get_crc(int32_t filenumber, int &failed)
     167uint32_t CrcManager::get_crc(int32_t filenumber, int &failed)
    168168{   
    169169  CHECK(filenumber>=0 && filenumber<total_files);
     
    177177}
    178178
    179 void crc_manager::set_crc(int32_t filenumber, uint32_t crc)
     179void CrcManager::set_crc(int32_t filenumber, uint32_t crc)
    180180{
    181181  CHECK(filenumber>=0 && filenumber<total_files);
     
    184184}
    185185
    186 void cache_list::unmalloc(cache_item *i)
     186void CacheList::unmalloc(CacheItem *i)
    187187{
    188188  switch (i->type)
     
    206206
    207207
    208 void cache_list::prof_init()
     208void CacheList::prof_init()
    209209{
    210210  if (prof_data)
     
    217217static int c_sorter(const void *a, const void *b)
    218218{
    219   return cash.compare(*(int *)a,*(int *)b);
    220 }
    221 
    222 int cache_list::compare(int a, int b)
     219  return cache.compare(*(int *)a,*(int *)b);
     220}
     221
     222int CacheList::compare(int a, int b)
    223223{
    224224  if (prof_data[a]<prof_data[b])
     
    230230
    231231
    232 int cache_list::prof_size()
     232int CacheList::prof_size()
    233233{
    234234  int size=0;     // count up the size for a spec enrty
    235235  size+=2;        // total filenames
    236236  int i;
    237   for (i=0;i<crc_man.total_filenames();i++)
    238       size+=strlen(crc_man.get_filename(i))+2;    // filename + 0 + size of string
     237  for (i=0;i<crc_manager.total_filenames();i++)
     238      size+=strlen(crc_manager.get_filename(i))+2;    // filename + 0 + size of string
    239239
    240240  size+=4;       // number of entries saved
     
    248248
    249249
    250 void cache_list::prof_write(bFILE *fp)
     250void CacheList::prof_write(bFILE *fp)
    251251{
    252252  if (prof_data)
     
    259259    if (fp)
    260260    {
    261       fp->write_uint16(crc_man.total_filenames());
    262       for (i=0;i<crc_man.total_filenames();i++)
     261      fp->write_uint16(crc_manager.total_filenames());
     262      for (i=0;i<crc_manager.total_filenames();i++)
    263263      {
    264         int l=strlen(crc_man.get_filename(i))+1;
     264        int l=strlen(crc_manager.get_filename(i))+1;
    265265        fp->write_uint8(l);
    266         fp->write(crc_man.get_filename(i),l);
     266        fp->write(crc_manager.get_filename(i),l);
    267267      }
    268268
     
    290290}
    291291
    292 void cache_list::prof_uninit()
     292void CacheList::prof_uninit()
    293293{
    294294  if (prof_data)
     
    304304static int s_offset_compare(const void *a, const void *b)
    305305{
    306   return cash.offset_compare(*(int *)a,*(int *)b);
    307 }
    308 
    309 int cache_list::offset_compare(int a, int b)
     306  return cache.offset_compare(*(int *)a,*(int *)b);
     307}
     308
     309int CacheList::offset_compare(int a, int b)
    310310{
    311311  if (list[a].offset<list[b].offset)
     
    321321
    322322
    323 int cache_list::search(int *sarray, uint16_t filenum, int32_t offset)
     323int CacheList::search(int *sarray, uint16_t filenum, int32_t offset)
    324324{
    325325  int x1=0,x2=total-1;
     
    328328  {
    329329    split=(x1+x2)/2;
    330     cache_item *e=list+sarray[split];
     330    CacheItem *e=list+sarray[split];
    331331
    332332    if (e->offset<offset)      // search to the right   
     
    359359}
    360360
    361 void cache_list::note_need(int id)
     361void CacheList::note_need(int id)
    362362{
    363363  if (list[id].last_access<0)
     
    367367}
    368368
    369 void cache_list::preload_cache_object(int type)
     369void CacheList::preload_cache_object(int type)
    370370{
    371371  if (type<0xffff)
     
    384384        push_onto_list(new_lisp_number(type),call_with);
    385385
    386         void *cache_list=eval_function((lisp_symbol *)cache_fun,call_with);
    387         p_ref r1(cache_list);
    388 
    389         if (cache_list && lcar(cache_list))
    390         {
    391           void *obj_list=lcar(cache_list);
     386        void *CacheList=eval_function((lisp_symbol *)cache_fun,call_with);
     387        p_ref r1(CacheList);
     388
     389        if (CacheList && lcar(CacheList))
     390        {
     391          void *obj_list=lcar(CacheList);
    392392          while (obj_list)
    393393          {
     
    400400          }
    401401        }
    402         if (cache_list && lcdr(cache_list))
    403         {
    404           void *id_list=lcar(lcdr(cache_list));
     402        if (CacheList && lcdr(CacheList))
     403        {
     404          void *id_list=lcar(lcdr(CacheList));
    405405          while (id_list)
    406406          {
     
    422422}
    423423
    424 void cache_list::preload_cache(level *lev)
     424void CacheList::preload_cache(level *lev)
    425425{
    426426  game_object *f;
     
    469469}
    470470
    471 void cache_list::load_cache_prof_info(char *filename, level *lev)
     471void CacheList::load_cache_prof_info(char *filename, level *lev)
    472472{
    473473  int j;
     
    504504
    505505          int j;
    506           for (j=0;j<crc_man.total_filenames();j++)
    507             if (!strcmp(crc_man.get_filename(j),name))
     506          for (j=0;j<crc_manager.total_filenames();j++)
     507            if (!strcmp(crc_manager.get_filename(j),name))
    508508              fnum_remap[i]=j;
    509509        }
     
    634634
    635635
    636 void cache_list::prof_poll_start()
     636void CacheList::prof_poll_start()
    637637{
    638638  poll_start_access=last_access; 
    639639}
    640640
    641 void cache_list::prof_poll_end()
     641void CacheList::prof_poll_end()
    642642{
    643643  if (prof_data)
     
    652652}
    653653
    654 void cache_list::unreg(int id)
     654void CacheList::unreg(int id)
    655655{
    656656  if (list[id].file_number)
     
    671671}
    672672
    673 void cache_list::create_lcache()
     673void CacheList::create_lcache()
    674674{
    675675#ifdef WIN32
     
    718718}
    719719
    720 cache_list::cache_list()
     720CacheList::CacheList()
    721721{
    722722  // start out with a decent sized cache buffer because it's going to get allocated anyway.
     
    734734}
    735735
    736 cache_list::~cache_list()
     736CacheList::~CacheList()
    737737{
    738738}
    739739
    740 void cache_list::empty()
     740void CacheList::empty()
    741741{
    742742  for (int i=0;i<total;i++)
     
    778778}
    779779
    780 void cache_list::locate(cache_item *i, int local_only)
    781 {
    782 //  dprintf("cache in %s, type %d, offset %d\n",crc_man.get_filename(i->file_number),i->type,i->offset);
     780void CacheList::locate(CacheItem *i, int local_only)
     781{
     782//  dprintf("cache in %s, type %d, offset %d\n",crc_manager.get_filename(i->file_number),i->type,i->offset);
    783783  if (i->file_number!=last_file)
    784784  {
     
    786786    if (last_dir) delete last_dir;
    787787    if (local_only)
    788       fp=new jFILE(crc_man.get_filename(i->file_number),"rb");
     788      fp=new jFILE(crc_manager.get_filename(i->file_number),"rb");
    789789    else
    790       fp=open_file(crc_man.get_filename(i->file_number),"rb");
     790      fp=open_file(crc_manager.get_filename(i->file_number),"rb");
    791791
    792792
    793793    if (fp->open_failure())
    794794    {
    795       printf("Ooch. Could not open file %s\n",crc_man.get_filename(i->file_number));
     795      printf("Ooch. Could not open file %s\n",crc_manager.get_filename(i->file_number));
    796796      delete fp;
    797797      exit(0);
     
    810810}
    811811
    812 int32_t cache_list::alloc_id()
     812int32_t CacheList::alloc_id()
    813813{
    814814  int id;
     
    827827  {
    828828    int i;
    829     cache_item *ci=list;
     829    CacheItem *ci=list;
    830830    for (i=0,id=-1;i<total && id<0;i++,ci++)        // scan list for a free id
    831831    {
     
    837837    {
    838838      int add_size=20;
    839       list=(cache_item *)jrealloc(list,(sizeof(cache_item)*(total+add_size)),"Cache list");
     839      list=(CacheItem *)jrealloc(list,(sizeof(CacheItem)*(total+add_size)),"Cache list");
    840840      for (i=0;i<add_size;i++)
    841841      {
     
    856856
    857857
    858 int32_t cache_list::reg_lisp_block(Cell *block)
     858int32_t CacheList::reg_lisp_block(Cell *block)
    859859{
    860860  uint32_t s;
    861861  if (lcache_number==-1)
    862     lcache_number=crc_man.get_filenumber(lfname);
     862    lcache_number=crc_manager.get_filenumber(lfname);
    863863
    864864  if (can_cache_lisp())
     
    882882    }
    883883  }
    884   int id=alloc_id(),fn=crc_man.get_filenumber(lfname);
    885   cache_item *ci=list+id;
     884  int id=alloc_id(),fn=crc_manager.get_filenumber(lfname);
     885  CacheItem *ci=list+id;
    886886  CHECK(id<total && list[id].file_number<0);
    887887
     
    903903}
    904904
    905 int32_t cache_list::reg_object(char const *filename, void *object, int type, int rm_dups)
     905int32_t CacheList::reg_object(char const *filename, void *object, int type, int rm_dups)
    906906{
    907907  char *name;
     
    917917extern int total_files_open;
    918918
    919 int32_t cache_list::reg(char const *filename, char const *name, int type, int rm_dups)
    920 {
    921         int id=alloc_id(),i,fn=crc_man.get_filenumber(filename);
    922         cache_item *ci=list+id;
     919int32_t CacheList::reg(char const *filename, char const *name, int type, int rm_dups)
     920{
     921        int id=alloc_id(),i,fn=crc_manager.get_filenumber(filename);
     922        CacheItem *ci=list+id;
    923923        CHECK(id<total && list[id].file_number<0);
    924924
     
    10021002
    10031003
    1004 void cache_list::normalize()
     1004void CacheList::normalize()
    10051005{
    10061006  int j;
    1007   cache_item *ci=list;
     1007  CacheItem *ci=list;
    10081008  last_access=-1;
    10091009  for (j=0;j<total;j++,ci++)
     
    10171017}
    10181018
    1019 backtile *cache_list::backt(int id)
    1020 {
    1021   cache_item *me=list+id;
     1019backtile *CacheList::backt(int id)
     1020{
     1021  CacheItem *me=list+id;
    10221022  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    10231023
     
    10401040
    10411041
    1042 foretile *cache_list::foret(int id)
    1043 {
    1044   cache_item *me=list+id;
     1042foretile *CacheList::foret(int id)
     1043{
     1044  CacheItem *me=list+id;
    10451045  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    10461046
     
    10621062}
    10631063
    1064 figure *cache_list::fig(int id)
    1065 {
    1066   cache_item *me=list+id;
     1064figure *CacheList::fig(int id)
     1065{
     1066  CacheItem *me=list+id;
    10671067//  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    10681068  if (me->last_access>=0) 
     
    10831083}
    10841084
    1085 image *cache_list::img(int id)
    1086 {
    1087   cache_item *me=list+id;
     1085image *CacheList::img(int id)
     1086{
     1087  CacheItem *me=list+id;
    10881088  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    10891089  if (me->last_access>=0) 
     
    11061106}
    11071107
    1108 sound_effect *cache_list::sfx(int id)
    1109 {
    1110   cache_item *me=list+id;
     1108sound_effect *CacheList::sfx(int id)
     1109{
     1110  CacheItem *me=list+id;
    11111111  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    11121112  if (me->last_access>=0) 
     
    11181118  {
    11191119    touch(me);                                           // hold me, feel me, be me!
    1120     char *fn=crc_man.get_filename(me->file_number);
     1120    char *fn=crc_manager.get_filename(me->file_number);
    11211121    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
    11221122    me->data=(void *)new sound_effect(fn);
     
    11271127
    11281128
    1129 part_frame *cache_list::part(int id)
    1130 {
    1131   cache_item *me=list+id;
     1129part_frame *CacheList::part(int id)
     1130{
     1131  CacheItem *me=list+id;
    11321132  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    11331133  if (me->last_access>=0) 
     
    11491149
    11501150
    1151 Cell *cache_list::lblock(int id)
    1152 {
    1153   cache_item *me=list+id;
     1151Cell *CacheList::lblock(int id)
     1152{
     1153  CacheItem *me=list+id;
    11541154  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    11551155  if (!can_cache_lisp()) return (Cell *)me->data;
     
    11701170    if (!cache_read_file)
    11711171    {
    1172       cache_read_file=new jFILE(crc_man.get_filename(me->file_number),"rb");
     1172      cache_read_file=new jFILE(crc_manager.get_filename(me->file_number),"rb");
    11731173     
    11741174      int cache_size=80*1024;                   // 80K
     
    12021202}
    12031203
    1204 cache_list cash;
     1204CacheList cache;
    12051205
    12061206void free_up_memory()
    12071207{
    1208   cash.free_oldest();
    1209 }
    1210 
    1211 void cache_list::free_oldest()
     1208  cache.free_oldest();
     1209}
     1210
     1211void CacheList::free_oldest()
    12121212{
    12131213  uint32_t i,old_time=last_access;
    1214   cache_item *ci=list,*oldest=NULL;
     1214  CacheItem *ci=list,*oldest=NULL;
    12151215  ful=1;
    12161216
     
    12381238
    12391239
    1240 void cache_list::show_accessed()
     1240void CacheList::show_accessed()
    12411241{
    12421242  int old=last_access,new_old_accessed;
    1243   cache_item *ci,*new_old;
     1243  CacheItem *ci,*new_old;
    12441244 
    12451245  do
     
    12611261      old=ci->last_access;
    12621262      printf("type=(%20s) file=(%20s) access=(%6ld)\n",spec_types[ci->type],
    1263              crc_man.get_filename(ci->file_number),
     1263             crc_manager.get_filename(ci->file_number),
    12641264             (long int)ci->last_access);
    12651265    }
     
    12681268
    12691269
    1270 int cache_list::loaded(int id)
    1271 {
    1272   cache_item *me=list+id;
     1270int CacheList::loaded(int id)
     1271{
     1272  CacheItem *me=list+id;
    12731273  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
    12741274  if (me->last_access>=0) 
     
    12791279
    12801280
    1281 char_tint *cache_list::ctint(int id)
    1282 {
    1283   cache_item *me=list+id;
     1281char_tint *CacheList::ctint(int id)
     1282{
     1283  CacheItem *me=list+id;
    12841284  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id" && me->type==SPEC_PALETTE);
    12851285  if (me->last_access>=0) 
Note: See TracChangeset for help on using the changeset viewer.