run.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "alloc/alloc.h"
  2. #include "tap.h"
  3. #include "alloc/alloc.c"
  4. #include <stdlib.h>
  5. #define POOL_ORD 16
  6. #define POOL_SIZE (1 << POOL_ORD)
  7. #define sort(p, num, cmp) \
  8. qsort((p), (num), sizeof(*p), (int(*)(const void *, const void *))cmp)
  9. static int addr_cmp(void **a, void **b)
  10. {
  11. return (*a) - (*b);
  12. }
  13. static bool unique(void *p[], unsigned int num)
  14. {
  15. unsigned int i;
  16. for (i = 1; i < num; i++)
  17. if (p[i] == p[i-1])
  18. return false;
  19. return true;
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. void *mem;
  24. unsigned int i, num, max_size;
  25. void *p[POOL_SIZE];
  26. plan_tests(141);
  27. /* FIXME: Needs to be page aligned for now. */
  28. posix_memalign(&mem, getpagesize(), POOL_SIZE);
  29. /* Small pool, all allocs fail, even 0-length. */
  30. alloc_init(mem, 0);
  31. ok1(alloc_check(mem, 0));
  32. ok1(alloc_get(mem, 0, 1, 1) == NULL);
  33. ok1(alloc_get(mem, 0, 128, 1) == NULL);
  34. ok1(alloc_get(mem, 0, 0, 1) == NULL);
  35. /* Free of NULL should work. */
  36. alloc_free(mem, 0, NULL);
  37. alloc_init(mem, POOL_SIZE);
  38. ok1(alloc_check(mem, POOL_SIZE));
  39. /* Find largest allocation which works. */
  40. for (max_size = POOL_SIZE * 2; max_size; max_size--) {
  41. p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
  42. if (p[0])
  43. break;
  44. }
  45. ok1(max_size < POOL_SIZE);
  46. ok1(max_size > 0);
  47. ok1(alloc_check(mem, POOL_SIZE));
  48. /* Free it, should be able to reallocate it. */
  49. alloc_free(mem, POOL_SIZE, p[0]);
  50. ok1(alloc_check(mem, POOL_SIZE));
  51. p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
  52. ok1(p[0]);
  53. ok1(alloc_check(mem, POOL_SIZE));
  54. alloc_free(mem, POOL_SIZE, p[0]);
  55. ok1(alloc_check(mem, POOL_SIZE));
  56. /* Allocate a whole heap. */
  57. for (i = 0; i < POOL_SIZE; i++) {
  58. p[i] = alloc_get(mem, POOL_SIZE, 1, 1);
  59. if (!p[i])
  60. break;
  61. }
  62. num = i;
  63. /* Can't allocate this many. */
  64. ok1(num != POOL_SIZE);
  65. ok1(alloc_check(mem, POOL_SIZE));
  66. /* Sort them. */
  67. sort(p, num, addr_cmp);
  68. /* Uniqueness check */
  69. ok1(unique(p, num));
  70. /* Free every second one. */
  71. for (i = 0; i < num; i += 2) {
  72. alloc_free(mem, POOL_SIZE, p[i]);
  73. ok1(alloc_check(mem, POOL_SIZE));
  74. }
  75. for (i = 1; i < num; i += 2) {
  76. alloc_free(mem, POOL_SIZE, p[i]);
  77. ok1(alloc_check(mem, POOL_SIZE));
  78. }
  79. ok1(alloc_check(mem, POOL_SIZE));
  80. /* Should be able to reallocate max size. */
  81. p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
  82. ok1(p[0]);
  83. ok1(alloc_check(mem, POOL_SIZE));
  84. /* Re-initializing should be the same as freeing everything */
  85. alloc_init(mem, POOL_SIZE);
  86. ok1(alloc_check(mem, POOL_SIZE));
  87. p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
  88. ok1(p[0]);
  89. ok1(alloc_check(mem, POOL_SIZE));
  90. alloc_free(mem, POOL_SIZE, p[0]);
  91. ok1(alloc_check(mem, POOL_SIZE));
  92. /* Alignment constraints should be met, as long as powers of two */
  93. for (i = 0; i < POOL_ORD-2 /* FIXME: Should be -1 */; i++) {
  94. p[i] = alloc_get(mem, POOL_SIZE, i, 1 << i);
  95. ok1(p[i]);
  96. ok1(((unsigned long)p[i] % (1 << i)) == 0);
  97. ok1(alloc_check(mem, POOL_SIZE));
  98. }
  99. for (i = 0; i < POOL_ORD-2 /* FIXME: Should be -1 */; i++) {
  100. alloc_free(mem, POOL_SIZE, p[i]);
  101. ok1(alloc_check(mem, POOL_SIZE));
  102. }
  103. /* Alignment constraints for a single-byte allocation. */
  104. for (i = 0; i < POOL_ORD; i++) {
  105. p[0] = alloc_get(mem, POOL_SIZE, 1, 1 << i);
  106. ok1(p[0]);
  107. ok1(alloc_check(mem, POOL_SIZE));
  108. alloc_free(mem, POOL_SIZE, p[0]);
  109. ok1(alloc_check(mem, POOL_SIZE));
  110. }
  111. return exit_status();
  112. }