summary refs log tree commit diff
path: root/malloc/tst-obstack.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2000-10-24 08:23:06 +0000
committerUlrich Drepper <drepper@redhat.com>2000-10-24 08:23:06 +0000
commit84832e0528058662f1294e40dfd506ea0133487b (patch)
tree7bd3fc36b2df6f09bccdeaf2b3856a218c20a154 /malloc/tst-obstack.c
parent42c4f32a44c76d1f4e2b744bf80f495dc36caa87 (diff)
downloadglibc-84832e0528058662f1294e40dfd506ea0133487b.tar.gz
glibc-84832e0528058662f1294e40dfd506ea0133487b.tar.xz
glibc-84832e0528058662f1294e40dfd506ea0133487b.zip
Update.
	* malloc/obstack.c (_obstack_newchunk): Correctly align first returned
	block.
	* malloc/tst-obstack.c: New file.
	Patch and test case by Alexandre Duret-Lutz <duret_g@epita.fr>.
Diffstat (limited to 'malloc/tst-obstack.c')
-rw-r--r--malloc/tst-obstack.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/malloc/tst-obstack.c b/malloc/tst-obstack.c
new file mode 100644
index 0000000000..bd00a0ab7b
--- /dev/null
+++ b/malloc/tst-obstack.c
@@ -0,0 +1,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;
+}