8#ifndef INCLUDED_BSLMA_ALLOCATORUTIL
9#define INCLUDED_BSLMA_ALLOCATORUTIL
361#include <bslscm_version.h>
386#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
392# define COMPILING_BSLMA_ALLOCATORUTIL_H
394# undef COMPILING_BSLMA_ALLOCATORUTIL_H
403template <
class t_ALLOCATOR,
class t_TYPE =
char>
404struct AllocatorUtil_Traits;
420 template <
class t_TYPE>
422 static long matchBslAlloc(
void *, ...);
429 template <
class t_TYPE>
431 void *allocateBytesImp(
434 std::size_t alignment);
435 template <
class t_TYPE>
439 std::size_t alignment);
440 template <
class t_ALLOCATOR>
443 allocateBytesImp(
const t_ALLOCATOR& allocator,
445 std::size_t alignment);
453 template <
class t_TYPE>
458 std::size_t alignment);
459 template <
class t_TYPE>
464 std::size_t alignment);
465 template <
class t_ALLOCATOR>
466 static void deallocateBytesImp(
467 const t_ALLOCATOR& allocator,
470 std::size_t alignment);
472 template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
473 static void deallocateObjectImp(
const t_ALLOCATOR& allocator,
476 const t_VALUE_TYPE& );
478 template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
479 static void deleteObjectImp(
const t_ALLOCATOR& allocator,
481 const t_VALUE_TYPE& );
485 static bool isPowerOf2(std::size_t alignment);
492 template <
class t_ALLOC>
493 struct IsDerivedFromBslAllocator
495 1 == sizeof(matchBslAlloc((t_ALLOC *) 0, 0))>
507 template <
class t_ALLOC>
511 adapt(
const t_ALLOC& from);
512 template <
class t_TYPE>
526 template <
class t_ALLOCATOR>
530 std::size_t alignment = 0);
538 template <
class t_TYPE,
class t_ALLOCATOR>
554 template <
class t_TYPE>
555 static t_TYPE&
assign(t_TYPE *lhs,
558 template <
class t_TYPE>
559 static t_TYPE&
assign(t_TYPE *lhs,
571 template <
class t_ALLOCATOR>
573 const t_ALLOCATOR& allocator,
576 std::size_t alignment =0);
586 template <
class t_ALLOCATOR,
class t_POINTER>
596 template <
class t_ALLOCATOR,
class t_POINTER>
597 static void deleteObject(
const t_ALLOCATOR& allocator, t_POINTER p);
604 template <
class t_TYPE,
class t_ALLOCATOR>
608#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
609# ifndef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
611 template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
615 t_ARGS&&... arguments);
628 template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
632 t_ARGS&&... arguments);
646 template <
class t_TYPE>
648 template <
class t_TYPE>
671template <
class t_ALLOCATOR,
class t_TYPE>
684template <
class t_MEMORY_RSRC,
class t_TYPE>
705template <
class t_TYPE>
707void *AllocatorUtil::allocateBytesImp(
710 std::size_t alignment)
715template <
class t_TYPE>
719 std::size_t alignment)
724template <
class t_ALLOCATOR>
725typename AllocatorUtil_Traits<t_ALLOCATOR>::void_pointer
726AllocatorUtil::allocateBytesImp(
const t_ALLOCATOR& allocator,
728 std::size_t alignment)
730 BSLMF_ASSERT(4 <= k_MAX_ALIGNMENT && k_MAX_ALIGNMENT <= 32);
732 static const int k_8 = k_MAX_ALIGNMENT < 8 ? k_MAX_ALIGNMENT : 8;
733 static const int k_16 = k_MAX_ALIGNMENT < 16 ? k_MAX_ALIGNMENT : 16;
742 if (alignment > k_MAX_ALIGNMENT) {
743 alignment = k_MAX_ALIGNMENT;
746 std::size_t n = (nbytes + alignment - 1) / alignment;
749 case 1:
return allocateObject<AlignType1 >(allocator, n);
750 case 2:
return allocateObject<AlignType2 >(allocator, n);
751 case 4:
return allocateObject<AlignType4 >(allocator, n);
752 case 8:
return allocateObject<AlignType8 >(allocator, n);
753 case 16:
return allocateObject<AlignType16 >(allocator, n);
754 default:
return allocateObject<AlignTypeMax>(allocator, n);
758template <
class t_TYPE>
760void AllocatorUtil::deallocateBytesImp(
764 std::size_t alignment)
769template <
class t_TYPE>
771void AllocatorUtil::deallocateBytesImp(
775 std::size_t alignment)
780template <
class t_ALLOCATOR>
781void AllocatorUtil::deallocateBytesImp(
782 const t_ALLOCATOR& allocator,
783 typename AllocatorUtil_Traits<t_ALLOCATOR>::void_pointer p,
785 std::size_t alignment)
787 BSLMF_ASSERT(4 <= k_MAX_ALIGNMENT && k_MAX_ALIGNMENT <= 32);
789 static const int k_8 = k_MAX_ALIGNMENT < 8 ? k_MAX_ALIGNMENT : 8;
790 static const int k_16 = k_MAX_ALIGNMENT < 16 ? k_MAX_ALIGNMENT : 16;
799 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
800 AlignType1 >::pointer Ptr1;
801 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
802 AlignType2 >::pointer Ptr2;
803 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
804 AlignType4 >::pointer Ptr4;
805 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
806 AlignType8 >::pointer Ptr8;
807 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
808 AlignType16 >::pointer Ptr16;
809 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
810 AlignTypeMax>::pointer PtrMax;
812 if (alignment > k_MAX_ALIGNMENT) {
813 alignment = k_MAX_ALIGNMENT;
816 std::size_t n = (nbytes + alignment - 1) / alignment;
828template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
830void AllocatorUtil::deallocateObjectImp(
const t_ALLOCATOR& allocator,
833 const t_VALUE_TYPE& )
835 typedef AllocatorUtil_Traits<t_ALLOCATOR, t_VALUE_TYPE> Traits;
839 typename Traits::allocator_type reboundAlloc(allocator);
840 reboundAlloc.deallocate(p, n);
843template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
845void AllocatorUtil::deleteObjectImp(
const t_ALLOCATOR& allocator,
847 const t_VALUE_TYPE& )
849 typedef AllocatorUtil_Traits<t_ALLOCATOR, t_VALUE_TYPE> Traits;
853 typename Traits::allocator_type reboundAlloc(allocator);
855 reboundAlloc.deallocate(p, 1);
859bool AllocatorUtil::isPowerOf2(std::size_t alignment)
861 return (0 < alignment) && (0 == (alignment & (alignment - 1)));
866template <
class t_ALLOC>
876template <
class t_TYPE>
883template <
class t_ALLOCATOR>
888 std::size_t alignment)
890 if (0 == alignment) {
898 return allocateBytesImp(StdAlloc(allocator), nbytes, alignment);
901template <
class t_TYPE,
class t_ALLOCATOR>
907 reboundAlloc(allocator);
908 return reboundAlloc.allocate(n);
911template <
class t_TYPE>
920template <
class t_TYPE>
928template <
class t_ALLOCATOR>
931 const t_ALLOCATOR& allocator,
934 std::size_t alignment)
936 if (0 == alignment) {
944 deallocateBytesImp(StdAlloc(allocator), p, nbytes, alignment);
947template <
class t_ALLOCATOR,
class t_POINTER>
954 deallocateObjectImp(allocator, p, n, *p);
957template <
class t_ALLOCATOR,
class t_POINTER>
962 deleteObjectImp(allocator, p, *p);
965template <
class t_TYPE,
class t_ALLOCATOR>
972 typename Traits::allocator_type reboundAlloc(allocator);
973 typename Traits::pointer p = reboundAlloc.allocate(1);
981 reboundAlloc.deallocate(p, 1);
987#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
988# ifndef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
989template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
994 t_ARGS&&... arguments)
998 typename Traits::allocator_type reboundAlloc(allocator);
999 typename Traits::pointer p = reboundAlloc.allocate(1);
1004 Traits::construct(reboundAlloc,
1010 reboundAlloc.deallocate(p, 1);
1017template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
1022 t_ARGS&&... arguments)
1026 typename Traits::allocator_type reboundAlloc(allocator);
1027 typename Traits::pointer p = reboundAlloc.allocate(1);
1032 Traits::construct(reboundAlloc,
1038 reboundAlloc.deallocate(p, 1);
1045template <
class t_TYPE>
1053template <
class t_TYPE>
Definition bslma_bslallocator.h:588
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1146
Definition bslma_memoryresource.h:443
BSLA_NODISCARD void * allocate(size_t bytes, size_t alignment=k_MAX_ALIGN)
Definition bslma_memoryresource.h:550
void deallocate(void *p, size_t bytes, size_t alignment=k_MAX_ALIGN)
Definition bslma_memoryresource.h:556
Definition bslma_polymorphicallocator.h:460
memory_resource * resource() const
Return the address of the memory resource supplied on construction.
Definition bslma_polymorphicallocator.h:1074
Definition bslma_allocator.h:545
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
static const bool value
Definition bslmf_integralconstant.h:267
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_COMPILERFEATURES_FORWARD_REF(T)
Definition bsls_compilerfeatures.h:2343
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2349
#define BSLS_CATCH(X)
Definition bsls_exceptionutil.h:372
#define BSLS_TRY
Definition bsls_exceptionutil.h:370
#define BSLS_RETHROW
Definition bsls_exceptionutil.h:378
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_UTIL_ADDRESSOF(OBJ)
Definition bsls_util.h:296
Definition baljsn_encoder_testtypes.h:76
Definition bslma_allocatortraits.h:1089
Definition bslmf_conditional.h:123
Definition bslmf_enableif.h:530
Definition bslmf_integralconstant.h:261
Definition bslmf_isconst.h:145
Definition bslmf_isconvertible.h:875
Definition bslmf_issame.h:146
Definition bslmf_isvolatile.h:145
BSLMF_ASSERT(! bsl::is_volatile< t_TYPE >::value)
BSLMF_ASSERT(! bsl::is_const< t_TYPE >::value)
BSLMF_ASSERT((bsl::is_convertible< t_MEMORY_RSRC *, bsl::memory_resource * >::value))
Definition bslma_allocatorutil.h:673
BSLMF_ASSERT(! bsl::is_volatile< t_TYPE >::value)
BSLMF_ASSERT(! bsl::is_const< t_TYPE >::value)
Definition bslma_allocatorutil.h:413
static bsl::enable_if<!IsDerivedFromBslAllocator< t_ALLOC >::value, t_ALLOC >::type adapt(const t_ALLOC &from)
Definition bslma_allocatorutil.h:871
static AllocatorUtil_Traits< t_ALLOCATOR, t_TYPE >::pointer newObject(const t_ALLOCATOR &allocator)
Definition bslma_allocatorutil.h:968
static AllocatorUtil_Traits< t_ALLOCATOR, t_TYPE >::pointer allocateObject(const t_ALLOCATOR &allocator, std::size_t n=1)
Definition bslma_allocatorutil.h:904
static void swap(t_TYPE *pa, t_TYPE *pb, bsl::false_type allowed)
Definition bslma_allocatorutil.h:1047
static AllocatorUtil_Traits< t_ALLOCATOR >::void_pointer allocateBytes(const t_ALLOCATOR &allocator, std::size_t nbytes, std::size_t alignment=0)
Definition bslma_allocatorutil.h:886
static void deleteObject(const t_ALLOCATOR &allocator, t_POINTER p)
Definition bslma_allocatorutil.h:959
static t_TYPE & assign(t_TYPE *lhs, const t_TYPE &rhs, bsl::true_type allowed)
Definition bslma_allocatorutil.h:922
static void deallocateObject(const t_ALLOCATOR &allocator, t_POINTER p, std::size_t n=1)
Definition bslma_allocatorutil.h:949
static void deallocateBytes(const t_ALLOCATOR &allocator, typename AllocatorUtil_Traits< t_ALLOCATOR >::void_pointer p, std::size_t nbytes, std::size_t alignment=0)
Definition bslma_allocatorutil.h:930
AlignmentImpPriorityToType< PRIORITY >::Type Type
Definition bsls_alignmenttotype.h:396
@ BSLS_MAX_ALIGNMENT
Definition bsls_alignmentutil.h:300
static int calculateAlignmentFromSize(std::size_t size)
Definition bsls_alignmentutil.h:398