named_allocation_test_template.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_NAMED_ALLOCATION_TEST_TEMPLATE_HEADER
  11. #define BOOST_INTERPROCESS_NAMED_ALLOCATION_TEST_TEMPLATE_HEADER
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. // interprocess
  14. #include <boost/interprocess/managed_shared_memory.hpp>
  15. #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
  16. #include <boost/interprocess/streams/bufferstream.hpp>
  17. #include <boost/interprocess/sync/mutex_family.hpp>
  18. // container
  19. #include <boost/container/detail/iterator.hpp>
  20. #include <boost/container/detail/minimal_char_traits_header.hpp> //char_traits
  21. // std
  22. #include <cstdio>
  23. #include <iostream>
  24. #include <new>
  25. #include <set>
  26. #include <vector>
  27. // local
  28. #include "get_process_id_name.hpp"
  29. namespace boost { namespace interprocess { namespace test {
  30. namespace {
  31. const wchar_t *get_prefix(wchar_t)
  32. { return L"prefix_name_"; }
  33. const char *get_prefix(char)
  34. { return "prefix_name_"; }
  35. }
  36. //This test allocates until there is no more memory
  37. //and after that deallocates all in the same order
  38. template<class ManagedMemory>
  39. bool test_names_and_types(ManagedMemory &m)
  40. {
  41. typedef typename ManagedMemory::char_type char_type;
  42. typedef std::char_traits<char_type> char_traits_type;
  43. std::vector<char*> buffers;
  44. const int BufferLen = 100;
  45. char_type name[BufferLen];
  46. basic_bufferstream<char_type> formatter(name, BufferLen);
  47. for(int i = 0; true; ++i){
  48. formatter.seekp(0);
  49. formatter << get_prefix(char_type()) << i << std::ends;
  50. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  51. if(!ptr)
  52. break;
  53. std::size_t namelen = char_traits_type::length(m.get_instance_name(ptr));
  54. if(namelen != char_traits_type::length(name)){
  55. return 1;
  56. }
  57. if(char_traits_type::compare(m.get_instance_name(ptr), name, namelen) != 0){
  58. return 1;
  59. }
  60. if(m.template find<char>(name).first == 0)
  61. return false;
  62. if(m.get_instance_type(ptr) != named_type)
  63. return false;
  64. buffers.push_back(ptr);
  65. }
  66. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  67. return false;
  68. for(int j = 0, max = (int)buffers.size()
  69. ;j < max
  70. ;++j){
  71. m.destroy_ptr(buffers[j]);
  72. }
  73. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  74. return false;
  75. m.shrink_to_fit_indexes();
  76. if(!m.all_memory_deallocated())
  77. return false;
  78. return true;
  79. }
  80. //This test allocates until there is no more memory
  81. //and after that deallocates all in the same order
  82. template<class ManagedMemory>
  83. bool test_named_iterators(ManagedMemory &m)
  84. {
  85. typedef typename ManagedMemory::char_type char_type;
  86. std::vector<char*> buffers;
  87. const int BufferLen = 100;
  88. char_type name[BufferLen];
  89. typedef std::basic_string<char_type> string_type;
  90. std::set<string_type> names;
  91. basic_bufferstream<char_type> formatter(name, BufferLen);
  92. string_type aux_str;
  93. for(int i = 0; true; ++i){
  94. formatter.seekp(0);
  95. formatter << get_prefix(char_type()) << i << std::ends;
  96. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  97. if(!ptr)
  98. break;
  99. aux_str = name;
  100. names.insert(aux_str);
  101. buffers.push_back(ptr);
  102. }
  103. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  104. return false;
  105. typedef typename ManagedMemory::const_named_iterator const_named_iterator;
  106. const_named_iterator named_beg = m.named_begin();
  107. const_named_iterator named_end = m.named_end();
  108. if((std::size_t)boost::container::iterator_distance(named_beg, named_end) != (std::size_t)buffers.size()){
  109. return 1;
  110. }
  111. for(; named_beg != named_end; ++named_beg){
  112. const char_type *name_str = named_beg->name();
  113. aux_str = name_str;
  114. if(names.find(aux_str) == names.end()){
  115. return 1;
  116. }
  117. if(aux_str.size() != named_beg->name_length()){
  118. return 1;
  119. }
  120. const void *found_value = m.template find<char>(name_str).first;
  121. if(found_value == 0)
  122. return false;
  123. if(found_value != named_beg->value())
  124. return false;
  125. }
  126. for(int j = 0, max = (int)buffers.size()
  127. ;j < max
  128. ;++j){
  129. m.destroy_ptr(buffers[j]);
  130. }
  131. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  132. return false;
  133. m.shrink_to_fit_indexes();
  134. if(!m.all_memory_deallocated())
  135. return false;
  136. return true;
  137. }
  138. //This test allocates until there is no more memory
  139. //and after that deallocates all in the same order
  140. template<class ManagedMemory>
  141. bool test_shrink_to_fit(ManagedMemory &m)
  142. {
  143. typedef typename ManagedMemory::char_type char_type;
  144. std::vector<char*> buffers;
  145. const int BufferLen = 100;
  146. char_type name[BufferLen];
  147. basic_bufferstream<char_type> formatter(name, BufferLen);
  148. std::size_t free_memory_before = m.get_free_memory();
  149. for(int i = 0; true; ++i){
  150. formatter.seekp(0);
  151. formatter << get_prefix(char_type()) << i << std::ends;
  152. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  153. if(!ptr)
  154. break;
  155. buffers.push_back(ptr);
  156. }
  157. for(int j = 0, max = (int)buffers.size()
  158. ;j < max
  159. ;++j){
  160. m.destroy_ptr(buffers[j]);
  161. }
  162. std::size_t free_memory_after = m.get_free_memory();
  163. if(free_memory_before != free_memory_after){
  164. m.shrink_to_fit_indexes();
  165. if(free_memory_before != free_memory_after)
  166. return false;
  167. }
  168. return true;
  169. }
  170. //This test allocates until there is no more memory
  171. //and after that deallocates all in the same order
  172. template<class ManagedMemory>
  173. bool test_direct_named_allocation_destruction(ManagedMemory &m)
  174. {
  175. typedef typename ManagedMemory::char_type char_type;
  176. std::vector<char*> buffers;
  177. const int BufferLen = 100;
  178. char_type name[BufferLen];
  179. basic_bufferstream<char_type> formatter(name, BufferLen);
  180. for(int i = 0; true; ++i){
  181. formatter.seekp(0);
  182. formatter << get_prefix(char_type()) << i << std::ends;
  183. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  184. if(!ptr)
  185. break;
  186. if(m.template find<char>(name).first == 0)
  187. return false;
  188. buffers.push_back(ptr);
  189. }
  190. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  191. return false;
  192. for(int j = 0, max = (int)buffers.size()
  193. ;j < max
  194. ;++j){
  195. m.destroy_ptr(buffers[j]);
  196. }
  197. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  198. return false;
  199. m.shrink_to_fit_indexes();
  200. if(!m.all_memory_deallocated())
  201. return false;
  202. return true;
  203. }
  204. //This test allocates until there is no more memory
  205. //and after that deallocates all in the inverse order
  206. template<class ManagedMemory>
  207. bool test_named_allocation_inverse_destruction(ManagedMemory &m)
  208. {
  209. typedef typename ManagedMemory::char_type char_type;
  210. std::vector<char*> buffers;
  211. const int BufferLen = 100;
  212. char_type name[BufferLen];
  213. basic_bufferstream<char_type> formatter(name, BufferLen);
  214. for(int i = 0; true; ++i){
  215. formatter.seekp(0);
  216. formatter << get_prefix(char_type()) << i << std::ends;
  217. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  218. if(!ptr)
  219. break;
  220. buffers.push_back(ptr);
  221. }
  222. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  223. return false;
  224. for(int j = (int)buffers.size()
  225. ;j--
  226. ;){
  227. m.destroy_ptr(buffers[j]);
  228. }
  229. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  230. return false;
  231. m.shrink_to_fit_indexes();
  232. if(!m.all_memory_deallocated())
  233. return false;
  234. return true;
  235. }
  236. //This test allocates until there is no more memory
  237. //and after that deallocates all following a pattern
  238. template<class ManagedMemory>
  239. bool test_named_allocation_mixed_destruction(ManagedMemory &m)
  240. {
  241. typedef typename ManagedMemory::char_type char_type;
  242. std::vector<char*> buffers;
  243. const int BufferLen = 100;
  244. char_type name[BufferLen];
  245. basic_bufferstream<char_type> formatter(name, BufferLen);
  246. for(int i = 0; true; ++i){
  247. formatter.seekp(0);
  248. formatter << get_prefix(char_type()) << i << std::ends;
  249. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  250. if(!ptr)
  251. break;
  252. buffers.push_back(ptr);
  253. }
  254. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  255. return false;
  256. for(int j = 0, max = (int)buffers.size()
  257. ;j < max
  258. ;++j){
  259. int pos = (j%4)*((int)buffers.size())/4;
  260. m.destroy_ptr(buffers[pos]);
  261. buffers.erase(buffers.begin()+pos);
  262. }
  263. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  264. return false;
  265. m.shrink_to_fit_indexes();
  266. if(!m.all_memory_deallocated())
  267. return false;
  268. return true;
  269. }
  270. //This test allocates until there is no more memory
  271. //and after that deallocates all in the same order
  272. template<class ManagedMemory>
  273. bool test_inverse_named_allocation_destruction(ManagedMemory &m)
  274. {
  275. typedef typename ManagedMemory::char_type char_type;
  276. std::vector<char*> buffers;
  277. const int BufferLen = 100;
  278. char_type name[BufferLen];
  279. basic_bufferstream<char_type> formatter(name, BufferLen);
  280. for(unsigned int i = 0; true; ++i){
  281. formatter.seekp(0);
  282. formatter << get_prefix(char_type()) << i << std::ends;
  283. char *ptr = m.template construct<char>(name, std::nothrow)(i);
  284. if(!ptr)
  285. break;
  286. buffers.push_back(ptr);
  287. }
  288. if(m.get_num_named_objects() != buffers.size() || !m.check_sanity())
  289. return false;
  290. for(unsigned int j = 0, max = (unsigned int)buffers.size()
  291. ;j < max
  292. ;++j){
  293. m.destroy_ptr(buffers[j]);
  294. }
  295. if(m.get_num_named_objects() != 0 || !m.check_sanity())
  296. return false;
  297. m.shrink_to_fit_indexes();
  298. if(!m.all_memory_deallocated())
  299. return false;
  300. return true;
  301. }
  302. ///This function calls all tests
  303. template<class ManagedMemory>
  304. bool test_all_named_allocation(ManagedMemory &m)
  305. {
  306. std::cout << "Starting test_names_and_types. Class: "
  307. << typeid(m).name() << std::endl;
  308. if(!test_names_and_types(m)){
  309. std::cout << "test_names_and_types failed. Class: "
  310. << typeid(m).name() << std::endl;
  311. return false;
  312. }
  313. std::cout << "Starting test_direct_named_allocation_destruction. Class: "
  314. << typeid(m).name() << std::endl;
  315. if(!test_direct_named_allocation_destruction(m)){
  316. std::cout << "test_direct_named_allocation_destruction failed. Class: "
  317. << typeid(m).name() << std::endl;
  318. return false;
  319. }
  320. std::cout << "Starting test_named_allocation_inverse_destruction. Class: "
  321. << typeid(m).name() << std::endl;
  322. if(!test_named_allocation_inverse_destruction(m)){
  323. std::cout << "test_named_allocation_inverse_destruction failed. Class: "
  324. << typeid(m).name() << std::endl;
  325. return false;
  326. }
  327. std::cout << "Starting test_named_allocation_mixed_destruction. Class: "
  328. << typeid(m).name() << std::endl;
  329. if(!test_named_allocation_mixed_destruction(m)){
  330. std::cout << "test_named_allocation_mixed_destruction failed. Class: "
  331. << typeid(m).name() << std::endl;
  332. return false;
  333. }
  334. std::cout << "Starting test_inverse_named_allocation_destruction. Class: "
  335. << typeid(m).name() << std::endl;
  336. if(!test_inverse_named_allocation_destruction(m)){
  337. std::cout << "test_inverse_named_allocation_destruction failed. Class: "
  338. << typeid(m).name() << std::endl;
  339. return false;
  340. }
  341. if(!test_named_iterators(m)){
  342. std::cout << "test_named_iterators failed. Class: "
  343. << typeid(m).name() << std::endl;
  344. return false;
  345. }
  346. return true;
  347. }
  348. //This function calls all tests
  349. template<template <class IndexConfig> class Index>
  350. bool test_named_allocation()
  351. {
  352. using namespace boost::interprocess;
  353. const int memsize = 163840;
  354. const char *const shMemName = test::get_process_id_name();
  355. try
  356. {
  357. //A shared memory with rbtree best fit algorithm
  358. typedef basic_managed_shared_memory
  359. <char
  360. ,rbtree_best_fit<mutex_family>
  361. ,Index
  362. > my_managed_shared_memory;
  363. //Create shared memory
  364. shared_memory_object::remove(shMemName);
  365. my_managed_shared_memory segment(create_only, shMemName, memsize);
  366. //Now take the segment manager and launch memory test
  367. if(!test::test_all_named_allocation(*segment.get_segment_manager())){
  368. return false;
  369. }
  370. }
  371. catch(...){
  372. shared_memory_object::remove(shMemName);
  373. throw;
  374. }
  375. shared_memory_object::remove(shMemName);
  376. //Now test it with wchar_t
  377. try
  378. {
  379. //A shared memory with simple sequential fit algorithm
  380. typedef basic_managed_shared_memory
  381. <wchar_t
  382. ,rbtree_best_fit<mutex_family>
  383. ,Index
  384. > my_managed_shared_memory;
  385. //Create shared memory
  386. shared_memory_object::remove(shMemName);
  387. my_managed_shared_memory segment(create_only, shMemName, memsize);
  388. //Now take the segment manager and launch memory test
  389. if(!test::test_all_named_allocation(*segment.get_segment_manager())){
  390. return false;
  391. }
  392. }
  393. catch(...){
  394. shared_memory_object::remove(shMemName);
  395. throw;
  396. }
  397. shared_memory_object::remove(shMemName);
  398. return true;
  399. }
  400. }}} //namespace boost { namespace interprocess { namespace test {
  401. #include <boost/interprocess/detail/config_end.hpp>
  402. #endif //BOOST_INTERPROCESS_NAMED_ALLOCATION_TEST_TEMPLATE_HEADER