12 |
|
#include <string.h> |
13 |
|
|
14 |
|
#include "platform.h" |
15 |
+ |
#include "rtprocess.h" |
16 |
|
|
17 |
|
#include "holo.h" |
18 |
|
|
19 |
|
#ifndef CACHESIZE |
20 |
|
#ifdef SMLMEM |
21 |
< |
#define CACHESIZE 5 |
21 |
> |
#define CACHESIZE 10 |
22 |
|
#else |
23 |
< |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
23 |
> |
#define CACHESIZE 100 /* default cache size (Mbytes, 0==inf) */ |
24 |
|
#endif |
25 |
|
#endif |
26 |
|
#ifndef FREEBEAMS |
30 |
|
#define PCTFREE 15 /* maximum fraction to free (%) */ |
31 |
|
#endif |
32 |
|
#ifndef MAXFRAGB |
33 |
< |
#define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */ |
33 |
> |
#define MAXFRAGB 64 /* fragment blocks/file to track (0==inf) */ |
34 |
|
#endif |
35 |
|
#ifndef FF_DEFAULT |
36 |
|
/* when to free a beam fragment */ |
64 |
|
|
65 |
|
static int nhdfragls; /* size of hdfragl array */ |
66 |
|
|
67 |
+ |
static HOLO *hdalloc(HDGRID *hproto); |
68 |
+ |
static char *hdrealloc(char *ptr, unsigned siz, char *rout); |
69 |
+ |
static void hdattach(int fd, int wr); |
70 |
+ |
static void hdrelease(int fd); |
71 |
+ |
static void hdmarkdirty(HOLO *hp, int i); |
72 |
+ |
static unsigned int hdmemuse(int all); |
73 |
+ |
static int hdfilord(const void *hb1, const void *hb2); |
74 |
+ |
static off_t hdallocfrag(int fd, uint32 nrays); |
75 |
+ |
static int hdsyncbeam(HOLO *hp, int i); |
76 |
+ |
static int hdlrulist(HDBEAMI *hb, int nents, int n, HOLO *hp); |
77 |
+ |
static int hdfreecache(int pct, HOLO *honly); |
78 |
|
|
79 |
+ |
|
80 |
+ |
|
81 |
|
HOLO * |
82 |
< |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
83 |
< |
HDGRID *hproto; |
82 |
> |
hdalloc( /* allocate and set holodeck section based on grid */ |
83 |
> |
HDGRID *hproto |
84 |
> |
) |
85 |
|
{ |
86 |
|
HOLO hdhead; |
87 |
|
register HOLO *hp; |
114 |
|
|
115 |
|
|
116 |
|
char * |
117 |
< |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
118 |
< |
char *ptr; |
119 |
< |
unsigned siz; |
120 |
< |
char *rout; |
117 |
> |
hdrealloc( /* (re)allocate memory, retry then error */ |
118 |
> |
char *ptr, |
119 |
> |
unsigned siz, |
120 |
> |
char *rout |
121 |
> |
) |
122 |
|
{ |
123 |
|
register char *newp; |
124 |
|
/* call malloc/realloc */ |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
< |
hdattach(fd, wr) /* start tracking file fragments for some section */ |
142 |
< |
register int fd; |
143 |
< |
int wr; |
141 |
> |
void |
142 |
> |
hdattach( /* start tracking file fragments for some section */ |
143 |
> |
register int fd, |
144 |
> |
int wr |
145 |
> |
) |
146 |
|
{ |
147 |
|
if (fd >= nhdfragls) { |
148 |
|
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
161 |
|
/* Do we need a routine to locate file fragments given known occupants? */ |
162 |
|
|
163 |
|
|
164 |
< |
hdrelease(fd) /* stop tracking file fragments for some section */ |
165 |
< |
register int fd; |
164 |
> |
void |
165 |
> |
hdrelease( /* stop tracking file fragments for some section */ |
166 |
> |
register int fd |
167 |
> |
) |
168 |
|
{ |
169 |
|
if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks) |
170 |
|
return; |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
< |
HOLO * |
180 |
< |
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
181 |
< |
int fd; /* corresponding file descriptor */ |
182 |
< |
HDGRID *hproto; /* holodeck section grid */ |
179 |
> |
extern HOLO * |
180 |
> |
hdinit( /* initialize a holodeck section in a file */ |
181 |
> |
int fd, /* corresponding file descriptor */ |
182 |
> |
HDGRID *hproto /* holodeck section grid */ |
183 |
> |
) |
184 |
|
{ |
185 |
|
off_t rtrunc; |
186 |
|
off_t fpos; |
246 |
|
} |
247 |
|
if (rtrunc) { |
248 |
|
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
249 |
< |
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
249 |
> |
(long)rtrunc, |
250 |
> |
100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
251 |
|
error(WARNING, errmsg); |
252 |
|
} |
253 |
|
/* add to holodeck list */ |
260 |
|
return(hp); |
261 |
|
memerr: |
262 |
|
error(SYSTEM, "cannot allocate holodeck grid"); |
263 |
+ |
return NULL; /* pro forma return */ |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
< |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
268 |
< |
register HOLO *hp; |
269 |
< |
int i; |
267 |
> |
void |
268 |
> |
hdmarkdirty( /* mark holodeck directory position dirty */ |
269 |
> |
register HOLO *hp, |
270 |
> |
int i |
271 |
> |
) |
272 |
|
{ |
273 |
|
static BEAMI smudge = {0, -1}; |
274 |
|
int mindist, minpos; |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
< |
int |
322 |
< |
hdsync(hp, all) /* update beams and directory on disk */ |
323 |
< |
register HOLO *hp; |
324 |
< |
int all; |
321 |
> |
extern int |
322 |
> |
hdsync( /* update beams and directory on disk */ |
323 |
> |
register HOLO *hp, |
324 |
> |
int all |
325 |
> |
) |
326 |
|
{ |
327 |
|
register int j, n; |
328 |
|
|
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
< |
unsigned |
356 |
< |
hdmemuse(all) /* return memory usage (in bytes) */ |
357 |
< |
int all; /* include overhead (painful) */ |
355 |
> |
unsigned int |
356 |
> |
hdmemuse( /* return memory usage (in bytes) */ |
357 |
> |
int all /* include overhead (painful) */ |
358 |
> |
) |
359 |
|
{ |
360 |
|
long total = 0; |
361 |
|
register int i, j; |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
< |
off_t |
386 |
< |
hdfilen(fd) /* return file length for fd */ |
387 |
< |
int fd; |
385 |
> |
extern off_t |
386 |
> |
hdfilen( /* return file length for fd */ |
387 |
> |
int fd |
388 |
> |
) |
389 |
|
{ |
390 |
|
off_t fpos, flen; |
391 |
|
|
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
< |
off_t |
406 |
< |
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
407 |
< |
int fd; /* open file descriptor to check */ |
408 |
< |
int all; /* include overhead and unflushed data */ |
405 |
> |
extern off_t |
406 |
> |
hdfiluse( /* compute file usage (in bytes) */ |
407 |
> |
int fd /* open file descriptor to check */ |
408 |
> |
) |
409 |
|
{ |
410 |
|
off_t total = 0; |
411 |
< |
register int i, j; |
411 |
> |
register int j; |
412 |
|
|
413 |
|
for (j = 0; hdlist[j] != NULL; j++) { |
414 |
|
if (hdlist[j]->fd != fd) |
415 |
|
continue; |
416 |
|
total += biglob(hdlist[j])->nrd * sizeof(RAYVAL); |
417 |
< |
if (all) { |
418 |
< |
for (i = nbeams(hdlist[j]); i > 0; i--) |
419 |
< |
if (hdlist[j]->bl[i] != NULL) |
420 |
< |
total += sizeof(RAYVAL) * |
417 |
> |
total += nbeams(hdlist[j])*sizeof(BEAMI) + sizeof(HDGRID); |
418 |
> |
#if 0 |
419 |
> |
for (i = nbeams(hdlist[j]); i > 0; i--) |
420 |
> |
if (hdlist[j]->bl[i] != NULL) |
421 |
> |
total += sizeof(RAYVAL) * |
422 |
|
(hdlist[j]->bl[i]->nrm - |
423 |
|
hdlist[j]->bi[i].nrd); |
424 |
< |
total += sizeof(HDGRID) + |
396 |
< |
nbeams(hdlist[j])*sizeof(BEAMI); |
397 |
< |
} |
424 |
> |
#endif |
425 |
|
} |
426 |
< |
return(total); /* does not include fragments */ |
426 |
> |
return(total); /* doesn't include fragments, unflushed rays */ |
427 |
|
} |
428 |
|
|
429 |
|
|
430 |
< |
RAYVAL * |
431 |
< |
hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */ |
432 |
< |
register HOLO *hp; |
433 |
< |
register int i; |
434 |
< |
int nr; /* number of new rays desired */ |
430 |
> |
extern RAYVAL * |
431 |
> |
hdnewrays( /* allocate space for add'l rays and return pointer */ |
432 |
> |
register HOLO *hp, |
433 |
> |
register int i, |
434 |
> |
int nr /* number of new rays desired */ |
435 |
> |
) |
436 |
|
{ |
437 |
|
RAYVAL *p; |
438 |
|
int n; |
472 |
|
} |
473 |
|
|
474 |
|
|
475 |
< |
BEAM * |
476 |
< |
hdgetbeam(hp, i) /* get beam (from file if necessary) */ |
477 |
< |
register HOLO *hp; |
478 |
< |
register int i; |
475 |
> |
extern BEAM * |
476 |
> |
hdgetbeam( /* get beam (from file if necessary) */ |
477 |
> |
register HOLO *hp, |
478 |
> |
register int i |
479 |
> |
) |
480 |
|
{ |
481 |
|
register int n; |
482 |
|
|
504 |
|
|
505 |
|
|
506 |
|
int |
507 |
< |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
508 |
< |
register HDBEAMI *hb1, *hb2; |
507 |
> |
hdfilord( /* order beams for quick loading */ |
508 |
> |
register const void *hb1, |
509 |
> |
register const void *hb2 |
510 |
> |
) |
511 |
|
{ |
512 |
|
register off_t c; |
513 |
|
/* residents go first */ |
514 |
< |
if (hb2->h->bl[hb2->b] != NULL) |
515 |
< |
return(hb1->h->bl[hb1->b] == NULL); |
516 |
< |
if (hb1->h->bl[hb1->b] != NULL) |
514 |
> |
if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL) |
515 |
> |
return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL); |
516 |
> |
if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL) |
517 |
|
return(-1); |
518 |
|
/* otherwise sort by file descriptor */ |
519 |
< |
if (hb1->h->fd != hb2->h->fd) |
520 |
< |
return(hb1->h->fd - hb2->h->fd); |
519 |
> |
if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd) |
520 |
> |
return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd); |
521 |
|
/* then by position in file */ |
522 |
< |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
522 |
> |
c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo |
523 |
> |
- ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo; |
524 |
|
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
< |
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
529 |
< |
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
530 |
< |
int n; /* list length */ |
531 |
< |
void (*bf)(); /* callback function (optional) */ |
528 |
> |
extern void |
529 |
> |
hdloadbeams( /* load a list of beams in optimal order */ |
530 |
> |
register HDBEAMI *hb, /* list gets sorted by hdfilord() */ |
531 |
> |
int n, /* list length */ |
532 |
> |
void (*bf)(BEAM *bp, HDBEAMI *hb) /* callback function (optional) */ |
533 |
> |
) |
534 |
|
{ |
535 |
|
unsigned origcachesize, memuse; |
536 |
|
int bytesloaded, needbytes, bytes2free; |
572 |
|
} |
573 |
|
|
574 |
|
|
575 |
< |
int |
576 |
< |
hdfreefrag(hp, i) /* free a file fragment */ |
577 |
< |
HOLO *hp; |
578 |
< |
int i; |
575 |
> |
extern int |
576 |
> |
hdfreefrag( /* free a file fragment */ |
577 |
> |
HOLO *hp, |
578 |
> |
int i |
579 |
> |
) |
580 |
|
{ |
581 |
|
register BEAMI *bi = &hp->bi[i]; |
582 |
|
register struct fraglist *f; |
652 |
|
} |
653 |
|
|
654 |
|
|
655 |
< |
int |
656 |
< |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
657 |
< |
int fd; |
658 |
< |
int *listlen; |
659 |
< |
register int32 *listsiz; |
655 |
> |
extern int |
656 |
> |
hdfragOK( /* get fragment list status for file */ |
657 |
> |
int fd, |
658 |
> |
int *listlen, |
659 |
> |
register int32 *listsiz |
660 |
> |
) |
661 |
|
{ |
662 |
|
register struct fraglist *f; |
663 |
|
register int i; |
678 |
|
|
679 |
|
|
680 |
|
off_t |
681 |
< |
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
682 |
< |
int fd; |
683 |
< |
uint32 nrays; |
681 |
> |
hdallocfrag( /* allocate a file fragment */ |
682 |
> |
int fd, |
683 |
> |
uint32 nrays |
684 |
> |
) |
685 |
|
{ |
686 |
|
register struct fraglist *f; |
687 |
|
register int j; |
708 |
|
|
709 |
|
|
710 |
|
int |
711 |
< |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
712 |
< |
register HOLO *hp; |
713 |
< |
register int i; |
711 |
> |
hdsyncbeam( /* sync beam in memory with beam on disk */ |
712 |
> |
register HOLO *hp, |
713 |
> |
register int i |
714 |
> |
) |
715 |
|
{ |
716 |
|
int fragfreed; |
717 |
|
uint32 nrays; |
749 |
|
} |
750 |
|
|
751 |
|
|
752 |
< |
int |
753 |
< |
hdfreebeam(hp, i) /* free beam, writing if dirty */ |
754 |
< |
register HOLO *hp; |
755 |
< |
register int i; |
752 |
> |
extern int |
753 |
> |
hdfreebeam( /* free beam, writing if dirty */ |
754 |
> |
register HOLO *hp, |
755 |
> |
register int i |
756 |
> |
) |
757 |
|
{ |
758 |
|
int nchanged; |
759 |
|
|
791 |
|
} |
792 |
|
|
793 |
|
|
794 |
< |
int |
795 |
< |
hdkillbeam(hp, i) /* delete beam from holodeck */ |
796 |
< |
register HOLO *hp; |
797 |
< |
register int i; |
794 |
> |
extern int |
795 |
> |
hdkillbeam( /* delete beam from holodeck */ |
796 |
> |
register HOLO *hp, |
797 |
> |
register int i |
798 |
> |
) |
799 |
|
{ |
800 |
|
int nchanged; |
801 |
|
|
838 |
|
|
839 |
|
|
840 |
|
int |
841 |
< |
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
842 |
< |
register HDBEAMI *hb; /* beam list */ |
843 |
< |
int nents; /* current list length */ |
844 |
< |
int n; /* maximum list length */ |
845 |
< |
register HOLO *hp; /* section we're adding from */ |
841 |
> |
hdlrulist( /* add beams from holodeck to LRU list */ |
842 |
> |
register HDBEAMI *hb, /* beam list */ |
843 |
> |
int nents, /* current list length */ |
844 |
> |
int n, /* maximum list length */ |
845 |
> |
register HOLO *hp /* section we're adding from */ |
846 |
> |
) |
847 |
|
{ |
848 |
|
register int i, j; |
849 |
|
/* insert each beam from hp */ |
871 |
|
|
872 |
|
|
873 |
|
int |
874 |
< |
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
875 |
< |
int pct; /* maximum percentage to free */ |
876 |
< |
register HOLO *honly; /* NULL means check all */ |
874 |
> |
hdfreecache( /* free up cache space, writing changes */ |
875 |
> |
int pct, /* maximum percentage to free */ |
876 |
> |
register HOLO *honly /* NULL means check all */ |
877 |
> |
) |
878 |
|
{ |
879 |
|
HDBEAMI hb[FREEBEAMS]; |
880 |
|
int freetarget; |
915 |
|
} |
916 |
|
|
917 |
|
|
918 |
< |
hddone(hp) /* clean up holodeck section and free */ |
919 |
< |
register HOLO *hp; /* NULL means clean up all */ |
918 |
> |
extern void |
919 |
> |
hddone( /* clean up holodeck section and free */ |
920 |
> |
register HOLO *hp /* NULL means clean up all */ |
921 |
> |
) |
922 |
|
{ |
923 |
|
register int i; |
924 |
|
|