tst-obstack.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Test case by Alexandre Duret-Lutz <duret_g@epita.fr>. */
  2. #include <obstack.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define obstack_chunk_alloc verbose_malloc
  7. #define obstack_chunk_free verbose_free
  8. #define ALIGN_BOUNDARY 64
  9. #define ALIGN_MASK (ALIGN_BOUNDARY - 1)
  10. #define OBJECT_SIZE 1000
  11. static void *
  12. verbose_malloc (size_t size)
  13. {
  14. void *buf = malloc (size);
  15. printf ("malloc (%zu) => %p\n", size, buf);
  16. return buf;
  17. }
  18. static void
  19. verbose_free (void *buf)
  20. {
  21. free (buf);
  22. printf ("free (%p)\n", buf);
  23. }
  24. int
  25. main (void)
  26. {
  27. int result = 0;
  28. int align = 2;
  29. while (align <= 64)
  30. {
  31. struct obstack obs;
  32. int i;
  33. int align_mask = align - 1;
  34. printf ("\n Alignment mask: %d\n", align_mask);
  35. obstack_init (&obs);
  36. obstack_alignment_mask (&obs) = align_mask;
  37. /* finish an empty object to take alignment into account */
  38. obstack_finish (&obs);
  39. /* let's allocate some objects and print their addresses */
  40. for (i = 15; i > 0; --i)
  41. {
  42. void *obj = obstack_alloc (&obs, OBJECT_SIZE);
  43. printf ("obstack_alloc (%u) => %p \t%s\n", OBJECT_SIZE, obj,
  44. ((uintptr_t) obj & align_mask) ? "(not aligned)" : "");
  45. result |= ((uintptr_t) obj & align_mask) != 0;
  46. }
  47. /* clean up */
  48. obstack_free (&obs, 0);
  49. align <<= 1;
  50. }
  51. return result;
  52. }