home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / malloc.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  4KB  |  166 lines

  1. /****************************************************************
  2. Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #ifndef CRAY
  25. #define STACKMIN 512
  26. #define MINBLK (2*sizeof(struct mem) + 16)
  27. #define MSTUFF _malloc_stuff_
  28. #define F MSTUFF.free
  29. #define B MSTUFF.busy
  30. #define SBGULP 8192
  31. #include "string.h"    /* for memcpy */
  32.  
  33. #ifdef KR_headers
  34. #define Char char
  35. #define Unsigned unsigned
  36. #define Int /*int*/
  37. #else
  38. #define Char void
  39. #define Unsigned size_t
  40. #define Int int
  41. #endif
  42.  
  43. struct mem {
  44.     struct mem *next;
  45.     Unsigned len;
  46.     };
  47.  
  48. struct {
  49.     struct mem *free;
  50.     Char *busy;
  51.     } MSTUFF;
  52.  
  53.  Char *
  54. #ifdef KR_headers
  55. malloc(size)
  56.     register Unsigned size;
  57. #else
  58. malloc(register Unsigned size)
  59. #endif
  60. {
  61.     register struct mem *p, *q, *r, *s;
  62.     unsigned register k, m;
  63.     extern Char *sbrk(Int);
  64.     char *top, *top1;
  65.  
  66.     size = (size+7) & ~7;
  67.     r = (struct mem *) &F;
  68.     for (p = F, q = 0; p; r = p, p = p->next) {
  69.         if ((k = p->len) >= size && (!q || m > k)) { m = k; q = p; s = r; }
  70.         }
  71.     if (q) {
  72.         if (q->len - size >= MINBLK) { /* split block */
  73.             p = (struct mem *) (((char *) (q+1)) + size);
  74.             p->next = q->next;
  75.             p->len = q->len - size - sizeof(struct mem);
  76.             s->next = p;
  77.             q->len = size;
  78.             }
  79.         else s->next = q->next;
  80.         }
  81.     else {
  82.         top = B ? B : (Char *)(((long)sbrk(0) + 7) & ~7);
  83.         if (F && (char *)(F+1) + F->len == B)
  84.             { q = F; F = F->next; }
  85.         else q = (struct mem *) top;
  86.         top1 = (char *)(q+1) + size;
  87.         if (top1 > top) {
  88.             if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)
  89.                 return 0;
  90.             r = (struct mem *)top1;
  91.             r->len = SBGULP - sizeof(struct mem);
  92.             r->next = F;
  93.             F = r;
  94.             top1 += SBGULP;
  95.             }
  96.         q->len = size;
  97.         B = top1;
  98.         }
  99.     return (Char *) (q+1);
  100.     }
  101.  
  102.  void
  103. #ifdef KR_headers
  104. free(f)
  105.     Char *f;
  106. #else
  107. free(Char *f)
  108. #endif
  109. {
  110.     struct mem *p, *q, *r;
  111.     char *pn, *qn;
  112.  
  113.     if (!f) return;
  114.     q = (struct mem *) ((char *)f - sizeof(struct mem));
  115.     qn = (char *)f + q->len;
  116.     for (p = F, r = (struct mem *) &F; ; r = p, p = p->next) {
  117.         if (qn == (Char *) p) {
  118.             q->len += p->len + sizeof(struct mem);
  119.             p = p->next;
  120.             }
  121.         pn = p ? ((char *) (p+1)) + p->len : 0;
  122.         if (pn == (Char *) q) {
  123.             p->len += sizeof(struct mem) + q->len;
  124.             q->len = 0;
  125.             q->next = p;
  126.             r->next = p;
  127.             break;
  128.             }
  129.         if (pn < (char *) q) {
  130.             r->next = q;
  131.             q->next = p;
  132.             break;
  133.             }
  134.         }
  135.     }
  136.  
  137.  Char *
  138. #ifdef KR_headers
  139. realloc(f, size)
  140.     Char *f;
  141.     Unsigned size;
  142. #else
  143. realloc(Char *f, Unsigned size)
  144. #endif
  145. {
  146.     struct mem *p;
  147.     Char *q, *f1;
  148.     Unsigned s1;
  149.  
  150.     if (!f) return malloc(size);
  151.     p = (struct mem *) ((char *)f - sizeof(struct mem));
  152.     s1 = p->len;
  153.     free(f);
  154.     if (s1 > size) s1 = size + 7 & ~7;
  155.     if (!p->len) {
  156.         f1 = (Char *)(p->next + 1);
  157.         memcpy(f1, f, s1);
  158.         f = f1;
  159.         }
  160.     q = malloc(size);
  161.     if (q && q != f)
  162.         memcpy(q, f, s1);
  163.     return q;
  164.     }
  165. #endif
  166.