test-memory.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <zebra.h>
  2. #include <memory.h>
  3. /* Memory torture tests
  4. *
  5. * Tests below are generic but comments are focused on interaction with
  6. * Paul's proposed memory 'quick' cache, which may never be included in
  7. * CVS
  8. */
  9. struct thread_master *master;
  10. #if 0 /* set to 1 to use system alloc directly */
  11. #undef XMALLOC
  12. #undef XCALLOC
  13. #undef XREALLOC
  14. #undef XFREE
  15. #define XMALLOC(T,S) malloc((S))
  16. #define XCALLOC(T,S) calloc(1, (S))
  17. #define XREALLOC(T,P,S) realloc((P),(S))
  18. #define XFREE(T,P) free((P))
  19. #endif
  20. #define TIMES 10
  21. int
  22. main(int argc, char **argv)
  23. {
  24. void *a[10];
  25. int i;
  26. printf ("malloc x, malloc x, free, malloc x, free free\n\n");
  27. /* simple case, test cache */
  28. for (i = 0; i < TIMES; i++)
  29. {
  30. a[0] = XMALLOC (MTYPE_VTY, 1024);
  31. memset (a[0], 1, 1024);
  32. a[1] = XMALLOC (MTYPE_VTY, 1024);
  33. memset (a[1], 1, 1024);
  34. XFREE(MTYPE_VTY, a[0]); /* should go to cache */
  35. a[0] = XMALLOC (MTYPE_VTY, 1024); /* should be satisfied from cache */
  36. XFREE(MTYPE_VTY, a[0]);
  37. XFREE(MTYPE_VTY, a[1]);
  38. }
  39. printf ("malloc x, malloc y, free x, malloc y, free free\n\n");
  40. /* cache should go invalid, valid, invalid, etc.. */
  41. for (i = 0; i < TIMES; i++)
  42. {
  43. a[0] = XMALLOC (MTYPE_VTY, 512);
  44. memset (a[0], 1, 512);
  45. a[1] = XMALLOC (MTYPE_VTY, 1024); /* invalidate cache */
  46. memset (a[1], 1, 1024);
  47. XFREE(MTYPE_VTY, a[0]);
  48. a[0] = XMALLOC (MTYPE_VTY, 1024);
  49. XFREE(MTYPE_VTY, a[0]);
  50. XFREE(MTYPE_VTY, a[1]);
  51. /* cache should become valid again on next request */
  52. }
  53. printf ("calloc\n\n");
  54. /* test calloc */
  55. for (i = 0; i < TIMES; i++)
  56. {
  57. a[0] = XCALLOC (MTYPE_VTY, 1024);
  58. memset (a[0], 1, 1024);
  59. a[1] = XCALLOC (MTYPE_VTY, 512); /* invalidate cache */
  60. memset (a[1], 1, 512);
  61. XFREE(MTYPE_VTY, a[1]);
  62. XFREE(MTYPE_VTY, a[0]);
  63. /* alloc == 0, cache can become valid again on next request */
  64. }
  65. printf ("calloc and realloc\n\n");
  66. /* check calloc + realloc */
  67. for (i = 0; i < TIMES; i++)
  68. {
  69. printf ("calloc a0 1024\n");
  70. a[0] = XCALLOC (MTYPE_VTY, 1024);
  71. memset (a[0], 1, 1024/2);
  72. printf ("calloc 1 1024\n");
  73. a[1] = XCALLOC (MTYPE_VTY, 1024);
  74. memset (a[1], 1, 1024/2);
  75. printf ("realloc 0 1024\n");
  76. a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */
  77. if (a[3] != NULL)
  78. a[0] = a[3];
  79. memset (a[0], 1, 1024);
  80. printf ("calloc 2 512\n");
  81. a[2] = XCALLOC (MTYPE_VTY, 512);
  82. memset (a[2], 1, 512);
  83. printf ("free 1 0 2\n");
  84. XFREE(MTYPE_VTY, a[1]);
  85. XFREE(MTYPE_VTY, a[0]);
  86. XFREE(MTYPE_VTY, a[2]);
  87. /* alloc == 0, cache valid next request */
  88. }
  89. return 0;
  90. }