home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / lib / zalloc.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-08  |  3.0 KB  |  134 lines

  1. /*    zalloc.c - memory management
  2. #
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.     g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. % Author:    Jin Guojun - LBL    1/1/91
  33. */
  34.  
  35. #include "stdef.h"
  36.  
  37.  
  38. char*    inst = "???";
  39. int    debug;
  40.  
  41. #ifdef    TC_Need
  42. # include    <alloc.h>
  43. #else
  44. # ifndef    coreleft
  45. #    define    coreleft()    (MType)-1
  46. # endif
  47. #endif
  48.  
  49.  
  50. VType*
  51. core_trace(m, p, i, j, ms)
  52. MType    m, i, j;
  53. char    *p, *ms;
  54. {
  55. char    *s;
  56. if (!ms
  57. #ifdef    sparc    /*    cc doesn't know how to make a call    */
  58.     || (int)ms < 11900 || (int)ms > 1750000
  59. #endif
  60. )    s = inst;
  61. else    s = ms;
  62.  
  63. DEBUG2MESSAGE("%-lu core [alloc at %10lu] - %-8lu(%lu * %lu) = %lu for %s\n",
  64.         m<0 ? (MType)&m : m, p, i*j, i, j, coreleft(), s);
  65. if (!p)    prgmerr(ms, "no enough core available for %s -> %lxH x %lxH = %lu",
  66.         s, i, j, i*j);
  67. return    p;
  68. }
  69.  
  70. #define    MemDef()    (i, j, ms)    register MType    i, j;    char*    ms;
  71.  
  72. VType*
  73. zalloc    MemDef()
  74. {
  75.     return    ZALLOC(i, j, ms);
  76. }
  77.  
  78. VType*
  79. nzalloc    MemDef()
  80. {
  81.     return    NZALLOC(i, j, ms);
  82. }
  83.  
  84. VType**
  85. alloc_2d_discrete(cols, rows, size)
  86. {
  87. register int    i;
  88. register VType    **p;
  89.     p = NZALLOC(rows, sizeof(*p), "2d-rows");
  90.     for (i=0; i<rows; i++)
  91.         p[i] = NZALLOC(cols, size, "2d-cols");
  92.     return    p;
  93. }
  94.  
  95. void
  96. free_2d_discrete(p, rows)
  97. VType    **p;
  98. register int    rows;
  99. {
  100.     while (rows--)
  101.         free(p[rows]);
  102.     free(p);
  103. }
  104.  
  105. p_buffer_size(p)    /* return the block size allocated for pointer *p */
  106. VType    *p;
  107. {
  108.     return    pointer_buffer_size(p);
  109. }
  110.  
  111. /*
  112. %    For IBM/PC, it returns the last pointer position.
  113. %    For other machine, it may be used for block link.
  114. */
  115. last_pointer_pos(p)
  116. VType    *p;
  117. {
  118.     return    *((int *)p - 1);
  119. }
  120.  
  121. VType*
  122. verify_buffer_size(pp, bsize, bytes, name)
  123. VType    **pp;
  124. char    *name;
  125. {
  126. register int    b = abs(bsize), vsize = b * bytes;
  127.     if (*pp && pointer_buffer_size(*pp) < vsize)
  128.         if (bsize > 0)
  129.             free(*pp),    *pp = NULL;
  130.         else    return    *pp = (VType*)realloc(*pp, vsize);
  131.     if (*pp)    return    NULL;
  132. return    *pp = NZALLOC(b, bytes, name);
  133. }
  134.