📄 hmem.c
字号:
/* EXPORT->PrintHeapStats: print summary stats for given memory heap */void PrintHeapStats(MemHeap *x){ char tc; BlockP p; int nBlocks = 0; switch (x->type){ case MHEAP: tc = 'M'; break; case MSTAK: tc = 'S'; break; case CHEAP: tc = 'C'; break; } for (p=x->heap; p != NULL; p = p->next) ++nBlocks; printf("nblk=%3d, siz=%6u*%-3u, used=%9u, alloc=%9u : %s[%c]\n", nBlocks, x->curElem, x->elemSize, x->totUsed, x->totAlloc*x->elemSize,x->name,tc) ; fflush(stdout);}/* EXPORT->PrintAllHeapStats: print summary stats for all memory heaps */void PrintAllHeapStats(void){ MemHeapRec *p; printf("\n---------------------- Heap Statistics ------------------------\n"); for (p = heapList; p != NULL; p = p->next) PrintHeapStats(p->heap); printf( "---------------------------------------------------------------\n");}/* ------------- Vector/Matrix Memory Management -------------- *//* Vectors are pointers to arrays of float (ie float*); matrices are pointers to an array of vectors (ie float**). All indexing is v[1..n] and m[1..r][1..c]. The actual size of each vector is stored in v[0]. For matrices, the row lengths (number of columns) are stored in every row since they are genuine vectors, the number of rows is stored in m[0]. Triangular matrices are the same except that the rows increase in length with the first row being a vector of one element. Short and IntVecs are arranged in a similar way to Vectors. Shared vectors and matrices have an extra 2 * sizeof(Ptr) bytes prepended to hold a usage count and a hook.*//* EXPORT->vectorElemSize: size of vectors for creating heaps */size_t ShortVecElemSize(int size) { return (size+1)*sizeof(short); }size_t IntVecElemSize(int size) { return (size+1)*sizeof(int); }size_t VectorElemSize(int size) { return (size+1)*sizeof(float); }size_t DVectorElemSize(int size){ return (size+1)*sizeof(double);}size_t SVectorElemSize(int size){ return (size+1)*sizeof(float)+2*sizeof(Ptr); }/* EXPORT->CreateShortVec: Allocate space for short array v[1..size] */ShortVec CreateShortVec(MemHeap *x,int size){ short *v; v = (short *)New(x,ShortVecElemSize(size)); *v = size; return (ShortVec)v;}/* EXPORT->CreateIntVec: Allocate space for int array v[1..size] */IntVec CreateIntVec(MemHeap *x,int size){ int *v; v = (int *)New(x,IntVecElemSize(size)); *v = size; return (IntVec)v;}/* EXPORT->CreateVector: Allocate space for vector v[1..size] */Vector CreateVector(MemHeap *x, int size){ Vector v; int *i; v = (Vector)New(x,VectorElemSize(size)); i = (int *) v; *i = size; return v;}/* EXPORT->CreateDVector: Allocate space for double vector v[1..size] */DVector CreateDVector(MemHeap *x, int size){ DVector v; int *i; v = (DVector)New(x,DVectorElemSize(size)); i = (int *) v; *i = size; return v;}/* EXPORT->CreateSVector: Shared version */Vector CreateSVector(MemHeap *x, int size){ SVector v; Ptr *p; int *i; p = (Ptr *)New(x,SVectorElemSize(size)); v = (SVector) (p+2); i = (int *) v; *i = size; SetHook(v,NULL); SetUse(v,0); return v;}/* EXPORT->ShortVecSize: returns number of components in v */int ShortVecSize(ShortVec v){ return (int)(*v);}/* EXPORT->IntVecSize: returns number of components in v */int IntVecSize(IntVec v){ return *v;}/* EXPORT->VectorSize: returns number of components in v */int VectorSize(Vector v){ int *i; i = (int *) v; return *i;}/* EXPORT->DVectorSize: returns number of components in v */int DVectorSize(DVector v){ int *i; i = (int *) v; return *i;}/* EXPORT->FreeShortVec: Free space allocated for short vector v */void FreeShortVec(MemHeap *x,ShortVec v){ Dispose(x,v);}/* EXPORT->FreeIntVec: Free space allocated for int vector v */void FreeIntVec(MemHeap *x,IntVec v){ Dispose(x,v);}/* EXPORT->FreeVector: Free space allocated for vector v[1..size] */void FreeVector(MemHeap *x, Vector v){ Dispose(x,v);}/* EXPORT->FreeDVector: Free space allocated for vector v[1..size] */void FreeDVector(MemHeap *x, DVector v){ Dispose(x,v);}/* EXPORT->FreeSVector: Free space allocated for vector v[1..size] */void FreeSVector(MemHeap *x, Vector v){ DecUse(v); if (GetUse(v) <= 0) Dispose(x,(Ptr *)(v)-2);}/* EXPORT->MatrixElemSize: size of matrices for creating heaps */size_t MatrixElemSize(int nrows,int ncols){ return VectorElemSize(ncols) * nrows + (nrows+1)*sizeof(Vector);}size_t DMatrixElemSize(int nrows,int ncols){ return MRound(DVectorElemSize(ncols) * nrows + (nrows+1)*sizeof(DVector));}size_t SMatrixElemSize(int nrows,int ncols){ return VectorElemSize(ncols) * nrows + (nrows+3)*sizeof(Vector);}size_t TriMatElemSize(int size){ return size*(VectorElemSize(0)*2 + (size+1)*sizeof(float))/2 + (size+1)*sizeof(Vector);}size_t STriMatElemSize(int size){ return size*(VectorElemSize(0)*2 + (size+1)*sizeof(float))/2 + (size+1)*sizeof(Vector) + 2*sizeof(Ptr);}/* EXPORT->CreateMatrix: Allocate space for matrix m[1..nrows][1..ncols] */Matrix CreateMatrix(MemHeap *x, int nrows, int ncols){ size_t vsize; int *i,j; Vector *m; char *p; p =(char *) New(x,MatrixElemSize(nrows,ncols)); i = (int *)p; *i = nrows; vsize = VectorElemSize(ncols); m = (Vector *)p; p += (nrows+1)*sizeof(Vector); for (j=1;j<=nrows; j++, p += vsize) { i = (int *) p; *i = ncols; m[j] = (Vector) p; } return m;}/* EXPORT->CreateTriMat: Allocate space for matrix m[1..size][1..i] */TriMat CreateTriMat(MemHeap *x,int size){ int *i,j; Vector *m; char *p; p = (char *) New(x,TriMatElemSize(size)); i = (int *)p; *i = size; m = (Vector *)p; p += (size+1)*sizeof(Vector); for (j=1;j<=size; j++) { i = (int *) p; *i = j; m[j] = (Vector) p; p += VectorElemSize(j); } return m;}/* EXPORT->CreateDMatrix: Allocate space for double matrix m[1..nrows][1..ncols] */DMatrix CreateDMatrix(MemHeap *x, int nrows,int ncols){ size_t vsize; int *i,j; DVector *m; char *p; p = (char *) New(x,DMatrixElemSize(nrows,ncols)); i = (int *) p; *i = nrows; vsize = DVectorElemSize(ncols); m = (DVector *) p; p += MRound((nrows+1)*sizeof(DVector)); for (j=1; j<=nrows; j++, p += vsize) { i = (int *) p; *i = ncols; m[j] = (DVector) p; } return m;}/* EXPORT->CreateSMatrix: Allocate space for matrix m[1..nrows][1..ncols] */Matrix CreateSMatrix(MemHeap *x, int nrows,int ncols){ size_t vsize; int *i,j; Vector *m; char *p; p = (char *)New(x,SMatrixElemSize(nrows,ncols)) + 2*sizeof(Ptr *); i = (int *)p; *i = nrows; vsize = VectorElemSize(ncols); m = (Vector *)p; p += (nrows+1)*sizeof(Vector); for (j=1;j<=nrows; j++, p += vsize) { i = (int *) p; *i = ncols; m[j] = (Vector) p; } SetHook(m,NULL); SetUse(m,0); return m;}/* EXPORT->CreateSTriMat: Allocate space for matrix m[1..size][1..i] */STriMat CreateSTriMat(MemHeap *x,int size){ int *i,j; Vector *m; char *p; p = (char *)New(x,STriMatElemSize(size)) + 2*sizeof(Ptr *); i = (int *)p; *i = size; m = (Vector *)p; p += (size+1)*sizeof(Vector); for (j=1;j<=size; j++) { i = (int *) p; *i = j; m[j] = (Vector) p; p += VectorElemSize(j); } SetHook(m,NULL); SetUse(m,0); return m;}/* EXPORT->IsTriMat: True if matrix is lower triangular */Boolean IsTriMat(Matrix m){ int i,n; n=NumRows(m); for(i=1;i<=n;i++) if (VectorSize(m[i])!=i) return(FALSE); return(TRUE);}/* EXPORT->NumRows: number of rows in matrix m */int NumRows(Matrix m){ int *nrows; nrows = (int *) m; return *nrows;}/* EXPORT->NumCols: number of columns in matrix m */int NumCols(Matrix m){ int *ncols; ncols = (int *) m[1]; return *ncols;}/* EXPORT->NumDRows: number of rows in double matrix m */int NumDRows(DMatrix m){ int *nrows; nrows = (int *) m; return *nrows;}/* EXPORT->NumDCols: number of columns in double matrix m */int NumDCols(DMatrix m){ int *ncols; ncols = (int *) m[1]; return *ncols;}/* EXPORT->TriMatSize: number of rows/cols in triangular matrix m */int TriMatSize(TriMat m){ int *nrows; nrows = (int *) m; return *nrows;}/* EXPORT->FreeMatrix: Free space allocated for matrix m */void FreeMatrix(MemHeap *x, Matrix m){ Dispose(x,m);}/* EXPORT->FreeDMatrix: Free space allocated for matrix m */void FreeDMatrix(MemHeap *x, DMatrix m){ Dispose(x,m);}/* EXPORT->FreeSMatrix: Free space allocated for matrix m */void FreeSMatrix(MemHeap *x, Matrix m){ DecUse(m); if (GetUse(m) <= 0) Dispose(x,(Ptr *)(m) - 2);}/* EXPORT->FreeTriMat: Free space allocated for tri matrix m */void FreeTriMat(MemHeap *x,TriMat m){ Dispose(x,m);}/* EXPORT->FreeSTriMat: Free space allocated for shared tri matrix m */void FreeSTriMat(MemHeap *x,STriMat m){ DecUse(m); if (GetUse(m) <= 0) Dispose(x,(Ptr *)(m) - 2);}/* EXPORT->SetUse: set usage count of m to n */void SetUse(Ptr m,int n){ Ptr *p; p = (Ptr *) m; --p; *((int *)p) = n;}/* EXPORT->IncUse: Increment usage count of m by one */void IncUse(Ptr m){ Ptr *p; p = (Ptr *) m; --p; ++(*((int *)p));}/* EXPORT->DecUse: Decrement usage count of m by one */void DecUse(Ptr m){ Ptr *p; p = (Ptr *) m; --p; --(*((int *)p));}/* EXPORT->GetUse: return usage count of m */int GetUse(Ptr m){ Ptr *p; p = (Ptr *) m; --p; return *((int *)p);}/* EXPORT->SetHook: set hook of m to p */void SetHook(Ptr m, Ptr ptr){ Ptr *p; p = (Ptr *) m; p -= 2; *p = ptr;}/* EXPORT->GetHook: return hook of m */Ptr GetHook(Ptr m){ Ptr *p; p = (Ptr *) m; p -=2; return *p;}/* EXPORT->IsSeenV: return true if seen */Boolean IsSeenV(Ptr m){ Ptr *p; int i; p = (Ptr *) m; --p; i = *((int *)p); return i<0;}/* EXPORT->TouchV: mark use flag as seen */void TouchV(Ptr m){ Ptr *p; int i; p = (Ptr *) m; --p; i = *((int *)p); if (i==0) *((int *)p) = INT_MIN; else if (i>0) *((int *)p) = -i;}/* EXPORT->UntouchV: mark use flag as unseen */void UntouchV(Ptr m){ Ptr *p; int i; p = (Ptr *) m; --p; i = *((int *)p); if (i==INT_MIN) *((int *)p) = 0; else if (i<0) *((int *)p) = -i;}/* ------------------ String Memory Management ----------------- *//* EXPORT->NewString: return a string of given size */ char *NewString(MemHeap *x, int size){ char *s; s = (char *) New(x,size+1); *s='\0'; /* make it empty */ return s;}/* EXPORT->CopyString: return a copy of string s */ char *CopyString(MemHeap *x, char *s){ char *t; t = (char *) New(x,strlen(s)+1); strcpy(t,s); return t;}/* -------------------------- End of HMem.c ---------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -