home *** CD-ROM | disk | FTP | other *** search
- /* zalloc.c - memory management
- #
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- This software is copyright (C) by the Lawrence Berkeley Laboratory.
- Permission is granted to reproduce this software for non-commercial
- purposes provided that this notice is left intact.
-
- It is acknowledged that the U.S. Government has rights to this software
- under Contract DE-AC03-765F00098 between the U.S. Department of Energy
- and the University of California.
-
- This software is provided as a professional and academic contribution
- for joint exchange. Thus, it is experimental, and is provided ``as is'',
- with no warranties of any kind whatsoever, no support, no promise of
- updates, or printed documentation. By using this software, you
- acknowledge that the Lawrence Berkeley Laboratory and Regents of the
- University of California shall have no liability with respect to the
- infringement of other copyrights by any part of this software.
-
- For further information about this notice, contact William Johnston,
- Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- (wejohnston@lbl.gov)
-
- For further information about this software, contact:
- Jin Guojun
- Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- g_jin@lbl.gov
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Author: Jin Guojun - LBL 1/1/91
- */
-
- #include "stdef.h"
-
-
- char* inst = "???";
- int debug;
-
- #ifdef TC_Need
- # include <alloc.h>
- #else
- # ifndef coreleft
- # define coreleft() (MType)-1
- # endif
- #endif
-
-
- VType*
- core_trace(m, p, i, j, ms)
- MType m, i, j;
- char *p, *ms;
- {
- char *s;
- if (!ms
- #ifdef sparc /* cc doesn't know how to make a call */
- || (int)ms < 11900 || (int)ms > 1750000
- #endif
- ) s = inst;
- else s = ms;
-
- DEBUG2MESSAGE("%-lu core [alloc at %10lu] - %-8lu(%lu * %lu) = %lu for %s\n",
- m<0 ? (MType)&m : m, p, i*j, i, j, coreleft(), s);
- if (!p) prgmerr(ms, "no enough core available for %s -> %lxH x %lxH = %lu",
- s, i, j, i*j);
- return p;
- }
-
- #define MemDef() (i, j, ms) register MType i, j; char* ms;
-
- VType*
- zalloc MemDef()
- {
- return ZALLOC(i, j, ms);
- }
-
- VType*
- nzalloc MemDef()
- {
- return NZALLOC(i, j, ms);
- }
-
- VType**
- alloc_2d_discrete(cols, rows, size)
- {
- register int i;
- register VType **p;
- p = NZALLOC(rows, sizeof(*p), "2d-rows");
- for (i=0; i<rows; i++)
- p[i] = NZALLOC(cols, size, "2d-cols");
- return p;
- }
-
- void
- free_2d_discrete(p, rows)
- VType **p;
- register int rows;
- {
- while (rows--)
- free(p[rows]);
- free(p);
- }
-
- p_buffer_size(p) /* return the block size allocated for pointer *p */
- VType *p;
- {
- return pointer_buffer_size(p);
- }
-
- /*
- % For IBM/PC, it returns the last pointer position.
- % For other machine, it may be used for block link.
- */
- last_pointer_pos(p)
- VType *p;
- {
- return *((int *)p - 1);
- }
-
- VType*
- verify_buffer_size(pp, bsize, bytes, name)
- VType **pp;
- char *name;
- {
- register int b = abs(bsize), vsize = b * bytes;
- if (*pp && pointer_buffer_size(*pp) < vsize)
- if (bsize > 0)
- free(*pp), *pp = NULL;
- else return *pp = (VType*)realloc(*pp, vsize);
- if (*pp) return NULL;
- return *pp = NZALLOC(b, bytes, name);
- }
-