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