pool.C 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: pool.cc /main/3 1996/06/11 17:34:18 cde-hal $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "storage/memory_pool.h"
  50. #include "utility/xtime.h"
  51. #define NUMS 100000
  52. #define SIZE 20
  53. pool_test1()
  54. {
  55. MESSAGE(cerr, "pool alloc/free test1");
  56. memory_pool x;
  57. char* ptrs[NUMS];
  58. for ( int i=0; i<NUMS; i++ ) {
  59. ptrs[i] = x.alloc(SIZE);
  60. }
  61. MESSAGE(cerr, "alloc done!");
  62. for ( i=0; i<NUMS; i++ ) {
  63. x.free(ptrs[i]);
  64. }
  65. MESSAGE(cerr, "free done!");
  66. }
  67. pool_test2()
  68. {
  69. MESSAGE(cerr, "pool alloc/free test2");
  70. memory_pool x;
  71. char* ptrs[100];
  72. for ( int i=0; i<NUMS/100; i++ ) {
  73. for ( int j=0; j<100; j++ ) {
  74. ptrs[j] = x.alloc(SIZE);
  75. }
  76. for ( j=0; j<100; j++ ) {
  77. x.free(ptrs[j]);
  78. }
  79. }
  80. }
  81. new_delete_test1()
  82. {
  83. MESSAGE(cerr, "new/delete test1");
  84. char* ptrs[NUMS];
  85. for ( int i=0; i<NUMS; i++ ) {
  86. ptrs[i] = new char[SIZE];
  87. }
  88. MESSAGE(cerr, "alloc done!");
  89. for ( i=NUMS-1; i>=0; i-- ) {
  90. delete ptrs[i];
  91. }
  92. MESSAGE(cerr, "free done!");
  93. }
  94. new_delete_test2()
  95. {
  96. MESSAGE(cerr, "new/delete test2");
  97. char* ptrs[100];
  98. for ( int i=0; i<NUMS/100; i++ ) {
  99. for ( int j=0; j<100; j++ ) {
  100. ptrs[j] = new char[SIZE];
  101. }
  102. for ( j=0; j<100; j++ ) {
  103. delete ptrs[j];
  104. }
  105. }
  106. }
  107. main( int argc, char** argv )
  108. {
  109. assert ( argc == 2 );
  110. int ok;
  111. float cpul
  112. long elap;
  113. xtime tm;
  114. tm.snap_shot();
  115. if ( strcmp(argv[1], "-pool1") == 0 ) {
  116. pool_test1();
  117. ok = 0;
  118. } else
  119. if ( strcmp(argv[1], "-pool2") == 0 ) {
  120. pool_test2();
  121. ok = 0;
  122. } else
  123. if ( strcmp(argv[1], "-new1") == 0 ) {
  124. new_delete_test1();
  125. ok = 0;
  126. } else
  127. if ( strcmp(argv[1], "-new2") == 0 ) {
  128. new_delete_test2();
  129. ok = 0;
  130. }
  131. else {
  132. MESSAGE(cerr, "apply a param: -pool[1|2] or -new[1|2]");
  133. ok = -1;
  134. }
  135. tm.duration(cpu, elap);
  136. debug(cerr, cpu);
  137. debug(cerr, elap);
  138. return ok;
  139. }