fill_flush.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "test/jemalloc_test.h"
  2. #include "test/bench.h"
  3. #define SMALL_ALLOC_SIZE 128
  4. #define LARGE_ALLOC_SIZE SC_LARGE_MINCLASS
  5. #define NALLOCS 1000
  6. /*
  7. * We make this volatile so the 1-at-a-time variants can't leave the allocation
  8. * in a register, just to try to get the cache behavior closer.
  9. */
  10. void *volatile allocs[NALLOCS];
  11. static void
  12. array_alloc_dalloc_small(void) {
  13. for (int i = 0; i < NALLOCS; i++) {
  14. void *p = mallocx(SMALL_ALLOC_SIZE, 0);
  15. assert_ptr_not_null(p, "mallocx shouldn't fail");
  16. allocs[i] = p;
  17. }
  18. for (int i = 0; i < NALLOCS; i++) {
  19. sdallocx(allocs[i], SMALL_ALLOC_SIZE, 0);
  20. }
  21. }
  22. static void
  23. item_alloc_dalloc_small(void) {
  24. for (int i = 0; i < NALLOCS; i++) {
  25. void *p = mallocx(SMALL_ALLOC_SIZE, 0);
  26. assert_ptr_not_null(p, "mallocx shouldn't fail");
  27. allocs[i] = p;
  28. sdallocx(allocs[i], SMALL_ALLOC_SIZE, 0);
  29. }
  30. }
  31. TEST_BEGIN(test_array_vs_item_small) {
  32. compare_funcs(1 * 1000, 10 * 1000,
  33. "array of small allocations", array_alloc_dalloc_small,
  34. "small item allocation", item_alloc_dalloc_small);
  35. }
  36. TEST_END
  37. static void
  38. array_alloc_dalloc_large(void) {
  39. for (int i = 0; i < NALLOCS; i++) {
  40. void *p = mallocx(LARGE_ALLOC_SIZE, 0);
  41. assert_ptr_not_null(p, "mallocx shouldn't fail");
  42. allocs[i] = p;
  43. }
  44. for (int i = 0; i < NALLOCS; i++) {
  45. sdallocx(allocs[i], LARGE_ALLOC_SIZE, 0);
  46. }
  47. }
  48. static void
  49. item_alloc_dalloc_large(void) {
  50. for (int i = 0; i < NALLOCS; i++) {
  51. void *p = mallocx(LARGE_ALLOC_SIZE, 0);
  52. assert_ptr_not_null(p, "mallocx shouldn't fail");
  53. allocs[i] = p;
  54. sdallocx(allocs[i], LARGE_ALLOC_SIZE, 0);
  55. }
  56. }
  57. TEST_BEGIN(test_array_vs_item_large) {
  58. compare_funcs(100, 1000,
  59. "array of large allocations", array_alloc_dalloc_large,
  60. "large item allocation", item_alloc_dalloc_large);
  61. }
  62. TEST_END
  63. int main(void) {
  64. return test_no_reentrancy(
  65. test_array_vs_item_small,
  66. test_array_vs_item_large);
  67. }