2
0

rallocm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_same_size)
  3. {
  4. void *p, *q;
  5. size_t sz, tsz;
  6. assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
  7. "Unexpected allocm() error");
  8. q = p;
  9. assert_d_eq(rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE), ALLOCM_SUCCESS,
  10. "Unexpected rallocm() error");
  11. assert_ptr_eq(q, p, "Unexpected object move");
  12. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  13. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  14. "Unexpected dallocm() error");
  15. }
  16. TEST_END
  17. TEST_BEGIN(test_extra_no_move)
  18. {
  19. void *p, *q;
  20. size_t sz, tsz;
  21. assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
  22. "Unexpected allocm() error");
  23. q = p;
  24. assert_d_eq(rallocm(&q, &tsz, sz, sz-42, ALLOCM_NO_MOVE),
  25. ALLOCM_SUCCESS, "Unexpected rallocm() error");
  26. assert_ptr_eq(q, p, "Unexpected object move");
  27. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  28. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  29. "Unexpected dallocm() error");
  30. }
  31. TEST_END
  32. TEST_BEGIN(test_no_move_fail)
  33. {
  34. void *p, *q;
  35. size_t sz, tsz;
  36. assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS,
  37. "Unexpected allocm() error");
  38. q = p;
  39. assert_d_eq(rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE),
  40. ALLOCM_ERR_NOT_MOVED, "Unexpected rallocm() result");
  41. assert_ptr_eq(q, p, "Unexpected object move");
  42. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  43. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  44. "Unexpected dallocm() error");
  45. }
  46. TEST_END
  47. TEST_BEGIN(test_grow_and_shrink)
  48. {
  49. void *p, *q;
  50. size_t tsz;
  51. #define NCYCLES 3
  52. unsigned i, j;
  53. #define NSZS 2500
  54. size_t szs[NSZS];
  55. #define MAXSZ ZU(12 * 1024 * 1024)
  56. assert_d_eq(allocm(&p, &szs[0], 1, 0), ALLOCM_SUCCESS,
  57. "Unexpected allocm() error");
  58. for (i = 0; i < NCYCLES; i++) {
  59. for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
  60. q = p;
  61. assert_d_eq(rallocm(&q, &szs[j], szs[j-1]+1, 0, 0),
  62. ALLOCM_SUCCESS,
  63. "Unexpected rallocm() error for size=%zu-->%zu",
  64. szs[j-1], szs[j-1]+1);
  65. assert_zu_ne(szs[j], szs[j-1]+1,
  66. "Expected size to at least: %zu", szs[j-1]+1);
  67. p = q;
  68. }
  69. for (j--; j > 0; j--) {
  70. q = p;
  71. assert_d_eq(rallocm(&q, &tsz, szs[j-1], 0, 0),
  72. ALLOCM_SUCCESS,
  73. "Unexpected rallocm() error for size=%zu-->%zu",
  74. szs[j], szs[j-1]);
  75. assert_zu_eq(tsz, szs[j-1],
  76. "Expected size=%zu, got size=%zu", szs[j-1], tsz);
  77. p = q;
  78. }
  79. }
  80. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  81. "Unexpected dallocm() error");
  82. }
  83. TEST_END
  84. int
  85. main(void)
  86. {
  87. return (test(
  88. test_same_size,
  89. test_extra_no_move,
  90. test_no_move_fail,
  91. test_grow_and_shrink));
  92. }