NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/MEX/opnml_mex5_allocs.c

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 /*----------------------------------------------------------------------
2
3   MATLAB C-MEX file functions:
4  
5   Allocation functions have been added for C-MEX function
6   writing.  These routines are prefixed with "mx" and are
7   available for int and double vector and matrix
8   allocation.  There is no "float" capability.
9  
10   This is the file opnml_mex5_allocs.c, routines for allocation of
11   vectors and matricies for use in MATLAB/C-MEX files.  It must
12   be complied with strict ANSI-compliance ("gcc -ansi ...", for
13   example, and is coded only for MATLAB5.1 or greater.  It should be
14   placed in the OPNML/MATLAB mex src directory, and is included in
15   c-source as:
16  
17   #include "opnml_mex5_allocs.c"
18  
19   These routines are taken from Numerical Recipes in C,
20   Press et. al., and turned into MATLAB-style allocation routines
21   by Brian Blanton (UNC).
22  
23   mxCalloc is used to allocate memory.  This function clears
24   the allocated space to (double)0.  "Free" functions have NOT been
25   added because MATLAB frees memory allocated within a mex
26   function automatically upon exit if it has been allocated with
27   mxCalloc functions. In fact, freeing memory allocated with mxCalloc
28   within the function seems to cause (atleast me) problems.
29  
30   The MATLAB header file "mex.h" must have been "included" in
31   the c source BEFORE "opnml_mex5_allocs.c".
32   Therefore,  the order of "includes" should look something like:
33  
34   #include <stdio.h>
35   #include <math.h>
36   #include "mex.h"
37   #include "opnml_mex5_allocs.c"
38   ...
39  
40   If you do not, cmex will NOT complain, but you will get a runtime
41   (in MATLAB) error similar to the following:
42
43   /lib/dld.sl: Unresolved symbol: mxfree_Dmatrix (code)  from
44      /home5/blanton/matlab/mex/contmex.mexhp7
45   /lib/dld.sl: Unresolved symbol: mxDmatrix (code)  from
46      /home5/blanton/matlab/mex/contour_mex.mexhp7
47   Unable to load mex
48      file: /home5/blanton/matlab/mex/contmex.mexhp7.
49   ??? Invalid MEX-file
50    
51   Recall that "matrix.h" is included by "mex.h", so don't do it here.
52   mxCalloc and mxFree WILL BE RESOLVED!!
53    
54 --------------------------------------------------------------------- */
55  
56 #ifndef _OPNML_ALLOCS_INCLUDED
57 #define _OPNML_ALLOCS_INCLUDED
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 static int imaxarg1,imaxarg2;
64 #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ? (imaxarg1) : (imaxarg2))
65 static int iminarg1,iminarg2;
66 #define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ? (iminarg1) : (iminarg2))
67
68
69 static double dmaxarg1,dmaxarg2;
70 #define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ? (dmaxarg1) : (dmaxarg2))
71
72 static double dminarg1,dminarg2;
73 #define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ? (dminarg1) : (dminarg2))
74
75 /* NRC DEFINITION */
76 #define BUMP 1
77 #define NR_END 1
78 #define FREE_ARG char*
79
80 /* ---------------------------------------------------------------------
81
82   ####           #    #  ######  #    #
83  #    #          ##  ##  #        #  #
84  #       ######  # ## #  #####     ##
85  #       ######  #    #  #         ##
86  #    #          #    #  #        #  #
87   ####           #    #  ######  #    #
88  
89 --------------------------------------------------------------------- */
90 #ifdef mex_h
91 /* ---- C-MEX ALLOCATION FUNCTION PROTOTYPES ------------------------- */
92 int     *mxIvector(int nl,int nh);
93 int    **mxImatrix(int nrl,int nrh,int ncl,int nch);
94 double  *mxDvector(int nl,int nh);
95 double **mxDmatrix(int nrl,int nrh,int ncl,int nch);
96
97 /* ---- C-MEX INTEGER VECTOR ALLOCATION ------------------------------ */
98 int *mxIvector(int nl,int nh)
99 {
100    int *v;
101  
102    v=(int *)mxCalloc((nh-nl+1),sizeof(int));
103    if (!v) {
104       puts("allocation failure in mxIvector()");
105       return(0);
106    }
107    return v-nl;
108 }
109
110 /* ---- C-MEX INTEGER MATRIX ALLOCATION ------------------------------ */
111 int **mxImatrix(int nrl,int nrh,int ncl,int nch)
112 {
113    int i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
114    int **m;
115
116    /* allocate pointers to rows */
117    m=(int **) mxCalloc(nrow,sizeof(int*));
118    if (!m){
119       puts("row allocation failure in mxImatrix()");
120       return(0);
121    }
122    m -= nrl;
123
124    /* allocate rows and set pointers to them */
125    m[nrl]=(int *) mxCalloc(nrow*ncol,sizeof(int));
126    if (!m[nrl]) {
127       puts("column allocation failure in mxImatrix()");
128       return(0);
129    }
130    m[nrl] -= ncl;
131
132    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
133
134    /* return pointer to array of pointers to rows */
135    return m;
136 }
137
138 /* ---- C-MEX DOUBLE VECTOR ALLOCATION ------------------------------- */
139    double *mxDvector(int nl,int nh)
140 {
141    double *v;
142
143    v=(double *) mxCalloc((nh-nl+1),sizeof(double));
144    if (!v) {
145       puts("allocation failure in mxDvector()");
146       return(0);
147    }
148    return v-nl;
149 }
150
151 /* ---- C-MEX DOUBLE MATRIX ALLOCATION ------------------------------- */
152 double **mxDmatrix(int nrl,int nrh,int ncl,int nch)
153 {
154    int i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
155    double **m;
156
157    /* allocate pointers to rows */
158    m=(double **) mxCalloc(nrow,sizeof(double*));
159    if (!m) {
160       puts("row allocation failure in mxDmatrix()");
161       return(0);
162    }
163    m -= nrl;
164
165    /* allocate rows and set pointers to them */
166    m[nrl]=(double *) mxCalloc(nrow*ncol,sizeof(double));
167    if (!m[nrl]) {
168       puts("column allocation failure in mxDmatrix()");
169       return(0);
170    }
171    m[nrl] -= ncl;
172
173    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
174
175    /* return pointer to array of pointers to rows */
176    return m;
177 }
178
179
180 /* ---------------------------------------------------------------------
181
182   ####    #####  #    #  ######  #####
183  #    #     #    #    #  #       #    #
184  #    #     #    ######  #####   #    #
185  #    #     #    #    #  #       #####
186  #    #     #    #    #  #       #   #
187   ####      #    #    #  ######  #    #
188
189 --------------------------------------------------------------------- */
190 void opnmlerror(char *error_text,char *routine_name,int line_number,int error_code)
191 {
192    fprintf(stderr,"%s\n",error_text);
193    fprintf(stderr,"Routine: %s\n",routine_name);
194    fprintf(stderr,"Line Number: %d\n",line_number);
195    fprintf(stderr,"INTERNAL ERROR CODE: %d\n\n",error_code);
196
197    exit(error_code);
198 }
199
200 void opnmlerror2(char *error_text,char *routine_name,int error_code)
201 {
202    int i,line_number=0,nrow;
203    nrow=sizeof(error_text)/sizeof(char *);
204    for (i=0;i<nrow;i++)
205      fprintf(stderr,"%s",error_text[i]);
206    
207    fprintf(stderr,"Routine: %s\n",routine_name);
208    fprintf(stderr,"Line Number: %d\n",line_number);
209    fprintf(stderr,"INTERNAL ERROR CODE: %d\n\n",error_code);
210    
211    exit(error_code);
212 }
213
214 void nrerror(char error_text[])
215 /* Numerical Recipes standard error handler */
216 {
217         fprintf(stderr,"Numerical Recipes run-time error...\n");
218         fprintf(stderr,"%s\n",error_text);
219         fprintf(stderr,"...now exiting to system...\n");
220         exit(1);
221 }
222 int *Ivector(long nl, long nh)
223 /* allocate an int vector with subscript range v[nl..nh] */
224 {
225         int *v;
226         v=(int *)malloc((size_t) ((nh-nl+1+BUMP)*sizeof(int)));
227         if (!v) nrerror("allocation failure in ivector()");
228         return v-nl+BUMP;
229 }
230 void free_Ivector(int *v, long nl, long nh)
231 /* free an int vector allocated with ivector() */
232 {
233         free((FREE_ARG) (v+nl-NR_END));
234 }
235
236 #endif  /* mex_h */
237
238 #endif  /* _OPNML_ALLOCS_INCLUDED */
Note: See TracBrowser for help on using the browser.