📄 talloctort.c
字号:
if (strcmp("food", p2) != 0) { printf("failed: talloc_asprintf_append doesn't work\n"); return False; } CHECK_BLOCKS(p2, 1); CHECK_BLOCKS(p1, 3); p2 = talloc_asprintf_append(NULL, "hello %s", "world"); if (strcmp("hello world", p2) != 0) { printf("failed: talloc_asprintf_append doesn't work\n"); return False; } CHECK_BLOCKS(p2, 1); CHECK_BLOCKS(p1, 3); talloc_free(p2); d = talloc_array(p1, double, 0x20000000); if (d) { printf("failed: integer overflow not detected\n"); return False; } d = talloc_realloc(p1, d, double, 0x20000000); if (d) { printf("failed: integer overflow not detected\n"); return False; } talloc_free(p1); CHECK_BLOCKS(root, 1); p1 = talloc_named(root, 100, "%d bytes", 100); CHECK_BLOCKS(p1, 2); CHECK_BLOCKS(root, 3); talloc_unlink(root, p1); p1 = talloc_init("%d bytes", 200); p2 = talloc_asprintf(p1, "my test '%s'", "string"); CHECK_BLOCKS(p1, 3); CHECK_SIZE(p2, 17); CHECK_BLOCKS(root, 1); talloc_unlink(NULL, p1); p1 = talloc_named_const(root, 10, "p1"); p2 = talloc_named_const(root, 20, "p2"); talloc_reference(p1, p2); talloc_report_full(root, stdout); talloc_unlink(root, p2); talloc_report_full(root, stdout); CHECK_BLOCKS(p2, 1); CHECK_BLOCKS(p1, 2); CHECK_BLOCKS(root, 3); talloc_unlink(p1, p2); talloc_unlink(root, p1); p1 = talloc_named_const(root, 10, "p1"); p2 = talloc_named_const(root, 20, "p2"); talloc_reference(NULL, p2); talloc_report_full(root, stdout); talloc_unlink(root, p2); talloc_report_full(root, stdout); CHECK_BLOCKS(p2, 1); CHECK_BLOCKS(p1, 1); CHECK_BLOCKS(root, 2); talloc_unlink(NULL, p2); talloc_unlink(root, p1); /* Test that talloc_unlink is a no-op */ if (talloc_unlink(root, NULL) != -1) { printf("failed: talloc_unlink(root, NULL) == -1\n"); return False; } talloc_report(root, stdout); talloc_report(NULL, stdout); CHECK_SIZE(root, 0); talloc_free(root); CHECK_SIZE(NULL, 0); talloc_enable_leak_report(); talloc_enable_leak_report_full(); return True;}/* test realloc*/static BOOL test_realloc(void){ void *root, *p1, *p2; printf("TESTING REALLOC\n"); root = talloc_new(NULL); p1 = talloc_size(root, 10); CHECK_SIZE(p1, 10); p1 = talloc_realloc_size(NULL, p1, 20); CHECK_SIZE(p1, 20); talloc_new(p1); p2 = talloc_realloc_size(p1, NULL, 30); talloc_new(p1); p2 = talloc_realloc_size(p1, p2, 40); CHECK_SIZE(p2, 40); CHECK_SIZE(root, 60); CHECK_BLOCKS(p1, 4); p1 = talloc_realloc_size(NULL, p1, 20); CHECK_SIZE(p1, 60); talloc_increase_ref_count(p2); if (talloc_realloc_size(NULL, p2, 5) != NULL) { printf("failed: talloc_realloc() on a referenced pointer should fail\n"); return False; } CHECK_BLOCKS(p1, 4); talloc_realloc_size(NULL, p2, 0); talloc_realloc_size(NULL, p2, 0); CHECK_BLOCKS(p1, 3); if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) { printf("failed: oversize talloc should fail\n"); return False; } talloc_realloc_size(NULL, p1, 0); CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); talloc_free(root); return True;}/* test realloc with a child*/static BOOL test_realloc_child(void){ void *root; struct el1 { int count; struct el2 { const char *name; } **list, **list2, **list3; } *el1; struct el2 *el2; printf("TESTING REALLOC WITH CHILD\n"); root = talloc_new(NULL); el1 = talloc(root, struct el1); el1->list = talloc(el1, struct el2 *); el1->list[0] = talloc(el1->list, struct el2); el1->list[0]->name = talloc_strdup(el1->list[0], "testing"); el1->list2 = talloc(el1, struct el2 *); el1->list2[0] = talloc(el1->list2, struct el2); el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2"); el1->list3 = talloc(el1, struct el2 *); el1->list3[0] = talloc(el1->list3, struct el2); el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2"); el2 = talloc(el1->list, struct el2); el2 = talloc(el1->list2, struct el2); el2 = talloc(el1->list3, struct el2); el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100); el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200); el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300); talloc_free(root); return True;}/* test type checking*/static BOOL test_type(void){ void *root; struct el1 { int count; }; struct el2 { int count; }; struct el1 *el1; printf("TESTING talloc type checking\n"); root = talloc_new(NULL); el1 = talloc(root, struct el1); el1->count = 1; if (talloc_get_type(el1, struct el1) != el1) { printf("type check failed on el1\n"); return False; } if (talloc_get_type(el1, struct el2) != NULL) { printf("type check failed on el1 with el2\n"); return False; } talloc_set_type(el1, struct el2); if (talloc_get_type(el1, struct el2) != (struct el2 *)el1) { printf("type set failed on el1 with el2\n"); return False; } talloc_free(root); return True;}/* test steal*/static BOOL test_steal(void){ void *root, *p1, *p2; printf("TESTING STEAL\n"); root = talloc_new(NULL); p1 = talloc_array(root, char, 10); CHECK_SIZE(p1, 10); p2 = talloc_realloc(root, NULL, char, 20); CHECK_SIZE(p1, 10); CHECK_SIZE(root, 30); if (talloc_steal(p1, NULL) != NULL) { printf("failed: stealing NULL should give NULL\n"); return False; } if (talloc_steal(p1, p1) != p1) { printf("failed: stealing to ourselves is a nop\n"); return False; } CHECK_BLOCKS(root, 3); CHECK_SIZE(root, 30); talloc_steal(NULL, p1); talloc_steal(NULL, p2); CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); talloc_free(p1); talloc_steal(root, p2); CHECK_BLOCKS(root, 2); CHECK_SIZE(root, 20); talloc_free(p2); CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); talloc_free(root); p1 = talloc_size(NULL, 3); CHECK_SIZE(NULL, 3); talloc_free(p1); return True;}/* test talloc_realloc_fn*/static BOOL test_realloc_fn(void){ void *root, *p1; printf("TESTING talloc_realloc_fn\n"); root = talloc_new(NULL); p1 = talloc_realloc_fn(root, NULL, 10); CHECK_BLOCKS(root, 2); CHECK_SIZE(root, 10); p1 = talloc_realloc_fn(root, p1, 20); CHECK_BLOCKS(root, 2); CHECK_SIZE(root, 20); p1 = talloc_realloc_fn(root, p1, 0); CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); talloc_free(root); return True;}static BOOL test_unref_reparent(void){ void *root, *p1, *p2, *c1; printf("TESTING UNREFERENCE AFTER PARENT FREED\n"); root = talloc_named_const(NULL, 0, "root"); p1 = talloc_named_const(root, 1, "orig parent"); p2 = talloc_named_const(root, 1, "parent by reference"); c1 = talloc_named_const(p1, 1, "child"); talloc_reference(p2, c1); talloc_free(p1); talloc_unlink(p2, c1); CHECK_SIZE(root, 1); talloc_free(p2); talloc_free(root); return True;}/* measure the speed of talloc versus malloc*/static BOOL test_speed(void){ void *ctx = talloc_new(NULL); unsigned count; struct timeval tv; printf("MEASURING TALLOC VS MALLOC SPEED\n"); tv = timeval_current(); count = 0; do { void *p1, *p2, *p3; p1 = talloc_size(ctx, count); p2 = talloc_strdup(p1, "foo bar"); p3 = talloc_size(p1, 300); talloc_free(p1); count += 3; } while (timeval_elapsed(&tv) < 5.0); printf("talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv)); talloc_free(ctx); tv = timeval_current(); count = 0; do { void *p1, *p2, *p3; p1 = malloc(count); p2 = strdup("foo bar"); p3 = malloc(300); free(p1); free(p2); free(p3); count += 3; } while (timeval_elapsed(&tv) < 5.0); printf("malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv)); return True; }BOOL torture_local_talloc(void) { BOOL ret = True; ret &= test_ref1(); ret &= test_ref2(); ret &= test_ref3(); ret &= test_ref4(); ret &= test_unlink1(); ret &= test_misc(); ret &= test_realloc(); ret &= test_realloc_child(); ret &= test_steal(); ret &= test_unref_reparent(); ret &= test_realloc_fn(); ret &= test_type(); if (ret) { ret &= test_speed(); } return ret;}#if !defined(_SAMBA_BUILD_) || ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9)) int main(void){ if (!torture_local_talloc()) { printf("ERROR: TESTSUIE FAILED\n"); return -1; } return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -