summaryrefslogtreecommitdiffstats
path: root/test/malloc/tst-obstack.c
diff options
context:
space:
mode:
author"Steven J. Hill" <sjhill@realitydiluted.com>2006-08-22 01:56:31 +0000
committer"Steven J. Hill" <sjhill@realitydiluted.com>2006-08-22 01:56:31 +0000
commitc969ef4b8fc1d06c13203b36f8cf5bb61a7730f0 (patch)
treeeb2da173a5661b2b2e615045f26f7b69e774290d /test/malloc/tst-obstack.c
parentfea84e591f94b025ef7c2da843ae80b809f93dbe (diff)
downloaduClibc-alpine-c969ef4b8fc1d06c13203b36f8cf5bb61a7730f0.tar.bz2
uClibc-alpine-c969ef4b8fc1d06c13203b36f8cf5bb61a7730f0.tar.xz
Merge from trunk. Whoa crap.
Diffstat (limited to 'test/malloc/tst-obstack.c')
-rw-r--r--test/malloc/tst-obstack.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/malloc/tst-obstack.c b/test/malloc/tst-obstack.c
new file mode 100644
index 000000000..769697f18
--- /dev/null
+++ b/test/malloc/tst-obstack.c
@@ -0,0 +1,64 @@
+/* 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 (%zu) => %p\n", size, buf);
+ return buf;
+}
+
+static void
+verbose_free (void *buf)
+{
+ free (buf);
+ printf ("free (%p)\n", buf);
+}
+
+int
+main (void)
+{
+ int result = 0;
+ int align = 2;
+
+ while (align <= 64)
+ {
+ struct obstack obs;
+ int i;
+ int align_mask = align - 1;
+
+ printf ("\n Alignment mask: %d\n", align_mask);
+
+ 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);
+
+ align <<= 1;
+ }
+
+ return result;
+}