1121
1125
void *p1, *p2, *p3, *p4;
1123
1128
pool = talloc_pool(NULL, 1024);
1125
1130
p1 = talloc_size(pool, 80);
1131
memset(p1, 0x11, talloc_get_size(p1));
1126
1132
p2 = talloc_size(pool, 20);
1133
memset(p2, 0x11, talloc_get_size(p2));
1127
1134
p3 = talloc_size(p1, 50);
1135
memset(p3, 0x11, talloc_get_size(p3));
1128
1136
p4 = talloc_size(p3, 1000);
1137
memset(p4, 0x11, talloc_get_size(p4));
1139
#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1140
p2_2 = talloc_realloc_size(pool, p2, 20+1);
1141
torture_assert("pool realloc 20+1", p2_2 == p2, "failed: pointer changed");
1142
memset(p2, 0x11, talloc_get_size(p2));
1143
p2_2 = talloc_realloc_size(pool, p2, 20-1);
1144
torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1145
memset(p2, 0x11, talloc_get_size(p2));
1146
p2_2 = talloc_realloc_size(pool, p2, 20-1);
1147
torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1148
memset(p2, 0x11, talloc_get_size(p2));
1152
/* this should reclaim the memory of p4 and p3 */
1153
p2_2 = talloc_realloc_size(pool, p2, 400);
1154
torture_assert("pool realloc 400", p2_2 == p2, "failed: pointer changed");
1155
memset(p2, 0x11, talloc_get_size(p2));
1159
/* this should reclaim the memory of p1 */
1160
p2_2 = talloc_realloc_size(pool, p2, 800);
1161
torture_assert("pool realloc 800", p2_2 == p1, "failed: pointer not changed");
1163
memset(p2, 0x11, talloc_get_size(p2));
1165
/* this should do a malloc */
1166
p2_2 = talloc_realloc_size(pool, p2, 1800);
1167
torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
1169
memset(p2, 0x11, talloc_get_size(p2));
1171
/* this should reclaim the memory from the pool */
1172
p3 = talloc_size(pool, 80);
1173
torture_assert("pool alloc 80", p3 == p1, "failed: pointer changed");
1174
memset(p3, 0x11, talloc_get_size(p3));
1179
p1 = talloc_size(pool, 80);
1180
memset(p1, 0x11, talloc_get_size(p1));
1181
p2 = talloc_size(pool, 20);
1182
memset(p2, 0x11, talloc_get_size(p2));
1186
p2_2 = talloc_realloc_size(pool, p2, 20-1);
1187
torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1188
memset(p2, 0x11, talloc_get_size(p2));
1189
p2_2 = talloc_realloc_size(pool, p2, 20-1);
1190
torture_assert("pool realloc 20-1", p2_2 == p2, "failed: pointer changed");
1191
memset(p2, 0x11, talloc_get_size(p2));
1193
/* this should do a malloc */
1194
p2_2 = talloc_realloc_size(pool, p2, 1800);
1195
torture_assert("pool realloc 1800", p2_2 != p2, "failed: pointer not changed");
1197
memset(p2, 0x11, talloc_get_size(p2));
1199
/* this should reclaim the memory from the pool */
1200
p3 = talloc_size(pool, 800);
1201
torture_assert("pool alloc 800", p3 == p1, "failed: pointer changed");
1202
memset(p3, 0x11, talloc_get_size(p3));
1204
#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1211
static bool test_pool_steal(void)
1220
root = talloc_new(NULL);
1221
pool = talloc_pool(root, 1024);
1223
p1 = talloc_size(pool, 4 * 16);
1224
torture_assert("pool allocate 4 * 16", p1 != NULL, "failed ");
1225
memset(p1, 0x11, talloc_get_size(p1));
1226
p2 = talloc_size(pool, 4 * 16);
1227
torture_assert("pool allocate 4 * 16", p2 > p1, "failed: !(p2 > p1) ");
1228
memset(p2, 0x11, talloc_get_size(p2));
1230
ofs1 = PTR_DIFF(p2, p1);
1231
hdr = ofs1 - talloc_get_size(p1);
1233
talloc_steal(root, p1);
1234
talloc_steal(root, p2);
1240
#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1241
p1_2 = talloc_realloc_size(root, p1, 5 * 16);
1242
torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
1243
memset(p1_2, 0x11, talloc_get_size(p1_2));
1244
ofs1 = PTR_DIFF(p1_2, p2);
1245
ofs2 = talloc_get_size(p2) + hdr;
1247
torture_assert("pool realloc ", ofs1 == ofs2, "failed: pointer offset unexpected");
1249
p2_2 = talloc_realloc_size(root, p2, 3 * 16);
1250
torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
1251
memset(p2_2, 0x11, talloc_get_size(p2_2));
1252
#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1258
#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1259
/* now we should reclaim the full pool */
1260
p2_2 = talloc_realloc_size(root, p2, 8 * 16);
1261
torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
1263
memset(p2_2, 0x11, talloc_get_size(p2_2));
1265
/* now we malloc and free the full pool space */
1266
p2_2 = talloc_realloc_size(root, p2, 2 * 1024);
1267
torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
1268
memset(p2_2, 0x11, talloc_get_size(p2_2));
1270
#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
1279
static bool test_free_ref_null_context(void)
1284
talloc_disable_null_tracking();
1285
p1 = talloc_new(NULL);
1286
p2 = talloc_new(NULL);
1288
p3 = talloc_reference(p2, p1);
1289
torture_assert("reference", p3 == p1, "failed: reference on null");
1291
ret = talloc_free(p1);
1292
torture_assert("ref free with null parent", ret == 0, "failed: free with null parent");
1295
talloc_enable_null_tracking_no_autofree();
1296
p1 = talloc_new(NULL);
1297
p2 = talloc_new(NULL);
1299
p3 = talloc_reference(p2, p1);
1300
torture_assert("reference", p3 == p1, "failed: reference on null");
1302
ret = talloc_free(p1);
1303
torture_assert("ref free with null tracked parent", ret == 0, "failed: free with null parent");
1309
static bool test_rusty(void)
1314
talloc_enable_null_tracking();
1315
root = talloc_new(NULL);
1316
p1 = talloc_strdup(root, "foo");
1317
talloc_increase_ref_count(p1);
1318
talloc_report_full(root, stdout);
1320
CHECK_BLOCKS("null_context", NULL, 2);
1324
static bool test_free_children(void)
1327
const char *p1, *p2, *name, *name2;
1329
talloc_enable_null_tracking();
1330
root = talloc_new(NULL);
1331
p1 = talloc_strdup(root, "foo1");
1332
p2 = talloc_strdup(p1, "foo2");
1334
talloc_set_name(p1, "%s", "testname");
1335
talloc_free_children(p1);
1336
/* check its still a valid talloc ptr */
1337
talloc_get_size(talloc_get_name(p1));
1338
if (strcmp(talloc_get_name(p1), "testname") != 0) {
1342
talloc_set_name(p1, "%s", "testname");
1343
name = talloc_get_name(p1);
1344
talloc_free_children(p1);
1345
/* check its still a valid talloc ptr */
1346
talloc_get_size(talloc_get_name(p1));
1347
torture_assert("name", name == talloc_get_name(p1), "name ptr changed");
1348
torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname") == 0,
1350
CHECK_BLOCKS("name1", p1, 2);
1352
/* note that this does not free the old child name */
1353
talloc_set_name_const(p1, "testname2");
1354
name2 = talloc_get_name(p1);
1356
talloc_free_children(p1);
1357
torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname2") == 0,
1359
CHECK_BLOCKS("name1", p1, 1);
1361
talloc_report_full(root, stdout);
1135
1367
static void test_reset(void)