34 #ifndef __INCLUDE_MIST_ALLOC_H__
35 #define __INCLUDE_MIST_ALLOC_H__
40 #ifndef __INCLUDE_MIST_CONF_H__
44 #ifndef __INCLUDE_MIST_TYPE_TRAIT_H__
67 template <
class Allocator >
68 static typename Allocator::pointer allocate_objects1( Allocator &allocator,
typename Allocator::size_type num )
70 #if _MIST_ALLOCATE_TEST_ != 0
71 ::std::cerr <<
"Normal Allocator" << ::std::endl;
73 typename Allocator::pointer ptr = allocator.allocate( num, 0 );
74 typename Allocator::value_type obj;
75 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) allocator.construct( &( ptr[i] ), obj );
90 template <
class Allocator >
91 static typename Allocator::pointer allocate_objects2( Allocator &allocator,
typename Allocator::size_type num,
typename Allocator::const_reference obj )
93 #if _MIST_ALLOCATE_TEST_ != 0
94 ::std::cerr <<
"Normal Allocator" << ::std::endl;
96 typename Allocator::pointer ptr = allocator.allocate( num, 0 );
97 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) allocator.construct( &( ptr[i] ), obj );
112 template <
class Allocator >
113 static typename Allocator::pointer allocate_objects3( Allocator &allocator,
typename Allocator::const_pointer s,
typename Allocator::const_pointer e )
115 #if _MIST_ALLOCATE_TEST_ != 0
116 ::std::cerr <<
"Normal Allocator" << ::std::endl;
118 typename Allocator::pointer ptr = allocator.allocate( e - s, 0 );
119 for(
typename Allocator::pointer p = ptr ; s != e ; p++, s++ ) allocator.construct( p, *s );
132 template <
class Allocator >
133 static void deallocate_objects( Allocator &allocator,
typename Allocator::pointer ptr,
typename Allocator::size_type num )
135 #if _MIST_ALLOCATE_TEST_ != 0
136 ::std::cerr <<
"Normal Deallocator" << ::std::endl;
138 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) allocator.destroy( &( ptr[i] ) );
139 allocator.deallocate( ptr, num );
154 template <
class Allocator >
155 static typename Allocator::pointer copy_objects1( Allocator & ,
typename Allocator::const_pointer s,
typename Allocator::const_pointer e,
typename Allocator::pointer x )
157 #if _MIST_ALLOCATE_TEST_ != 0
158 ::std::cerr <<
"Normal Copy Function" << ::std::endl;
181 template <
class Allocator >
182 static typename Allocator::pointer copy_objects2( Allocator & ,
typename Allocator::const_pointer ptr,
typename Allocator::size_type num,
typename Allocator::pointer to )
184 #if _MIST_ALLOCATE_TEST_ != 0
185 ::std::cerr <<
"Normal Copy Function" << ::std::endl;
187 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) to[i] = ptr[i];
201 template <
class Allocator >
202 static void fill_objects1( Allocator & ,
typename Allocator::pointer ptr,
typename Allocator::size_type num,
typename Allocator::const_reference obj )
204 #if _MIST_ALLOCATE_TEST_ != 0
205 ::std::cerr <<
"Normal Clean Function" << ::std::endl;
207 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) ptr[i] = obj;
219 template <
class Allocator >
220 static void fill_objects2( Allocator & ,
typename Allocator::pointer ptr,
typename Allocator::size_type num )
222 #if _MIST_ALLOCATE_TEST_ != 0
223 ::std::cerr <<
"Normal Clean Function" << ::std::endl;
225 typename Allocator::value_type obj;
226 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) ptr[i] = obj;
246 template <
class Allocator >
247 static typename Allocator::pointer allocate_objects1( Allocator &allocator,
typename Allocator::size_type num )
249 #if _MIST_ALLOCATE_TEST_ != 0
250 ::std::cerr <<
"Memset Allocator" << ::std::endl;
252 typename Allocator::pointer ptr = allocator.allocate( num, 0 );
253 ::memset( ptr, 0, num *
sizeof(
typename Allocator::value_type ) );
268 template <
class Allocator >
269 static typename Allocator::pointer allocate_objects2( Allocator &allocator,
typename Allocator::size_type num,
typename Allocator::const_reference obj )
271 #if _MIST_ALLOCATE_TEST_ != 0
272 ::std::cerr <<
"Specialized Allocator" << ::std::endl;
274 typename Allocator::pointer ptr = allocator.allocate( num, 0 );
275 for(
typename Allocator::size_type i = 0 ; i < num ; i++ ) ptr[i] = obj;
290 template <
class Allocator >
291 static typename Allocator::pointer allocate_objects3( Allocator &allocator,
typename Allocator::const_pointer s,
typename Allocator::const_pointer e )
293 #if _MIST_ALLOCATE_TEST_ != 0
294 ::std::cerr <<
"Memcpy Allocator" << ::std::endl;
296 typename Allocator::pointer ptr = allocator.allocate( e - s, 0 );
297 memcpy( ptr, s, ( e - s ) *
sizeof(
typename Allocator::value_type ) );
310 template <
class Allocator >
311 static void deallocate_objects( Allocator &allocator,
typename Allocator::pointer ptr,
typename Allocator::size_type num )
313 #if _MIST_ALLOCATE_TEST_ != 0
314 ::std::cerr <<
"Specialized Deallocator" << ::std::endl;
316 allocator.deallocate( ptr, num );
331 template <
class Allocator >
332 static typename Allocator::pointer copy_objects1( Allocator & ,
typename Allocator::const_pointer s,
typename Allocator::const_pointer e,
typename Allocator::pointer x )
334 #if _MIST_ALLOCATE_TEST_ != 0
335 ::std::cerr <<
"Memcpy Copy Function" << ::std::endl;
337 memcpy( x, s, ( e - s ) *
sizeof(
typename Allocator::value_type ) );
338 return( x + ( e - s ) );
353 template <
class Allocator >
354 static typename Allocator::pointer copy_objects2( Allocator & ,
typename Allocator::const_pointer ptr,
typename Allocator::size_type num,
typename Allocator::pointer to )
356 #if _MIST_ALLOCATE_TEST_ != 0
357 ::std::cerr <<
"Memcpy Copy Function" << ::std::endl;
359 memcpy( to, ptr, num *
sizeof(
typename Allocator::value_type ) );
373 template <
class Allocator >
374 static void fill_objects1( Allocator & ,
typename Allocator::pointer ptr,
typename Allocator::size_type num,
typename Allocator::const_reference obj )
376 #if _MIST_ALLOCATE_TEST_ != 0
377 ::std::cerr <<
"Memcpy Clean Function" << ::std::endl;
379 memset( ptr, obj, num *
sizeof(
typename Allocator::value_type ) );
391 template <
class Allocator >
392 static void fill_objects2( Allocator & ,
typename Allocator::pointer ptr,
typename Allocator::size_type num )
394 #if _MIST_ALLOCATE_TEST_ != 0
395 ::std::cerr <<
"Memcpy Clean Function" << ::std::endl;
397 memset( ptr, 0, num *
sizeof(
typename Allocator::value_type ) );
410 template <
class T,
class Allocator >
432 if( num == 0 || num > max_size( ) )
436 else if( !is_memory_shared( ) )
444 return( shared_pointer );
451 if( num == 0 || num > max_size( ) )
455 else if( !is_memory_shared( ) )
464 return( shared_pointer );
471 if( s >= e || e - s > max_size( ) )
475 else if( !is_memory_shared( ) )
484 return( shared_pointer );
491 if( !is_memory_shared( ) )
494 if( num <= 0 )
return;
502 if( s >= e )
return( x );
509 if( num <= 0 )
return( to );
516 if( num <= 0 )
return;
523 if( num <= 0 )
return;
533 if( num < dest_num )
return( ptr );
535 if( num == dest_num )
return( ptr );
536 if( num == 0 )
return( NULL );
538 if( !is_memory_shared( ) )
541 #if _MIST_ALLOCATOR_MEMORY_TRIM_ != 0
542 deallocate_objects( ptr + dest_num, num - dest_num );
543 fill_objects( ptr, dest_num );
545 deallocate_objects( ptr, num );
546 ptr = allocate_objects( dest_num );
549 return( dest_num == 0 ? NULL : ptr );
558 if( num < dest_num )
return( ptr );
559 if( num < 0 )
return( NULL );
560 if( num == dest_num )
return( ptr );
561 if( num == 0 )
return( NULL );
563 if( !is_memory_shared( ) )
566 #if _MIST_ALLOCATOR_MEMORY_TRIM_ != 0
567 deallocate_objects( ptr + dest_num, num - dest_num );
568 fill_objects( ptr, dest_num, obj );
570 deallocate_objects( ptr, num );
571 ptr = allocate_objects( dest_num, obj );
574 return( dest_num == 0 ? NULL : ptr );
580 return( shared_memory == 0 ? base::max_size( ) : shared_memory );
584 bool is_memory_shared( )
const
586 return( shared_memory != 0 );
590 const Allocator &operator=(
const Allocator &alloc )
595 base::operator=( alloc );
606 base::operator=( alloc );
627 mist_allocator(
const Allocator &alloc ) :
base( alloc ), shared_pointer( NULL ), shared_memory( 0 ){}
639 #endif // __INCLUDE_MIST_ALLOC_H__