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