mremap.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #define JEMALLOC_MANGLE
  2. #include "jemalloc_test.h"
  3. int
  4. main(void)
  5. {
  6. int ret, err;
  7. size_t sz, lg_chunk, chunksize, i;
  8. char *p, *q;
  9. malloc_printf("Test begin\n");
  10. sz = sizeof(lg_chunk);
  11. if ((err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0))) {
  12. assert(err != ENOENT);
  13. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  14. strerror(err));
  15. ret = 1;
  16. goto label_return;
  17. }
  18. chunksize = ((size_t)1U) << lg_chunk;
  19. p = (char *)malloc(chunksize);
  20. if (p == NULL) {
  21. malloc_printf("malloc(%zu) --> %p\n", chunksize, p);
  22. ret = 1;
  23. goto label_return;
  24. }
  25. memset(p, 'a', chunksize);
  26. q = (char *)realloc(p, chunksize * 2);
  27. if (q == NULL) {
  28. malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize * 2,
  29. q);
  30. ret = 1;
  31. goto label_return;
  32. }
  33. for (i = 0; i < chunksize; i++) {
  34. assert(q[i] == 'a');
  35. }
  36. p = q;
  37. q = (char *)realloc(p, chunksize);
  38. if (q == NULL) {
  39. malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize, q);
  40. ret = 1;
  41. goto label_return;
  42. }
  43. for (i = 0; i < chunksize; i++) {
  44. assert(q[i] == 'a');
  45. }
  46. free(q);
  47. ret = 0;
  48. label_return:
  49. malloc_printf("Test end\n");
  50. return (ret);
  51. }