allocated.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #define JEMALLOC_MANGLE
  2. #include "jemalloc_test.h"
  3. void *
  4. je_thread_start(void *arg)
  5. {
  6. int err;
  7. void *p;
  8. uint64_t a0, a1, d0, d1;
  9. uint64_t *ap0, *ap1, *dp0, *dp1;
  10. size_t sz, usize;
  11. sz = sizeof(a0);
  12. if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) {
  13. if (err == ENOENT) {
  14. #ifdef JEMALLOC_STATS
  15. assert(false);
  16. #endif
  17. goto label_return;
  18. }
  19. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  20. strerror(err));
  21. exit(1);
  22. }
  23. sz = sizeof(ap0);
  24. if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) {
  25. if (err == ENOENT) {
  26. #ifdef JEMALLOC_STATS
  27. assert(false);
  28. #endif
  29. goto label_return;
  30. }
  31. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  32. strerror(err));
  33. exit(1);
  34. }
  35. assert(*ap0 == a0);
  36. sz = sizeof(d0);
  37. if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) {
  38. if (err == ENOENT) {
  39. #ifdef JEMALLOC_STATS
  40. assert(false);
  41. #endif
  42. goto label_return;
  43. }
  44. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  45. strerror(err));
  46. exit(1);
  47. }
  48. sz = sizeof(dp0);
  49. if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) {
  50. if (err == ENOENT) {
  51. #ifdef JEMALLOC_STATS
  52. assert(false);
  53. #endif
  54. goto label_return;
  55. }
  56. malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
  57. strerror(err));
  58. exit(1);
  59. }
  60. assert(*dp0 == d0);
  61. p = malloc(1);
  62. if (p == NULL) {
  63. malloc_printf("%s(): Error in malloc()\n", __func__);
  64. exit(1);
  65. }
  66. sz = sizeof(a1);
  67. mallctl("thread.allocated", &a1, &sz, NULL, 0);
  68. sz = sizeof(ap1);
  69. mallctl("thread.allocatedp", &ap1, &sz, NULL, 0);
  70. assert(*ap1 == a1);
  71. assert(ap0 == ap1);
  72. usize = malloc_usable_size(p);
  73. assert(a0 + usize <= a1);
  74. free(p);
  75. sz = sizeof(d1);
  76. mallctl("thread.deallocated", &d1, &sz, NULL, 0);
  77. sz = sizeof(dp1);
  78. mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0);
  79. assert(*dp1 == d1);
  80. assert(dp0 == dp1);
  81. assert(d0 + usize <= d1);
  82. label_return:
  83. return (NULL);
  84. }
  85. int
  86. main(void)
  87. {
  88. int ret = 0;
  89. je_thread_t thread;
  90. malloc_printf("Test begin\n");
  91. je_thread_start(NULL);
  92. je_thread_create(&thread, je_thread_start, NULL);
  93. je_thread_join(thread, (void *)&ret);
  94. je_thread_start(NULL);
  95. je_thread_create(&thread, je_thread_start, NULL);
  96. je_thread_join(thread, (void *)&ret);
  97. je_thread_start(NULL);
  98. malloc_printf("Test end\n");
  99. return (ret);
  100. }