tst-obstack.c 1.3 KB

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