ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

134 lines
3.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #define TEST_ALLOCATOR
  9. //#define DEBUG_ALLOCATOR
  10. #include "test.h"
  11. #include "buddy_allocator.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <vector>
  16. #include <iostream>
  17. #include <iomanip>
  18. static const size_t objects = 100;
  19. void *memory[objects];
  20. size_t sizes[objects];
  21. char vals[objects];
  22. void check_all()
  23. {
  24. for(size_t pos = 0;pos < objects;pos++) {
  25. if(!memory[pos])
  26. continue;
  27. char c=vals[pos];
  28. size_t size = sizes[pos];
  29. char *s = static_cast<char *>(memory[pos]);
  30. for(size_t j=0;j<size;j++) {
  31. TEST(s[j]==c);
  32. }
  33. }
  34. }
  35. int main()
  36. {
  37. size_t memory_size = 1024*1024*10ULL;
  38. void *ptr = malloc(memory_size);
  39. cppcms::impl::buddy_allocator *buddy=new(ptr) cppcms::impl::buddy_allocator(memory_size);
  40. try{
  41. int fail = 0;
  42. int limit = 1000;
  43. buddy->test_free();
  44. for(int i=0;i<limit;i++) {
  45. if(i*100 % limit==0)
  46. std::cout << std::setw(5) << i*100.0 / limit << "%"<< "\b\b\b\b\b\b" << std::flush;
  47. size_t pos = rand() % objects;
  48. size_t size = (1 << (rand() % 22));
  49. size += rand() % size;
  50. if(memory[pos]) {
  51. buddy->free(memory[pos]);
  52. memory[pos] = 0;
  53. check_all();
  54. }
  55. if((memory[pos]=buddy->malloc(size))==0) {
  56. check_all();
  57. fail++;
  58. }
  59. else {
  60. sizes[pos] = size;
  61. char c=rand();
  62. vals[pos] = c;
  63. char *s = static_cast<char *>(memory[pos]);
  64. for(size_t j=0;j<size;j++)
  65. s[j] = c;
  66. check_all();
  67. }
  68. buddy->test_consistent(memory,objects);
  69. }
  70. for(size_t i=0;i<objects;i++) {
  71. if(memory[i]) {
  72. buddy->free(memory[i]);
  73. memory[i]=0;
  74. check_all();
  75. }
  76. buddy->test_consistent(memory,objects);
  77. }
  78. buddy->test_free();
  79. std::cout << "\n malloc fail to all " << double(fail) / limit * 100 << std::endl;
  80. for(int dir=0;dir<=1;dir++) {
  81. for(size_t i=1;i<memory_size;i*=2) {
  82. std::cout << "Object size " << i << std::endl;
  83. std::vector<void *> ptrs;
  84. void *tmp=0;
  85. size_t exp = memory_size / i;
  86. while((tmp=buddy->malloc(i))!=0) {
  87. ptrs.push_back(tmp);
  88. if(ptrs.size() * 100 % exp==0)
  89. buddy->test_consistent(&ptrs[0],ptrs.size());
  90. }
  91. if(ptrs.empty())
  92. break;
  93. buddy->test_consistent(&ptrs[0],ptrs.size());
  94. if(dir == 0) {
  95. for(size_t i=0;i<ptrs.size();i++) {
  96. buddy->free(ptrs[i]);
  97. ptrs[i]=0;
  98. if(i*100 % ptrs.size() == 0)
  99. buddy->test_consistent(&ptrs[0]+i,ptrs.size()-i);
  100. }
  101. }
  102. else {
  103. for(int i=ptrs.size()-1;i>=0;i--) {
  104. buddy->free(ptrs[i]);
  105. ptrs[i]=0;
  106. if(i*100 % ptrs.size() == 0)
  107. buddy->test_consistent(&ptrs[0],i);
  108. }
  109. }
  110. buddy->test_free();
  111. }
  112. }
  113. }
  114. catch(std::exception const &e) {
  115. std::cerr << "Fail " << e.what() << std::endl;
  116. return 1;
  117. }
  118. buddy->~buddy_allocator();
  119. free(ptr);
  120. std::cout << "Ok\n";
  121. }