xref: /original-bsd/usr.bin/window/wwalloc.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)wwalloc.c	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 
17 char **
18 wwalloc(row, col, nrow, ncol, size)
19 {
20 	register char *p, **pp;
21 	register int i;
22 
23 	/* fast, call malloc only once */
24 	pp = (char **)
25 		malloc((unsigned) sizeof (char **) * nrow + size * nrow * ncol);
26 	if (pp == 0) {
27 		wwerrno = WWE_NOMEM;
28 		return 0;
29 	}
30 	p = (char *)&pp[nrow];
31 	col *= size;
32 	size /= sizeof (char);		/* paranoid */
33 	size *= ncol;
34 	for (i = 0; i < nrow; i++) {
35 		pp[i] = p - col;
36 		p += size;
37 	}
38 	return pp - row;
39 }
40 
41 wwfree(p, row)
42 register char **p;
43 {
44 	free((char *)(p + row));
45 }
46