st_alloc.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* st_alloc - get a structure from a free list. If no structures left,
  7. create new ones.
  8. The counterpart, st_free, is a macro, defined in alloc.h
  9. */
  10. #if __STDC__
  11. #include <stdlib.h>
  12. #else
  13. extern char *malloc();
  14. #endif
  15. #include "alloc.h"
  16. char *
  17. st_alloc(phead, size, count)
  18. char **phead;
  19. register unsigned int size;
  20. {
  21. register char *p;
  22. register long *q;
  23. char *retval;
  24. if (*phead == 0) {
  25. while (count >= 1 && (p = malloc(size * count)) == 0) {
  26. count >>= 1;
  27. }
  28. if (p == 0) {
  29. No_Mem();
  30. }
  31. ((_PALLOC_) p)->_A_next = 0;
  32. while (--count) {
  33. p += size;
  34. ((_PALLOC_) p)->_A_next = (_PALLOC_) (p - size);
  35. }
  36. *phead = p;
  37. }
  38. else p = *phead;
  39. *phead = (char *) (((_PALLOC_)p)->_A_next);
  40. retval = p;
  41. q = (long *) p;
  42. while (size >= 8*sizeof(long)) {
  43. *q++ = 0;
  44. *q++ = 0;
  45. *q++ = 0;
  46. *q++ = 0;
  47. *q++ = 0;
  48. *q++ = 0;
  49. *q++ = 0;
  50. *q++ = 0;
  51. size -= 8*sizeof(long);
  52. }
  53. while (size >= sizeof(long)) {
  54. *q++ = 0;
  55. size -= sizeof(long);
  56. }
  57. p = (char *) q;
  58. while (size--) *p++ = 0;
  59. return retval;
  60. }