test_malloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
  3. * Original Author: Hans Boehm
  4. *
  5. * This file may be redistributed and/or modified under the
  6. * terms of the GNU General Public License as published by the Free Software
  7. * Foundation; either version 2, or (at your option) any later version.
  8. *
  9. * It is distributed in the hope that it will be useful, but WITHOUT ANY
  10. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU General Public License in the
  12. * file doc/COPYING for more details.
  13. */
  14. #if defined(HAVE_CONFIG_H)
  15. # include "config.h"
  16. #endif
  17. #include "run_parallel.inc"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include "atomic_ops_malloc.h"
  21. #define MAX_NTHREADS 100
  22. #define N_REVERSALS 1000 /* must be even */
  23. #define LENGTH 1000
  24. #ifdef USE_STANDARD_MALLOC
  25. # define AO_malloc(n) malloc(n)
  26. # define AO_free(p) free(p)
  27. # define AO_malloc_enable_mmap()
  28. #endif
  29. typedef struct list_node {
  30. struct list_node *next;
  31. int data;
  32. } ln;
  33. ln *cons(int d, ln *tail)
  34. {
  35. static size_t extra = 0;
  36. size_t my_extra = extra;
  37. ln *result;
  38. int * extras;
  39. int i;
  40. if (my_extra > 100)
  41. extra = my_extra = 0;
  42. else
  43. ++extra;
  44. result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra);
  45. if (result == 0)
  46. {
  47. fprintf(stderr, "Out of memory\n");
  48. /* Normal for more than about 10 threads without mmap? */
  49. abort();
  50. }
  51. result -> data = d;
  52. result -> next = tail;
  53. extras = (int *)(result+1);
  54. for (i = 0; i < my_extra; ++i) extras[i] = 42;
  55. return result;
  56. }
  57. void print_list(ln *l)
  58. {
  59. ln *p;
  60. for (p = l; p != 0; p = p -> next)
  61. {
  62. fprintf(stderr, "%d, ", p -> data);
  63. }
  64. fprintf(stderr, "\n");
  65. }
  66. /* Check that l contains numbers from m to n inclusive in ascending order */
  67. void check_list(ln *l, int m, int n)
  68. {
  69. ln *p;
  70. int i;
  71. for (p = l, i = m; p != 0; p = p -> next, ++i)
  72. {
  73. if (i != p -> data)
  74. {
  75. fprintf(stderr, "Found %d, expected %d\n", p -> data, i);
  76. abort();
  77. }
  78. }
  79. }
  80. /* Create a list of integers from m to n */
  81. ln *
  82. make_list(int m, int n)
  83. {
  84. if (m > n) return 0;
  85. return cons(m, make_list(m+1, n));
  86. }
  87. /* Reverse list x, and concatenate it to y, deallocating no longer needed */
  88. /* nodes in x. */
  89. ln *
  90. reverse(ln *x, ln *y)
  91. {
  92. ln * result;
  93. if (x == 0) return y;
  94. result = reverse(x -> next, cons(x -> data, y));
  95. AO_free(x);
  96. return result;
  97. }
  98. int dummy_test(void) { return 1; }
  99. #define LARGE 200000
  100. void * run_one_test(void * arg) {
  101. ln * x = make_list(1, LENGTH);
  102. int i;
  103. char *p = AO_malloc(LARGE);
  104. char *q;
  105. if (0 == p) {
  106. fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
  107. LARGE);
  108. AO_free(p);
  109. } else {
  110. p[0] = p[LARGE/2] = p[LARGE-1] = 'a';
  111. q = AO_malloc(LARGE);
  112. q[0] = q[LARGE/2] = q[LARGE-1] = 'b';
  113. if (p[0] != 'a' || p[LARGE/2] != 'a' || p[LARGE-1] != 'a') {
  114. fprintf(stderr, "First large allocation smashed\n");
  115. abort();
  116. }
  117. AO_free(p);
  118. if (q[0] != 'b' || q[LARGE/2] != 'b' || q[LARGE-1] != 'b') {
  119. fprintf(stderr, "Second large allocation smashed\n");
  120. abort();
  121. }
  122. AO_free(q);
  123. }
  124. # if 0 /* enable for debugging */
  125. x = reverse(x, 0);
  126. print_list(x);
  127. x = reverse(x, 0);
  128. print_list(x);
  129. # endif
  130. for (i = 0; i < N_REVERSALS; ++i) {
  131. x = reverse(x, 0);
  132. }
  133. check_list(x, 1, LENGTH);
  134. return 0;
  135. }
  136. int main(int argc, char **argv) {
  137. int nthreads;
  138. int exper_n;
  139. if (1 == argc) {
  140. # if !defined(HAVE_MMAP)
  141. nthreads = 3;
  142. # else
  143. nthreads = 10;
  144. # endif
  145. } else if (2 == argc) {
  146. nthreads = atoi(argv[1]);
  147. if (nthreads < 1 || nthreads > MAX_NTHREADS) {
  148. fprintf(stderr, "Invalid # of threads argument\n");
  149. exit(1);
  150. }
  151. } else {
  152. fprintf(stderr, "Usage: %s [# of threads]\n", argv[0]);
  153. exit(1);
  154. }
  155. printf("Performing %d reversals of %d element lists in %d threads\n",
  156. N_REVERSALS, LENGTH, nthreads);
  157. AO_malloc_enable_mmap();
  158. run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free");
  159. return 0;
  160. }