Back to index

glibc  2.9
tst-obstack.c
Go to the documentation of this file.
00001 /* Test case by Alexandre Duret-Lutz <duret_g@epita.fr>.  */
00002 #include <obstack.h>
00003 #include <stdint.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 
00007 #define obstack_chunk_alloc verbose_malloc
00008 #define obstack_chunk_free verbose_free
00009 #define ALIGN_BOUNDARY 64
00010 #define ALIGN_MASK (ALIGN_BOUNDARY - 1)
00011 #define OBJECT_SIZE 1000
00012 
00013 static void *
00014 verbose_malloc (size_t size)
00015 {
00016   void *buf = malloc (size);
00017   printf ("malloc (%zu) => %p\n", size, buf);
00018   return buf;
00019 }
00020 
00021 static void
00022 verbose_free (void *buf)
00023 {
00024   free (buf);
00025   printf ("free (%p)\n", buf);
00026 }
00027 
00028 int
00029 main (void)
00030 {
00031   int result = 0;
00032   int align = 2;
00033 
00034   while (align <= 64)
00035     {
00036       struct obstack obs;
00037       int i;
00038       int align_mask = align - 1;
00039 
00040       printf ("\n Alignment mask: %d\n", align_mask);
00041 
00042       obstack_init (&obs);
00043       obstack_alignment_mask (&obs) = align_mask;
00044       /* finish an empty object to take alignment into account */
00045       obstack_finish (&obs);
00046 
00047       /* let's allocate some objects and print their addresses */
00048       for (i = 15; i > 0; --i)
00049        {
00050          void *obj = obstack_alloc (&obs, OBJECT_SIZE);
00051 
00052          printf ("obstack_alloc (%u) => %p \t%s\n", OBJECT_SIZE, obj,
00053                 ((uintptr_t) obj & align_mask) ? "(not aligned)" : "");
00054          result |= ((uintptr_t) obj & align_mask) != 0;
00055        }
00056 
00057       /* clean up */
00058       obstack_free (&obs, 0);
00059 
00060       align <<= 1;
00061     }
00062 
00063   return result;
00064 }