Debian Codegraph @

src/cpu/core_dynrec/cache.h

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19
20 class CodePageHandlerDynRec; // forward
21
22 // basic cache block representation
23 class CacheBlockDynRec {
24 public:
25 void Clear(void);
26 // link this cache block to another block, index specifies the code
27 // path (always zero for unconditional links, 0/1 for conditional ones
28 void LinkTo(Bitu index,CacheBlockDynRec * toblock) {
29 assert(toblock);
30 link[index].to=toblock;
31 link[index].next=toblock->link[index].from; // set target block
32 toblock->link[index].from=this; // remember who links me
33 }
34 struct {
35 Bit16u start,end; // where in the page is the original code
36 CodePageHandlerDynRec * handler; // page containing this code
37 } page;
38 struct {
39 Bit8u * start; // where in the cache are we
40 Bitu size;
41 CacheBlockDynRec * next;
42 // writemap masking maskpointer/start/length
43 // to allow holes in the writemap
44 Bit8u * wmapmask;
45 Bit16u maskstart;
46 Bit16u masklen;
47 } cache;
48 struct {
49 Bitu index;
50 CacheBlockDynRec * next;
51 } hash;
52 struct {
53 CacheBlockDynRec * to; // this block can transfer control to the to-block
54 CacheBlockDynRec * next;
55 CacheBlockDynRec * from; // the from-block can transfer control to this block
56 } link[2]; // maximal two links (conditional jumps)
57 CacheBlockDynRec * crossblock;
58 };
59
60 static struct {
61 struct {
62 CacheBlockDynRec * first; // the first cache block in the list
63 CacheBlockDynRec * active; // the current cache block
64 CacheBlockDynRec * free; // pointer to the free list
65 CacheBlockDynRec * running; // the last block that was entered for execution
66 } block;
67 Bit8u * pos; // position in the cache block
68 CodePageHandlerDynRec * free_pages; // pointer to the free list
69 CodePageHandlerDynRec * used_pages; // pointer to the list of used pages
70 CodePageHandlerDynRec * last_page; // the last used page
71 } cache;
72
73
74 // cache memory pointers, to be malloc'd later
75 static Bit8u * cache_code_start_ptr=NULL;
76 static Bit8u * cache_code=NULL;
77 static Bit8u * cache_code_link_blocks=NULL;
78
79 static CacheBlockDynRec * cache_blocks=NULL;
80 static CacheBlockDynRec link_blocks[2]; // default linking (specially marked)
81
82
83 // the CodePageHandlerDynRec class provides access to the contained
84 // cache blocks and intercepts writes to the code for special treatment
85 class CodePageHandlerDynRec : public PageHandler {
86 public:
87 CodePageHandlerDynRec() {
88 invalidation_map=NULL;
89 }
90
91 void SetupAt(Bitu _phys_page,PageHandler * _old_pagehandler) {
92 // initialize this codepage handler
93 phys_page=_phys_page;
94 // save the old pagehandler to provide direct read access to the memory,
95 // and to be able to restore it later on
96 old_pagehandler=_old_pagehandler;
97
98 // adjust flags
99 flags=old_pagehandler->flags|PFLAG_HASCODE;
100 flags&=~PFLAG_WRITEABLE;
101
102 active_blocks=0;
103 active_count=16;
104
105 // initialize the maps with zero (no cache blocks as well as code present)
106 memset(&hash_map,0,sizeof(hash_map));
107 memset(&write_map,0,sizeof(write_map));
108 if (invalidation_map!=NULL) {
109 free(invalidation_map);
110 invalidation_map=NULL;
111 }
112 }
113
114 // clear out blocks that contain code which has been modified
115 bool InvalidateRange(Bitu start,Bitu end) {
116 Bits index=1+(end>>DYN_HASH_SHIFT);
117 bool is_current_block=false; // if the current block is modified, it has to be exited as soon as possible
118
119 Bit32u ip_point=SegPhys(cs)+reg_eip;
120 ip_point=(PAGING_GetPhysicalPage(ip_point)-(phys_page<<12))+(ip_point&0xfff);
121 while (index>=0) {
122 Bitu map=0;
123 // see if there is still some code in the range
124 for (Bitu count=start;count<=end;count++) map+=write_map[count];
125 if (!map) return is_current_block; // no more code, finished
126
127 CacheBlockDynRec * block=hash_map[index];
128 while (block) {
129 CacheBlockDynRec * nextblock=block->hash.next;
130 // test if this block is in the range
131 if (start<=block->page.end && end>=block->page.start) {
132 if (ip_point<=block->page.end && ip_point>=block->page.start) is_current_block=true;
133 block->Clear(); // clear the block, decrements the write_map accordingly
134 }
135 block=nextblock;
136 }
137 index--;
138 }
139 return is_current_block;
140 }
141
142 // the following functions will clean all cache blocks that are invalid now due to the write
143 void writeb(PhysPt addr,Bitu val){
144 addr&=4095;
145 if (host_readb(hostmem+addr)==(Bit8u)val) return;
146 host_writeb(hostmem+addr,val);
147 // see if there's code where we are writing to
148 if (!host_readb(&write_map[addr])) {
149 if (active_blocks) return; // still some blocks in this page
150 active_count--;
151 if (!active_count) Release(); // delay page releasing until active_count is zero
152 return;
153 } else if (!invalidation_map) {
154 invalidation_map=(Bit8u*)malloc(4096);
155 memset(invalidation_map,0,4096);
156 }
157 invalidation_map[addr]++;
158 InvalidateRange(addr,addr);
159 }
160 void writew(PhysPt addr,Bitu val){
161 addr&=4095;
162 if (host_readw(hostmem+addr)==(Bit16u)val) return;
163 host_writew(hostmem+addr,val);
164 // see if there's code where we are writing to
165 if (!host_readw(&write_map[addr])) {
166 if (active_blocks) return; // still some blocks in this page
167 active_count--;
168 if (!active_count) Release(); // delay page releasing until active_count is zero
169 return;
170 } else if (!invalidation_map) {
171 invalidation_map=(Bit8u*)malloc(4096);
172 memset(invalidation_map,0,4096);
173 }
174 #if defined(WORDS_BIGENDIAN) || !defined(C_UNALIGNED_MEMORY)
175 host_writew(&invalidation_map[addr],
176 host_readw(&invalidation_map[addr])+0x101);
177 #else
178 (*(Bit16u*)&invalidation_map[addr])+=0x101;
179 #endif
180 InvalidateRange(addr,addr+1);
181 }
182 void writed(PhysPt addr,Bitu val){
183 addr&=4095;
184 if (host_readd(hostmem+addr)==(Bit32u)val) return;
185 host_writed(hostmem+addr,val);
186 // see if there's code where we are writing to
187 if (!host_readd(&write_map[addr])) {
188 if (active_blocks) return; // still some blocks in this page
189 active_count--;
190 if (!active_count) Release(); // delay page releasing until active_count is zero
191 return;
192 } else if (!invalidation_map) {
193 invalidation_map=(Bit8u*)malloc(4096);
194 memset(invalidation_map,0,4096);
195 }
196 #if defined(WORDS_BIGENDIAN) || !defined(C_UNALIGNED_MEMORY)
197 host_writed(&invalidation_map[addr],
198 host_readd(&invalidation_map[addr])+0x1010101);
199 #else
200 (*(Bit32u*)&invalidation_map[addr])+=0x1010101;
201 #endif
202 InvalidateRange(addr,addr+3);
203 }
204 bool writeb_checked(PhysPt addr,Bitu val) {
205 addr&=4095;
206 if (host_readb(hostmem+addr)==(Bit8u)val) return false;
207 // see if there's code where we are writing to
208 if (!host_readb(&write_map[addr])) {
209 if (!active_blocks) {
210 // no blocks left in this page, still delay the page releasing a bit
211 active_count--;
212 if (!active_count) Release();
213 }
214 } else {
215 if (!invalidation_map) {
216 invalidation_map=(Bit8u*)malloc(4096);
217 memset(invalidation_map,0,4096);
218 }
219 invalidation_map[addr]++;
220 if (InvalidateRange(addr,addr)) {
221 cpu.exception.which=SMC_CURRENT_BLOCK;
222 return true;
223 }
224 }
225 host_writeb(hostmem+addr,val);
226 return false;
227 }
228 bool writew_checked(PhysPt addr,Bitu val) {
229 addr&=4095;
230 if (host_readw(hostmem+addr)==(Bit16u)val) return false;
231 // see if there's code where we are writing to
232 if (!host_readw(&write_map[addr])) {
233 if (!active_blocks) {
234 // no blocks left in this page, still delay the page releasing a bit
235 active_count--;
236 if (!active_count) Release();
237 }
238 } else {
239 if (!invalidation_map) {
240 invalidation_map=(Bit8u*)malloc(4096);
241 memset(invalidation_map,0,4096);
242 }
243 #if defined(WORDS_BIGENDIAN) || !defined(C_UNALIGNED_MEMORY)
244 host_writew(&invalidation_map[addr],
245 host_readw(&invalidation_map[addr])+0x101);
246 #else
247 (*(Bit16u*)&invalidation_map[addr])+=0x101;
248 #endif
249 if (InvalidateRange(addr,addr+1)) {
250 cpu.exception.which=SMC_CURRENT_BLOCK;
251 return true;
252 }
253 }
254 host_writew(hostmem+addr,val);
255 return false;
256 }
257 bool writed_checked(PhysPt addr,Bitu val) {
258 addr&=4095;
259 if (host_readd(hostmem+addr)==(Bit32u)val) return false;
260 // see if there's code where we are writing to
261 if (!host_readd(&write_map[addr])) {
262 if (!active_blocks) {
263 // no blocks left in this page, still delay the page releasing a bit
264 active_count--;
265 if (!active_count) Release();
266 }
267 } else {
268 if (!invalidation_map) {
269 invalidation_map=(Bit8u*)malloc(4096);
270 memset(invalidation_map,0,4096);
271 }
272 #if defined(WORDS_BIGENDIAN) || !defined(C_UNALIGNED_MEMORY)
273 host_writed(&invalidation_map[addr],
274 host_readd(&invalidation_map[addr])+0x1010101);
275 #else
276 (*(Bit32u*)&invalidation_map[addr])+=0x1010101;
277 #endif
278 if (InvalidateRange(addr,addr+3)) {
279 cpu.exception.which=SMC_CURRENT_BLOCK;
280 return true;
281 }
282 }
283 host_writed(hostmem+addr,val);
284 return false;
285 }
286
287 // add a cache block to this page and note it in the hash map
288 void AddCacheBlock(CacheBlockDynRec * block) {
289 Bitu index=1+(block->page.start>>DYN_HASH_SHIFT);
290 block->hash.next=hash_map[index]; // link to old block at index from the new block
291 block->hash.index=index;
292 hash_map[index]=block; // put new block at hash position
293 block->page.handler=this;
294 active_blocks++;
295 }
296 // there's a block whose code started in a different page
297 void AddCrossBlock(CacheBlockDynRec * block) {
298 block->hash.next=hash_map[0];
299 block->hash.index=0;
300 hash_map[0]=block;
301 block->page.handler=this;
302 active_blocks++;
303 }
304 // remove a cache block
305 void DelCacheBlock(CacheBlockDynRec * block) {
306 active_blocks--;
307 active_count=16;
308 CacheBlockDynRec * * bwhere=&hash_map[block->hash.index];
309 while (*bwhere!=block) {
310 bwhere=&((*bwhere)->hash.next);
311 //Will crash if a block isn't found, which should never happen.
312 }
313 *bwhere=block->hash.next;
314
315 // remove the cleared block from the write map
316 if (GCC_UNLIKELY(block->cache.wmapmask!=NULL)) {
317 // first part is not influenced by the mask
318 for (Bitu i=block->page.start;i<block->cache.maskstart;i++) {
319 if (write_map[i]) write_map[i]--;
320 }
321 Bitu maskct=0;
322 // last part sticks to the writemap mask
323 for (Bitu i=block->cache.maskstart;i<=block->page.end;i++,maskct++) {
324 if (write_map[i]) {
325 // only adjust writemap if it isn't masked
326 if ((maskct>=block->cache.masklen) || (!block->cache.wmapmask[maskct])) write_map[i]--;
327 }
328 }
329 free(block->cache.wmapmask);
330 block->cache.wmapmask=NULL;
331 } else {
332 for (Bitu i=block->page.start;i<=block->page.end;i++) {
333 if (write_map[i]) write_map[i]--;
334 }
335 }
336 }
337
338 void Release(void) {
339 MEM_SetPageHandler(phys_page,1,old_pagehandler); // revert to old handler
340 PAGING_ClearTLB();
341
342 // remove page from the lists
343 if (prev) prev->next=next;
344 else cache.used_pages=next;
345 if (next) next->prev=prev;
346 else cache.last_page=prev;
347 next=cache.free_pages;
348 cache.free_pages=this;
349 prev=0;
350 }
351 void ClearRelease(void) {
352 // clear out all cache blocks in this page
353 for (Bitu index=0;index<(1+DYN_PAGE_HASH);index++) {
354 CacheBlockDynRec * block=hash_map[index];
355 while (block) {
356 CacheBlockDynRec * nextblock=block->hash.next;
357 block->page.handler=0; // no need, full clear
358 block->Clear();
359 block=nextblock;
360 }
361 }
362 Release(); // now can release this page
363 }
364
365 CacheBlockDynRec * FindCacheBlock(Bitu start) {
366 CacheBlockDynRec * block=hash_map[1+(start>>DYN_HASH_SHIFT)];
367 // see if there's a cache block present at the start address
368 while (block) {
369 if (block->page.start==start) return block; // found
370 block=block->hash.next;
371 }
372 return 0; // none found
373 }
374
375 HostPt GetHostReadPt(Bitu phys_page) {
376 hostmem=old_pagehandler->GetHostReadPt(phys_page);
377 return hostmem;
378 }
379 HostPt GetHostWritePt(Bitu phys_page) {
380 return GetHostReadPt( phys_page );
381 }
382 public:
383 // the write map, there are write_map[i] cache blocks that cover the byte at address i
384 Bit8u write_map[4096];
385 Bit8u * invalidation_map;
386 CodePageHandlerDynRec * next, * prev; // page linking
387 private:
388 PageHandler * old_pagehandler;
389
390 // hash map to quickly find the cache blocks in this page
391 CacheBlockDynRec * hash_map[1+DYN_PAGE_HASH];
392
393 Bitu active_blocks; // the number of cache blocks in this page
394 Bitu active_count; // delaying parameter to not immediately release a page
395 HostPt hostmem;
396 Bitu phys_page;
397 };
398
399
400 static INLINE void cache_addunusedblock(CacheBlockDynRec * block) {
401 // block has become unused, add it to the freelist
402 block->cache.next=cache.block.free;
403 cache.block.free=block;
404 }
405
406 static CacheBlockDynRec * cache_getblock(void) {
407 // get a free cache block and advance the free pointer
408 CacheBlockDynRec * ret=cache.block.free;
409 if (!ret) E_Exit("Ran out of CacheBlocks" );
410 cache.block.free=ret->cache.next;
411 ret->cache.next=0;
412 return ret;
413 }
414
415 void CacheBlockDynRec::Clear(void) {
416 Bitu ind;
417 // check if this is not a cross page block
418 if (hash.index) for (ind=0;ind<2;ind++) {
419 CacheBlockDynRec * fromlink=link[ind].from;
420 link[ind].from=0;
421 while (fromlink) {
422 CacheBlockDynRec * nextlink=fromlink->link[ind].next;
423 // clear the next-link and let the block point to the standard linkcode
424 fromlink->link[ind].next=0;
425 fromlink->link[ind].to=&link_blocks[ind];
426
427 fromlink=nextlink;
428 }
429 if (link[ind].to!=&link_blocks[ind]) {
430 // not linked to the standard linkcode, find the block that links to this block
431 CacheBlockDynRec * * wherelink=&link[ind].to->link[ind].from;
432 while (*wherelink != this && *wherelink) {
433 wherelink = &(*wherelink)->link[ind].next;
434 }
435 // now remove the link
436 if(*wherelink)
437 *wherelink = (*wherelink)->link[ind].next;
438 else {
439 LOG(LOG_CPU,LOG_ERROR)("Cache anomaly. please investigate");
440 }
441 }
442 } else
443 cache_addunusedblock(this);
444 if (crossblock) {
445 // clear out the crossblock (in the page before) as well
446 crossblock->crossblock=0;
447 crossblock->Clear();
448 crossblock=0;
449 }
450 if (page.handler) {
451 // clear out the code page handler
452 page.handler->DelCacheBlock(this);
453 page.handler=0;
454 }
455 if (cache.wmapmask){
456 free(cache.wmapmask);
457 cache.wmapmask=NULL;
458 }
459 }
460
461
462 static CacheBlockDynRec * cache_openblock(void) {
463 CacheBlockDynRec * block=cache.block.active;
464 // check for enough space in this block
465 Bitu size=block->cache.size;
466 CacheBlockDynRec * nextblock=block->cache.next;
467 if (block->page.handler)
468 block->Clear();
469 // block size must be at least CACHE_MAXSIZE
470 while (size<CACHE_MAXSIZE) {
471 if (!nextblock)
472 goto skipresize;
473 // merge blocks
474 size+=nextblock->cache.size;
475 CacheBlockDynRec * tempblock=nextblock->cache.next;
476 if (nextblock->page.handler)
477 nextblock->Clear();
478 // block is free now
479 cache_addunusedblock(nextblock);
480 nextblock=tempblock;
481 }
482 skipresize:
483 // adjust parameters and open this block
484 block->cache.size=size;
485 block->cache.next=nextblock;
486 cache.pos=block->cache.start;
487 return block;
488 }
489
490 static void cache_closeblock(void) {
491 CacheBlockDynRec * block=cache.block.active;
492 // links point to the default linking code
493 block->link[0].to=&link_blocks[0];
494 block->link[1].to=&link_blocks[1];
495 block->link[0].from=0;
496 block->link[1].from=0;
497 block->link[0].next=0;
498 block->link[1].next=0;
499 // close the block with correct alignment
500 Bitu written=(Bitu)(cache.pos-block->cache.start);
501 if (written>block->cache.size) {
502 if (!block->cache.next) {
503 if (written>block->cache.size+CACHE_MAXSIZE) E_Exit("CacheBlock overrun 1 %d",written-block->cache.size);
504 } else E_Exit("CacheBlock overrun 2 written %d size %d",written,block->cache.size);
505 } else {
506 Bitu new_size;
507 Bitu left=block->cache.size-written;
508 // smaller than cache align then don't bother to resize
509 if (left>CACHE_ALIGN) {
510 new_size=((written-1)|(CACHE_ALIGN-1))+1;
511 CacheBlockDynRec * newblock=cache_getblock();
512 // align block now to CACHE_ALIGN
513 newblock->cache.start=block->cache.start+new_size;
514 newblock->cache.size=block->cache.size-new_size;
515 newblock->cache.next=block->cache.next;
516 block->cache.next=newblock;
517 block->cache.size=new_size;
518 }
519 }
520 // advance the active block pointer
521 if (!block->cache.next || (block->cache.next->cache.start>(cache_code_start_ptr + CACHE_TOTAL - CACHE_MAXSIZE))) {
522 // LOG_MSG("Cache full restarting");
523 cache.block.active=cache.block.first;
524 } else {
525 cache.block.active=block->cache.next;
526 }
527 }
528
529
530 // place an 8bit value into the cache
531 static INLINE void cache_addb(Bit8u val) {
532 *cache.pos++=val;
533 }
534
535 // place a 16bit value into the cache
536 static INLINE void cache_addw(Bit16u val) {
537 *(Bit16u*)cache.pos=val;
538 cache.pos+=2;
539 }
540
541 // place a 32bit value into the cache
542 static INLINE void cache_addd(Bit32u val) {
543 *(Bit32u*)cache.pos=val;
544 cache.pos+=4;
545 }
546
547 // place a 64bit value into the cache
548 static INLINE void cache_addq(Bit64u val) {
549 *(Bit64u*)cache.pos=val;
550 cache.pos+=8;
551 }
552
553
554 static void dyn_return(BlockReturn retcode,bool ret_exception);
555 static void dyn_run_code(void);
556
557
558 /* Define temporary pagesize so the MPROTECT case and the regular case share as much code as possible */
559 #if (C_HAVE_MPROTECT)
560 #define PAGESIZE_TEMP PAGESIZE
561 #else
562 #define PAGESIZE_TEMP 4096
563 #endif
564
565 static bool cache_initialized = false;
566
567 static void cache_init(bool enable) {
568 Bits i;
569 if (enable) {
570 // see if cache is already initialized
571 if (cache_initialized) return;
572 cache_initialized = true;
573 if (cache_blocks == NULL) {
574 // allocate the cache blocks memory
575 cache_blocks=(CacheBlockDynRec*)malloc(CACHE_BLOCKS*sizeof(CacheBlockDynRec));
576 if(!cache_blocks) E_Exit("Allocating cache_blocks has failed");
577 memset(cache_blocks,0,sizeof(CacheBlockDynRec)*CACHE_BLOCKS);
578 cache.block.free=&cache_blocks[0];
579 // initialize the cache blocks
580 for (i=0;i<CACHE_BLOCKS-1;i++) {
581 cache_blocks[i].link[0].to=(CacheBlockDynRec *)1;
582 cache_blocks[i].link[1].to=(CacheBlockDynRec *)1;
583 cache_blocks[i].cache.next=&cache_blocks[i+1];
584 }
585 }
586 if (cache_code_start_ptr==NULL) {
587 // allocate the code cache memory
588 #if defined (WIN32)
589 cache_code_start_ptr=(Bit8u*)VirtualAlloc(0,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP,
590 MEM_COMMIT,PAGE_EXECUTE_READWRITE);
591 if (!cache_code_start_ptr)
592 cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP);
593 #else
594 cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP);
595 #endif
596 if(!cache_code_start_ptr) E_Exit("Allocating dynamic cache failed");
597
598 // align the cache at a page boundary
599 cache_code=(Bit8u*)(((Bitu)cache_code_start_ptr + PAGESIZE_TEMP-1) & ~(PAGESIZE_TEMP-1));//Bitu is same size as a pointer.
600
601 cache_code_link_blocks=cache_code;
602 cache_code=cache_code+PAGESIZE_TEMP;
603
604 #if (C_HAVE_MPROTECT)
605 if(mprotect(cache_code_link_blocks,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP,PROT_WRITE|PROT_READ|PROT_EXEC))
606 LOG_MSG("Setting excute permission on the code cache has failed");
607 #endif
608 CacheBlockDynRec * block=cache_getblock();
609 cache.block.first=block;
610 cache.block.active=block;
611 block->cache.start=&cache_code[0];
612 block->cache.size=CACHE_TOTAL;
613 block->cache.next=0; // last block in the list
614 }
615 // setup the default blocks for block linkage returns
616 cache.pos=&cache_code_link_blocks[0];
617 link_blocks[0].cache.start=cache.pos;
618 // link code that returns with a special return code
619 dyn_return(BR_Link1,false);
620 cache.pos=&cache_code_link_blocks[32];
621 link_blocks[1].cache.start=cache.pos;
622 // link code that returns with a special return code
623 dyn_return(BR_Link2,false);
624
625 cache.pos=&cache_code_link_blocks[64];
626 core_dynrec.runcode=(BlockReturn (*)(Bit8u*))cache.pos;
627 // link_blocks[1].cache.start=cache.pos;
628 dyn_run_code();
629
630 cache.free_pages=0;
631 cache.last_page=0;
632 cache.used_pages=0;
633 // setup the code pages
634 for (i=0;i<CACHE_PAGES;i++) {
635 CodePageHandlerDynRec * newpage=new CodePageHandlerDynRec();
636 newpage->next=cache.free_pages;
637 cache.free_pages=newpage;
638 }
639 }
640 }
641
642 static void cache_close(void) {
643 /* for (;;) {
644 if (cache.used_pages) {
645 CodePageHandler * cpage=cache.used_pages;
646 CodePageHandler * npage=cache.used_pages->next;
647 cpage->ClearRelease();
648 delete cpage;
649 cache.used_pages=npage;
650 } else break;
651 }
652 if (cache_blocks != NULL) {
653 free(cache_blocks);
654 cache_blocks = NULL;
655 }
656 if (cache_code_start_ptr != NULL) {
657 ### care: under windows VirtualFree() has to be used if
658 ### VirtualAlloc was used for memory allocation
659 free(cache_code_start_ptr);
660 cache_code_start_ptr = NULL;
661 }
662 cache_code = NULL;
663 cache_code_link_blocks = NULL;
664 cache_initialized = false; */
665 }