File: segment_manager_test.hpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (871 lines) | stat: -rw-r--r-- 30,750 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2019. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/interprocess/indexes/flat_map_index.hpp>
#include <boost/interprocess/indexes/map_index.hpp>
#include <boost/interprocess/indexes/null_index.hpp>
#include <boost/interprocess/indexes/iset_index.hpp>
#include <boost/interprocess/indexes/iunordered_set_index.hpp>

#include <boost/interprocess/mem_algo/simple_seq_fit.hpp>
#include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/segment_manager.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/sync/mutex_family.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/utilities.hpp>
#include "get_process_id_name.hpp"
#include <cstddef>
#include <new>
#include <cstring>
#include <typeinfo>
#include <iostream>

using namespace boost::interprocess;

template<std::size_t Align>
struct IntLike;

//Old GCC versions emit incorrect warnings like
//"requested alignment 256 is larger than 128"
#if defined(BOOST_GCC) && (BOOST_GCC <= 100000)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif

#define BOOST_INTERPROCESS_ALIGNED_INTLIKE(A)\
template<>\
struct IntLike<A>\
{\
   IntLike(){}\
\
   IntLike(int i) : data(i) {}\
\
   BOOST_ALIGNMENT(A) int data;\
\
   operator int() const {  return data;   }\
};\
//

//Up to 4K alignment (typical page size)
BOOST_INTERPROCESS_ALIGNED_INTLIKE(16)
BOOST_INTERPROCESS_ALIGNED_INTLIKE(32)
BOOST_INTERPROCESS_ALIGNED_INTLIKE(64)
BOOST_INTERPROCESS_ALIGNED_INTLIKE(128)
BOOST_INTERPROCESS_ALIGNED_INTLIKE(256)

#undef BOOST_INTERPROCESS_ALIGNED_INTLIKE

#if defined(BOOST_GCC) && (BOOST_GCC <= 100000)
#pragma GCC diagnostic pop
#endif

template <class SegmentManager>
struct atomic_func_test
{
   SegmentManager &rsm;
   int *object;

   atomic_func_test(SegmentManager &sm)
      : rsm(sm), object()
   {}

   void operator()()
   {
      object = rsm.template find<int>("atomic_func_find_object").first;
   }
   private:
   atomic_func_test operator=(const atomic_func_test&);
   atomic_func_test(const atomic_func_test&);
};


template <class SegmentManager>
bool test_allocate_deallocate(SegmentManager* seg_mgr, mapped_region& mapping)
{
   typedef typename SegmentManager::size_type size_type;
   const std::size_t MappedRegionSize = mapping.get_size();

   for (std::size_t size = 1; size <= (MappedRegionSize / 2); size <<= 1 ) {
      const std::size_t free_mem_before = seg_mgr->get_free_memory();

      //Allocate memory
      void* mem = seg_mgr->allocate(size + 1);
      const size_type free_mem = seg_mgr->get_free_memory();
      if (free_mem >= (free_mem_before-size))
         return false;
      if (seg_mgr->all_memory_deallocated())
         return false;
      //Allocate half of the rest
      const size_type Size2 = free_mem / 2;
      void* mem2 = seg_mgr->allocate(size_type(Size2 + 1), std::nothrow);

      //Sanity checks
      if (seg_mgr->get_free_memory() >= Size2)
         return false;
      if (seg_mgr->size(mem) < (size + 1))
         return false;
      if (seg_mgr->size(mem2) < (Size2 + 1))
         return false;

      //Deallocate both
      seg_mgr->deallocate(mem);
      seg_mgr->deallocate(mem2);

      //Sanity checks again
      if (!seg_mgr->all_memory_deallocated())
         return false;
      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;

      //Try an imposible size to test error is signalled
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->allocate(MappedRegionSize * 2); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;

      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;

      if (seg_mgr->allocate(MappedRegionSize*2u, std::nothrow))
         return false;

      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;
   }
   return true;
}

template <class SegmentManager>
bool test_allocate_aligned(SegmentManager* seg_mgr, mapped_region& mapping)
{
   const std::size_t MappedRegionSize = mapping.get_size();
   const std::size_t free_mem_before = seg_mgr->get_free_memory();
   const std::size_t InitialAlignment = SegmentManager::memory_algorithm::Alignment;
   const std::size_t RegionAlignment = mapped_region::get_page_size();

   for( std::size_t alignment = InitialAlignment
      ; (alignment <= MappedRegionSize/8 && alignment <= RegionAlignment/4)
      ; alignment <<= 1u) {

      //Allocate two buffers and test the alignment inside the mapped region
      void *mem = seg_mgr->allocate_aligned(MappedRegionSize/8, alignment);
      if(seg_mgr->all_memory_deallocated())
         return false;

      std::size_t offset = static_cast<std::size_t>
         (static_cast<const char *>(mem) -  static_cast<const char *>(mapping.get_address()));
      if(offset & (alignment -1))
         return false;
      void *mem2 = seg_mgr->allocate_aligned(MappedRegionSize/4, alignment, std::nothrow);
      std::size_t offset2 = static_cast<std::size_t>
         (static_cast<const char *>(mem2) -  static_cast<const char *>(mapping.get_address()));
      if(offset2 & (alignment -1))
         return false;

      //Deallocate them
      seg_mgr->deallocate(mem);
      seg_mgr->deallocate(mem2);
      if(!seg_mgr->all_memory_deallocated())
         return false;
      if(seg_mgr->get_free_memory() != free_mem_before)
         return false;

      //Try an imposible size to test error is signalled
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{  seg_mgr->allocate_aligned(MappedRegionSize*2, alignment);  }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&){ operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;

      if (seg_mgr->allocate_aligned(MappedRegionSize*2, alignment, std::nothrow))
         return false;
      if(seg_mgr->get_free_memory() != free_mem_before)
         return false;
      if(seg_mgr->allocate_aligned(MappedRegionSize*2, alignment, std::nothrow))
         return false;
      if(seg_mgr->get_free_memory() != free_mem_before)
         return false;
   }
   return true;
}

template <class SegmentManager>
bool test_shrink_to_fit(SegmentManager* seg_mgr, mapped_region &)
{
   typedef typename SegmentManager::size_type size_type;
   const std::size_t free_mem_before = seg_mgr->get_free_memory();
   std::size_t size_before = seg_mgr->get_size();
   seg_mgr->shrink_to_fit();
   if (!seg_mgr->all_memory_deallocated())
      return false;
   std::size_t empty_shrunk_size = seg_mgr->get_size();
   std::size_t empty_shrunk_free_mem = seg_mgr->get_free_memory();
   if (empty_shrunk_size >= size_before)
      return false;
   if (empty_shrunk_free_mem >= size_before)
      return false;
   seg_mgr->grow(size_type(size_before - empty_shrunk_size));
   if (seg_mgr->get_size() != size_before)
      return false;
   if (seg_mgr->get_free_memory() != free_mem_before)
      return false;
   if (!seg_mgr->all_memory_deallocated())
      return false;

   return true;
}

template <class SegmentManager>
bool test_zero_free_memory(SegmentManager* seg_mgr, mapped_region &mapping)
{
   typedef typename SegmentManager::size_type size_type;
   const std::size_t MappedRegionSize = mapping.get_size();
   const std::size_t free_mem_before = seg_mgr->get_free_memory();
   const size_type Size(MappedRegionSize / 2 + 1), Size2(MappedRegionSize / 8);
   void* mem = seg_mgr->allocate(Size);
   void* mem2 = seg_mgr->allocate(Size2);
   //Mark memory to non-zero
   std::memset(mem, 0xFF, Size);
   std::memset(mem2, 0xFF, Size2);
   //Deallocate and check still non-zero
   seg_mgr->deallocate(mem);
   seg_mgr->deallocate(mem2);
   {  //Use byte per byte comparison as "static unsigned char zerobuf[Size]"
      //seems to be problematic in some compilers
      unsigned char* const mem_uch_ptr = static_cast<unsigned char*>(mem);
      unsigned char* const mem2_uch_ptr = static_cast<unsigned char*>(mem2);
      size_type zeroes = 0;
      for (size_type i = 0; i != Size; ++i) {
         if (!mem_uch_ptr[i])
            ++zeroes;
      }
      if (zeroes == Size)
         return false;

      zeroes = 0;
      for (size_type i = 0; i != Size2; ++i) {
         if (!mem2_uch_ptr[i])
            ++zeroes;
      }
      if (zeroes == Size2)
         return false;
   }
   //zero_free_memory and check it's zeroed
   seg_mgr->zero_free_memory();
   //TODO: some parts are not zeroed because they are used
   //as internal metadata, find a way to test this
   //if(std::memcmp(mem,  zerobuf, Size))
      //return false;
   //if(std::memcmp(mem2, zerobuf, Size2))
      //return false;
   if (seg_mgr->get_free_memory() != free_mem_before)
      return false;
   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}


template <class IntLike, class SegmentManager>
bool test_anonymous_object_type(SegmentManager* seg_mgr, mapped_region& mapping)
{
   const std::size_t MappedRegionSize = mapping.get_size();
   const std::size_t free_mem_before = seg_mgr->get_free_memory();

   //Construct single object
   {
      IntLike* int_object = seg_mgr->template construct<IntLike>(anonymous_instance)();
      BOOST_ASSERT(is_ptr_aligned(int_object, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_object, boost::move_detail::alignment_of<IntLike>::value))
         return false;
      if (1 != seg_mgr->get_instance_length(int_object))
         return false;
      if (anonymous_type != seg_mgr->get_instance_type(int_object))
         return false;
      if (seg_mgr->get_instance_name(int_object))
         return false;
      seg_mgr->destroy_ptr(int_object);
   }
   {
      //Construct array object
      IntLike* int_array = seg_mgr->template construct_it<IntLike>(anonymous_instance, std::nothrow)[5]();
      BOOST_ASSERT(is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value))
         return false;
      if (5 != seg_mgr->get_instance_length(int_array))
         return false;
      if (anonymous_type != seg_mgr->get_instance_type(int_array))
         return false;
      if (seg_mgr->get_instance_name(int_array))
         return false;
      seg_mgr->destroy_ptr(int_array);
   }
   {
      //Construct array object from it
      const signed char int_array_values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      IntLike* int_array = seg_mgr->template construct_it<IntLike>(anonymous_instance, std::nothrow)[10](&int_array_values[0]);
      BOOST_ASSERT(is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value))
         return false;
      if (10 != seg_mgr->get_instance_length(int_array))
         return false;
      if (anonymous_type != seg_mgr->get_instance_type(int_array))
         return false;
      if (seg_mgr->get_instance_name(int_array))
         return false;
      seg_mgr->destroy_ptr(int_array);
   }

   //Try an imposible size to test error is signalled
   {
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->template construct<IntLike>(anonymous_instance)[MappedRegionSize](); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;
      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;
   }
   {
      if (seg_mgr->template construct<IntLike>(anonymous_instance, std::nothrow)[MappedRegionSize]())
      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;
   }
   {
      const signed char int_array_values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      if (seg_mgr->template construct_it<IntLike>(anonymous_instance, std::nothrow)[MappedRegionSize](&int_array_values[0]))
         return false;
      if (seg_mgr->get_free_memory() != free_mem_before)
         return false;
   }

   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}

template <class SegmentManager>
bool test_anonymous_object(SegmentManager* seg_mgr, mapped_region& mapping)
{
   if (!test_anonymous_object_type<signed char>(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<short int>(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<int>(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<long int>(seg_mgr, mapping))
      return false;
   #if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   if (!test_anonymous_object_type<long long int>(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<IntLike<16> >(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<IntLike<32> >(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<IntLike<64> >(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object_type<IntLike<128> >(seg_mgr, mapping))
      return false;
   #endif   //#if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   return true;
}

template <class IntLike, class SegmentManager>
bool test_named_object_type(SegmentManager* seg_mgr, mapped_region& mapping)
{
   const std::size_t MappedRegionSize = mapping.get_size();
   const std::size_t free_mem_before = seg_mgr->get_free_memory();

   const char* const object1_name = "object1";
   const char* const object2_name = "object2";
   const signed char int_array_values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

   for (std::size_t i = 0; i != 4; ++i) {
      if (seg_mgr->template find<IntLike>(object1_name).first)
         return false;
      //Single element construction
      IntLike* int_object = 0;
      switch (i) {
      case 0:
         int_object = seg_mgr->template construct<IntLike>(object1_name)();
         break;
      case 1:
         int_object = seg_mgr->template construct<IntLike>(object1_name, std::nothrow)();
         break;
      case 2:
         int_object = seg_mgr->template find_or_construct<IntLike>(object1_name)();
         break;
      case 3:
         int_object = seg_mgr->template find_or_construct<IntLike>(object1_name, std::nothrow)();
         break;
      }


      if (!is_ptr_aligned(int_object, boost::move_detail::alignment_of<IntLike>::value)){
         std::cout << "\ntype/alignment: " << typeid(IntLike).name() << "/" << boost::move_detail::alignment_of<IntLike>::value << "\n segment_manager: " << typeid(SegmentManager).name()
                   << "\nmem alignment: " << SegmentManager::memory_algorithm::Alignment <<std::endl;
         return false;
      }

      std::pair<IntLike*, std::size_t> find_ret = seg_mgr->template find<IntLike>(object1_name);
      if (int_object != find_ret.first)
         return false;
      if (1 != find_ret.second)
         return false;
      if (1 != seg_mgr->get_instance_length(int_object))
         return false;
      if (named_type != seg_mgr->get_instance_type(int_object))
         return false;
      if (std::strcmp(object1_name, seg_mgr->get_instance_name(int_object)))
         return false;

      //Array construction
      if (seg_mgr->template find<IntLike>(object2_name).first)
         return false;
      IntLike* int_array = 0;
      switch (i) {
      case 0:
         int_array = seg_mgr->template construct_it<IntLike>(object2_name)[10](&int_array_values[0]);
         break;
      case 1:
         int_array = seg_mgr->template construct_it<IntLike>(object2_name, std::nothrow)[10](&int_array_values[0]);
         break;
      case 2:
         int_array = seg_mgr->template find_or_construct_it<IntLike>(object2_name)[10](&int_array_values[0]);
         break;
      case 3:
         int_array = seg_mgr->template find_or_construct_it<IntLike>(object2_name, std::nothrow)[10](&int_array_values[0]);
         break;
      }

      BOOST_ASSERT(is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value))
         return false;

      std::pair<IntLike*, std::size_t> find_ret2 = seg_mgr->template find<IntLike>(object2_name);
      if (int_array != find_ret2.first)
         return false;
      if (10 != find_ret2.second)
         return false;
      if (10 != seg_mgr->get_instance_length(int_array))
         return false;
      if (named_type != seg_mgr->get_instance_type(int_array))
         return false;
      if (std::strcmp(object2_name, seg_mgr->get_instance_name(int_array)))
         return false;
      if (seg_mgr->get_num_named_objects() != 2)
         return false;
      typename SegmentManager::const_named_iterator nb(seg_mgr->named_begin());
      typename SegmentManager::const_named_iterator ne(seg_mgr->named_end());
      for (std::size_t j = 0, imax = seg_mgr->get_num_named_objects(); j != imax; ++j) { ++nb; }
      if (nb != ne)
         return false;
      seg_mgr->destroy_ptr(int_object);
      seg_mgr->template destroy<IntLike>(object2_name);
   }

   //Try an imposible size to test error is signalled
   {
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->template construct<IntLike>(object1_name)[MappedRegionSize](); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true;}
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;

      if (seg_mgr->template construct<IntLike>(object2_name, std::nothrow)[MappedRegionSize]())
         return false;
   }
   {
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->template construct_it<IntLike>(object1_name)[MappedRegionSize](&int_array_values[0]); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;

      if (seg_mgr->template construct_it<IntLike>(object2_name, std::nothrow)[MappedRegionSize](&int_array_values[0]))
         return false;
   }

   seg_mgr->shrink_to_fit_indexes();
   if (seg_mgr->get_free_memory() != free_mem_before)
      return false;
   if (!seg_mgr->all_memory_deallocated())
      return false;
   seg_mgr->reserve_named_objects(1);

   //In indexes with no capacity() memory won't be allocated so don't check anything was allocated.
   //if(seg_mgr->all_memory_deallocated())  return false;
   seg_mgr->shrink_to_fit_indexes();
   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}

template <class SegmentManager>
bool test_named_object(SegmentManager* seg_mgr, mapped_region& mapping)
{
   if (!test_named_object_type<signed char>(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<short int>(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<int>(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<long int>(seg_mgr, mapping))
      return false;
   #if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   if (!test_named_object_type<long long int>(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<IntLike<16> >(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<IntLike<32> >(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<IntLike<64> >(seg_mgr, mapping))
      return false;

   if (!test_named_object_type<IntLike<128> >(seg_mgr, mapping))
      return false;
   #endif   //#if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   return true;
}

template <class IntLike, class SegmentManager>
bool test_unique_object_type(SegmentManager* seg_mgr, mapped_region& mapping)
{
   const std::size_t MappedRegionSize = mapping.get_size();
   const std::size_t free_mem_before = seg_mgr->get_free_memory();

   const signed char int_array_values[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

   for (std::size_t i = 0; i != 4; ++i) {
      if (seg_mgr->template find<IntLike>(unique_instance).first)
         return false;
      //Single element construction
      IntLike* int_object = 0;
      switch (i) {
      case 0:
         int_object = seg_mgr->template construct<IntLike>(unique_instance)();
         break;
      case 1:
         int_object = seg_mgr->template construct<IntLike>(unique_instance, std::nothrow)();
         break;
      case 2:
         int_object = seg_mgr->template find_or_construct<IntLike>(unique_instance)();
         break;
      case 3:
         int_object = seg_mgr->template find_or_construct<IntLike>(unique_instance, std::nothrow)();
         break;
      }

      BOOST_ASSERT(is_ptr_aligned(int_object, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_object, boost::move_detail::alignment_of<IntLike>::value))
         return false;

      std::pair<IntLike*, std::size_t> find_ret = seg_mgr->template find<IntLike>(unique_instance);
      if (int_object != find_ret.first)
         return false;
      if (1 != find_ret.second)
         return false;
      if (1 != seg_mgr->get_instance_length(int_object))
         return false;
      if (unique_type != seg_mgr->get_instance_type(int_object))
         return false;
      if (std::strcmp(typeid(IntLike).name(), seg_mgr->get_instance_name(int_object)))
         return false;
      //Array construction
      if (!seg_mgr->template find<IntLike>(unique_instance).first)
         return false;

      seg_mgr->destroy_ptr(int_object);

      IntLike* int_array = 0;
      switch (i) {
      case 0:
         int_array = seg_mgr->template construct_it<IntLike>(unique_instance)[10](&int_array_values[0]);
         break;
      case 1:
         int_array = seg_mgr->template construct_it<IntLike>(unique_instance, std::nothrow)[10](&int_array_values[0]);
         break;
      case 2:
         int_array = seg_mgr->template find_or_construct_it<IntLike>(unique_instance)[10](&int_array_values[0]);
         break;
      case 3:
         int_array = seg_mgr->template find_or_construct_it<IntLike>(unique_instance, std::nothrow)[10](&int_array_values[0]);
         break;
      }

      BOOST_ASSERT(is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value));
      if (!is_ptr_aligned(int_array, boost::move_detail::alignment_of<IntLike>::value))
         return false;

      std::pair<IntLike*, std::size_t> find_ret2 = seg_mgr->template find<IntLike>(unique_instance);
      if (int_array != find_ret2.first)
         return false;
      if (10 != find_ret2.second)
         return false;
      if (10 != seg_mgr->get_instance_length(int_array))
         return false;
      if (unique_type != seg_mgr->get_instance_type(int_array))
         return false;
      if (std::strcmp(typeid(IntLike).name(), seg_mgr->get_instance_name(int_array)))
         return false;
      if (seg_mgr->get_num_unique_objects() != 1)
         return false;
      typename SegmentManager::const_unique_iterator nb(seg_mgr->unique_begin());
      typename SegmentManager::const_unique_iterator ne(seg_mgr->unique_end());
      for (std::size_t j = 0, imax = seg_mgr->get_num_unique_objects(); j != imax; ++j) { ++nb; }
      if (nb != ne)
         return false;
      seg_mgr->template destroy<IntLike>(unique_instance);
   }
   //Try an imposible size to test error is signalled
   {
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->template construct<IntLike>(unique_instance)[MappedRegionSize](); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;
      if (seg_mgr->template construct<IntLike>(unique_instance, std::nothrow)[MappedRegionSize]())
         return false;
   }
   {
      bool operation_throws = false;
      BOOST_INTERPROCESS_TRY{ seg_mgr->template construct_it<IntLike>(unique_instance)[MappedRegionSize](&int_array_values[0]); }
      BOOST_INTERPROCESS_CATCH(interprocess_exception&) { operation_throws = true; }
      BOOST_INTERPROCESS_CATCH_END
      if (!operation_throws)
         return false;
      if (seg_mgr->template construct_it<IntLike>(unique_instance, std::nothrow)[MappedRegionSize](&int_array_values[0]))
         return false;
   }

   seg_mgr->shrink_to_fit_indexes();

   if (seg_mgr->get_free_memory() != free_mem_before)
      return false;
   if (!seg_mgr->all_memory_deallocated())
      return false;

   seg_mgr->reserve_unique_objects(1);

   //In indexes with no capacity() memory won't be allocated so don't check anything was allocated.
   //if(seg_mgr->all_memory_deallocated())  return false;
   seg_mgr->shrink_to_fit_indexes();
   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}

template <class SegmentManager>
bool test_unique_object(SegmentManager* seg_mgr, mapped_region& mapping)
{
   if (!test_unique_object_type<signed char>(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<short int>(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<int>(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<long int>(seg_mgr, mapping))
      return false;
   #if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   if (!test_unique_object_type<long long int>(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<IntLike<16> >(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<IntLike<32> >(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<IntLike<64> >(seg_mgr, mapping))
      return false;

   if (!test_unique_object_type<IntLike<128> >(seg_mgr, mapping))
      return false;
   #endif   //#if (BOOST_INTERPROCESS_SEGMENT_MANAGER_ABI >= 2)
   return true;
}

template <class SegmentManager>
bool test_atomic_func(SegmentManager* seg_mgr, mapped_region& )
{
   if (!seg_mgr->all_memory_deallocated())
      return false;
   int* int_object = seg_mgr->template construct<int>("atomic_func_find_object")();
   atomic_func_test<SegmentManager> func(*seg_mgr);
   seg_mgr->atomic_func(func);
   if (int_object != func.object)
      return 1;
   seg_mgr->destroy_ptr(int_object);
   seg_mgr->shrink_to_fit_indexes();
   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}

template <class SegmentManager>
bool test_allocator_deleter(SegmentManager* seg_mgr, mapped_region&)
{//test allocator/deleter
   if (!seg_mgr->all_memory_deallocated())
      return false;
   typedef typename SegmentManager::template allocator<float>::type allocator_t;

   allocator_t alloc(seg_mgr->template get_allocator<float>());

   if (!seg_mgr->all_memory_deallocated())
      return false;
   offset_ptr<float> f = alloc.allocate(50);
   if (seg_mgr->all_memory_deallocated())
      return false;
   alloc.deallocate(f, 50);
   if (!seg_mgr->all_memory_deallocated())
      return false;
   typedef typename SegmentManager::template deleter<float>::type deleter_t;
   deleter_t delet(seg_mgr->template get_deleter<float>());
   delet(seg_mgr->template construct<float>(anonymous_instance)());
   if (!seg_mgr->all_memory_deallocated())
      return false;
   return true;
}

template <class SegmentManager>
bool test_get_memory_algorithm(SegmentManager* seg_mgr, mapped_region&)
{
   {
      typename SegmentManager::memory_algorithm& mem_algo =
         seg_mgr->get_memory_algorithm();
      const typename SegmentManager::memory_algorithm& const_mem_algo =
         const_cast<const SegmentManager*>(seg_mgr)->get_memory_algorithm();
      if (&mem_algo != &const_mem_algo)
         return false;
   }
   return true;
}


template <class SegmentManager>
bool test_segment_manager()
{
   const unsigned int MappedRegionSize = 1024*64u;
   std::string shmname(test::get_process_id_name());

   shared_memory_object::remove(shmname.c_str());
   shared_memory_object sh_mem( create_only, shmname.c_str(), read_write );
   sh_mem.truncate( MappedRegionSize );
   mapped_region mapping( sh_mem, read_write );

   //Remove shared memory to minimize risk of garbage on crash
   shared_memory_object::remove(shmname.c_str());

   SegmentManager* seg_mgr = new( mapping.get_address() ) SegmentManager( MappedRegionSize );
   std::size_t size_before = seg_mgr->get_size();

   if(size_before != MappedRegionSize)
      return false;
   if(!seg_mgr->all_memory_deallocated())
      return false;
   if(seg_mgr->get_min_size() >= MappedRegionSize)
      return false;

   if (!test_allocate_deallocate(seg_mgr, mapping))
      return false;

   if (!test_allocate_aligned(seg_mgr, mapping))
      return false;

   if (!test_shrink_to_fit(seg_mgr, mapping))
      return false;

   if (!test_zero_free_memory(seg_mgr, mapping))
      return false;

   if (!test_anonymous_object(seg_mgr, mapping))
      return false;

   if (!test_named_object(seg_mgr, mapping))
      return false;

   if (!test_unique_object(seg_mgr, mapping))
      return false;

   if (!test_allocator_deleter(seg_mgr, mapping))
      return false;

   if (!test_atomic_func(seg_mgr, mapping))
      return false;

   if (!test_allocator_deleter(seg_mgr, mapping))
      return false;
   
   if (!test_get_memory_algorithm(seg_mgr, mapping))
      return false;

   return true;
}

template<class MemoryAlgorithm>
bool test_each_algo()
{
   {
      typedef segment_manager< char, MemoryAlgorithm, flat_map_index > segment_manager_t;
      if(!test_segment_manager<segment_manager_t>())
         return false;
   }
   {
      typedef segment_manager< char, MemoryAlgorithm, map_index > segment_manager_t;
      if(!test_segment_manager<segment_manager_t>())
         return false;
   }
   {
      typedef segment_manager< char, MemoryAlgorithm, iset_index > segment_manager_t;
      if(!test_segment_manager<segment_manager_t>())
         return false;
   }
   {
      typedef segment_manager< char, MemoryAlgorithm, iunordered_set_index > segment_manager_t;
      if(!test_segment_manager<segment_manager_t>())
         return false;
   }
   return true;
}

int main()
{
   if(!test_each_algo< simple_seq_fit< null_mutex_family > >())
      return 1;
   if(!test_each_algo< rbtree_best_fit< null_mutex_family > >())
      return 1;

   return 0;
}