X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=utils%2Fprof%2Fcgprof%2Fmatrix.c;fp=utils%2Fprof%2Fcgprof%2Fmatrix.c;h=b4ca43f96bb7febb0636a6f35e265bd2b45d65cb;hb=0065d5ab628975892cea1ec7303f968c3338cbe1;hp=0000000000000000000000000000000000000000;hpb=28a464a75e14cece5db40f2765a29348273ff2d2;p=ghc-hetmet.git diff --git a/utils/prof/cgprof/matrix.c b/utils/prof/cgprof/matrix.c new file mode 100644 index 0000000..b4ca43f --- /dev/null +++ b/utils/prof/cgprof/matrix.c @@ -0,0 +1,98 @@ +/* ------------------------------------------------------------------------ + * $Id: matrix.c,v 1.3 2006/01/09 14:32:31 simonmar Exp $ + * + * Copyright (C) 1995-2000 University of Oxford + * + * Permission to use, copy, modify, and distribute this software, + * and to incorporate it, in whole or in part, into other software, + * is hereby granted without fee, provided that + * (1) the above copyright notice and this permission notice appear in + * all copies of the source code, and the above copyright notice + * appear in clearly visible form on all supporting documentation + * and distribution media; + * (2) modified versions of this software be accompanied by a complete + * change history describing author, date, and modifications made; + * and + * (3) any redistribution of the software, in original or modified + * form, be without fee and subject to these same conditions. + * --------------------------------------------------------------------- */ + +/* Not very clever sparse representation of a matrix. However, it will do + * for the call graph profiler. + */ + +#include +#include +#include +#include "matrix.h" + +Matrix newMat(int rows,int cols, int elsize, void *zero) { + Matrix res; + + res.elsize= elsize; + res.zero = malloc(elsize); + if (res.zero==NULL) { + fprintf(stderr,"{newMat} unable to allocate storage\n"); + exit(1); + } + memcpy(res.zero,zero,elsize); + res.rows = rows; + res.cols = cols; + res.mat=NULL; + return res; +} + +void freeMat(Matrix *mat) { + Matrix_element *tmp_ptr, *ptr=mat->mat; + free(mat->zero); + + while(ptr!=NULL) { + free(ptr->data); + tmp_ptr = ptr->next; + free(ptr); + ptr=tmp_ptr; + } +} + +void *_Mat(Matrix *mat,int x, int y,int lineno, char *filename) { + Matrix_element *ptr= mat->mat; + if (x<0 || x>=mat->rows || y<0 || y>=mat->cols) { + fprintf(stderr,"Mat[%d,%d] out of bound index at line %d of \"%s\"\n", + x,y,lineno,filename); + exit(1); + } + while(ptr) { + if ((x==ptr->x) && (y==ptr->y)) { + return ptr->data; + } + ptr=ptr->next; + } + /* Not in list */ + ptr = (Matrix_element*) malloc(sizeof(Matrix_element)); + if (ptr==NULL) { + fprintf(stderr,"{_Mat} failed to allocate %zd bytes\n", + sizeof(Matrix_element)); + exit(1); + } + ptr->data = (void*) malloc(mat->elsize); + if (ptr->data==NULL) { + fprintf(stderr,"{_Mat} failed to allocate element of size %d bytes\n", + mat->elsize); + exit(1); + } + ptr->x=x; + ptr->y=y; + memcpy(ptr->data,mat->zero,mat->elsize); + ptr->next=mat->mat; + mat->mat=ptr; + return ptr->data; +} + +int Mat_dense(Matrix mat,int x,int y) { + Matrix_element *ptr= mat.mat; + while (ptr) { + if ((x==ptr->x) && (y==ptr->y)) return 1; + ptr=ptr->next; + } + return 0; +}