home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / tooltool2.1c / part01 / emalloc.c next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  2.5 KB  |  83 lines

  1. /************************************************************************/
  2. /*    Copyright 1988 by Chuck Musciano and Harris Corporation        */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.        */
  15. /*                                    */
  16. /*    The sale of any product based wholely or in part upon the     */
  17. /*    technology provided by tooltool is strictly forbidden without    */
  18. /*    specific, prior written permission from Harris Corporation.    */
  19. /*    Tooltool technology includes, but is not limited to, the source    */
  20. /*    code, executable binary files, specification language, and    */
  21. /*    sample specification files.                    */
  22. /************************************************************************/
  23.  
  24. #include    "tooltool.h"
  25.  
  26. #define        MIN_BUF_SIZE    32768
  27.  
  28. typedef    struct    em_rec    em_data, *em_ptr;
  29.  
  30. struct    em_rec    {int    size;
  31.          int    remaining;
  32.          char    *buf;
  33.          em_ptr    next;
  34.         };
  35.  
  36. PRIVATE    em_ptr    buf_list = NULL;
  37.  
  38. /************************************************************************/
  39. EXPORT    char    *tt_emalloc(size)
  40.  
  41. int    size;
  42.  
  43. {    em_ptr    em;
  44.     char    *p;
  45.  
  46.     size = ((size + 3) & ~3);
  47.     for (em = buf_list; em; em = em->next)
  48.        if (em->remaining >= size)
  49.           break;
  50.     if (em == NULL) {
  51.        em = (em_ptr) safe_malloc(sizeof(em_data));
  52.        em->size = em->remaining = max(size, MIN_BUF_SIZE);
  53.        em->buf = (char *) safe_malloc(em->size);
  54.        em->next = buf_list;
  55.        buf_list = em;
  56.        }
  57.     p = em->buf + em->size - em->remaining;
  58.     em->remaining -= size;
  59.     return(p);
  60. }
  61.  
  62. /************************************************************************/
  63. EXPORT    tt_reset_emalloc()
  64.  
  65. {    em_ptr    em;
  66.  
  67.     for (em = buf_list; em; em = em->next)
  68.        em->remaining = em->size;
  69. }
  70.  
  71. /************************************************************************/
  72. EXPORT    int    tt_is_temp(p)
  73.  
  74. char    *p;
  75.  
  76. {    em_ptr    em;
  77.  
  78.     for (em = buf_list; em; em = em->next)
  79.        if (p >= em->buf && p <= em->buf + em->size)
  80.           return(TRUE);
  81.     return(FALSE);
  82. }
  83.