storage.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* $Header$ */
  2. /* S T R U C T U R E - S T O R A G E M A N A G E M E N T */
  3. /* Assume that each structure contains a field "next", of pointer
  4. type, as first tagfield.
  5. struct xxx serves as a general structure: it just declares the
  6. tagfield "next" as first field of a structure.
  7. Please don't worry about any warnings when compiling this file
  8. because some dirty tricks are performed to obtain the necessary
  9. actions.
  10. */
  11. #include "debug.h" /* UF */
  12. #include "botch_free.h" /* UF */
  13. #include "assert.h"
  14. #include "alloc.h"
  15. #include "storage.h"
  16. struct xxx {
  17. char *next;
  18. };
  19. char *
  20. head_alloc(phead, size)
  21. char **phead;
  22. int size;
  23. {
  24. struct xxx *tmp;
  25. if (*phead == 0) {
  26. return Malloc(size);
  27. }
  28. tmp = (struct xxx *) (*phead);
  29. *phead = (char *) tmp->next;
  30. return (char *) tmp;
  31. }
  32. /* instead of Calloc: */
  33. clear(ptr, n)
  34. char *ptr;
  35. int n;
  36. {
  37. ASSERT((long)ptr % sizeof (long) == 0);
  38. while (n >= sizeof (long)) { /* high-speed clear loop */
  39. *(long *)ptr = 0L;
  40. ptr += sizeof (long), n -= sizeof (long);
  41. }
  42. while (n--)
  43. *ptr++ = '\0';
  44. }
  45. #ifdef BOTCH_FREE
  46. botch(ptr, n)
  47. char *ptr;
  48. int n;
  49. { /* Writes garbage over n chars starting from ptr.
  50. Used to check if freed memory is used inappropriately.
  51. */
  52. ASSERT((long)ptr % sizeof (long) == 0);
  53. while (n >= sizeof (long)) { /* high-speed botch loop */
  54. *(long *)ptr = 025252525252L;
  55. ptr += sizeof (long), n -= sizeof (long);
  56. }
  57. while (n--)
  58. *ptr++ = '\252';
  59. }
  60. #endif BOTCH_FREE