b4ca43f96bb7febb0636a6f35e265bd2b45d65cb
[ghc-hetmet.git] / utils / prof / cgprof / matrix.c
1 /* ------------------------------------------------------------------------
2  * $Id: matrix.c,v 1.3 2006/01/09 14:32:31 simonmar Exp $
3  *                                                                      
4  *      Copyright (C) 1995-2000 University of Oxford
5  *                                                                      
6  * Permission to use, copy, modify, and distribute this software,
7  * and to incorporate it, in whole or in part, into other software,
8  * is hereby granted without fee, provided that
9  *   (1) the above copyright notice and this permission notice appear in
10  *       all copies of the source code, and the above copyright notice
11  *       appear in clearly visible form on all supporting documentation
12  *       and distribution media;
13  *   (2) modified versions of this software be accompanied by a complete
14  *       change history describing author, date, and modifications made;
15  *       and
16  *   (3) any redistribution of the software, in original or modified
17  *       form, be without fee and subject to these same conditions.
18  * --------------------------------------------------------------------- */
19
20 /* Not very clever sparse representation of a matrix. However, it will do
21  * for the call graph profiler. 
22  */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "matrix.h"
28
29 Matrix newMat(int rows,int cols, int elsize, void *zero) {
30   Matrix res;
31
32   res.elsize= elsize;
33   res.zero  = malloc(elsize);
34   if (res.zero==NULL) {
35     fprintf(stderr,"{newMat} unable to allocate storage\n");
36     exit(1);
37   }
38   memcpy(res.zero,zero,elsize);
39   res.rows  = rows;
40   res.cols  = cols;
41   res.mat=NULL;
42   return res;
43 }
44
45 void freeMat(Matrix *mat) {
46   Matrix_element *tmp_ptr, *ptr=mat->mat;
47   free(mat->zero);
48
49   while(ptr!=NULL) {
50     free(ptr->data);
51     tmp_ptr = ptr->next;
52     free(ptr);
53     ptr=tmp_ptr;
54   }
55 }
56
57 void *_Mat(Matrix *mat,int x, int y,int lineno, char *filename) {
58   Matrix_element *ptr= mat->mat;
59   if (x<0 || x>=mat->rows || y<0 || y>=mat->cols) {
60     fprintf(stderr,"Mat[%d,%d] out of bound index at line %d of \"%s\"\n",
61             x,y,lineno,filename);
62     exit(1);
63   }
64   while(ptr) {
65     if ((x==ptr->x) && (y==ptr->y)) {
66       return ptr->data;
67     }
68     ptr=ptr->next;
69   }
70   /* Not in list */
71   ptr = (Matrix_element*) malloc(sizeof(Matrix_element));
72   if (ptr==NULL) {
73     fprintf(stderr,"{_Mat} failed to allocate %zd bytes\n",
74             sizeof(Matrix_element));
75     exit(1);
76   }
77   ptr->data = (void*) malloc(mat->elsize);
78   if (ptr->data==NULL) {
79     fprintf(stderr,"{_Mat} failed to allocate element of size %d bytes\n",
80             mat->elsize);
81     exit(1);
82   }
83   ptr->x=x;
84   ptr->y=y;
85   memcpy(ptr->data,mat->zero,mat->elsize);
86   ptr->next=mat->mat;
87   mat->mat=ptr;
88   return ptr->data;
89 }
90
91 int Mat_dense(Matrix mat,int x,int y) {
92   Matrix_element *ptr= mat.mat;
93   while (ptr) {
94     if ((x==ptr->x) && (y==ptr->y)) return 1;
95     ptr=ptr->next;
96   }
97   return 0;
98 }