mercredi 21 janvier 2015

Custom allocators vs. promises and packaged tasks

Are the allocator-taking constructors of std::promise/std::packaged_task supposed to use the allocator for just the state object itself, or should this be guaranteed for all (internal) related objects?



[futures.promise]: "...allocate memory for the shared state"

[futures.task.members]: "...allocate memory needed to store the internal data structures"



In particular, are the below bugs or features?


*MSVC 2013.4, Boost 1.57, short_alloc.h by Howard Hinnant


Example 1



#define BOOST_THREAD_VERSION 4
#include <boost/thread/future.hpp>
#include "short_alloc.h"
#include <cstdio>

void *operator new( std::size_t s ) {
printf( "alloc %Iu\n", s );
return malloc( s );
}

void operator delete( void *p ) {
free( p );
}

int main() {

const int N = 1024;
arena< N > a;
short_alloc< int, N > al( a );

printf( "[promise]\n" );
auto p = boost::promise< int >( std::allocator_arg, al );
p.set_value( 123 );

printf( "[packaged_task]\n" );
auto q = boost::packaged_task< int() >( std::allocator_arg, al, [] { return 123; } );
q();

return 0;

}


Output:



...
[promise]
alloc 8
alloc 12
alloc 8
alloc 24
[packaged_task]
alloc 8
alloc 12
alloc 8
alloc 24


FWIW, the output with the default allocator is



...
[promise]
alloc 144
alloc 8
alloc 12
alloc 8
alloc 16
[packaged_task]
alloc 160
alloc 8
alloc 12
alloc 8
alloc 16


Example 2


AFAICT, MSVC's std::mutex does an unavoidable heap allocation, and therefore, so does std::promise which uses it. Is this a conformant behaviour?


Aucun commentaire:

Enregistrer un commentaire