source: abuse/trunk/src/cache.cpp @ 32

Last change on this file since 32 was 17, checked in by Sam Hocevar, 17 years ago
  • absolute shitloads of 64 bit fixes.
File size: 28.4 KB
Line 
1#include "cache.hpp"
2#include "lisp.hpp"
3#include "video.hpp"
4#include "dprint.hpp"
5#include "exitproc.hpp"
6#include "lcache.hpp"
7#include "status.hpp"
8#include "game.hpp"
9#include "lisp_gc.hpp"
10#include "level.hpp"
11#include "status.hpp"
12#include "crc.hpp"
13#include "specache.hpp"
14
15#if (defined(__MACH__) || !defined(__APPLE__))
16#include <sys/stat.h>
17#endif
18
19#include <fcntl.h>
20char lfname[100]="";          // name of compiled lisp code cache file
21
22#define touch(x) { (x)->last_access=last_access++; \
23                   if ((x)->last_access<0) { normalize(); (x)->last_access=1; } }
24
25extern char *symbol_str(char *name);
26
27crc_manager crc_man;
28
29int past_startup=0;
30
31int crc_man_write_crc_file(char *filename)
32{
33  return crc_man.write_crc_file(filename);
34}
35
36int crc_manager::write_crc_file(char *filename)  // return 0 on failure
37{
38  char msg[100];
39  sprintf(msg,symbol_str("calc_crc"));  // this may take some time, show the user a status indicator
40  if (stat_man) stat_man->push(msg,NULL);
41
42  int i,total=0;
43  for (i=0;i<total_files;i++)
44  {
45    int failed=0;
46    get_crc(i,failed);
47
48    if (failed)
49    {
50      jFILE *fp=new jFILE(get_filename(i),"rb");
51      if (!fp->open_failure())
52      {
53        set_crc(i,crc_file(fp));
54        total++;
55      }
56      delete fp;
57    } else total++;
58    if (stat_man)
59      stat_man->update(i*100/total_files);
60  }
61  if (stat_man) stat_man->pop();
62  jFILE *fp=new jFILE("#net_crc","wb");
63  if (fp->open_failure())
64  {
65    delete fp;
66    return 0;
67  }
68
69  fp->write_uint16(total);
70  total=0;
71  for (i=0;i<total_files;i++)
72  {
73    uint32_t crc;
74    int failed=0;
75    crc=get_crc(i,failed);
76    if (!failed)
77    {
78      fp->write_uint32(crc);
79      uint8_t len=strlen(get_filename(i))+1;
80      fp->write_uint8(len);
81      fp->write(get_filename(i),len);
82      total++;
83    }
84  }
85  delete fp;
86  return 1; 
87}
88
89int crc_manager::load_crc_file(char *filename)
90{
91  bFILE *fp=open_file(filename,"rb");
92  if (fp->open_failure())
93  {
94    delete fp;
95    return 0;
96  } else
97  {
98    short total=fp->read_uint16();
99    int i;
100    for (i=0;i<total;i++)
101    {
102      char name[256];
103      uint32_t crc=fp->read_uint32();
104      uint8_t len=fp->read_uint8();
105      fp->read(name,len);
106      set_crc(get_filenumber(name),crc);
107    }
108    delete fp;
109  }
110  return 1; 
111}
112
113void crc_manager::clean_up()
114{
115  for (int i=0;i<total_files;i++)
116    delete files[i];
117  if (total_files)
118    jfree(files);
119  total_files=0;
120  files=NULL;
121}
122
123crced_file::~crced_file()
124{
125  jfree(filename);
126}
127
128crced_file::crced_file(char *name)
129{
130  filename=strcpy((char *)jmalloc(strlen(name)+1,"crc_file"),name);
131  crc_calculated=0;
132}
133
134crc_manager::crc_manager()
135{
136  total_files=0;
137  files=NULL;
138}
139
140int crc_manager::get_filenumber(char *filename)
141{
142  for (int i=0;i<total_files;i++)
143    if (!strcmp(filename,files[i]->filename)) return i;
144  total_files++;
145  files=(crced_file **)jrealloc(files,total_files*sizeof(crced_file *),"crc_file_list");
146  files[total_files-1]=new crced_file(filename);
147  return total_files-1;
148}
149
150char *crc_manager::get_filename(int32_t filenumber)
151{
152  CHECK(filenumber>=0 && filenumber<total_files);
153  return files[filenumber]->filename;
154}
155
156uint32_t crc_manager::get_crc(int32_t filenumber, int &failed)
157{   
158  CHECK(filenumber>=0 && filenumber<total_files);
159  if (files[filenumber]->crc_calculated)
160  {
161    failed=0;
162    return files[filenumber]->crc;
163  }
164  failed=1;
165  return 0;
166}
167
168void crc_manager::set_crc(int32_t filenumber, uint32_t crc)
169{
170  CHECK(filenumber>=0 && filenumber<total_files);
171  files[filenumber]->crc_calculated=1;
172  files[filenumber]->crc=crc;
173}
174
175void cache_list::unmalloc(cache_item *i)
176{
177  switch (i->type)
178  {
179    case SPEC_CHARACTER2 :
180    case SPEC_CHARACTER : delete ((figure *)i->data);   break;
181    case SPEC_FORETILE : delete ((foretile *)i->data);  break;
182    case SPEC_BACKTILE : delete ((backtile *)i->data);  break;
183    case SPEC_IMAGE    : delete ((image *)i->data);     break;
184    case SPEC_EXTERN_SFX : delete ((sound_effect *)i->data); break;
185    case SPEC_PARTICLE : delete ((part_frame *)i->data); break;
186    case SPEC_EXTERNAL_LCACHE : if (i->data) jfree(i->data); break;
187    case SPEC_PALETTE : delete ((char_tint *)i->data); break;
188    default :
189      printf("Trying to unmalloc unknown type\n");
190  }
191  i->data=NULL;
192  i->last_access=-1;
193}
194
195
196
197void cache_list::prof_init()
198{
199  if (prof_data)
200    jfree(prof_data);
201 
202  prof_data=(int *)jmalloc(sizeof(int)*total,"cache profile");
203  memset(prof_data,0,sizeof(int)*total);
204}
205
206static int c_sorter(const void *a, const void *b)
207{
208  return cash.compare(*(int *)a,*(int *)b);
209}
210
211int cache_list::compare(int a, int b)
212{
213  if (prof_data[a]<prof_data[b])
214    return 1;
215  else if (prof_data[a]>prof_data[b])
216    return -1;
217  else return 0;
218}
219
220
221int cache_list::prof_size()
222{
223  int size=0;     // count up the size for a spec enrty
224  size+=2;        // total filenames
225  int i;
226  for (i=0;i<crc_man.total_filenames();i++)
227      size+=strlen(crc_man.get_filename(i))+2;    // filename + 0 + size of string
228
229  size+=4;       // number of entries saved
230
231  for (i=0;i<total;i++)
232    if (list[i].last_access>0)       // don't save unaccessed counts
233      size+=2+4+1;                   // filenumber & offset & type
234
235  return size;
236}
237
238
239void cache_list::prof_write(bFILE *fp)
240{
241  if (prof_data)
242  {
243    int *ordered_ids=(int *)jmalloc(sizeof(int)*total,"profile order");
244    int i;
245    for (i=0;i<total;i++) ordered_ids[i]=i;
246    qsort(ordered_ids,total,sizeof(int),c_sorter);
247
248    if (fp)
249    {
250      fp->write_uint16(crc_man.total_filenames());
251      for (i=0;i<crc_man.total_filenames();i++)
252      {
253        int l=strlen(crc_man.get_filename(i))+1;
254        fp->write_uint8(l);
255        fp->write(crc_man.get_filename(i),l);
256      }
257
258      int tsaved=0;
259      for (i=0;i<total;i++)
260        if (list[i].last_access>0) tsaved++;
261      fp->write_uint32(tsaved);
262
263      for (i=0;i<total;i++)
264      {
265        int id=ordered_ids[i];
266        if (list[id].last_access>0)       // don't save unaccessed counts     
267        {
268          fp->write_uint8(list[id].type);    // save type, if type changed on reload
269                                            // don't cache in-> its a different refrence
270          fp->write_uint16(list[id].file_number);
271          fp->write_uint32(list[id].offset);
272        }
273      }     
274    }
275
276    jfree(ordered_ids);
277
278  } else dprintf("Cache profiling was not initialized\n");
279}
280
281void cache_list::prof_uninit()
282{
283  if (prof_data)
284  {
285    jfree(prof_data);
286    prof_data=NULL;
287  }
288}
289
290int *sorted_id_list;
291
292
293static int s_offset_compare(const void *a, const void *b)
294{
295  return cash.offset_compare(*(int *)a,*(int *)b);
296}
297
298int cache_list::offset_compare(int a, int b)
299{
300  if (list[a].offset<list[b].offset)
301    return -1;
302  else if (list[a].offset>list[b].offset)
303    return 1;
304  else if (list[a].file_number<list[b].file_number)
305    return -1;
306  else if (list[a].file_number>list[b].file_number)
307    return 1;
308  else return 0;
309}
310
311
312int cache_list::search(int *sarray, uint16_t filenum, int32_t offset)
313{
314  int x1=0,x2=total-1;
315  int split;
316  do
317  {
318    split=(x1+x2)/2;
319    cache_item *e=list+sarray[split];
320
321    if (e->offset<offset)      // search to the right   
322      x1=split+1;
323    else if (e->offset>offset)
324      x2=split-1;
325    else if (e->file_number<filenum)
326      x1=split+1;
327    else if (e->file_number>filenum)
328      x2=split-1;
329    else return sarray[split];
330  } while (x1<=x2);
331  return -1;
332}
333
334static int load_chars()  // returns 0 if cache filled
335{
336  int i;
337  for (i=0;i<total_objects;i++)
338  {
339    if (figures[i]->get_cflag(CFLAG_NEED_CACHE_IN))
340    {
341      figures[i]->set_cflag(CFLAG_CACHED_IN,0);
342      figures[i]->cache_in();
343      figures[i]->set_cflag(CFLAG_NEED_CACHE_IN,0);
344    }
345  }
346  return 1;
347 
348}
349
350void cache_list::note_need(int id)
351{
352  if (list[id].last_access<0)
353    list[id].last_access=-2;
354  else
355    list[id].last_access=2;
356}
357
358void cache_list::preload_cache_object(int type)
359{
360  if (type<0xffff)
361  {
362    if (!figures[type]->get_cflag(CFLAG_NEED_CACHE_IN))  // see if it's already marked
363    {       
364      figures[type]->set_cflag(CFLAG_NEED_CACHE_IN,1);
365      void *cache_fun=figures[type]->get_fun(OFUN_GET_CACHE_LIST);
366
367      if (cache_fun)
368      {
369        int sp=current_space;
370        current_space=PERM_SPACE;
371
372        void *call_with=NULL;
373        push_onto_list(new_lisp_number(type),call_with);
374
375        void *cache_list=eval_function((lisp_symbol *)cache_fun,call_with);
376        p_ref r1(cache_list);
377
378        if (cache_list && lcar(cache_list))
379        {
380          void *obj_list=lcar(cache_list);
381          while (obj_list)
382          {
383            int t=lnumber_value(CAR(obj_list));
384            if (t<0 || t>=total_objects)
385              lbreak("Get cache list returned a bad object number %d\n",t);
386            else
387              preload_cache_object(t);
388            obj_list=CDR(obj_list);
389          }
390        }
391        if (cache_list && lcdr(cache_list))
392        {
393          void *id_list=lcar(lcdr(cache_list));
394          while (id_list)
395          {
396            int id=lnumber_value(CAR(id_list));
397            if (id<0 || id>=total)
398              lbreak("Get cache list returned a bad id number %d\n",id);
399            else if (list[id].last_access<0)
400              list[id].last_access=-2;
401            else list[id].last_access=2;
402
403            id_list=CDR(id_list);
404          }
405        }
406        current_space=sp;
407
408      }
409    }
410  }
411}
412
413void cache_list::preload_cache(level *lev)
414{
415  game_object *f;
416  int i;
417  for (i=0;i<total_objects;i++)                       // mark all types as not needing loading
418    figures[i]->set_cflag(CFLAG_NEED_CACHE_IN,0);
419
420  for (f=lev->first_object();f;f=f->next)               // go through each object and get requested items to cache in
421    preload_cache_object(f->otype);
422
423
424  int j;
425  uint16_t *fg_line;
426  for (j=0;j<lev->foreground_height();j++)
427  {
428    fg_line=lev->get_fgline(j);
429    for (i=0;i<lev->foreground_width();i++,fg_line++)
430    {
431      int id=foretiles[fgvalue(*fg_line)];
432      if (id>=0 && id<nforetiles)
433      {
434        if (list[id].last_access<0)
435          list[id].last_access=-2;
436        else list[id].last_access=2;     
437      }
438    }     
439  }
440
441  uint16_t *bg_line;
442  for (j=0;j<lev->background_height();j++)
443  {
444    bg_line=lev->get_bgline(j);
445    for (i=0;i<lev->background_width();i++,bg_line++)
446    {   
447      int id=backtiles[bgvalue(*bg_line)];
448      if (id>=0 && id<nbacktiles)
449      {
450        if (list[id].last_access<0)
451          list[id].last_access=-2;
452        else list[id].last_access=2;     
453      }
454    }     
455  }
456
457  load_chars();
458}
459
460void cache_list::load_cache_prof_info(char *filename, level *lev)
461{
462  int j;
463  for (j=0;j<this->total;j++)
464    if (list[j].last_access>=0)      // reset all loaded cache items to 0, all non-load to -1
465      list[j].last_access=0;
466
467  preload_cache(lev);                // preliminary guesses at stuff to load
468
469  int load_fail=1;
470  bFILE *fp=open_file(filename,"rb");
471  if (!fp->open_failure())
472  {
473    spec_directory sd(fp);
474    spec_entry *se=sd.find("cache profile info");   // see if the cache profile info is in the file
475    if (se)
476    {
477      fp->seek(se->offset,0);
478
479      char name[255];
480      int tnames=0;
481      int *fnum_remap;    // remaps old filenumbers into current ones
482     
483      tnames=fp->read_uint16();
484      if (tnames)                     /// make sure there isn't bad info in the file
485      {
486        fnum_remap=(int *)jmalloc(sizeof(int)*tnames,"pfname remap");
487
488        int i;
489        for (i=0;i<tnames;i++)
490        {
491          fp->read(name,fp->read_uint8());
492          fnum_remap[i]=-1;                    // initialize the map to no-map
493
494          int j;
495          for (j=0;j<crc_man.total_filenames();j++)
496            if (!strcmp(crc_man.get_filename(j),name))
497              fnum_remap[i]=j;
498        }
499       
500        uint32_t tsaved=fp->read_uint32();
501
502
503        int *priority=(int *)jmalloc(tsaved*sizeof(int),"priorities");
504        memset(priority,0xff,tsaved*sizeof(int));   // initialize to -1
505        int tmatches=0;
506
507        sorted_id_list=(int *)jmalloc(sizeof(int)*total,"sorted ids");
508        for (j=0;j<total;j++) sorted_id_list[j]=j;
509        qsort(sorted_id_list,total,sizeof(int),s_offset_compare);
510
511        for (i=0;i<tsaved;i++)
512        {
513          uint8_t type=fp->read_uint8();
514          short file_num=fp->read_uint16();
515          if (file_num>=tnames)  // bad data?
516            file_num=-1;
517          else file_num=fnum_remap[file_num];
518
519          uint32_t offset=fp->read_uint32();
520
521          // search for a match
522          j=search(sorted_id_list,file_num,offset);     
523          if (j!=-1)
524          {           
525            if (list[j].last_access<0)  // if not loaded
526              list[j].last_access=-2;      // mark as needing loading
527            else list[j].last_access=2;   // mark as loaded and needing to stay that way
528            priority[i]=j;
529            tmatches++;
530          }
531        }
532
533        jfree(sorted_id_list);            // was used for searching, no longer needed
534
535        for (j=0;j<total;j++)
536          if (list[j].last_access==0)
537            unmalloc(list+j);             // free any cache entries that are not accessed at all in the level
538
539
540        ful=0;
541        int tcached=0;
542        for (j=0;j<total;j++)    // now load all of the objects until full
543        {
544//        stat_man->update(j*70/total+25);
545          if (list[j].file_number>=0 && list[j].last_access==-2)
546          {
547            list[j].last_access=-1;
548            if (!ful)
549            {
550              switch (list[j].type)
551              {
552                case SPEC_BACKTILE : backt(j); break;
553                case SPEC_FORETILE : foret(j); break;
554                case SPEC_CHARACTER :
555                case SPEC_CHARACTER2 : fig(j); break;
556                case SPEC_IMAGE : img(j); break;
557                case SPEC_PARTICLE : part(j); break;
558                case SPEC_EXTERN_SFX : sfx(j); break;
559                case SPEC_EXTERNAL_LCACHE : lblock(j); break;
560                case SPEC_PALETTE : ctint(j); break;
561              }
562              tcached++;
563            }
564          }
565        }
566        load_fail=0;
567//      if (full())
568//        dprintf("Cache filled while loading\n");
569
570        if (tsaved>tmatches)
571          tmatches=tsaved+1;
572
573        last_access=tmatches+1;
574        for (i=0;i<tsaved;i++)      // reorder the last access of each cache to reflect prioirties
575        {
576          if (priority[i]!=-1)
577          {
578            if (list[priority[i]].last_access!=-1)            // make sure this wasn't the last item
579              list[priority[i]].last_access=tmatches--;
580          }
581        }
582
583        jfree(priority);
584        jfree(fnum_remap);
585
586
587      }
588    }   
589  }
590
591  if (load_fail) // no cache file, go solely on above gueses
592  {
593    int j;
594    for (j=0;j<total;j++)    // now load all of the objects until full, don't free old stuff
595    {
596//      stat_man->update(j*70/total+25);
597
598      if (list[j].file_number>=0 && list[j].last_access==-2)
599      {
600        list[j].last_access=-1;
601        if (!ful)
602        {
603          switch (list[j].type)
604          {
605            case SPEC_BACKTILE : backt(j); break;
606            case SPEC_FORETILE : foret(j); break;
607            case SPEC_CHARACTER :
608            case SPEC_CHARACTER2 : fig(j); break;
609            case SPEC_IMAGE : img(j); break;
610            case SPEC_PARTICLE : part(j); break;
611            case SPEC_EXTERN_SFX : sfx(j); break;
612            case SPEC_EXTERNAL_LCACHE : lblock(j); break;
613            case SPEC_PALETTE : ctint(j); break;
614          }
615        }
616      }
617    }
618    if (full())
619      dprintf("Cache filled while loading\n");
620  }
621  delete fp;
622}
623
624
625void cache_list::prof_poll_start()
626{
627  poll_start_access=last_access; 
628}
629
630void cache_list::prof_poll_end()
631{
632  if (prof_data)
633  {
634    int i=0;
635    for (;i<total;i++)
636    {
637      if (list[i].last_access>=poll_start_access)
638        prof_data[i]++;
639    }
640  }
641}
642
643void cache_list::unreg(int id)
644{
645  if (list[id].file_number)
646  {
647    unmalloc(&list[id]);
648    list[id].file_number=-1;
649  }
650  else
651    printf("Error : trying to unregister free object\n");
652}
653
654void cache_cleanup2()
655{ unlink(lfname);
656}
657
658void cache_cleanup(int ret, void *arg)
659{ unlink(lfname);
660}
661
662FILE *open_FILE(char *filename, char *mode);
663extern char *macify_name(char *s);
664
665void cache_list::create_lcache()
666{
667#ifdef WIN32
668        char *prefix="c:\\";
669#else
670        char *prefix="/tmp/";     // for UNIX store lisp cache in tmp dir
671        int flags=O_CREAT | O_RDWR;
672#endif
673
674        int cfail = 1, num = 0;
675        do
676        {
677                sprintf(lfname,"%slcache%02d.tmp",prefix,num);
678
679#if defined( __APPLE__ )
680                unlink(lfname);
681#if (defined(__APPLE__) && !defined(__MACH__))
682                macify_name(lfname);
683#endif
684                FILE *fp=fopen(lfname,"wb");
685                if (fp)
686                {
687                        fclose(fp);
688                        cfail=0;
689                }
690#else
691                int fd=open(lfname,flags,S_IRWXU | S_IRWXG | S_IRWXO);     // can we get exclusive rights to this file?
692                if (fd<0) close(fd); else cfail=0;
693#endif
694
695                if (cfail)
696                        num++;
697
698        } while (cfail && num<15);
699
700        if (cfail)
701        {
702                fprintf(stderr,"Error : Unable to open cache file for compiled code.\n"
703                        "        Please delete all files named lcacheXX.tmp\n"
704                        "        and make sure you have write permission to\n"
705                        "        directory (%s)\n",prefix);
706                exit(0);
707        }
708        else
709        {
710                exit_proc(cache_cleanup,cache_cleanup2);    // make sure this file gets deleted on exit..
711        }
712        lcache_number=-1;
713}
714
715cache_list::cache_list()
716{
717  // start out with a decent sized cache buffer because it's going to get allocated anyway.
718  total=0;
719  list=NULL;
720  last_registered=-1;   
721  cache_file=fp=NULL;
722  last_access=1;
723  used=ful=0;
724  last_dir=NULL;
725  last_file=-1;
726  prof_data=NULL;
727  cache_read_file=NULL;
728  create_lcache();
729}
730
731cache_list::~cache_list()
732{
733}
734
735void cache_list::empty()
736{
737  for (int i=0;i<total;i++)
738  {
739    if (list[i].file_number>=0 && list[i].last_access!=-1)
740      unmalloc(&list[i]);
741  }
742  jfree(list);
743  if (fp) delete fp;
744  if (last_dir) delete last_dir;
745  if (cache_file)
746  {
747    delete cache_file;
748    cache_file=NULL;   
749  }
750  unlink(lfname);
751
752  if (prof_data)
753  {
754    delete prof_data;
755    prof_data=NULL;
756  }
757
758  total=0;                    // reinitalize
759  list=NULL;
760  last_registered=-1;   
761  cache_file=fp=NULL;
762  if (cache_read_file)
763  {
764    delete cache_read_file;
765    cache_read_file=NULL;
766  }
767
768  last_access=1;
769  used=ful=0;
770  last_dir=NULL;
771  last_file=-1;
772  prof_data=NULL;
773}
774
775void cache_list::locate(cache_item *i, int local_only)
776{
777//  dprintf("cache in %s, type %d, offset %d\n",crc_man.get_filename(i->file_number),i->type,i->offset);
778  if (i->file_number!=last_file)
779  {
780    if (fp) delete fp;
781    if (last_dir) delete last_dir;
782    if (local_only)
783      fp=new jFILE(crc_man.get_filename(i->file_number),"rb");
784    else
785      fp=open_file(crc_man.get_filename(i->file_number),"rb");
786
787
788    if (fp->open_failure())
789    {
790      printf("Ooch. Could not open file %s\n",crc_man.get_filename(i->file_number));
791      delete fp;
792      exit(0);
793    }
794
795    last_offset=-1;
796    last_dir=new spec_directory(fp);
797    last_file=i->file_number;
798  }
799  if (i->offset!=last_offset)
800  {
801    fp->seek(i->offset,SEEK_SET);
802    last_offset=i->offset;
803  }
804  used=1;
805}
806
807int32_t cache_list::alloc_id()
808{
809  int id;
810  if (prof_data)
811  {
812    the_game->show_help("new id allocated, cache profiling turned off\n");
813    prof_uninit();
814  }
815
816  // see if we previously allocated an id, if so check the next spot in the array
817  // otherwise we will have to scan the whole list for a free id and possible
818  // grow the list.
819  if (last_registered+1<total && list[last_registered+1].file_number<0)
820    id=last_registered+1;
821  else
822  {
823    int i;
824    cache_item *ci=list;
825    for (i=0,id=-1;i<total && id<0;i++,ci++)        // scan list for a free id
826    {
827      if (ci->file_number<0)
828        id=i;
829    }
830
831    if (id<0)                                 // if no free id's then make list bigger
832    {
833      int add_size=20;
834      list=(cache_item *)jrealloc(list,(sizeof(cache_item)*(total+add_size)),"Cache list");
835      for (i=0;i<add_size;i++)
836      {
837        list[total+i].file_number=-1;         // mark new entries as new
838        list[total+i].last_access=-1;
839        list[total+i].data=NULL;
840      }
841      id=total;
842      if (prof_data)                          // new id's have been added old prof_data size won't work
843      { jfree(prof_data); prof_data=NULL; }
844      total+=add_size;
845    }
846  }
847  last_registered=id;
848  return id;
849}
850
851
852
853int32_t cache_list::reg_lisp_block(Cell *block)
854{
855  uint32_t s;
856  if (lcache_number==-1)
857    lcache_number=crc_man.get_filenumber(lfname);
858
859  if (can_cache_lisp())
860  {
861    if (!cache_file)
862    {
863      if (cache_read_file)
864      {
865        delete cache_read_file;
866        cache_read_file=NULL;
867
868        cache_file=new jFILE(lfname,"ab");     
869      } else cache_file=new jFILE(lfname,"wb");  // first time we opened
870    }
871    if (cache_file->open_failure())
872    {
873      delete cache_file;
874      lprint(block);
875      fprintf(stderr,"Unable to open lisp cache file name %s\n",lfname);
876      exit(0);
877    }
878  }
879  int id=alloc_id(),fn=crc_man.get_filenumber(lfname);
880  cache_item *ci=list+id;
881  CHECK(id<total && list[id].file_number<0);
882
883  ci->file_number=fn;
884  ci->last_access=-1;
885  ci->type=SPEC_EXTERNAL_LCACHE;
886  if (!can_cache_lisp())
887  {
888    ci->data=(void *)block;                // we can't cache it out so it must be in memory
889    return id;
890  }
891  ci->data=NULL;                  // assume that it is in tmp memory, need to cache in on access
892  ci->offset=cache_file->tell();
893
894  s=block_size(block);
895  cache_file->write_uint32(s);
896  write_level(cache_file,block);
897  return id;   
898}
899
900int32_t cache_list::reg_object(char *filename, void *object, int type, int rm_dups)
901{
902  char *name;
903  if (item_type(object)==L_CONS_CELL)      // see if we got a object with a filename included
904  {
905    filename=lstring_value(lcar(object));
906    name=lstring_value(lcdr(object));
907  }
908  else name=lstring_value(object);        // otherwise should be a string
909  return reg(filename,name,type,rm_dups);
910}
911
912extern int total_files_open;
913
914int32_t cache_list::reg(char *filename, char *name, int type, int rm_dups)
915{
916        int id=alloc_id(),i,fn=crc_man.get_filenumber(filename);
917        cache_item *ci=list+id;
918        CHECK(id<total && list[id].file_number<0);
919
920        if( type == SPEC_EXTERN_SFX ) // If a extern sound effect then just make sure it's there
921        {
922                bFILE *check=open_file(filename,"rb");
923                if (check->open_failure())
924                {
925                        delete check;
926                        if( sound_avail )
927                        {
928                                printf("Unable to open file '%s' for reading\n",filename);
929                                exit(0);
930                        }
931                        else
932                        {
933                                // Sound is disabled, we don't really care if the sound file
934                                // is there or not, just pretend it's all ok.
935                                return id;
936                        }
937                }
938                char buf[4];
939                check->read(buf,4);
940                delete check;
941                if (memcmp(buf,"RIFF",4))
942                {
943                        printf("File %s is not a WAV file\n",filename);
944                        exit(0);
945                }
946                ci->file_number=fn;
947                ci->last_access=-1;
948                ci->data=NULL;
949                ci->offset=0;
950                ci->type=type;
951                return id;
952        }
953
954        spec_directory *sd=sd_cache.get_spec_directory(filename);
955
956        if (!sd)
957        {
958                printf("Unable to open filename %s for requested item %s\n",filename,name);
959                exit(0);
960        }
961
962        spec_entry *se;
963        if (type!=-1)
964        {
965                se=sd->find(name,type);
966                if (!se) se=sd->find(name);
967        }
968        else se=sd->find(name);
969
970
971        if (!se)
972        {
973                printf("No such item %s in file %s\n",name,filename);
974                exit(0);
975        }
976        else if (type>=0 && (type!=se->type && ((type!=SPEC_CHARACTER2 && type!=SPEC_CHARACTER)  ||
977                                                (se->type!=SPEC_CHARACTER && se->type!=SPEC_CHARACTER2))))
978        {
979                printf("Item %s of file %s should be type %s\n",name,filename,spec_types[type]);
980                exit(0);
981        }
982
983        if (rm_dups)
984        {
985                for (i=0;i<total;i++)
986                        if (list[i].file_number == fn && (unsigned)list[i].offset == se->offset)
987                                return i;
988        }
989
990        ci->file_number=fn;
991        ci->last_access=-1;
992        ci->data=NULL;
993        ci->offset=se->offset;
994        ci->type=se->type; 
995        return id; 
996}
997
998
999void cache_list::normalize()
1000{
1001  int j;
1002  cache_item *ci=list;
1003  last_access=-1;
1004  for (j=0;j<total;j++,ci++)
1005  {
1006    if (ci->last_access>=0)
1007      ci->last_access=ci->last_access>>16;        // shift everything over by 16
1008    if (ci->last_access>last_access)            //  and find new largest timestamp
1009      last_access=ci->last_access;
1010  }
1011  last_access++;
1012}
1013
1014backtile *cache_list::backt(int id)
1015{
1016  cache_item *me=list+id;
1017  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1018
1019  if (me->last_access>=0) 
1020  {
1021    touch(me);
1022    return (backtile *)me->data;
1023  }
1024  else
1025  {
1026    touch(me);
1027    locate(me);
1028    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1029    me->data=(void *)new backtile(fp);
1030    alloc_space=sp;
1031    last_offset=fp->tell();
1032    return (backtile *)me->data;
1033  } 
1034}
1035
1036
1037foretile *cache_list::foret(int id)
1038{
1039  cache_item *me=list+id;
1040  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1041
1042  if (me->last_access>=0) 
1043  {
1044    touch(me);
1045    return (foretile *)me->data;
1046  }
1047  else
1048  {
1049    touch(me);
1050    locate(me);
1051    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1052    me->data=(void *)new foretile(fp);
1053    alloc_space=sp;
1054    last_offset=fp->tell();
1055    return (foretile *)me->data;
1056  } 
1057}
1058
1059figure *cache_list::fig(int id)
1060{
1061  cache_item *me=list+id;
1062//  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1063  if (me->last_access>=0) 
1064  {
1065    touch(me);
1066    return (figure *)me->data;
1067  }
1068  else
1069  {
1070    touch(me);
1071    locate(me);
1072    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1073    me->data=(void *)new figure(fp,me->type);
1074    alloc_space=sp;
1075    last_offset=fp->tell();
1076    return (figure *)me->data;
1077  } 
1078}
1079
1080image *cache_list::img(int id)
1081{
1082  cache_item *me=list+id;
1083  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1084  if (me->last_access>=0) 
1085  {
1086    touch(me);
1087    return (image *)me->data;
1088  }
1089  else
1090  {
1091    touch(me);                                           // hold me, feel me, be me!
1092    locate(me);
1093    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1094    image *im=new image(fp);
1095    alloc_space=sp;
1096    me->data=(void *)im;
1097    last_offset=fp->tell();
1098
1099    return (image *)me->data;
1100  } 
1101}
1102
1103sound_effect *cache_list::sfx(int id)
1104{
1105  cache_item *me=list+id;
1106  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1107  if (me->last_access>=0) 
1108  {
1109    touch(me);                                           // hold me, feel me, be me!
1110    return (sound_effect *)me->data;
1111  }
1112  else
1113  {
1114    touch(me);                                           // hold me, feel me, be me!
1115    char *fn=crc_man.get_filename(me->file_number);
1116    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1117    me->data=(void *)new sound_effect(fn);
1118    alloc_space=sp;
1119    return (sound_effect *)me->data;
1120  } 
1121}
1122
1123
1124part_frame *cache_list::part(int id)
1125{
1126  cache_item *me=list+id;
1127  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1128  if (me->last_access>=0) 
1129  {
1130    touch(me);                                           // hold me, feel me, be me!
1131    return (part_frame *)me->data;
1132  }
1133  else
1134  {
1135    touch(me);
1136    locate(me);
1137    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1138    me->data=(void *)new part_frame(fp);
1139    alloc_space=sp;
1140    last_offset=fp->tell();
1141    return (part_frame *)me->data;
1142  } 
1143}
1144
1145
1146Cell *cache_list::lblock(int id)
1147{
1148  cache_item *me=list+id;
1149  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1150  if (!can_cache_lisp()) return (Cell *)me->data;
1151  if (me->last_access>=0) 
1152  {
1153    touch(me);
1154    return (Cell *)me->data;
1155  }
1156  else
1157  {
1158    if (cache_file)
1159    {
1160      delete cache_file;
1161      cache_file=NULL;
1162    }
1163    touch(me);
1164
1165    if (!cache_read_file)
1166    {
1167      cache_read_file=new jFILE(crc_man.get_filename(me->file_number),"rb");
1168     
1169      int cache_size=80*1024;                   // 80K
1170      cache_read_file->set_read_buffer_size(cache_size);
1171      uint8_t mini_buf;
1172      cache_read_file->read(&mini_buf,1);       // prime the buffer
1173    }
1174
1175    cache_read_file->seek(me->offset,0);
1176
1177    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1178
1179    uint32_t size=cache_read_file->read_uint32();
1180    void *space;
1181
1182    if (size)
1183      space=jmalloc(size,"cached lisp block");
1184    else space=NULL;
1185
1186    int cs=current_space;
1187    use_user_space(space,size);   
1188    load_block(cache_read_file);
1189    current_space=cs;
1190   
1191    alloc_space=sp;
1192    if (size)
1193      me->data=(Cell *)space;
1194    else me->data=NULL;
1195    return (Cell *)me->data;
1196  } 
1197}
1198
1199cache_list cash;
1200
1201void free_up_memory()
1202{
1203  cash.free_oldest();
1204}
1205
1206void cache_list::free_oldest()
1207{
1208  uint32_t i,old_time=last_access;
1209  cache_item *ci=list,*oldest=NULL;
1210  ful=1;
1211
1212  for (i=0;i<total;i++,ci++)
1213  {
1214    if (ci->data && ci->last_access<old_time)
1215    {
1216      oldest=ci;
1217      old_time=ci->last_access;
1218    }
1219  }
1220  if (oldest)
1221  {
1222    dprintf("mem_maker : freeing %s\n",spec_types[oldest->type]);
1223    unmalloc(oldest);   
1224  }
1225  else
1226  {
1227    close_graphics();
1228    printf("Out of memory, please remove any TSR's device drivers you can\n");
1229    mem_report("out_of_mem");
1230    exit(0);
1231  }         
1232}
1233
1234
1235void cache_list::show_accessed()
1236{
1237  int old=last_access,new_old_accessed;
1238  cache_item *ci,*new_old;
1239 
1240  do
1241  {
1242    new_old_accessed=-1;
1243    new_old=NULL;
1244    ci=list;
1245    for (int i=0;i<total;i++,ci++) 
1246    {
1247      if (ci->last_access<old && ci->last_access>0 && ci->last_access>new_old_accessed)   
1248      {
1249        new_old_accessed=ci->last_access;
1250        new_old=ci;   
1251      }
1252    }
1253    if (new_old)
1254    {
1255      ci=new_old;
1256      old=ci->last_access;
1257      printf("type=(%20s) file=(%20s) access=(%6ld)\n",spec_types[ci->type],
1258             crc_man.get_filename(ci->file_number),
1259             (long int)ci->last_access);
1260    }
1261  } while (new_old);
1262}
1263
1264
1265int cache_list::loaded(int id)
1266{
1267  cache_item *me=list+id;
1268  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1269  if (me->last_access>=0) 
1270    return 1;
1271  else return 0;
1272}
1273
1274
1275
1276char_tint *cache_list::ctint(int id)
1277{
1278  cache_item *me=list+id;
1279  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id" && me->type==SPEC_PALETTE);
1280  if (me->last_access>=0) 
1281  {
1282    touch(me);
1283    return (char_tint *)me->data;
1284  }
1285  else
1286  {
1287    touch(me);
1288    locate(me);
1289    int sp=alloc_space; alloc_space=ALLOC_SPACE_CACHE;
1290    me->data=(void *)new char_tint(fp);
1291    alloc_space=sp;
1292    last_offset=fp->tell();
1293    return (char_tint *)me->data;
1294  }   
1295}
1296
1297
1298
1299
Note: See TracBrowser for help on using the repository browser.