talloc: test talloc_steal out of a talloc_pool
authorStefan Metzmacher <metze@samba.org>
Mon, 16 May 2011 17:25:47 +0000 (19:25 +0200)
committerStefan Metzmacher <metze@samba.org>
Tue, 17 May 2011 07:43:01 +0000 (09:43 +0200)
metze

Autobuild-User: Stefan Metzmacher <metze@samba.org>
Autobuild-Date: Tue May 17 09:43:01 CEST 2011 on sn-devel-104

lib/talloc/testsuite.c

index 14085454f4e5d5779e4f3aefcebf992f782792bf..90417c6ade5e7b75a74fc911744f520fa58ff9ab 100644 (file)
@@ -1208,6 +1208,73 @@ static bool test_pool(void)
        return true;
 }
 
+static bool test_pool_steal(void)
+{
+       void *root;
+       void *pool;
+       void *p1, *p2;
+       void *p1_2, *p2_2;
+       size_t hdr;
+       size_t ofs1, ofs2;
+
+       root = talloc_new(NULL);
+       pool = talloc_pool(root, 1024);
+
+       p1 = talloc_size(pool, 4 * 16);
+       torture_assert("pool allocate 4 * 16", p1 != NULL, "failed ");
+       memset(p1, 0x11, talloc_get_size(p1));
+       p2 = talloc_size(pool, 4 * 16);
+       torture_assert("pool allocate 4 * 16", p2 > p1, "failed: !(p2 > p1) ");
+       memset(p2, 0x11, talloc_get_size(p2));
+
+       ofs1 = PTR_DIFF(p2, p1);
+       hdr = ofs1 - talloc_get_size(p1);
+
+       talloc_steal(root, p1);
+       talloc_steal(root, p2);
+
+       talloc_free(pool);
+
+       p1_2 = p1;
+
+#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
+       p1_2 = talloc_realloc_size(root, p1, 5 * 16);
+       torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
+       memset(p1_2, 0x11, talloc_get_size(p1_2));
+       ofs1 = PTR_DIFF(p1_2, p2);
+       ofs2 = talloc_get_size(p2) + hdr;
+
+       torture_assert("pool realloc ", ofs1 == ofs2, "failed: pointer offset unexpected");
+
+       p2_2 = talloc_realloc_size(root, p2, 3 * 16);
+       torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
+       memset(p2_2, 0x11, talloc_get_size(p2_2));
+#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
+
+       talloc_free(p1_2);
+
+       p2_2 = p2;
+
+#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
+       /* now we should reclaim the full pool */
+       p2_2 = talloc_realloc_size(root, p2, 8 * 16);
+       torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
+       p2 = p2_2;
+       memset(p2_2, 0x11, talloc_get_size(p2_2));
+
+       /* now we malloc and free the full pool space */
+       p2_2 = talloc_realloc_size(root, p2, 2 * 1024);
+       torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
+       memset(p2_2, 0x11, talloc_get_size(p2_2));
+
+#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
+
+       talloc_free(p2_2);
+
+       talloc_free(root);
+
+       return true;
+}
 
 static bool test_free_ref_null_context(void)
 {
@@ -1307,6 +1374,8 @@ bool torture_local_talloc(struct torture_context *tctx)
        test_reset();
        ret &= test_pool();
        test_reset();
+       ret &= test_pool_steal();
+       test_reset();
        ret &= test_free_ref_null_context();
        test_reset();
        ret &= test_rusty();