std_alloc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /* std_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. This is a counting version of st_alloc.
  10. */
  11. #if __STDC__
  12. #include <stdlib.h>
  13. #else
  14. extern char *malloc();
  15. #endif
  16. #include "alloc.h"
  17. char *
  18. std_alloc(phead, size, count, pcnt)
  19. char **phead;
  20. register unsigned int size;
  21. int *pcnt;
  22. {
  23. register char *p;
  24. register long *q;
  25. char *retval;
  26. if (*phead == 0) {
  27. while (count >= 1 && (p = malloc(size * count)) == 0) {
  28. count >>= 1;
  29. }
  30. if (p == 0) {
  31. No_Mem();
  32. }
  33. *pcnt += count;
  34. ((_PALLOC_) p)->_A_next = 0;
  35. while (--count) {
  36. p += size;
  37. ((_PALLOC_) p)->_A_next = (_PALLOC_) (p - size);
  38. }
  39. *phead = p;
  40. }
  41. else p = *phead;
  42. *phead = (char *) (((_PALLOC_) p)->_A_next);
  43. retval = p;
  44. q = (long *) p;
  45. while (size >= 8*sizeof(long)) {
  46. *q++ = 0;
  47. *q++ = 0;
  48. *q++ = 0;
  49. *q++ = 0;
  50. *q++ = 0;
  51. *q++ = 0;
  52. *q++ = 0;
  53. *q++ = 0;
  54. size -= 8*sizeof(long);
  55. }
  56. while (size >= sizeof(long)) {
  57. *q++ = 0;
  58. size -= sizeof(long);
  59. }
  60. p = (char *) q;
  61. while (size--) *p++ = 0;
  62. return retval;
  63. }