home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume20
/
xxalloc
/
nx.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-16
|
6KB
|
321 lines
/*xxalloc.c**************************************************************/
/* */
/* xxalloc */
/* */
/* Placed in the public domain */
/* by Harold G. Walters 1988 */
/* */
/* Bug fixes, ect to walters@ce.okstate.edu until May 1989 */
/* */
/* This software is provided as is. No claims are made for */
/* this software regarding its fitness and/or correctness for */
/* any purpose. Use of this software is at the user's own risk. */
/* */
/************************************************************************/
#include <stdio.h>
extern char *calloc();
extern void free();
#define STRLEN 132
static char msg[STRLEN];
static long amount = 0L;
extern void xx_error();
char *xx_alloc(n)
unsigned n;
{
char *m;
amount += n;
if ((m = calloc(1, n)) == (char *) NULL)
{
sprintf(msg, "xx_alloc: memory allocation error at %ld bytes\n",
amount);
xx_error(msg);
return((char *) NULL);
}
return((char *) m);
}
void xx_free(p, n)
char *p;
unsigned n;
{
free(p);
amount -= n;
return;
}
long xx_amount()
{
return(amount);
}
/* end index >= begin index */
static void index_test(b, e)
int b, e;
{
if (e < b)
{
sprintf(msg, "index_test: end index %d < begin index %d\n",
e, b);
xx_error(msg);
/* exit here even if xx_error() returns */
/* this is a programming error if this ever happens */
exit(2);
}
return;
}
char *n1_alloc(ib, ie, n)
int ib, ie, n;
{
char *m;
index_test(ib, ie);
m = (char *) xx_alloc((unsigned) (ie - ib + 1) * n);
if (m == (char *) NULL) return((char *) NULL);
m -= ib * n;
return((char *) m);
}
char *n1_renum(m, ib, ie, nib, nie, n)
char *m;
int ib, ie, nib, nie, n;
{
index_test(ib, ie);
index_test(nib, nie);
m += (ib - nib) * n;
return((char *) m);
}
void n1_free(m, ib, ie, n)
char *m;
int ib, ie, n;
{
index_test(ib, ie);
xx_free((char*) (m + ib * n), (unsigned) ((ie - ib + 1) * n));
return;
}
void n1_init(m, ib, ie, n, nini)
char *m;
int ib, ie, n;
void (*nini)();
{
int i;
index_test(ib, ie);
if (nini == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
(*nini)(&m[i*n]);
return;
}
void n1_prnt(fp, nprn, m, ib, ie, n)
FILE *fp;
void (*nprn)();
char *m;
int ib, ie, n;
{
int i;
index_test(ib, ie);
if (nprn == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
(*nprn)(fp, &m[i*n]);
return;
}
char **n2_alloc(ib, ie, jb, je, n)
int ib, ie, jb, je, n;
{
int i;
char **m;
index_test(ib, ie);
index_test(jb, je);
m = (char **) xx_alloc((unsigned) (ie - ib + 1) * sizeof(char *));
if (m == (char **) NULL) return((char **) NULL);
m -= ib;
for(i = ib; i <= ie; i++)
{
m[i] = (char *) xx_alloc((unsigned) (je - jb + 1) * n);
if (m[i] == (char *) NULL) return((char **) NULL);
m[i] -= jb * n;
}
return((char **) m);
}
char **n2_renum(m, ib, ie, jb, je, nib, nie, njb, nje, n)
char **m;
int ib, ie, jb, je, nib, nie, njb, nje, n;
{
int i;
index_test(ib, ie);
index_test(jb, je);
index_test(nib, nie);
index_test(njb, nje);
for(i = ie; i >= ib; i--)
m[i] += (jb - njb) * n;
m += (ib - nib);
return((char **) m);
}
void n2_free(m, ib, ie, jb, je, n)
char **m;
int ib, ie, jb, je, n;
{
int i;
index_test(ib, ie);
index_test(jb, je);
for(i = ie; i >= ib; i--)
xx_free((char*) (m[i] + jb * n),
(unsigned) ((je - jb + 1) * n));
xx_free((char*) (m + ib), (unsigned) ((ie - ib + 1) * sizeof(char *)));
return;
}
void n2_init(m, ib, ie, jb, je, n, nini)
char **m;
int ib, ie, jb, je, n;
void (*nini)();
{
int i, j;
index_test(ib, ie);
index_test(jb, je);
if (nini == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
for (j = jb; j <= je; j++)
(*nini)(&m[i][j*n]);
return;
}
void n2_prnt(fp, nprn, m, ib, ie, jb, je, n)
FILE *fp;
void (*nprn)();
char **m;
int ib, ie, jb, je, n;
{
int i, j;
index_test(ib, ie);
index_test(jb, je);
if (nprn == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
for (j = jb; j <= je; j++)
(*nprn)(fp, &m[i][j*n]);
return;
}
char ***n3_alloc(ib, ie, jb, je, kb, ke, n)
int ib, ie, jb, je, kb, ke, n;
{
int i, j;
char ***m;
index_test(ib, ie);
index_test(jb, je);
index_test(kb, ke);
m = (char ***) xx_alloc((unsigned) (ie - ib + 1) * sizeof(char **));
if (m == (char ***) NULL) return((char ***) NULL);
m -= ib;
for(i = ib; i <= ie; i++)
{
m[i] = (char **) xx_alloc((unsigned) (je - jb + 1) *
sizeof(char *));
if (m[i] == (char **) NULL) return((char ***) NULL);
m[i] -= jb;
for(j = jb; j <= je; j++)
{
m[i][j] = (char *) xx_alloc((unsigned) (ke - kb + 1)
* n);
if (m[i][j] == (char *) NULL) return((char ***) NULL);
m[i][j] -= kb * n;
}
}
return((char ***) m);
}
char ***n3_renum(m, ib, ie, jb, je, kb, ke, nib, nie, njb, nje, nkb, nke, n)
char ***m;
int ib, ie, jb, je, kb, ke, nib, nie, njb, nje, nkb, nke, n;
{
int i, j;
index_test(ib, ie);
index_test(jb, je);
index_test(kb, ke);
index_test(nib, nie);
index_test(njb, nje);
index_test(nkb, nke);
for(i = ie; i >= ib; i--)
for (j = je; j >= jb; j--)
m[i][j] += (kb - nkb) * n;
for(i = ie; i >= ib; i--)
m[i] += (jb - njb);
m += (ib - nib);
return((char ***) m);
}
void n3_free(m, ib, ie, jb, je, kb, ke, n)
char ***m;
int ib, ie, jb, je, kb, ke, n;
{
int i, j;
index_test(ib, ie);
index_test(jb, je);
index_test(kb, ke);
for(i = ie; i >= ib; i--)
for (j = je; j >= jb; j--)
xx_free((char*) (m[i][j] + kb * n),
(unsigned) (ke - kb + 1) * n);
for(i = ie; i >= ib; i--)
xx_free((char*) (m[i] + jb),
(unsigned) ((je - jb + 1) * sizeof(char *)));
xx_free((char*) (m + ib),
(unsigned) ((ie - ib + 1) * sizeof(char **)));
return;
}
void n3_init(m, ib, ie, jb, je, kb, ke, n, nini)
char ***m;
int ib, ie, jb, je, kb, ke, n;
void (*nini)();
{
int i, j, k;
index_test(ib, ie);
index_test(jb, je);
index_test(kb, ke);
if (nini == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
for (j = jb; j <= je; j++)
for (k = kb; k <= ke; k++)
(*nini)(&m[i][j][k*n]);
return;
}
void n3_prnt(fp, nprn, m, ib, ie, jb, je, kb, ke, n)
FILE *fp;
void (*nprn)();
char ***m;
int ib, ie, jb, je, kb, ke, n;
{
int i, j, k;
index_test(ib, ie);
index_test(jb, je);
index_test(kb, ke);
if (nprn == (void (*)()) NULL)
return;
for (i = ib; i <= ie; i++)
{
fprintf(fp, "%d\n", i);
for (j = jb; j <= je; j++)
for (k = kb; k <= ke; k++)
(*nprn)(fp, &m[i][j][k*n]);
}
return;
}