void str_line_init(OBJREC *o,void **x) //source_list { o->userptr=(void *)getmem(16); memcpy(o->userptr,x,sizeof(*x)); }
static void putaddr(int mode, unsigned char val) { unsigned short ad,ad2; switch(mode) { case abs: cycles+=4; ad=getmem(pc++); ad|=getmem(pc++)<<8; setmem(ad,val); return; case absx: cycles+=4; ad=getmem(pc++); ad|=getmem(pc++)<<8; ad2=ad+x; setmem(ad2,val); return; case absy: cycles+=4; ad=getmem(pc++); ad|=getmem(pc++)<<8; ad2=ad+y; if ((ad2&0xff00)!=(ad&0xff00)) cycles++; setmem(ad2,val); return; case zp: cycles+=3; ad=getmem(pc++); setmem(ad,val); return; case zpx: cycles+=4; ad=getmem(pc++); ad+=x; setmem(ad&0xff,val); return; case zpy: cycles+=4; ad=getmem(pc++); ad+=y; setmem(ad&0xff,val); return; case indx: cycles+=6; ad=getmem(pc++); ad+=x; ad2=getmem(ad&0xff); ad++; ad2|=getmem(ad&0xff)<<8; setmem(ad2,val); return; case indy: cycles+=5; ad=getmem(pc++); ad2=getmem(ad); ad2|=getmem((ad+1)&0xff)<<8; ad=ad2+y; setmem(ad,val); return; case acc: cycles+=2; a=val; return; } }
static inline void cpuParse(unsigned char opc) { int cmd = opcodes[opc]; int addr = modes[opc]; int c; switch (cmd) { case adc: wval=(unsigned short)a+getaddr(addr)+((p&FLAG_C)?1:0); setflags(FLAG_C, wval&0x100); a=(unsigned char)wval; setflags(FLAG_Z, !a); setflags(FLAG_N, a&0x80); setflags(FLAG_V, (!!(p&FLAG_C)) ^ (!!(p&FLAG_N))); break; case and_: bval=getaddr(addr); a&=bval; setflags(FLAG_Z, !a); setflags(FLAG_N, a&0x80); break; case asl: wval=getaddr(addr); wval<<=1; setaddr(addr,(unsigned char)wval); setflags(FLAG_Z,!wval); setflags(FLAG_N,wval&0x80); setflags(FLAG_C,wval&0x100); break; case bcc: branch(!(p&FLAG_C)); break; case bcs: branch(p&FLAG_C); break; case bne: branch(!(p&FLAG_Z)); break; case beq: branch(p&FLAG_Z); break; case bpl: branch(!(p&FLAG_N)); break; case bmi: branch(p&FLAG_N); break; case bvc: branch(!(p&FLAG_V)); break; case bvs: branch(p&FLAG_V); break; case bit: bval=getaddr(addr); setflags(FLAG_Z,!(a&bval)); setflags(FLAG_N,bval&0x80); setflags(FLAG_V,bval&0x40); break; case brk: pc=0; /* Just quit the emulation */ break; case clc: setflags(FLAG_C,0); break; case cld: setflags(FLAG_D,0); break; case cli: setflags(FLAG_I,0); break; case clv: setflags(FLAG_V,0); break; case cmp: bval=getaddr(addr); wval=(unsigned short)a-bval; setflags(FLAG_Z,!wval); setflags(FLAG_N,wval&0x80); setflags(FLAG_C,a>=bval); break; case cpx: bval=getaddr(addr); wval=(unsigned short)x-bval; setflags(FLAG_Z,!wval); setflags(FLAG_N,wval&0x80); setflags(FLAG_C,x>=bval); break; case cpy: bval=getaddr(addr); wval=(unsigned short)y-bval; setflags(FLAG_Z,!wval); setflags(FLAG_N,wval&0x80); setflags(FLAG_C,y>=bval); break; case dec: bval=getaddr(addr); bval--; setaddr(addr,bval); setflags(FLAG_Z,!bval); setflags(FLAG_N,bval&0x80); break; case dex: x--; setflags(FLAG_Z,!x); setflags(FLAG_N,x&0x80); break; case dey: y--; setflags(FLAG_Z,!y); setflags(FLAG_N,y&0x80); break; case eor: bval=getaddr(addr); a^=bval; setflags(FLAG_Z,!a); setflags(FLAG_N,a&0x80); break; case inc: bval=getaddr(addr); bval++; setaddr(addr,bval); setflags(FLAG_Z,!bval); setflags(FLAG_N,bval&0x80); break; case inx: x++; setflags(FLAG_Z,!x); setflags(FLAG_N,x&0x80); break; case iny: y++; setflags(FLAG_Z,!y); setflags(FLAG_N,y&0x80); break; case jmp: wval=getmem(pc++); wval|=256*getmem(pc++); switch (addr) { case abs: pc=wval; break; case ind: pc=getmem(wval); pc|=256*getmem(wval+1); break; } break; case jsr: push((pc+1)>>8); push((pc+1)); wval=getmem(pc++); wval|=256*getmem(pc++); pc=wval; break; case lda: a=getaddr(addr); setflags(FLAG_Z,!a); setflags(FLAG_N,a&0x80); break; case ldx: x=getaddr(addr); setflags(FLAG_Z,!x); setflags(FLAG_N,x&0x80); break; case ldy: y=getaddr(addr); setflags(FLAG_Z,!y); setflags(FLAG_N,y&0x80); break; case lsr: bval=getaddr(addr); wval=(unsigned char)bval; wval>>=1; setaddr(addr,(unsigned char)wval); setflags(FLAG_Z,!wval); setflags(FLAG_N,wval&0x80); setflags(FLAG_C,bval&1); break; case nop: break; case ora: bval=getaddr(addr); a|=bval; setflags(FLAG_Z,!a); setflags(FLAG_N,a&0x80); break; case pha: push(a); break; case php: push(p); break; case pla: a=pop(); setflags(FLAG_Z,!a); setflags(FLAG_N,a&0x80); break; case plp: p=pop(); break; case rol: bval=getaddr(addr); c=!!(p&FLAG_C); setflags(FLAG_C,bval&0x80); bval<<=1; bval|=c; setaddr(addr,bval); setflags(FLAG_N,bval&0x80); setflags(FLAG_Z,!bval); break; case ror: bval=getaddr(addr); c=!!(p&FLAG_C); setflags(FLAG_C,bval&1); bval>>=1; bval|=128*c; setaddr(addr,bval); setflags(FLAG_N,bval&0x80); setflags(FLAG_Z,!bval); break; case rti: /* Treat RTI like RTS */ case rts: wval=pop(); wval|=pop()<<8; pc=wval+1; break; case sbc: bval=getaddr(addr)^0xff; wval=(unsigned short)a+bval+((p&FLAG_C)?1:0); setflags(FLAG_C, wval&0x100); a=(unsigned char)wval; setflags(FLAG_Z, !a); setflags(FLAG_N, a>127); setflags(FLAG_V, (!!(p&FLAG_C)) ^ (!!(p&FLAG_N))); break; case sec: setflags(FLAG_C,1); break; case sed: setflags(FLAG_D,1); break; case sei: setflags(FLAG_I,1); break; case sta: putaddr(addr,a); break; case stx: putaddr(addr,x); break; case sty: putaddr(addr,y); break; case tax: x=a; setflags(FLAG_Z, !x); setflags(FLAG_N, x&0x80); break; case tay: y=a; setflags(FLAG_Z, !y); setflags(FLAG_N, y&0x80); break; case tsx: x=s; setflags(FLAG_Z, !x); setflags(FLAG_N, x&0x80); break; case txa: a=x; setflags(FLAG_Z, !a); setflags(FLAG_N, a&0x80); break; case txs: s=x; break; case tya: a=y; setflags(FLAG_Z, !a); setflags(FLAG_N, a&0x80); break; case slo: bval = getaddr(addr); setflags(FLAG_C, bval >> 7); bval <<= 1; setaddr(addr, bval); a |= bval; setflags(FLAG_Z, !a); setflags(FLAG_N, a&0x80); break; case axs: x = (x & a) - getaddr(addr); setflags(FLAG_Z, a == 0); setflags(FLAG_N, a > 127); break; case lax: a = x = getaddr(addr); setflags(FLAG_Z, a == 0); setflags(FLAG_N, a & 0x80); break; default: break; } }
/* * Returns error code equal to the HYBSVD error code (plus additional err=-6). * err=0: OK * err>0: SVD failed * err=-1 to -5: Various SVD errors * err=-6: More parameters than points */ int linfit_setup(float *x, float *sigma, int npts, int npars, int nvars, void (*func)(int npts, int npars, float *pars, int nvars, float *xvecs, float *yvec) ) { int i; int j; int k; float sum; float minw; float maxw; float winv; float *pars; float *a; float *y1; float *w; float *u; float *v; float *z; float *work; integer lnpts; integer lnpars; integer trueflag; integer err = 0; /* Note that matrices a, u, and v are passed/returned from the * Fortran derived routine "hybsvd_". They are stored in column * order (row number cycles fastest). The following macros hide * this storage convention from the user. */ #define A(i,j) a[j*npts+i] #define U(i,j) u[j*npts+i] #define V(i,j) v[j*npars+i] if (npts < npars){ return -6; /* System excessively singular */ } /* Allocate scratch memory */ pars = (float *)malloc(sizeof(float) * npars); a = (float *)malloc(sizeof(float) * npts * npars); y1 = (float *)malloc(sizeof(float) * npts); w = (float *)malloc(sizeof(float) * npars); u = (float *)malloc(sizeof(float) * npts * npars); v = (float *)malloc(sizeof(float) * npars * npars); z = (float *)malloc(sizeof(float) * npars * npars); work = (float *)malloc(sizeof(float) * npars); /* Memory for transformation matrix */ /* (Free any memory allocated on previous calls) */ release_memitem(yconst); release_memitem(transf); release_memitem(covar); yconst = (float *)getmem(sizeof(float) * npts); transf = (float *)getmem(sizeof(float) * npts * npars); covar = (float *)getmem(sizeof(float) * (npars * (npars+1))/2); /* Load up the "a" matrix */ for (i=0; i<npars; pars[i++]=0);/* Clear parameter vector */ (*func)(npts, npars, pars, nvars, x, yconst); /* Y values with null parms */ for (i=0; i<npars; i++){ pars[i] = 1; (*func)(npts, npars, pars, nvars, x, y1); for (j=0; j<npts; j++){ A(j,i) = y1[j] - yconst[j]; } pars[i] = 0; } /*fprintf(stderr,"a=\n"); for (i=0; i<npts; i++){ for (j=0; j<npars; j++){ fprintf(stderr,"%12.4g ", A(i,j)); } fprintf(stderr,"\n"); }/*CMP*/ /* Get the Singular Value Decomposition of "a" into "uwv'" */ /* Variables needed for Fortran linkage */ trueflag = TRUE_; lnpts = npts; lnpars = npars; hybsvd_(&lnpts, &lnpts, &lnpars, &lnpars, &lnpts, &lnpts, &lnpars, a, w, &trueflag, u, &trueflag, v, z, NULL, &c__0, &err, work); if (!err){ /* Find max "w", so we can set minimum threshold */ maxw = w[0]; for (i=1; i<npars; i++){ if (maxw < w[i]) maxw = w[i]; } minw = srelpr_() * npts * maxw; /* Multiply out transf = vw'u' to get the transformation matrix * that takes a vector of y values into parameter values. */ /* inverse(w) * u' */ for (i=0; i<npars; i++){ winv = w[i] < minw ? 0 : 1 / w[i]; /* Edit singular values */ for (j=0; j<npts; j++){ U(j,i) *= winv; } } /* Mult on left by v */ for (i=0; i<npars; i++){ for (j=0; j<npts; j++){ TRANSF(i,j) = 0; for (k=0; k<npars; k++){ TRANSF(i,j) += V(i,k) * U(j,k); } } } /*fprintf(stderr,"transf=\n"); for (i=0; i<npars; i++){ for (j=0; j<npts; j++){ fprintf(stderr,"%9.3g ", TRANSF(i,j)); } fprintf(stderr,"\n"); }/*CMP*/ /* Get covariance matrix (assuming sigma of each point = 1 */ for (i=0; i<npars; i++){ work[i] = w[i] == 0 ? 0 : 1 / (w[i] * w[i]); } for (i=0; i<npars; i++){ for (j=0; j<=i; j++){ sum = 0; for (k=0; k<npars; k++){ sum += V(i,k) * V(j,k) * work[k]; } COVAR(i,j) = sum; } } } /* Release memory */ if (pars) free(pars); if (a) free(a); if (y1) free(y1); if (w) free(w); if (u) free(u); if (v) free(v); if (z) free(z); if (work) free(work); return err; #undef A #undef U #undef V }
/*------------------------------------------------------------------------ * vfreemem - Free a memory blkaddr, returning the blkaddr to the free list *------------------------------------------------------------------------ */ syscall vfreemem( char *blkaddr, /* Pointer to memory blkaddr */ uint32 nbytes /* Size of blkaddr in bytes */ ) { intmask mask; /* Saved interrupt mask */ struct vmemblk *next, *prev; uint32 top; mask = disable(); if ((nbytes == 0) || ((uint32) blkaddr < (uint32) vminheap) || ((uint32) blkaddr > (uint32) vmaxheap)) { restore(mask); kprintf("vfreemem returning SYSERR\n"); return SYSERR; } struct vmemblk* vmemlist = proctab[currpid].prvmemlist; prev = vmemlist; /* Walk along free list */ next = vmemlist->mnext; while ((next != NULL) && (next->mbegin < blkaddr)) { prev = next; next = next->mnext; } if (prev == vmemlist) { /* Compute top of previous blkaddr*/ top = (uint32) NULL; } else { top = (uint32) (prev->mbegin) + prev->mlength; } /* Ensure new blkaddr does not overlap previous or next blkaddrs */ if (((prev != vmemlist) && (uint32) blkaddr < top) || ((next != NULL) && (uint32) blkaddr+nbytes>(uint32)next->mbegin)) { restore(mask); kprintf("vfreemem returning SYSERR\n"); return SYSERR; } vmemlist->mlength += nbytes; /* Either coalesce with previous blkaddr or add to free list */ bool8 lc = (top == (uint32) blkaddr), rc = (next!=NULL && blkaddr + nbytes == next->mbegin); if (lc && rc) { prev->mlength += nbytes + next->mlength; prev->mnext = next->mnext; freemem((char*)next,sizeof(struct vmemblk)); } else if (lc) { prev->mlength += nbytes; } else if (rc) { next->mbegin = blkaddr; next->mlength += nbytes; } else { struct vmemblk* newblk = (struct vmemblk*)getmem(sizeof(struct vmemblk)); newblk->mbegin = blkaddr; newblk->mlength = nbytes; prev->mnext = newblk; newblk->mnext = next; } restore(mask); return OK; }
double *dgetmem(const int leng) { return ((double *) getmem((size_t) leng, sizeof(double))); }
/*------------------------------------------------------------------------ * x_rls - (command rls) list contents of remote file system directory *------------------------------------------------------------------------ */ COMMAND x_rls(int stdin, int stdout, int stderr, int nargs, char *args[]) { char *p, *buf; int dev, len; char str[256]; struct dirent { /* UNIX directory entry */ long d_inum; /* file's inode number */ short d_rlen; /* length of this record */ short d_nlen; /* length of this file's name */ char d_nam[1]; /* start of file name */ }; struct dirent *d; Bool aflag; aflag = FALSE; if (nargs > 1 && strcmp(p=args[1],"-a") == 0) { nargs--; aflag = TRUE; p = args[2]; } if (nargs == 1) p = "."; else if (nargs != 2) { printf("Usage: rls [-a] directory\n"); return(SYSERR); } if ( ((long)(buf=(char *)getmem(512))) == SYSERR) { fprintf(stderr, "rls: no memory\n"); return(SYSERR); } if (nammap(p, buf) != RFILSYS || (dev=open(NAMESPACE, p, "ro")) == SYSERR) { fprintf(stderr, "cannot open %s\n", p); freemem(buf, 512); return(SYSERR); } while ( (len = read(dev, buf, 512)) > 0) { for (p=buf ; p< &buf[512] ; p += d->d_rlen) { d = (struct dirent *)p; /* this could be a Vax or a Sun, so be */ /* prepared to swap integer fields */ if (d->d_nlen != strlen(d->d_nam)) { d->d_nlen = net2hs(d->d_nlen); d->d_rlen = net2hs(d->d_rlen); } if (d->d_inum == 0) continue; if (len < 512 || d->d_nlen != strlen(d->d_nam) || d->d_nlen > 255 || d->d_rlen < sizeof(struct dirent) || d->d_rlen > &buf[512] - p) { fprintf(stderr, "Not a directory\n"); close(dev); freemem(buf, 512L); return(SYSERR); } if (aflag || d->d_nam[0] != '.') { strcpy(str, d->d_nam); strcat(str, "\n"); write(stdout, str, strlen(str)); } if (d->d_rlen == 0) break; } } freemem(buf, 512); close(dev); return(OK); }
int main(int argc, char **argv) { FILE *fp = stdin; GMM gmm, tgmm, floor; double E = DEF_E, V = DEF_V, W = DEF_W, *dat, *pd, *cb, *icb, *logwgd, logb, *sum, *sum_m, **sum_v, diff, sum_w, ave_logp0, ave_logp1, change = MAXVALUE, tmp1, tmp2; int ispipe, l, ll, L = DEF_L, m, M = DEF_M, N, t, T = DEF_T, S = DEF_S, full = FULL, n1, i, j, Imin = DEF_IMIN, Imax = DEF_IMAX, *tindex, *cntcb; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; /* -- Check options -- */ while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'h': usage(0); break; case 'l': L = atoi(*++argv); --argc; break; case 'm': M = atoi(*++argv); --argc; break; case 't': T = atoi(*++argv); --argc; break; case 's': S = atoi(*++argv); --argc; break; case 'a': Imin = atoi(*++argv); --argc; break; case 'b': Imax = atoi(*++argv); --argc; break; case 'e': E = atof(*++argv); --argc; break; case 'v': V = atof(*++argv); --argc; break; case 'w': W = atof(*++argv); --argc; break; case 'f': full = 1 - full; break; default: fprintf(stderr, "%s: Illegal option \"%s\".\n", cmnd, *argv); usage(1); } } else fp = getfp(*argv, "rb"); /* -- Count number of training vectors -- */ if (T == -1) { ispipe = fseek(fp, 0L, SEEK_END); T = (int) (ftell(fp) / (double) L / (double) sizeof(float)); rewind(fp); if (ispipe == -1) { /* training data is from standard input via pipe */ fprintf(stderr, "\n %s (Error) -t option must be specified for the standard input via pipe.\n", cmnd); usage(1); } } /* Memory allocation */ /* Training data */ dat = dgetmem(T * L); /* for VQ */ N = 1; while (N < M) N *= 2; cb = dgetmem(N * L); icb = dgetmem(L); tindex = (int *) getmem(T, sizeof(int)); cntcb = (int *) getmem(M, sizeof(int)); /* GMM */ gmm.weight = dgetmem(M); gmm.gauss = (Gauss *) getmem(M, sizeof(Gauss)); for (m = 0; m < M; m++) { gmm.gauss[m].mean = dgetmem(L); gmm.gauss[m].var = dgetmem(L); if (full == 1) { gmm.gauss[m].cov = (double **) malloc(sizeof(double *) * L); gmm.gauss[m].inv = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) { gmm.gauss[m].cov[l] = dgetmem(L); gmm.gauss[m].inv[l] = dgetmem(L); } } } if (full == 1) { floor.gauss = (Gauss *) getmem(1, sizeof(Gauss)); floor.gauss[0].cov = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) floor.gauss[0].cov[l] = dgetmem(L); sum_m = dgetmem(L); sum_v = (double **) malloc(sizeof(double *) * L); } /* temporary */ tgmm.weight = dgetmem(M); tgmm.gauss = (Gauss *) getmem(M, sizeof(Gauss)); for (m = 0; m < M; m++) { tgmm.gauss[m].mean = dgetmem(L); tgmm.gauss[m].var = dgetmem(L); if (full == 1) { tgmm.gauss[m].cov = (double **) malloc(sizeof(double *) * L); tgmm.gauss[m].inv = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) { tgmm.gauss[m].cov[l] = dgetmem(L); tgmm.gauss[m].inv[l] = dgetmem(L); } } } logwgd = dgetmem(M); sum = dgetmem(M); /* Read training data */ freadf(dat, sizeof(*dat), T * L, fp); /* Initialization of GMM parameters */ /* LBG */ vaverage(dat, L, T, icb); lbg(dat, L, T, icb, 1, cb, N, ITER, MINTRAIN, S, CENTUP, DELTA, END); for (t = 0, pd = dat; t < T; t++, pd += L) { tindex[t] = vq(pd, cb, L, M); cntcb[tindex[t]]++; } for (m = 0; m < M; m++) if (cntcb[m] == 0) { fprintf(stderr, "Error: No data for mixture No.%d\n", m); usage(1); } fprintf(stderr, "T = %d L = %d M = %d\n", T, L, M); /* flooring value for weights */ W = 1.0 / (double) M *(double) W; /* weights */ for (m = 0, sum_w = 0.0; m < M; m++) { gmm.weight[m] = (double) cntcb[m] / (double) T; if (gmm.weight[m] < W) gmm.weight[m] = W; sum_w += gmm.weight[m]; } if (sum_w != 1.0) for (m = 0; m < M; m++) gmm.weight[m] /= sum_w; /* mean */ for (m = 0, pd = cb; m < M; m++, pd += L) movem(pd, gmm.gauss[m].mean, sizeof(double), L); /* variance */ if (full != 1) { for (t = 0, pd = dat; t < T; t++, pd += L) for (l = 0; l < L; l++) { diff = gmm.gauss[tindex[t]].mean[l] - pd[l]; gmm.gauss[tindex[t]].var[l] += sq(diff); } for (m = 0; m < M; m++) for (l = 0; l < L; l++) { gmm.gauss[m].var[l] /= (double) cntcb[m]; if (gmm.gauss[m].var[l] < V) gmm.gauss[m].var[l] = V; } for (m = 0; m < M; m++) gmm.gauss[m].gconst = cal_gconst(gmm.gauss[m].var, L); } /* full covariance */ else { for (t = 0, pd = dat; t < T; t++, pd += L) { for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { if (l == i) { diff = (gmm.gauss[tindex[t]].mean[l] - pd[l]) * (gmm.gauss[tindex[t]].mean[i] - pd[i]); floor.gauss[0].cov[l][i] += diff; } } } } for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { if (l == i) { floor.gauss[0].cov[l][i] /= T; floor.gauss[0].cov[l][i] *= V; } } } for (t = 0, pd = dat; t < T; t++, pd += L) { for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { diff = (gmm.gauss[tindex[t]].mean[l] - pd[l]) * (gmm.gauss[tindex[t]].mean[i] - pd[i]); gmm.gauss[tindex[t]].cov[l][i] += diff; } } } for (m = 0; m < M; m++) for (l = 0; l < L; l++) for (i = 0; i <= l; i++) { gmm.gauss[m].cov[l][i] /= (double) cntcb[m]; } } /* EM training of GMM parameters */ for (i = 0; (i <= Imax) && ((i <= Imin) || (fabs(change) > E)); i++) { if (full != 1) fillz_gmm(&tgmm, M, L); else fillz_gmmf(&tgmm, M, L); fillz(sum, sizeof(double), M); if (full != 1) { for (m = 0; m < M; m++) gmm.gauss[m].gconst = cal_gconst(gmm.gauss[m].var, L); } else { for (m = 0, n1 = 0; m < M; m++) { gmm.gauss[m].gconst = cal_gconstf(gmm.gauss[m].cov, L); if (gmm.gauss[m].gconst == 0) { n1++; for (l = 0; l < L; l++) gmm.gauss[m].cov[l][l] += floor.gauss[0].cov[l][l]; gmm.gauss[m].gconst = cal_gconstf(gmm.gauss[m].cov, L); } if (gmm.gauss[m].gconst == 0) { fprintf(stderr, "ERROR : Can't caluculate covdet"); exit(EXIT_FAILURE); } /* calculate inv */ cal_inv(gmm.gauss[m].cov, gmm.gauss[m].inv, L); } } if (full == 1) fprintf(stderr, "%d cov can't caluculate covdet\n", n1); for (t = 0, ave_logp1 = 0.0, pd = dat; t < T; t++, pd += L) { for (m = 0, logb = LZERO; m < M; m++) { if (full != 1) { logwgd[m] = log_wgd(&gmm, m, pd, L); logb = log_add(logb, logwgd[m]); } /* full */ else { logwgd[m] = log_wgdf(&gmm, m, pd, L); logb = log_add(logb, logwgd[m]); } } ave_logp1 += logb; for (m = 0; m < M; m++) { tmp1 = exp(logwgd[m] - logb); sum[m] += tmp1; for (l = 0; l < L; l++) { tmp2 = tmp1 * pd[l]; tgmm.gauss[m].mean[l] += tmp2; if (full != 1) tgmm.gauss[m].var[l] += tmp2 * pd[l]; else { for (j = 0; j <= l; j++) { tgmm.gauss[m].cov[l][j] += tmp1 * (pd[l] - gmm.gauss[m].mean[l]) * (pd[j] - gmm. gauss[m].mean [j]); } } } } } /* Output average log likelihood at each iteration */ ave_logp1 /= (double) T; if (i == 1 && m == 1) ave_logp0 = ave_logp1; fprintf(stderr, "iter %3d : ", i); fprintf(stderr, "ave_logprob = %g", ave_logp1); if (i) { change = ave_logp1 - ave_logp0; fprintf(stderr, " change = %g", change); } fprintf(stderr, "\n"); ave_logp0 = ave_logp1; /* Update perameters */ /* weights */ for (m = 0; m < M; m++) gmm.weight[m] = sum[m] / (double) T; /* mean, variance */ for (m = 0; m < M; m++) { for (l = 0; l < L; l++) gmm.gauss[m].mean[l] = tgmm.gauss[m].mean[l] / sum[m]; if (full != 1) { for (l = 0; l < L; l++) { gmm.gauss[m].var[l] = tgmm.gauss[m].var[l] / sum[m] - sq(gmm.gauss[m].mean[l]); if (gmm.gauss[m].var[l] < V) gmm.gauss[m].var[l] = V; } } /* full */ else { for (l = 0; l < L; l++) { for (j = 0; j <= l; j++) { gmm.gauss[m].cov[l][j] = tgmm.gauss[m].cov[l][j] / sum[m]; } } } } } /* Output GMM parameters */ fwritef(gmm.weight, sizeof(double), M, stdout); if (full != 1) { for (m = 0; m < M; m++) { fwritef(gmm.gauss[m].mean, sizeof(double), L, stdout); fwritef(gmm.gauss[m].var, sizeof(double), L, stdout); } } else { for (m = 0; m < M; m++) { fwritef(gmm.gauss[m].mean, sizeof(double), L, stdout); for (i = 0; i < L; i++) for (j = 0; j < i; j++) gmm.gauss[m].cov[j][i] = gmm.gauss[m].cov[i][j]; for (l = 0; l < L; l++) fwritef(gmm.gauss[m].cov[l], sizeof(double), L, stdout); } } return (0); }
size_t hsc_argv_asize; int i; struct array_object **body; int len; size_t strobj_size; size_t strobj_asize; void scan1(void){ argv = move(argv); return; } gc_init(0, 0, 0, 0); hsc_argv_size = sizeof(struct array_object) + argc * sizeof(struct array_object *); hsc_argv_asize = (hsc_argv_size + sizeof(align_t) + -1) / sizeof(align_t) * sizeof(align_t); hsc_argv = (struct array_object *)getmem(scan1, hsc_argv_asize); hsc_argv->d = (desc_t) 1; hsc_argv->length = argc; hsc_argv->el_size = sizeof(struct array_object *); hsc_argv->size = hsc_argv_size; hsc_argv->asize = hsc_argv_asize; body = (struct array_object **)hsc_argv->body; for (i = 0; i < argc; ++i) { len = 1 + strlen(argv[i]); strobj_size = sizeof(struct array_object) + len * sizeof(char); strobj_asize = (strobj_size + sizeof(align_t) + -1) / sizeof(align_t) * sizeof(align_t); body[i] = (struct array_object *)getmem(scan1, strobj_asize); (body[i])->d = (desc_t) 3; (body[i])->length = len; (body[i])->el_size = sizeof(char); (body[i])->size = strobj_size;
/*------------------------------------------------------------------------ * snleaf - perform the requested operation on the leaf SNMP variable *------------------------------------------------------------------------ */ int snleaf(struct snbentry *bindl, struct mib_info *mip, int op) { int len; char *strp; struct oid *oip; if (op == SOP_GETN) { if (mip->mi_next) return((*mip->mi_next->mi_func) (bindl, mip->mi_next, SOP_GETF)); return SERR_NO_SUCH; } if (op == SOP_SET) { if (! mip->mi_writable) return SERR_NO_SUCH; switch(mip->mi_vartype) { case ASN1_INT: if (SVTYPE(bindl) != ASN1_INT) return SERR_BAD_VALUE; if (mip->mi_param == 0) return SERR_NO_SUCH; *((int *) mip->mi_param) = SVINT(bindl); break; case ASN1_OCTSTR: if (SVTYPE(bindl) != ASN1_OCTSTR) return SERR_BAD_VALUE; strp = (char *) mip->mi_param; memcpy(strp, SVSTR(bindl), SVSTRLEN(bindl)); *(strp + SVSTRLEN(bindl)) = '\0'; break; case ASN1_OBJID: if (SVTYPE(bindl) != ASN1_OBJID) return SERR_BAD_VALUE; oip = (struct oid *) mip->mi_param; oip->len = SVSTRLEN(bindl); memcpy(oip->id, SVSTR(bindl), oip->len * 2); break; } return SNMP_OK; } if (op == SOP_GETF) { /* put the correct objid into the binding list. */ bindl->sb_oid.len = mip->mi_objid.len; memcpy(bindl->sb_oid.id, mip->mi_objid.id, mip->mi_objid.len * 2); } SVTYPE(bindl) = mip->mi_vartype; switch(mip->mi_vartype) { case ASN1_INT: case ASN1_TIMETICKS: case ASN1_GAUGE: case ASN1_COUNTER: /* kprintf("snleaf: grabbed %d\n", *((int *) mip->mi_param)); */ SVINT(bindl) = *((int *) mip->mi_param); break; case ASN1_OCTSTR: strp = mip->mi_param; if (strp == NULL) { SVSTRLEN(bindl) = 0; SVSTR(bindl) = NULL; break; } len = SVSTRLEN(bindl) = strlen(strp); SVSTR(bindl) = (char *) getmem(len); memcpy(SVSTR(bindl), strp, len); break; case ASN1_OBJID: oip = (struct oid *) mip->mi_param; SVOIDLEN(bindl) = oip->len; memcpy(SVOID(bindl), oip->id, oip->len * 2); break; } return SNMP_OK; }
void printmem(const char *str, u2_t addr) { printf("%s %04x: %04x\n", str, addr, (int) getmem(addr)); }
static struct rr* dnskey_cdnskey_parse(char *name, long ttl, int type, char *s) { struct rr_dnskey *rr = getmem(sizeof(*rr)); struct binary_data key; int flags, proto, algorithm; unsigned int ac; int i; static struct rr *result; flags = extract_integer(&s, "flags", NULL); if (flags < 0) return NULL; if (flags & 0xfe7e) return bitch("reserved flags bits are set"); if (flags & 0x0001 && !(flags & 0x0100)) return bitch("SEP bit is set but Zone Key bit is unset"); rr->flags = flags; /* TODO validate that `name` is the name of the zone if flags have Zone Key bit set */ proto = extract_integer(&s, "protocol", NULL); if (proto < 0) return NULL; if (proto != 3) return bitch("bad protocol value"); rr->protocol = proto; algorithm = extract_algorithm(&s, "algorithm"); if (algorithm == ALG_UNSUPPORTED) return NULL; if (algorithm == ALG_PRIVATEDNS || algorithm == ALG_PRIVATEOID) { return bitch("private algorithms are not supported in %s", type == T_CDNSKEY ? "CDNSKEY" : "DNSKEY"); } rr->algorithm = algorithm; key = extract_base64_binary_data(&s, "public key"); if (key.length < 0) return NULL; /* TODO validate key length based on algorithm */ rr->pubkey = key; ac = 0; ac += rr->flags; ac += rr->protocol << 8; ac += rr->algorithm; for (i = 0; i < rr->pubkey.length; i++) { ac += (i & 1) ? (unsigned char)rr->pubkey.data[i] : ((unsigned char)rr->pubkey.data[i]) << 8; } ac += (ac >> 16) & 0xFFFF; rr->key_tag = ac & 0xFFFF; rr->pkey_built = 0; rr->pkey = NULL; rr->key_type = KEY_TYPE_UNUSED; if (*s) { return bitch("garbage after valid %s data", type == T_CDNSKEY ? "CDNSKEY" : "DNSKEY"); } result = store_record(type, name, ttl, rr); if (result && type == T_DNSKEY) { rr->next_key = all_dns_keys; all_dns_keys = rr; } return result; }
/*------------------------------------------------------------------------ * rdsInit - initialize the remote disk system device *------------------------------------------------------------------------ */ devcall rdsInit ( struct dentry *devptr /* entry in device switch table */ ) { struct rdscblk *rdptr; /* ptr to device contol block */ struct rdbuff *bptr; /* ptr to buffer in memory */ /* used to form linked list */ struct rdbuff *pptr; /* ptr to previous buff on list */ struct rdbuff *buffend; /* last address in buffer memory*/ uint32 size; /* total size of memory needed */ /* buffers */ /* Obtain address of control block */ rdptr = &rdstab[devptr->dvminor]; /* Set control block to unused */ rdptr->rd_state = RD_FREE; rdptr->rd_id[0] = NULLCH; /* Set initial message sequence number */ rdptr->rd_seq = 1; /* Initialize request queue and cache to empty */ rdptr->rd_rhnext = (struct rdbuff *) &rdptr->rd_rtnext; rdptr->rd_rhprev = (struct rdbuff *)NULL; rdptr->rd_rtnext = (struct rdbuff *)NULL; rdptr->rd_rtprev = (struct rdbuff *) &rdptr->rd_rhnext; rdptr->rd_chnext = (struct rdbuff *) &rdptr->rd_ctnext; rdptr->rd_chprev = (struct rdbuff *)NULL; rdptr->rd_ctnext = (struct rdbuff *)NULL; rdptr->rd_ctprev = (struct rdbuff *) &rdptr->rd_chnext; /* Allocate memory for a set of buffers (actually request */ /* blocks and link them to form the initial free list */ size = sizeof(struct rdbuff) * RD_BUFFS; bptr = (struct rdbuff *)getmem(size); rdptr->rd_free = bptr; if ((int32)bptr == SYSERR) { panic("Cannot allocate memory for remote disk buffers"); } pptr = (struct rdbuff *) NULL; /* to avoid a compiler warning */ buffend = (struct rdbuff *) ((char *)bptr + size); while (bptr < buffend) { /* walk through memory */ pptr = bptr; bptr = (struct rdbuff *) (sizeof(struct rdbuff)+ (char *)bptr); pptr->rd_status = RD_INVALID; /* buffer is empty */ pptr->rd_next = bptr; /* point to next buffer */ } pptr->rd_next = (struct rdbuff *) NULL; /* last buffer on list */ /* Create the request list and available buffer semaphores */ rdptr->rd_availsem = semcreate(RD_BUFFS); rdptr->rd_reqsem = semcreate(0); /* Set the server IP address, server port, and local port */ if ( dot2ip(RD_SERVER_IP, &rdptr->rd_ser_ip) == SYSERR ) { panic("invalid IP address for remote disk server"); } /* Set the port numbers */ rdptr->rd_ser_port = RD_SERVER_PORT; rdptr->rd_loc_port = RD_LOC_PORT + devptr->dvminor; /* Specify that the server port is not yet registered */ rdptr->rd_registered = FALSE; /* Create a communication process, but leave it suspended */ rdptr->rd_comproc = create(rdsprocess, RD_STACK, RD_PRIO, "rdsproc", 1, rdptr); if (rdptr->rd_comproc == SYSERR) { panic("Cannot create remote disk process"); } rdptr->rd_comruns = FALSE; return OK; }
int main(int argc, char **argv) { size_t i, source_vlen = DEF_L, target_vlen = 0, len_total = 0, num_mix = DEF_M, total_frame = 0; char *coef = NULL, **dw_fn = (char **) getmem(argc, sizeof(*(dw_fn))); int j, k, dw_num = 1, dw_calccoef = -1, dw_coeflen = 1, win_max_width = 0; double floor = FLOOR; double *source = NULL, *target = NULL, *gv_mean = NULL, *gv_vari = NULL; FILE *fp = stdin, *fgmm = NULL, *fgv = NULL; Boolean full = TR; GMM gmm; DELTAWINDOW window; memset(dw_fn, 0, argc * sizeof(*dw_fn)); if ((cmnd = strrchr(argv[0], '/')) == NULL) { cmnd = argv[0]; } else { cmnd++; } while (--argc) { if (**++argv == '-') { switch (*(*argv + 1)) { case 'l': source_vlen = atoi(*++argv); --argc; break; case 'n': source_vlen = atoi(*++argv) + 1; --argc; break; case 'L': target_vlen = atoi(*++argv); --argc; break; case 'N': target_vlen = atoi(*++argv) + 1; --argc; break; case 'm': num_mix = atoi(*++argv); --argc; break; case 'd': if (dw_calccoef == 1 || dw_calccoef == 2) { fprintf(stderr, "%s : Options '-r' and '-d' should not be defined simultaneously!\n", cmnd); usage(EXIT_FAILURE); } dw_calccoef = 0; if (isfloat(*++argv)) { dw_coeflen = 0; for (k = 0; (k < argc - 1) && isfloat(argv[k]); k++) { dw_coeflen += strlen(argv[k]) + 1; } dw_coeflen += 1; coef = dw_fn[dw_num] = getmem(dw_coeflen, sizeof(*coef)); for (j = 0; j < k; j++) { sprintf(coef, " %s", *argv); coef += strlen(*argv) + 1; if (j < k - 1) { argv++; argc--; } } } else { dw_fn[dw_num] = getmem(strlen(*argv) + 1, sizeof(**dw_fn)); strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1); } dw_num++; --argc; break; case 'r': if (dw_calccoef == 0 || dw_calccoef == 2) { fprintf(stderr, "%s : Options '-r' and '-d' should not be defined simultaneously!\n", cmnd); usage(EXIT_FAILURE); } dw_calccoef = 1; dw_coeflen = atoi(*++argv); --argc; if ((dw_coeflen != 1) && (dw_coeflen != 2)) { fprintf(stderr, "%s : Number of delta parameter should be 1 or 2!\n", cmnd); usage(EXIT_FAILURE); } if (argc <= 1) { fprintf(stderr, "%s : Window size for delta parameter required!\n", cmnd); usage(EXIT_FAILURE); } dw_fn[dw_num] = getmem(strlen(*++argv) + 1, sizeof(**dw_fn)); strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1); dw_num++; --argc; if (dw_coeflen == 2) { if (argc <= 1) { fprintf(stderr, "%s : Window size for delta-delta parameter required!\n", cmnd); usage(EXIT_FAILURE); } dw_fn[dw_num] = getmem(strlen(*++argv) + 1, sizeof(**dw_fn)); strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1); dw_num++; --argc; } break; case 'g': fgv = getfp(*++argv, "rb"); --argc; break; case 'e': floor = atof(*++argv); if (floor < 0.0 || isdigit(**argv) == 0) { fprintf(stderr, "%s : '-e' option must be specified with positive value.\n", cmnd); usage(1); } --argc; break; case 'h': usage(EXIT_SUCCESS); default: fprintf(stderr, "%s: Illegal option %s.\n", cmnd, *argv); usage(EXIT_FAILURE); } } else if (fgmm == NULL) { fgmm = getfp(*argv, "rb"); } else { fp = getfp(*argv, "rb"); } } if (fgmm == NULL) { fprintf(stderr, "%s: GMM file must be specified!\n", cmnd); usage(EXIT_FAILURE); } /* set dimensionarity of joint vector */ if (target_vlen == 0) { target_vlen = source_vlen; } len_total = (source_vlen + target_vlen) * dw_num; /* read sequence of source feature vectors */ source = read_input(fp, source_vlen, &total_frame); fclose(fp); target = dgetmem(target_vlen * total_frame); /* load GMM parameters */ alloc_GMM(&gmm, num_mix, len_total, full); load_GMM(&gmm, fgmm); prepareCovInv_GMM(&gmm); prepareGconst_GMM(&gmm); fclose(fgmm); /* flooring for diagonal component of covariance */ if (floor != 0.0) { for (i = 0; i < num_mix; i++) { for (j = 0; j < (int) len_total; j++) { gmm.gauss[i].cov[j][j] += floor; } } } /* load GV parameters */ if (fgv != NULL) { gv_mean = dgetmem(target_vlen); gv_vari = dgetmem(target_vlen); freadf(gv_mean, sizeof(*gv_mean), target_vlen, fgv); freadf(gv_vari, sizeof(*gv_vari), target_vlen, fgv); fclose(fgv); } /* set window parameters */ window.win_size = dw_num; window.win_l_width = (int *) getmem(window.win_size, sizeof(*(window.win_l_width))); window.win_r_width = (int *) getmem(window.win_size, sizeof(*(window.win_r_width))); window.win_coefficient = (double **) getmem(window.win_size, sizeof(*(window.win_coefficient))); window.win_l_width[0] = 0; window.win_r_width[0] = 0; window.win_coefficient[0] = dgetmem(1); window.win_coefficient[0][0] = 1.0; if (dw_calccoef == 0) { int fsize, dw_leng; FILE *fpc = NULL; for (i = 1; i < window.win_size; i++) { if (dw_fn[i][0] == ' ') { fsize = str2darray(dw_fn[i], &(window.win_coefficient[i])); } else { /* read from file */ fpc = getfp(dw_fn[i], "rb"); /* check the number of coefficients */ fseek(fpc, 0L, SEEK_END); fsize = ftell(fpc) / sizeof(float); fseek(fpc, 0L, SEEK_SET); if (fsize % 2 == 0) { fprintf(stderr, "%s : number of delta coefficients must be odd!\n", cmnd); usage(EXIT_FAILURE); } /* read coefficients */ window.win_coefficient[i] = dgetmem(fsize); freadf(window.win_coefficient[i], sizeof(*(window.win_coefficient[i])), fsize, fpc); } /* set pointer */ dw_leng = fsize / 2; window.win_coefficient[i] += dw_leng; window.win_l_width[i] = -dw_leng; window.win_r_width[i] = dw_leng; } fclose(fpc); } else if (dw_calccoef == 1) { int a0, a1, a2, dw_leng; for (i = 1; i < window.win_size; i++) { dw_leng = atoi(dw_fn[i]); if (dw_leng < 1) { fprintf(stderr, "%s : Width for regression coefficient shuould be more than 1!\n", cmnd); usage(EXIT_FAILURE); } window.win_l_width[i] = -dw_leng; window.win_r_width[i] = dw_leng; window.win_coefficient[i] = dgetmem(dw_leng * 2 + 1); window.win_coefficient[i] += dw_leng; } dw_leng = atoi(dw_fn[1]); for (a1 = 0, j = -dw_leng; j <= dw_leng; a1 += j * j, j++); for (j = -dw_leng; j <= dw_leng; j++) { window.win_coefficient[1][j] = (double) j / (double) a1; } if (window.win_size > 2) { dw_leng = atoi(dw_fn[2]); for (a0 = a1 = a2 = 0, j = -dw_leng; j <= dw_leng; a0++, a1 += j * j, a2 += j * j * j * j, j++); for (j = -dw_leng; j <= dw_leng; j++) { window.win_coefficient[2][j] = 2 * ((double) (a0 * j * j - a1)) / ((double) (a2 * a0 - a1 * a1)); } } } win_max_width = window.win_r_width[0]; /* width of static window is 0 */ for (i = 1; i < window.win_size; i++) { if (win_max_width < window.win_r_width[i]) { win_max_width = window.win_r_width[i]; } if (win_max_width < -window.win_l_width[i]) { win_max_width = -window.win_l_width[i]; } } window.win_max_width = win_max_width; /* perform conversion */ vc(&gmm, &window, total_frame, source_vlen, target_vlen, gv_mean, gv_vari, source, target); /* output sequence of converted target static feature vectors */ fwritef(target, sizeof(*target), target_vlen * total_frame, stdout); /* release memory */ free(source); free(target); free(gv_mean); free(gv_vari); free_GMM(&gmm); for (i = 0; i < window.win_size; i++) { if (dw_fn[i]) { free(dw_fn[i]); } free(window.win_coefficient[i] + window.win_l_width[i]); } free(dw_fn); free(window.win_l_width); free(window.win_r_width); free(window.win_coefficient); return (0); }
short *sgetmem(const int leng) { return ((short *) getmem((size_t) leng, sizeof(short))); }
Zufallsgenerator createZufallsgenerator() { Zufallsgenerator obj = ((Zufallsgenerator)getmem(sizeof(struct SZufallsgenerator))); return obj; }
long *lgetmem(const int leng) { return ((long *) getmem((size_t) leng, sizeof(long))); }
/*------------------------------------------------------------------------ * gname - use the DNS to look up the name *------------------------------------------------------------------------ */ static IPaddr gname(char *nam) { IPaddr ip; char tmpstr[64]; /* temporary string buffer */ char *buf; /* buffer to hold domain query */ int dg, i; register char *p, *p2, *p3; register struct dn_mesg *dnptr; dnptr = (struct dn_mesg *) (buf = (char *) getmem(DN_MLEN)); dnptr->dn_id = 0; dnptr->dn_opparm = hs2net(DN_RD); dnptr->dn_qcount = hs2net(1); dnptr->dn_acount = dnptr->dn_ncount = dnptr->dn_dcount = 0; p = dnptr->dn_qaaa; strcpy(tmpstr, nam); p2 = tmpstr; while (p3=index(p2, '.')) { *p3 = '\0'; dn_cat(p, p2); p2 = p3+1; } dn_cat(p, p2); *p++ = NULLCH; /* terminate name */ /* Add query type and query class fields to name */ ( (struct dn_qsuf *)p )->dn_type = hs2net(DN_QTHA); ( (struct dn_qsuf *)p )->dn_clas = hs2net(DN_QCIN); p += sizeof(struct dn_qsuf); /* send query */ dg = open(UDP, NSERVER, ANYLPORT); control(dg, DG_SETMODE, DG_DMODE | DG_TMODE); write (dg, buf, p - buf); if ( (i = read(dg, buf, DN_MLEN)) == SYSERR || i == TIMEOUT) { close(dg); freemem(buf, DN_MLEN); return (IPaddr)SYSERR; } close(dg); if (net2hs(dnptr->dn_opparm) & DN_RESP || net2hs(dnptr->dn_acount) <= 0) { freemem(buf, DN_MLEN); return (IPaddr)SYSERR; } /* In answer, skip name and remainder of resource record header */ while (*p != NULLCH) if (*p & DN_CMPRS) /* compressed section of name */ *++p = NULLCH; else p += *p + 1; p += DN_RLEN + 1; /* Copy IP to user */ for (i=0; i < IP_ALEN; ++i) ((char *)&ip)[i] = *p++; freemem(buf, DN_MLEN); return ip; }
/** * @ingroup shell * * Shell command fstest. * @param nargs number of arguments in args array * @param args array of arguments * @return OK for success, SYSERR for syntax error */ shellcmd xsh_fstest(int nargs, char *args[]) { int rval; int fd, i, j; char *buf1, *buf2; /* Output help, if '--help' argument was supplied */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tFilesystem Test\n"); printf("Options:\n"); printf("\t--help\tdisplay this help and exit\n"); return OK; } /* Check for correct number of arguments */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs < 1) { fprintf(stderr, "%s: too few arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } #ifdef FS bs_mkdev(0, MDEV_BLOCK_SIZE, MDEV_NUM_BLOCKS); /* device "0" and default blocksize (=0) and count */ fs_mkfs(0,DEFAULT_NUM_INODES); /* bsdev 0*/ testbitmask(); buf1 = getmem(SIZE*sizeof(char)); buf2 = getmem(SIZE*sizeof(char)); // Create test file fd = fs_create("Test_File", O_CREAT); // Fill buffer with random stuff for(i=0; i<SIZE; i++) { j = i%(127-33); j = j+33; buf1[i] = (char) j; } rval = fs_write(fd,buf1,SIZE); if(rval == 0 || rval != SIZE ) { printf("\n\r File write failed"); goto clean_up; } // Now my file offset is pointing at EOF file, i need to bring it back to start of file // Assuming here implementation of fs_seek is like "original_offset = original_offset + input_offset_from_fs_seek" fs_seek(fd,-rval); //read the file rval = fs_read(fd, buf2, rval); buf2[rval] = '\0'; if(rval == 0) { printf("\n\r File read failed"); goto clean_up; } printf("\n\rContent of file %s",buf2); rval = fs_close(fd); if(rval != OK) { printf("\n\rReturn val for fclose : %d",rval); } clean_up: freemem(buf1,SIZE); freemem(buf2,SIZE); #else printf("No filesystem support\n"); #endif return OK; }
int main(int argc, char *argv[]) { char * bytes_count_str = NULL; char * devname; struct stat statbuf; int c; int dev; struct reiserfs_super_block *sb, *sb_old; while ((c = getopt(argc, argv, "fvcqs:")) != EOF) { switch (c) { case 's' : if (!optarg) die("%s: Missing argument to -s option", argv[0]); bytes_count_str = optarg; break; case 'f': opt_force = 1; break; case 'v': opt_verbose++; break; case 'n': /* no nowrite option at this moment */ /* opt_nowrite = 1; */ break; case 'c': opt_safe = 1; break; case 'q': opt_verbose = 0; break; default: print_usage_and_exit (); } } if (optind == argc || (!bytes_count_str)) print_usage_and_exit(); devname = argv[optind]; /* open_device will die if it could not open device */ dev = open (devname, O_RDWR); if (dev == -1) die ("%s: can not open '%s': %s", argv[0], devname, strerror (errno)); if (fstat (dev, &statbuf) < 0) die ("%s: unable to stat %s", argv[0], devname); if (!S_ISBLK (statbuf.st_mode) && opt_force ) die ("%s: '%s (%o)' is not a block device", argv[0], devname, statbuf.st_mode); read_superblock(dev); sb = (struct reiserfs_super_block *) g_sb_bh->b_data; g_block_count_new = calc_new_fs_size(sb->s_block_count, sb->s_blocksize, bytes_count_str); if (is_mounted (devname)) { close(dev); if (!opt_force) die ("%s: '%s' contains a mounted file system,\n" "\tspecify -f option to resize the fs online\n", argv[0], devname); resize_fs_online(devname, g_block_count_new); return 0; } if (sb->s_state != REISERFS_VALID_FS) die ("%s: the file system isn't in valid state\n", argv[0]); if(!valid_offset(dev, (loff_t) g_block_count_new * sb->s_blocksize - 1)) die ("%s: %s too small", argv[0], devname); sb_old = 0; /* Needed to keep idiot compiler from issuing false warning */ /* save SB for reporting */ if(opt_verbose) { sb_old = getmem(sizeof(struct reiserfs_super_block)); memcpy(sb_old, sb, sizeof(struct reiserfs_super_block)); } if (g_block_count_new == sb->s_block_count) die ("%s: Calculated fs size is the same as the previous one.", argv[0]); if (g_block_count_new > sb->s_block_count) expand_fs(); else shrink_fs(g_block_count_new); if(opt_verbose) { sb_report(sb, sb_old); freemem(sb_old); } check_and_free_mem (); if (opt_verbose) { printf("\nSyncing.."); fflush(stdout); } fsync (dev); if (opt_verbose) printf("done\n"); close(dev); return 0; }
object * vis_loadobject(char *fname) { int error; int a,b,c; object *o; long l; char *d,*d0; d=readfile(fname); o=getmem(sizeof(object)); o->flags=F_DEFAULT; o->r=getmem(sizeof(rmatrix)); o->r0=getmem(sizeof(rmatrix)); memset(o->r,0,sizeof(rmatrix)); memset(o->r0,0,sizeof(rmatrix)); o->vnum=0; o->nnum=0; o->v0=NULL; o->n0=NULL; o->v=NULL; o->n=NULL; o->pv=NULL; o->plnum=1; for(;;) { error=0; d0=d; d+=8; if(!memcmp(d0,"END ",4)) break; else if(!memcmp(d0,"VERS",4)) { a=GINT; if(a!=0x100) { printf("Version not 1.00\n"); } } else if(!memcmp(d0,"NAME",4)) { o->name=(char *)d; } else if(!memcmp(d0,"VERT",4)) { o->vnum=GINT; GINT; o->v0=(vlist *)d; o->v=getmem(sizeof(vlist)*o->vnum); o->pv=getmem(sizeof(pvlist)*o->vnum); } else if(!memcmp(d0,"NORM",4)) { o->nnum=GINT; o->nnum1=GINT; o->n0=(nlist *)d; o->n=getmem(sizeof(vlist)*o->nnum); } else if(!memcmp(d0,"POLY",4)) { o->pd=(polydata *)d; } else if(!memcmp(d0,"ORD",3)) { a=d0[3]; if(a=='0') b=0; else if(a=='E') b=o->plnum++; else error=1; if(!error) { o->pl[b]=(polylist *)d; } } else error=1; if(error) { printf("Unknown block: %c%c%c%c\n",d0[0],d0[1],d0[2],d0[3]); } l=*(long *)(d0+4); d=d0+l+8; } #if 0 printf("Sortlists: "); for(a=0;a<9;a++) printf("%i ",o->plv[a]); printf("Vertices: %i (%Fp=>%Fp=>%Fp)\n",o->vnum,o->v0,o->v,o->pv); printf("Normals: %i (%Fp=>%Fp)\n",o->nnum,o->n0,o->n); getch(); #endif return(o); }
int main(int argc, char **argv) { FILE *pdffp = stdin, *parfp = stdout; int nframe, delay; char *coef; int coeflen; PStream pst; int i, j; void InitPStream(PStream *); double *mlpg(PStream *); pst.order = ORDER; pst.range = RANGE; pst.iType = ITYPE; pst.dw.fn = (char **) calloc(sizeof(char *), argc); pst.dw.num = 1; pst.dw.calccoef = -1; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (**++argv == '-') { switch (*(*argv + 1)) { case 'd': if (pst.dw.calccoef == 1) { fprintf(stderr, "%s : Options '-r' and '-d' should not be defined simultaneously!\n", cmnd); return (1); } pst.dw.calccoef = 0; if (isfloat(*++argv)) { coeflen = 0; for (i = 0; (i < argc - 1) && isfloat(argv[i]); i++) { coeflen += strlen(argv[i]) + 1; } coeflen += 1; coef = pst.dw.fn[pst.dw.num] = getmem(coeflen, sizeof(char)); for (j = 0; j < i; j++) { sprintf(coef, " %s", *argv); coef += strlen(*argv) + 1; if (j < i - 1) { argv++; argc--; } } } else { pst.dw.fn[pst.dw.num] = *argv; } pst.dw.num++; --argc; break; case 'r': if (pst.dw.calccoef == 0) { fprintf(stderr, "%s : Options '-r' and '-d' should not be defined simultaneously!\n", cmnd); return (1); } pst.dw.calccoef = 1; coeflen = atoi(*++argv); --argc; if ((coeflen != 1) && (coeflen != 2)) { fprintf(stderr, "%s : Number of delta parameter should be 1 or 2!\n", cmnd); return (1); } if (argc <= 1) { fprintf(stderr, "%s : Window size for delta parameter required!\n", cmnd); return (1); } pst.dw.fn[pst.dw.num] = *++argv; pst.dw.num++; --argc; if (coeflen == 2) { if (argc <= 1) { fprintf(stderr, "%s : Window size for delta-delta parameter required!\n", cmnd); return (1); } pst.dw.fn[pst.dw.num] = *++argv; pst.dw.num++; --argc; } break; case 'm': pst.order = atoi(*++argv); --argc; break; case 'l': pst.order = atoi(*++argv) - 1; --argc; break; case 'i': pst.iType = atoi(*++argv); --argc; break; case 's': pst.range = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else pdffp = getfp(*argv, "rb"); } InitPStream(&pst); delay = pst.range + pst.dw.maxw[WRIGHT]; nframe = 0; while (freadf(pst.mean, sizeof(*(pst.mean)), pst.vSize * 2, pdffp) == pst.vSize * 2) { if (pst.dw.num == 1) fwritef(pst.mean, sizeof(*pst.mean), pst.order + 1, parfp); else { if (pst.iType == 0) for (i = 0; i < pst.vSize; i++) pst.ivar[i] = finv(pst.ivar[i]); mlpg(&pst); if (nframe >= delay) fwritef(pst.par, sizeof(*(pst.par)), pst.order + 1, parfp); } nframe++; } if (pst.dw.num > 1) { for (i = 0; i < pst.vSize; i++) { pst.mean[i] = 0.0; pst.ivar[i] = 0.0; } for (i = 0; i < min(nframe, delay); i++) { mlpg(&pst); fwritef(pst.par, sizeof(*(pst.par)), pst.order + 1, parfp); } } return (0); }
MonomialOrder *monomialOrderMake(const MonomialOrdering *mo) { MonomialOrder *result; int i, j, nv, this_block; deg_t *wts = NULL; /* Determine the number of variables, the number of blocks, and the location of the component */ int nblocks = 0; int nvars = 0; int hascomponent = 0; for (i = 0; i < mo->len; i++) { struct mon_part_rec_ *t = mo->array[i]; nblocks++; if (t->type == MO_POSITION_DOWN || t->type == MO_POSITION_UP) hascomponent++; else if (t->type == MO_NC_LEX) { // Currently, do nothing. } if (t->type != MO_WEIGHTS) nvars += t->nvars; } nblocks -= hascomponent; /* Now create the blocks, and fill them in. Also fill in the deg vector */ result = getmemstructtype(MonomialOrder *); result->nvars = nvars; result->nslots = 0; result->nblocks = nblocks; result->blocks = (struct mo_block *)getmem(nblocks * sizeof(result->blocks[0])); result->degs = (deg_t *)getmem_atomic(nvars * sizeof(result->degs[0])); if (hascomponent == 0) result->nblocks_before_component = nblocks; this_block = 0; nvars = 0; for (i = 0; i < mo->len; i++) { struct mon_part_rec_ *t = mo->array[i]; if (t->type != MO_WEIGHTS) { if (t->wts == 0) for (j = 0; j < t->nvars; j++) result->degs[nvars++] = 1; else for (j = 0; j < t->nvars; j++) result->degs[nvars++] = t->wts[j]; } else { wts = (deg_t *)getmem_atomic(t->nvars * sizeof(wts[0])); for (j = 0; j < t->nvars; j++) wts[j] = t->wts[j]; } switch (t->type) { case MO_REVLEX: mo_block_revlex(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX: mo_block_grevlex(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX2: mo_block_grevlex2(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX4: mo_block_grevlex4(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX_WTS: mo_block_grevlex_wts(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX2_WTS: mo_block_grevlex2_wts(result->blocks + this_block++, t->nvars); break; case MO_GREVLEX4_WTS: mo_block_grevlex4_wts(result->blocks + this_block++, t->nvars); break; case MO_LEX: mo_block_lex(result->blocks + this_block++, t->nvars); break; case MO_LEX2: mo_block_lex2(result->blocks + this_block++, t->nvars); break; case MO_LEX4: mo_block_lex4(result->blocks + this_block++, t->nvars); break; case MO_WEIGHTS: // if extra weight values are given (more than "nvars", ignore the // rest. mo_block_wt_function( result->blocks + this_block++, (t->nvars <= result->nvars ? t->nvars : result->nvars), wts); break; case MO_LAURENT: mo_block_group_lex(result->blocks + this_block++, t->nvars); break; case MO_LAURENT_REVLEX: mo_block_group_revlex(result->blocks + this_block++, t->nvars); break; case MO_NC_LEX: /* MES */ break; case MO_POSITION_UP: if (--hascomponent == 0) { // Set the information about the component result->component_up = 1; result->nblocks_before_component = this_block; } // mo_block_position_up(result->blocks + this_block); break; case MO_POSITION_DOWN: if (--hascomponent == 0) { // Set the information about the component result->component_up = 0; result->nblocks_before_component = this_block; } // mo_block_position_down(result->blocks + this_block); break; } } /* Go back and fill in the 'slots' information */ /* Now fix the first_exp, first_slot values, and also result->{nslots,nvars}; */ nv = 0; result->nslots = 0; result->nslots_before_component = 0; for (i = 0; i < nblocks; i++) { enum MonomialOrdering_type typ = result->blocks[i].typ; result->blocks[i].first_exp = nv; result->blocks[i].first_slot = result->nslots; nv += result->blocks[i].nvars; result->nslots += result->blocks[i].nslots; if (typ == MO_WEIGHTS) { result->blocks[i].first_exp = 0; /* divide the wt vector by the degree vector */ for (j = 0; j < result->blocks[i].nvars; j++) safe::div_by(result->blocks[i].weights[j], result->degs[j]); ; } else if (typ == MO_GREVLEX_WTS || typ == MO_GREVLEX2_WTS || typ == MO_GREVLEX4_WTS) { result->blocks[i].weights = result->degs + result->blocks[i].first_exp; } if (i == result->nblocks_before_component - 1) { result->nslots_before_component = result->nslots; } } /* Set is_laurent */ result->is_laurent = (int *)getmem_atomic(result->nvars * sizeof(int)); for (i = 0; i < result->nvars; i++) result->is_laurent[i] = 0; for (i = 0; i < result->nblocks; i++) if (result->blocks[i].typ == MO_LAURENT || result->blocks[i].typ == MO_LAURENT_REVLEX) { for (j = 0; j < result->blocks[i].nvars; j++) result->is_laurent[result->blocks[i].first_exp + j] = 1; } return result; }
void mexFunction (int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { int rc, enabled; UINT32_T masterid = 0; time_t timallow = 0; UINT64_T memallow = 0; UINT64_T rss, vs; /* this function will be called upon unloading of the mex file */ mexAtExit(exitFun); if (nrhs<3) mexErrMsgTxt ("invalid number of input arguments"); if (mxIsScalar(prhs[0])) masterid = mxGetScalar(prhs[0]); else if (mxIsEmpty(prhs[0])) masterid = 0; else mexErrMsgTxt ("invalid input argument #1"); if (mxIsScalar(prhs[1])) timallow = mxGetScalar(prhs[1]); else if (mxIsEmpty(prhs[1])) timallow = 0; else mexErrMsgTxt ("invalid input argument #2"); if (mxIsScalar(prhs[2])) memallow = mxGetScalar(prhs[2]); else if (mxIsEmpty(prhs[2])) memallow = 0; else mexErrMsgTxt ("invalid input argument #3"); if (masterid!=0 || timallow!=0 || memallow!=0) { enabled = 1; /* in this case the mex file is not allowed to be cleared from memory */ if (!mexIsLocked()) mexLock(); } if (masterid==0 && timallow==0 && memallow==0) { enabled = 0; /* in this case the mex file is allowed to be cleared from memory */ #ifdef SOLUTION_FOR_UNEXPLAINED_CRASH if (mexIsLocked()) mexUnlock(); #endif } if (!peerInitialized) { mexPrintf("watchdog: init\n"); peerinit(NULL); peerInitialized = 1; } /* start the discover thread */ pthread_mutex_lock(&mutexstatus); if (!discoverStatus) { pthread_mutex_unlock(&mutexstatus); mexPrintf("watchdog: spawning discover thread\n"); rc = pthread_create(&discoverThread, NULL, discover, (void *)NULL); if (rc) mexErrMsgTxt("problem with return code from pthread_create()"); else { /* wait until the thread has properly started */ pthread_mutex_lock(&mutexstatus); if (!discoverStatus) pthread_cond_wait(&condstatus, &mutexstatus); pthread_mutex_unlock(&mutexstatus); } } else { pthread_mutex_unlock(&mutexstatus); } /* start the expire thread */ pthread_mutex_lock(&mutexstatus); if (!expireStatus) { pthread_mutex_unlock(&mutexstatus); mexPrintf("watchdog: spawning expire thread\n"); rc = pthread_create(&expireThread, NULL, expire, (void *)NULL); if (rc) mexErrMsgTxt("problem with return code from pthread_create()"); else { /* wait until the thread has properly started */ pthread_mutex_lock(&mutexstatus); if (!expireStatus) pthread_cond_wait(&condstatus, &mutexstatus); pthread_mutex_unlock(&mutexstatus); } } else { pthread_mutex_unlock(&mutexstatus); } if (timallow>0) { /* timallow should be relative to now */ timallow += time(NULL); } if (memallow>0) { /* memallow should be in absolute numbers, add the current memory footprint */ getmem(&rss, &vs); memallow += rss; } /* enable the watchdog: the expire thread will exit if the master is not seen any more */ pthread_mutex_lock(&mutexwatchdog); watchdog.enabled = enabled; watchdog.evidence = 0; watchdog.masterid = masterid; watchdog.memory = memallow; watchdog.time = timallow; pthread_mutex_unlock(&mutexwatchdog); if (enabled) mexPrintf("watchdog: enabled for masterid = %lu, time = %d, memory = %lu\n", masterid, timallow, memallow); else mexPrintf("watchdog: disabled for masterid = %lu, time = %d, memory = %lu\n", masterid, timallow, memallow); return; } /* main */
/*------------------------------------------------------------------------- * pfint - paging fault ISR *------------------------------------------------------------------------- */ SYSCALL pfint() { STATWORD ps; disable(ps); unsigned long int eip = read_cr2(); //kprintf("\n\n#PF in %s, cr2:%x",proctab[currpid].pname, eip); unsigned long int pd_offset = eip >> 22; unsigned long int pt_offset = eip >>12; pt_offset = pt_offset & 1023; unsigned long offset = eip; offset = offset & 4095; //kprintf("\nEIP read is %lu\n",eip); eip = eip >> 12; //vpno //kprintf("\nEIP shifted is %lu\n",eip); int i = 0, flag = 0; int backing_store_page, bs_id; for(i = 0 ; i < NBS; i++) { if(proctab[currpid].map[i].pid == currpid) { //kprintf("\nif\n"); if(proctab[currpid].map[i].next == NULL) { //kprintf("\nif if\n"); if(proctab[currpid].map[i].vpno == eip) //we found the exact page { backing_store_page = proctab[currpid].map[i].base_page; flag = 1; //kprintf("\nif if if\n"); } else if(proctab[currpid].map[i].vpno < eip && (proctab[currpid].map[i].vpno+proctab[currpid].map[i].npages) >= eip) //we found the page in the range of base_page - npages { backing_store_page = proctab[currpid].map[i].base_page + eip - proctab[currpid].map[i].vpno; flag = 1; //kprintf("\nif if else if\n"); } } else { //kprintf("\nelse\n"); bs_map_t *jump = &(proctab[currpid].map[i]); while(jump != NULL) { //kprintf("\nwhile\n"); if(jump->vpno == eip) //we found the exact page { backing_store_page = jump->base_page; flag = 1; //kprintf("\nif else if\n"); break; } else if(jump->vpno < eip && (jump->npages+jump->vpno) >= eip) //we found the page in the range of base_page - npages { backing_store_page = jump->base_page + eip - jump->vpno; flag = 1; //kprintf("\nif else else if\n"); break; } jump = jump->next; } } } if(flag) { bs_id = i; break; } } //kprintf("\nin pfint bs %d, bs_page %d",bs_id,backing_store_page); unsigned long *bs_addr = (unsigned long *)(backing_store_page*NBPG); //populate page table //checking if page dir is empty pd_t *ptr1 = (pd_t *)(proctab[currpid].pdbr); //kprintf("\nbefore %d, %lu\n",ptr1,pd_offset); ptr1 += pd_offset; pt_t *ptr; if(ptr1->pd_pres == 1) //page table exists hence add entry to that { ptr = (pt_t *)((ptr1->pd_base)*NBPG); frm_tab[(ptr1->pd_base)-FRAME0].refcnt++; frm_map[(ptr1->pd_base)-FRAME0].fr_refcnt++; } else //we need to create a page table, add our free_frm entry to it and add the page table entry to the directory { //kprintf("\nin else %d\n",ptr1); int pt_frame = get_frm(); frm_tab[pt_frame-FRAME0].status = FRM_PGT; frm_tab[pt_frame-FRAME0].refcnt++; frm_map[pt_frame-FRAME0].fr_status = FRM_MAPPED; frm_map[pt_frame-FRAME0].fr_pid = currpid; frm_map[pt_frame-FRAME0].fr_refcnt++; frm_map[pt_frame-FRAME0].fr_type = FR_TBL; ptr = (pt_t*)(pt_frame*NBPG); //add the above table to page directory ptr1->pd_pres = 1; ptr1->pd_write = 1; ptr1->pd_user = 0; ptr1->pd_pwt = 0; ptr1->pd_pcd = 0; ptr1->pd_acc = 0; ptr1->pd_mbz = 0; ptr1->pd_fmb = 0; ptr1->pd_global = 0; ptr1->pd_avail = 0; ptr1->pd_base = pt_frame; //kprintf("\nget_frm return frame %d. To be page table for process %s",pt_frame, proctab[currpid].pname); } //add entry to page table ptr += pt_offset; ptr->pt_pres = 1; ptr->pt_write = 1; ptr->pt_user = 0; ptr->pt_pwt = 0; ptr->pt_pcd = 0; ptr->pt_acc = 0; ptr->pt_dirty = 0; ptr->pt_mbz = 0; ptr->pt_global = 0; ptr->pt_avail = 0; //getting a free frame an setting the frame mappings int free_frm; if(ni_page_table[backing_store_page-total_bs_left] != -1) // if backing store is already mapped then we share the frame { free_frm = ni_page_table[backing_store_page-total_bs_left]; //kprintf("\nIN NI frm = %d, ni = %d\n",free_frm, ni_page_table[backing_store_page-total_bs_left]); frm_map[free_frm-FRAME0].fr_refcnt++; frm_tab[free_frm-FRAME0].refcnt++; //adding this vpno and pid to the shared frame map list fr_map_t *map = (fr_map_t *)getmem(sizeof(fr_map_t)); map->fr_pid = currpid; map->fr_vpno = eip; map->shared = NULL; fr_map_t *next = (fr_map_t *)getmem(sizeof(fr_map_t)); next = &(frm_map[free_frm-FRAME0]); while(next->shared != NULL) next = next->shared; next->shared = map; //printing the list /*kprintf("\nSHARED VPNO: "); next = &(frm_map[free_frm-FRAME0]); while(next != NULL) { kprintf("%d -> ",next->fr_vpno); next = next->shared; }*/ } else { free_frm = get_frm(); frm_tab[free_frm-FRAME0].status = FRM_BS; frm_tab[free_frm-FRAME0].refcnt++; frm_tab[free_frm-FRAME0].bs = bs_id; frm_tab[free_frm-FRAME0].bs_page = backing_store_page; frm_tab[free_frm-FRAME0].bs_next = NULL; frm_tab[free_frm-FRAME0].fifo = NULL; frm_tab[free_frm-FRAME0].age = 128; frm_map[free_frm-FRAME0].fr_status = FRM_MAPPED; frm_map[free_frm-FRAME0].fr_pid = currpid; frm_map[free_frm-FRAME0].fr_vpno = eip; frm_map[free_frm-FRAME0].fr_refcnt++; frm_map[free_frm-FRAME0].fr_type = FR_PAGE; frm_map[free_frm-FRAME0].bs_page_num = backing_store_page; ni_page_table[backing_store_page-total_bs_left] = free_frm; //set bs mappings if(bs_tab[bs_id].frm == NULL) bs_tab[bs_id].frm = &frm_tab[free_frm-FRAME0]; else { //frame_t *jump = &frm_tab[free_frm-FRAME0]; frame_t *jump = (frame_t *)getmem(sizeof(frame_t)); jump = bs_tab[bs_id].frm; while(jump->bs_next != NULL) { jump = jump->bs_next; //kprintf("\njumping\n"); } jump->bs_next = &frm_tab[free_frm-FRAME0]; } //adding this frame to the fifo queue if(fifo_head == NULL) { //queue is empty fifo_head = &frm_tab[free_frm-FRAME0]; fifo_tail = fifo_head; } else { fifo_tail->fifo = &frm_tab[free_frm-FRAME0]; fifo_tail = &frm_tab[free_frm-FRAME0]; } } //kprintf("\nget_frm return frame %d.", free_frm); unsigned long *dst_addr = (unsigned long *)(free_frm*NBPG); //kprintf("\n\n Virtual page %d mapped to bs page %d, bs id %d, mapped to frame %d, %lu",eip, backing_store_page, bs_id, free_frm,dst_addr); /*frame_t *next = fifo_head; kprintf("\nFIFO : "); while(next != NULL) { kprintf("%d-> ",next->frame_num); next = next->fifo; } kprintf("\n");*/ //copy page from bs to phy for(i = 0; i < NBPG/sizeof(unsigned long); i++) { *dst_addr = *bs_addr; dst_addr++; bs_addr++; } ptr->pt_base = free_frm; restore(ps); //kprintf("\nmap bs%d/page: %d to frame %d",bs_id, (backing_store_page-proctab[currpid].map[bs_id].base_page), free_frm); return OK; }
float *fgetmem(const int leng) { return ((float *) getmem((size_t) leng, sizeof(float))); }
static inline unsigned char pop() { if (s<0xff) s++; return getmem(0x100+s); }
real *rgetmem(const int leng) { return ((real *) getmem((size_t) leng, sizeof(real))); }
static unsigned char getaddr(int mode) { unsigned short ad, ad2; switch(mode) { case imp: cycles+=2; return 0; case imm: cycles+=2; return getmem(pc++); case abs: cycles+=4; ad=getmem(pc++); ad|=getmem(pc++)<<8; return getmem(ad); case absx: cycles+=4; ad=getmem(pc++); ad|=256*getmem(pc++); ad2=ad+x; if ((ad2&0xff00)!=(ad&0xff00)) cycles++; return getmem(ad2); case absy: cycles+=4; ad=getmem(pc++); ad|=256*getmem(pc++); ad2=ad+y; if ((ad2&0xff00)!=(ad&0xff00)) cycles++; return getmem(ad2); case zp: cycles+=3; ad=getmem(pc++); return getmem(ad); case zpx: cycles+=4; ad=getmem(pc++); ad+=x; return getmem(ad&0xff); case zpy: cycles+=4; ad=getmem(pc++); ad+=y; return getmem(ad&0xff); case indx: cycles+=6; ad=getmem(pc++); ad+=x; ad2=getmem(ad&0xff); ad++; ad2|=getmem(ad&0xff)<<8; return getmem(ad2); case indy: cycles+=5; ad=getmem(pc++); ad2=getmem(ad); ad2|=getmem((ad+1)&0xff)<<8; ad=ad2+y; if ((ad2&0xff00)!=(ad&0xff00)) cycles++; return getmem(ad); case acc: cycles+=2; return a; } return 0; }
SYSCALL pfint() { unsigned long cr2,physical_addr; virt_addr_t * vaddr; int vp,s,o,avail,*store,*pageth; unsigned int p,q,pt; pd_t *pd; pt_t *new_pt; STATWORD ps; // Disable interrupts disable(ps); if(GDB) kprintf("\n*************pfint is running!************\n"); // Get the faulted address. The processor loads the CR2 register // with the 32-bit address that generated the exception. /* 1. Get the faulted address. */ cr2 = read_cr2(); vaddr = (virt_addr_t *)(&cr2); if(GDB) kprintf("&cr2=%x, cr2=%x, &vaddr=%x, vaddr=%x\n",&cr2,cr2,&vaddr,vaddr); /* 2. Let 'vp' be the virtual page number of the page containing of the faulted address */ vp = a2pno(cr2); if(GDB) kprintf("vp=%d,\n",vp); /* 3. Let pd point to the current page directory. */ pd = proctab[currpid].pdbr; if(GDB) kprintf("pd=%x,\n",pd); /* 4. Check that a is a legal address (i.e., it has been mapped). If it is not, print an error message and kill the process. */ pageth = getmem( sizeof(int *) ); store = getmem( sizeof(int *) ); if( SYSERR == bsm_lookup(currpid, vp, store, pageth)){ kprintf("ERROR: This virtual address hasn't been mapped!\n"); kill(currpid); } /* 5. Let p be the upper ten bits of a. [p represents page dirctory offset] */ /* 6. Let q be the bits [21:12] of a. [p represents page table offset.] /* 7.1 Let pt point to the pth page table.*/ p = vaddr->pd_offset; q = vaddr->pt_offset; pt = vaddr->pg_offset; if(GDB) kprintf("p=%d,q=%d,pt=%d\n",p,q,pt); /* 7.2 If the pth page table does not exist obtain a frame for it and initialize it. */ if(pd[p].pd_pres != 1){ if(GDB) kprintf("**obtain a frame for the new page table. \n"); avail = get_frm(); //get the id of a new frame from frm_tab[]; if (avail == -1) { if(GDB) kprintf("Could not create page table!\n"); restore(ps); return SYSERR; } //initialize frame[avail], update the process_id and frame_type of this frame. init_frm(avail, currpid, FR_TBL); frm_tab[avail].fr_upper_t = pa2frid((unsigned long) pd); if(GDB) kprintf("upper page table @frame[%d] pd=%x, a2pno(pd)=%d\n",frm_tab[avail].fr_upper_t, pd, a2pno((unsigned long) pd)); new_pt = frid2pa(avail); init_pt(new_pt); //update this page_table_entry in the page_directory. pd[p].pd_pres = 1; pd[p].pd_write = 1; pd[p].pd_user = 0; // not sure about the usage; pd[p].pd_pwt = 0; pd[p].pd_pcd = 0; pd[p].pd_acc = 0; pd[p].pd_mbz = 0; pd[p].pd_fmb = 0; pd[p].pd_global = 0; pd[p].pd_avail = 0; // not in use right now. pd[p].pd_base = a2pno((unsigned long) new_pt); /* location of page table */ if(GDB) kprintf("New page_table(%x)@frame[%d] updated in page_directory[%d]@(frame[%d])\n", new_pt, avail, p, frm_tab[avail].fr_upper_t); if(GDB) kprintf("q=%d, new_pt[q]=%x, new_pt=%x, pd[p].pd_base=%d\n", q, new_pt[q], new_pt, pd[p].pd_base); } //if the page table has already existed, just need to refcnt++; else { int avail = pd[p].pd_base -1024; frm_tab[avail].fr_refcnt++; if(GDB) kprintf("frm_tab[%d].fr_refcnt = %d, frame_type: %d\n",avail, frm_tab[avail].fr_refcnt, frm_tab[avail].fr_type); } /* 8.1 Using the backing store map, find the store s and page offset o which correspond to vp. */ //already saved in 'store' and 'pageth' s = *store; o = *pageth; /* 8.2 In the inverted page table increment the reference count of the frame which holds pt. This indicates that one more of pt's entries is marked "present." */ avail = find_frm(currpid,vp); if (avail == -1) { if(GDB) kprintf("allocating a page for the page fault\n"); avail = get_frm(); if(avail == -1) { if(GDB) kprintf("ATTENTION! Frames full. ###Replacement NEEDED!###\n"); int frame_number = proctab[currpid].nframes-1; int frame_id = proc_frames[currpid][0]; //update_proc_frames(pid,frame_number); int i; for (i = 0; i+1 < frame_number; ++i) { proc_frames[currpid][i] = proc_frames[currpid][i+1]; } proctab[currpid].nframes = frame_number; int pid = frm_tab[frame_id].fr_pid; int upper_id = frm_tab[frame_id].fr_upper_t; vp = frm_tab[frame_id].fr_vpno; if(GDB) kprintf("currpid=%d, frame[%d].pid=%d .vpno=%d, upper_frame[%d].ref=%d\n",currpid,frame_id,pid,vp,upper_id,frm_tab[upper_id].fr_refcnt); p = vp>>10; q = vp &0x003ff; new_pt = vp2pa(pd[p].pd_base); new_pt[q].pt_pres = 0; new_pt[q].pt_write = 1; new_pt[q].pt_base = 0; if(GDB) kprintf("pd_offset=%d, pt_offset=%d, pt_dirty=%d\n",p,q,new_pt[q].pt_dirty); if(new_pt[q].pt_dirty == 1) { //write back and pageth = getmem( sizeof(int *) ); store = getmem( sizeof(int *) ); if( SYSERR == bsm_lookup(currpid, vp, store, pageth)){ kprintf("ERROR: This virtual address hasn't been mapped!\n"); kill(currpid); } if(GDB) kprintf("maping found: {pid: %d, vpno: %d, store: %d, pageth: %d}\n",currpid,vp,*store,*pageth); write_bs((char *)new_pt, *store, *pageth); } init_pt(new_pt); reset_frm(frame_id); frm_tab[upper_id].fr_refcnt -= 2; //it is 2, not 1. if(frm_tab[upper_id].fr_refcnt <= 0){ //mark the appropriate entry in pd as being not present, and free pt. } //invalidate the TLB entry for the page vp using the invlpg instruction if(pid == currpid) { set_PDBR(currpid); } } else { init_frm(avail, currpid, FR_PAGE); frm_tab[avail].fr_upper_t = pd[p].pd_base-FRAME0; if(GDB) kprintf("upper page table @frame[%d]\n",frm_tab[avail].fr_upper_t); frm_tab[avail].fr_vpno = vp; int counter = proctab[currpid].nframes; proc_frames[currpid][counter] = frm_tab[avail].fr_id; proctab[currpid].nframes++; if(GDB) kprintf("proc_frames[%d][%d] = frame[%d]\n",currpid,counter,avail); // Add this frame to head of the frame list within the bs of this process //(frm_tab[avail].bs_next)->fr_vpno //, proctab[currpid].bsmap[s].frames->bs_next if(GDB) kprintf("&frm_tab[avail].bs_next = %x\n",frm_tab[avail].bs_next, &frm_tab[avail].bs_next); if(GDB) kprintf("proctab[%d].bsmap[%d].frames = %x, ->vpno=%d, ->bs_next=%x\n",currpid, s, proctab[currpid].bsmap[s].frames, proctab[currpid].bsmap[s].frames->fr_vpno, proctab[currpid].bsmap[s].frames->bs_next); frm_tab[avail].bs_next = getmem(sizeof(fr_map_t *)); frm_tab[avail].bs_next = proctab[currpid].bsmap[s].frames; proctab[currpid].bsmap[s].frames = &frm_tab[avail]; fr_map_t *frame = proctab[currpid].bsmap[s].frames; int i = frame->fr_vpno; if(GDB) kprintf("i = %d\n",i); if(GDB) kprintf("~~~frame[%d] linked to frame[%d]\n", avail, frame->bs_next==NULL?-1:frame->bs_next->fr_id); if(GDB) kprintf("frame[%d].bs_next = %x, &**=%x\n",avail,frm_tab[avail].bs_next, &frm_tab[avail].bs_next); if(GDB) kprintf("proctab[%d].bsmap[%d].frames = %x, ->vpno=%d, ->bs_next=%x\n",currpid, s, proctab[currpid].bsmap[s].frames, proctab[currpid].bsmap[s].frames->fr_vpno, proctab[currpid].bsmap[s].frames->bs_next); if(GDB) kprintf("Mapping frame[%d](ppno[%d]) to {pid[%d], vpno[%d]} -> {bs[%d],offset:%d}\n", avail,frid2vpno(avail),currpid,vp,s,o); physical_addr = frid2pa(avail); read_bs(physical_addr,s,o); if(GDB) kprintf("copied from bs[%d]:offset[%d] to vp[%d]@(%x)\n",s,o,vp,vp2pa(vp)); } }