about summary refs log tree commit diff
path: root/malloc/tst-obstack.c
blob: bd00a0ab7b2ed5e98a6de56f2a2f387e066c9911 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Test case by Alexandre Duret-Lutz <duret_g@epita.fr>.  */
#include <obstack.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define obstack_chunk_alloc verbose_malloc
#define obstack_chunk_free verbose_free
#define ALIGN_BOUNDARY 64
#define ALIGN_MASK (ALIGN_BOUNDARY - 1)
#define OBJECT_SIZE 1000

static void *
verbose_malloc (size_t size)
{
  void *buf = malloc (size);
  printf ("malloc (%u) => %p\n", size, buf);
  return buf;
}

static void
verbose_free (void *buf)
{
  free (buf);
  printf ("free (%p)\n", buf);
}

int
main (void)
{
  struct obstack obs;
  int i;
  int result = 0;

  obstack_init (&obs);
  obstack_alignment_mask (&obs) = ALIGN_MASK;
  /* finish an empty object to take alignment into account */
  obstack_finish (&obs);

  /* let's allocate some objects and print their addresses */
  for (i = 15; i > 0; --i)
    {
      void *obj = obstack_alloc (&obs, OBJECT_SIZE);

      printf ("obstack_alloc (%u) => %p \t%s\n", OBJECT_SIZE, obj,
	      ((uintptr_t) obj & ALIGN_MASK) ? "(not aligned)" : "");
      result |= ((uintptr_t) obj & ALIGN_MASK) != 0;
  }

  /* clean up */
  obstack_free (&obs, 0);

  return result;
}