SWZ: replace calloc with swzCalloc
parent
08f2b2b5c4
commit
571f430085
|
@ -34,15 +34,15 @@ mexFunction(int nlhs, mxArray *plhs[],
|
|||
* Allocate memory
|
||||
*/
|
||||
maxnargs = (int)(mxGetN(prhs[0])/2+1);
|
||||
argument = (char *)calloc(mxGetN(prhs[0])+1, sizeof(char));
|
||||
args = (char **)calloc(maxnargs, sizeof(char *));
|
||||
argument = (char *)swzCalloc(mxGetN(prhs[0])+1, sizeof(char));
|
||||
args = (char **)swzCalloc(maxnargs, sizeof(char *));
|
||||
if (argument==NULL || args==NULL)
|
||||
mexErrMsgTxt("In swz_mex: could not allocate memory. (1)");
|
||||
|
||||
/*
|
||||
* Create argument string from prhs and parse to create args / nargs
|
||||
*/
|
||||
if (!(args[nargs] = (char *)calloc(strlen(mainarg)+1, sizeof(char))))
|
||||
if (!(args[nargs] = (char *)swzCalloc(strlen(mainarg)+1, sizeof(char))))
|
||||
mexErrMsgTxt("In swz_mex: could not allocate memory. (2)");
|
||||
strncpy(args[nargs++], mainarg, strlen(mainarg));
|
||||
|
||||
|
@ -52,7 +52,7 @@ mexFunction(int nlhs, mxArray *plhs[],
|
|||
beginarg = &argument[0];
|
||||
while(n=strcspn(beginarg, " "))
|
||||
{
|
||||
if (!(args[nargs] = (char *)calloc(n+1, sizeof(char))))
|
||||
if (!(args[nargs] = (char *)swzCalloc(n+1, sizeof(char))))
|
||||
mexErrMsgTxt("In swz_mex: could not allocate memory. (3)");
|
||||
strncpy(args[nargs++], beginarg, n);
|
||||
beginarg += (isspace(beginarg[n]) || isblank(beginarg[n]) ? ++n : n);
|
||||
|
|
|
@ -25,12 +25,11 @@ extern int constant_seed;
|
|||
|
||||
#define swz_fprintf_stdout mexPrintf
|
||||
|
||||
#undef calloc
|
||||
#undef realloc
|
||||
#undef free
|
||||
|
||||
#define swzMalloc mxMalloc
|
||||
#define calloc mxCalloc
|
||||
#define swzCalloc mxCalloc
|
||||
#define realloc mxRealloc
|
||||
#define free mxFree
|
||||
|
||||
|
@ -38,4 +37,5 @@ extern int constant_seed;
|
|||
#else
|
||||
#define swz_fprintf_stdout printf
|
||||
#define swzMalloc malloc
|
||||
#define swzCalloc calloc
|
||||
#endif
|
||||
|
|
|
@ -349,7 +349,7 @@ void* dw_CopyArray(void* d, void* s)
|
|||
Assumes
|
||||
Both d and s are valid pointers and both *d and *s are either null or a
|
||||
null terminated string. If *d is a null terminated string, then it must
|
||||
have been created via a call to swzMalloc(), calloc() or realloc().
|
||||
have been created via a call to swzMalloc(), swzCalloc() or realloc().
|
||||
|
||||
Returns
|
||||
Returns one upon success and zero upon failure.
|
||||
|
@ -361,7 +361,7 @@ void* dw_CopyArray(void* d, void* s)
|
|||
|
||||
Notes
|
||||
It is critical that this function be called only if the destination string
|
||||
was dynamically created via a call to swzMalloc(), calloc() or realloc(). If
|
||||
was dynamically created via a call to swzMalloc(), swzCalloc() or realloc(). If
|
||||
this is not the case, then servere memory problems can result.
|
||||
*/
|
||||
static int dw_CopyString(void *d, void *s)
|
||||
|
|
|
@ -74,7 +74,7 @@ FILE *dw_AppendTextFile(char *filename)
|
|||
the buffer containing the file and resets *n if necessary. The if the
|
||||
passed buffer is null or is not large enough to contain the line, buffer is
|
||||
freed and a new buffer is allocated. Because of this, the passed buffer
|
||||
must either null or allocated with swzMalloc(), realloc(), or calloc() and the
|
||||
must either null or allocated with swzMalloc(), realloc(), or swzCalloc() and the
|
||||
calling routine is responsible for eventually freeing the memory if the
|
||||
return value is not null.
|
||||
|
||||
|
|
|
@ -514,7 +514,7 @@ int bLU(int *p, PRECISION *x, int m, int n, int xt)
|
|||
|
||||
int minmn = (m < n) ? m : n;
|
||||
|
||||
if(!(p2 = (lapack_int *)calloc(minmn, sizeof(lapack_int))))
|
||||
if(!(p2 = (lapack_int *)swzCalloc(minmn, sizeof(lapack_int))))
|
||||
return MEM_ERR;
|
||||
|
||||
for(i=0; i<minmn; i++)
|
||||
|
@ -1839,7 +1839,7 @@ int bReorderQZ_real(int *select, PRECISION *QQ, PRECISION *ZZ, PRECISION *SS, PR
|
|||
ijob = 0;
|
||||
liwork=1;
|
||||
|
||||
if(!(select2 = (lapack_int *)calloc(n, sizeof(lapack_int))))
|
||||
if(!(select2 = (lapack_int *)swzCalloc(n, sizeof(lapack_int))))
|
||||
return MEM_ERR;
|
||||
|
||||
for(i=0; i<n; i++)
|
||||
|
|
|
@ -159,11 +159,11 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims),
|
|||
*fcount = -1; /* fcount: number of evaluations of the function */
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
x[i] = tzMalloc(n, double); /* x[i] = calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
x[i] = tzMalloc(n, double); /* x[i] = swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
memcpy(x[0],xh,n*sizeof(double));
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
g[i] = tzMalloc(n, double); /* calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
g[i] = tzMalloc(n, double); /* swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
|
||||
f[0] = fcn(x[0],n,args,dims);
|
||||
|
||||
|
@ -207,7 +207,7 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims),
|
|||
/* Bad gradient or back and forth on step length.
|
||||
Possibly at cliff edge. Try perturbing search direction. */
|
||||
if (badg[1]) {
|
||||
double *Hcliff = tzMalloc(nn, double); /* calloc(nn,sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
double *Hcliff = tzMalloc(nn, double); /* swzCalloc(nn,sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
double randmax=1.0/(double)RAND_MAX; /* 03/10/2006, changed from 1/ to 1.0/ to make randmax a legal double. ansi-c*/
|
||||
/* if stuck, give it another try by perturbing Hessian */
|
||||
memcpy(Hcliff,H,nn*sizeof(double));
|
||||
|
@ -224,7 +224,7 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims),
|
|||
if (f[2] < f[0]) {
|
||||
badg[2] = peakwall(g[2],retcode[1],x[2],n,gfcn,fcn,args,dims);
|
||||
if (badg[2]) {
|
||||
double *xx = tzMalloc(n, double), nx; /* calloc(n,sizeof(double)), nx; Commented out by TZ. ansi-c*/
|
||||
double *xx = tzMalloc(n, double), nx; /* swzCalloc(n,sizeof(double)), nx; Commented out by TZ. ansi-c*/
|
||||
|
||||
#ifdef VERBOSE_WARNINGS
|
||||
printf("Cliff again. Try traversing.\n");
|
||||
|
@ -239,8 +239,8 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims),
|
|||
badg[3] = 1;
|
||||
retcode[2] = 101;
|
||||
} else {
|
||||
double *gcliff = tzMalloc(n, double), /* calloc(n,sizeof(double)), Commented out by TZ. ansi-c*/
|
||||
*eye = tzMalloc(nn, double); /* calloc(n,sizeof(double)); Bugs of Iskander. Changed from n to nn. 03/10/06. ansi-c*/
|
||||
double *gcliff = tzMalloc(n, double), /* swzCalloc(n,sizeof(double)), Commented out by TZ. ansi-c*/
|
||||
*eye = tzMalloc(nn, double); /* swzCalloc(n,sizeof(double)); Bugs of Iskander. Changed from n to nn. 03/10/06. ansi-c*/
|
||||
double dfnx = (f[2]-f[1])/nx;
|
||||
for (i=0; i<n; i++) {
|
||||
gcliff[i] = dfnx*xx[i];
|
||||
|
@ -588,7 +588,7 @@ static void csminit(double *fhat, double *xhat, int *fcount, int *retcode,
|
|||
else {
|
||||
/* with badg 1, we don't try to match rate of improvement to directional
|
||||
derivative. We're satisfied just to get some improvement in f. */
|
||||
dx = tzMalloc(n, double); /* dx = calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
dx = tzMalloc(n, double); /* dx = swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
/* //if (!dx) printf("Dynamic memory allocation error.\n"); Commnted out by TZ. ansi-c*/
|
||||
for (i=0; i<n; i++)
|
||||
dx[i] = -times(&H0[i*n],g,n);
|
||||
|
@ -622,7 +622,7 @@ static void csminit(double *fhat, double *xhat, int *fcount, int *retcode,
|
|||
printf("Predicted improvement: %18.9f, Norm of gradient: %18.9f\n", -dfhat*0.5, gnorm);
|
||||
#endif
|
||||
|
||||
dxtest = tzMalloc(n, double); /* calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
dxtest = tzMalloc(n, double); /* swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
while (!done) {
|
||||
for (i=0; i<n; i++)
|
||||
dxtest[i] = x0[i]+dx[i]*lambda;
|
||||
|
@ -772,7 +772,7 @@ static void bfgsi(double *H, double *dg, double *dx, int n, int nn) {
|
|||
int i;
|
||||
TSdmatrix *H_dm = NULL;
|
||||
|
||||
Hdg = tzMalloc(n, double); /* calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
Hdg = tzMalloc(n, double); /* swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
/* //if (!Hdg) printf("Dynamic memory allocation error.\n"); Commented out by TZ. ansi-c*/
|
||||
|
||||
/* Hdg = H0*dg; */
|
||||
|
@ -817,7 +817,7 @@ static double *mtimes(double *x, double *y, int n, int nn) {
|
|||
double *x0;
|
||||
double *z;
|
||||
int i, j;
|
||||
z = tzMalloc(nn, double); /* calloc(nn, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
z = tzMalloc(nn, double); /* swzCalloc(nn, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
for (i=0, x0=x; i<n; i++, y++)
|
||||
for (j=0, x=x0; j<n; j++, x++, z++)
|
||||
*z = (*x)*(*y);
|
||||
|
@ -827,7 +827,7 @@ static double *mtimes(double *x, double *y, int n, int nn) {
|
|||
static double *mminus(double *x, double *y, int n) {
|
||||
double *z;
|
||||
int i;
|
||||
z = tzMalloc(n, double); /* calloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
z = tzMalloc(n, double); /* swzCalloc(n, sizeof(double)); Commented out by TZ. ansi-c*/
|
||||
for (i=0; i<n; i++, x++, y++, z++)
|
||||
*z = (*x)-(*y);
|
||||
return z-n;
|
||||
|
|
|
@ -1116,7 +1116,7 @@ void permute_matrix(double *a, int n, int *indx) {
|
|||
double *b;
|
||||
int nn=n*n;
|
||||
register int i;
|
||||
b = calloc(nn,sizeof(double));
|
||||
b = swzCalloc(nn,sizeof(double));
|
||||
memcpy(b, a, nn*sizeof(double));
|
||||
for (i=0; i<nn; i++, a++)
|
||||
*a = b[indx[i%n]+indx[i/n]*n];
|
||||
|
|
|
@ -1116,7 +1116,7 @@ void permute_matrix(double *a, int n, int *indx) {
|
|||
double *b;
|
||||
int nn=n*n;
|
||||
register int i;
|
||||
b = calloc(nn,sizeof(double));
|
||||
b = swzCalloc(nn,sizeof(double));
|
||||
memcpy(b, a, nn*sizeof(double));
|
||||
for (i=0; i<nn; i++, a++)
|
||||
*a = b[indx[i%n]+indx[i/n]*n];
|
||||
|
|
|
@ -5620,7 +5620,7 @@ void permute_matrix(double *a, int n, int *indx) {
|
|||
double *b;
|
||||
int nn=n*n;
|
||||
register int i;
|
||||
b = calloc(nn,sizeof(double));
|
||||
b = swzCalloc(nn,sizeof(double));
|
||||
memcpy(b, a, nn*sizeof(double));
|
||||
for (i=0; i<nn; i++, a++)
|
||||
*a = b[indx[i%n]+indx[i/n]*n];
|
||||
|
|
|
@ -73,7 +73,7 @@ void *m_alloc(size_t size) {
|
|||
/* //+ ansi-c*/
|
||||
void *c_alloc(size_t elt_count, size_t elt_size) {
|
||||
void *new_mem;
|
||||
if ( (new_mem = calloc(elt_count, elt_size)) == NULL ) fn_DisplayError("Out of Memory!");
|
||||
if ( (new_mem = swzCalloc(elt_count, elt_size)) == NULL ) fn_DisplayError("Out of Memory!");
|
||||
return(new_mem);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue