mremap.c 993 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_mremap)
  3. {
  4. int err;
  5. size_t sz, lg_chunk, chunksize, i;
  6. char *p, *q;
  7. sz = sizeof(lg_chunk);
  8. err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0);
  9. assert_d_eq(err, 0, "Error in mallctl(): %s", strerror(err));
  10. chunksize = ((size_t)1U) << lg_chunk;
  11. p = (char *)malloc(chunksize);
  12. assert_ptr_not_null(p, "malloc(%zu) --> %p", chunksize, p);
  13. memset(p, 'a', chunksize);
  14. q = (char *)realloc(p, chunksize * 2);
  15. assert_ptr_not_null(q, "realloc(%p, %zu) --> %p", p, chunksize * 2,
  16. q);
  17. for (i = 0; i < chunksize; i++) {
  18. assert_c_eq(q[i], 'a',
  19. "realloc() should preserve existing bytes across copies");
  20. }
  21. p = q;
  22. q = (char *)realloc(p, chunksize);
  23. assert_ptr_not_null(q, "realloc(%p, %zu) --> %p", p, chunksize, q);
  24. for (i = 0; i < chunksize; i++) {
  25. assert_c_eq(q[i], 'a',
  26. "realloc() should preserve existing bytes across copies");
  27. }
  28. free(q);
  29. }
  30. TEST_END
  31. int
  32. main(void)
  33. {
  34. return (test(
  35. test_mremap));
  36. }