test-memory.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This file is part of Quagga.
  3. *
  4. * Quagga is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2, or (at your option) any
  7. * later version.
  8. *
  9. * Quagga is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Quagga; see the file COPYING. If not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  17. * 02111-1307, USA.
  18. */
  19. #include <zebra.h>
  20. #include <memory.h>
  21. /* Memory torture tests
  22. *
  23. * Tests below are generic but comments are focused on interaction with
  24. * Paul's proposed memory 'quick' cache, which may never be included in
  25. * CVS
  26. */
  27. struct thread_master *master;
  28. #if 0 /* set to 1 to use system alloc directly */
  29. #undef XMALLOC
  30. #undef XCALLOC
  31. #undef XREALLOC
  32. #undef XFREE
  33. #define XMALLOC(T,S) malloc((S))
  34. #define XCALLOC(T,S) calloc(1, (S))
  35. #define XREALLOC(T,P,S) realloc((P),(S))
  36. #define XFREE(T,P) free((P))
  37. #endif
  38. #define TIMES 10
  39. int
  40. main(int argc, char **argv)
  41. {
  42. void *a[10];
  43. int i;
  44. printf ("malloc x, malloc x, free, malloc x, free free\n\n");
  45. /* simple case, test cache */
  46. for (i = 0; i < TIMES; i++)
  47. {
  48. a[0] = XMALLOC (MTYPE_VTY, 1024);
  49. memset (a[0], 1, 1024);
  50. a[1] = XMALLOC (MTYPE_VTY, 1024);
  51. memset (a[1], 1, 1024);
  52. XFREE(MTYPE_VTY, a[0]); /* should go to cache */
  53. a[0] = XMALLOC (MTYPE_VTY, 1024); /* should be satisfied from cache */
  54. XFREE(MTYPE_VTY, a[0]);
  55. XFREE(MTYPE_VTY, a[1]);
  56. }
  57. printf ("malloc x, malloc y, free x, malloc y, free free\n\n");
  58. /* cache should go invalid, valid, invalid, etc.. */
  59. for (i = 0; i < TIMES; i++)
  60. {
  61. a[0] = XMALLOC (MTYPE_VTY, 512);
  62. memset (a[0], 1, 512);
  63. a[1] = XMALLOC (MTYPE_VTY, 1024); /* invalidate cache */
  64. memset (a[1], 1, 1024);
  65. XFREE(MTYPE_VTY, a[0]);
  66. a[0] = XMALLOC (MTYPE_VTY, 1024);
  67. XFREE(MTYPE_VTY, a[0]);
  68. XFREE(MTYPE_VTY, a[1]);
  69. /* cache should become valid again on next request */
  70. }
  71. printf ("calloc\n\n");
  72. /* test calloc */
  73. for (i = 0; i < TIMES; i++)
  74. {
  75. a[0] = XCALLOC (MTYPE_VTY, 1024);
  76. memset (a[0], 1, 1024);
  77. a[1] = XCALLOC (MTYPE_VTY, 512); /* invalidate cache */
  78. memset (a[1], 1, 512);
  79. XFREE(MTYPE_VTY, a[1]);
  80. XFREE(MTYPE_VTY, a[0]);
  81. /* alloc == 0, cache can become valid again on next request */
  82. }
  83. printf ("calloc and realloc\n\n");
  84. /* check calloc + realloc */
  85. for (i = 0; i < TIMES; i++)
  86. {
  87. printf ("calloc a0 1024\n");
  88. a[0] = XCALLOC (MTYPE_VTY, 1024);
  89. memset (a[0], 1, 1024/2);
  90. printf ("calloc 1 1024\n");
  91. a[1] = XCALLOC (MTYPE_VTY, 1024);
  92. memset (a[1], 1, 1024/2);
  93. printf ("realloc 0 1024\n");
  94. a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */
  95. if (a[3] != NULL)
  96. a[0] = a[3];
  97. memset (a[0], 1, 1024);
  98. printf ("calloc 2 512\n");
  99. a[2] = XCALLOC (MTYPE_VTY, 512);
  100. memset (a[2], 1, 512);
  101. printf ("free 1 0 2\n");
  102. XFREE(MTYPE_VTY, a[1]);
  103. XFREE(MTYPE_VTY, a[0]);
  104. XFREE(MTYPE_VTY, a[2]);
  105. /* alloc == 0, cache valid next request */
  106. }
  107. return 0;
  108. }