8#ifndef INCLUDED_BSLMA_ALLOCATORUTIL
9#define INCLUDED_BSLMA_ALLOCATORUTIL
360#include <bslscm_version.h>
384#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
388# define COMPILING_BSLMA_ALLOCATORUTIL_H
390# undef COMPILING_BSLMA_ALLOCATORUTIL_H
397template <
class t_ALLOCATOR,
class t_TYPE =
char>
398struct AllocatorUtil_Traits;
412 template <
class t_TYPE>
414 static long matchBslAlloc(
void *, ...);
421 template <
class t_TYPE>
423 void *allocateBytesImp(
426 std::size_t alignment);
427 template <
class t_TYPE>
431 std::size_t alignment);
432 template <
class t_ALLOCATOR>
435 allocateBytesImp(
const t_ALLOCATOR& allocator,
437 std::size_t alignment);
444 template <
class t_TYPE>
449 std::size_t alignment);
450 template <
class t_TYPE>
455 std::size_t alignment);
456 template <
class t_ALLOCATOR>
457 static void deallocateBytesImp(
458 const t_ALLOCATOR& allocator,
461 std::size_t alignment);
463 template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
464 static void deallocateObjectImp(
const t_ALLOCATOR& allocator,
467 const t_VALUE_TYPE& );
469 template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
470 static void deleteObjectImp(
const t_ALLOCATOR& allocator,
472 const t_VALUE_TYPE& );
476 static bool isPowerOf2(std::size_t alignment);
483 template <
class t_ALLOC>
484 struct IsDerivedFromBslAllocator
486 1 == sizeof(matchBslAlloc((t_ALLOC *) 0, 0))>
498 template <
class t_ALLOC>
502 adapt(
const t_ALLOC& from);
503 template <
class t_TYPE>
515 template <
class t_ALLOCATOR>
519 std::size_t alignment = k_MAX_ALIGNMENT);
528 template <
class t_TYPE,
class t_ALLOCATOR>
543 template <
class t_TYPE>
544 static t_TYPE&
assign(t_TYPE *lhs,
547 template <
class t_TYPE>
548 static t_TYPE&
assign(t_TYPE *lhs,
557 template <
class t_ALLOCATOR>
559 const t_ALLOCATOR& allocator,
562 std::size_t alignment
572 template <
class t_ALLOCATOR,
class t_POINTER>
581 template <
class t_ALLOCATOR,
class t_POINTER>
582 static void deleteObject(
const t_ALLOCATOR& allocator, t_POINTER p);
589 template <
class t_TYPE,
class t_ALLOCATOR>
593#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
594# ifndef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
596 template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
600 t_ARGS&&... arguments);
613 template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
617 t_ARGS&&... arguments);
631 template <
class t_TYPE>
633 template <
class t_TYPE>
656template <
class t_ALLOCATOR,
class t_TYPE>
669template <
class t_MEMORY_RSRC,
class t_TYPE>
690template <
class t_TYPE>
692void *AllocatorUtil::allocateBytesImp(
695 std::size_t alignment)
700template <
class t_TYPE>
704 std::size_t alignment)
709template <
class t_ALLOCATOR>
710typename AllocatorUtil_Traits<t_ALLOCATOR>::void_pointer
711AllocatorUtil::allocateBytesImp(
const t_ALLOCATOR& allocator,
713 std::size_t alignment)
715 BSLMF_ASSERT(4 <= k_MAX_ALIGNMENT && k_MAX_ALIGNMENT <= 32);
717 static const int k_8 = k_MAX_ALIGNMENT < 8 ? k_MAX_ALIGNMENT : 8;
718 static const int k_16 = k_MAX_ALIGNMENT < 16 ? k_MAX_ALIGNMENT : 16;
727 if (alignment > k_MAX_ALIGNMENT) {
728 alignment = k_MAX_ALIGNMENT;
731 std::size_t n = (nbytes + alignment - 1) / alignment;
734 case 1:
return allocateObject<AlignType1 >(allocator, n);
735 case 2:
return allocateObject<AlignType2 >(allocator, n);
736 case 4:
return allocateObject<AlignType4 >(allocator, n);
737 case 8:
return allocateObject<AlignType8 >(allocator, n);
738 case 16:
return allocateObject<AlignType16 >(allocator, n);
739 default:
return allocateObject<AlignTypeMax>(allocator, n);
743template <
class t_TYPE>
745void AllocatorUtil::deallocateBytesImp(
749 std::size_t alignment)
754template <
class t_TYPE>
756void AllocatorUtil::deallocateBytesImp(
760 std::size_t alignment)
765template <
class t_ALLOCATOR>
766void AllocatorUtil::deallocateBytesImp(
767 const t_ALLOCATOR& allocator,
768 typename AllocatorUtil_Traits<t_ALLOCATOR>::void_pointer p,
770 std::size_t alignment)
772 BSLMF_ASSERT(4 <= k_MAX_ALIGNMENT && k_MAX_ALIGNMENT <= 32);
774 static const int k_8 = k_MAX_ALIGNMENT < 8 ? k_MAX_ALIGNMENT : 8;
775 static const int k_16 = k_MAX_ALIGNMENT < 16 ? k_MAX_ALIGNMENT : 16;
784 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
785 AlignType1 >::pointer Ptr1;
786 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
787 AlignType2 >::pointer Ptr2;
788 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
789 AlignType4 >::pointer Ptr4;
790 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
791 AlignType8 >::pointer Ptr8;
792 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
793 AlignType16 >::pointer Ptr16;
794 typedef typename AllocatorUtil_Traits<t_ALLOCATOR,
795 AlignTypeMax>::pointer PtrMax;
797 if (alignment > k_MAX_ALIGNMENT) {
798 alignment = k_MAX_ALIGNMENT;
801 std::size_t n = (nbytes + alignment - 1) / alignment;
813template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
815void AllocatorUtil::deallocateObjectImp(
const t_ALLOCATOR& allocator,
818 const t_VALUE_TYPE& )
820 typedef AllocatorUtil_Traits<t_ALLOCATOR, t_VALUE_TYPE> Traits;
824 typename Traits::allocator_type reboundAlloc(allocator);
825 reboundAlloc.deallocate(p, n);
828template <
class t_ALLOCATOR,
class t_POINTER,
class t_VALUE_TYPE>
830void AllocatorUtil::deleteObjectImp(
const t_ALLOCATOR& allocator,
832 const t_VALUE_TYPE& )
834 typedef AllocatorUtil_Traits<t_ALLOCATOR, t_VALUE_TYPE> Traits;
838 typename Traits::allocator_type reboundAlloc(allocator);
840 reboundAlloc.deallocate(p, 1);
844bool AllocatorUtil::isPowerOf2(std::size_t alignment)
846 return (0 < alignment) && (0 == (alignment & (alignment - 1)));
851template <
class t_ALLOC>
861template <
class t_TYPE>
868template <
class t_ALLOCATOR>
873 std::size_t alignment)
879 return allocateBytesImp(StdAlloc(allocator), nbytes, alignment);
882template <
class t_TYPE,
class t_ALLOCATOR>
888 reboundAlloc(allocator);
889 return reboundAlloc.allocate(n);
892template <
class t_TYPE>
901template <
class t_TYPE>
909template <
class t_ALLOCATOR>
912 const t_ALLOCATOR& allocator,
915 std::size_t alignment)
921 deallocateBytesImp(StdAlloc(allocator), p, nbytes, alignment);
924template <
class t_ALLOCATOR,
class t_POINTER>
931 deallocateObjectImp(allocator, p, n, *p);
934template <
class t_ALLOCATOR,
class t_POINTER>
939 deleteObjectImp(allocator, p, *p);
942template <
class t_TYPE,
class t_ALLOCATOR>
949 typename Traits::allocator_type reboundAlloc(allocator);
950 typename Traits::pointer p = reboundAlloc.allocate(1);
958 reboundAlloc.deallocate(p, 1);
964#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
965# ifndef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
966template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
971 t_ARGS&&... arguments)
975 typename Traits::allocator_type reboundAlloc(allocator);
976 typename Traits::pointer p = reboundAlloc.allocate(1);
981 Traits::construct(reboundAlloc,
987 reboundAlloc.deallocate(p, 1);
994template <
class t_TYPE,
class t_ALLOCATOR,
class t_ARG1,
class... t_ARGS>
999 t_ARGS&&... arguments)
1003 typename Traits::allocator_type reboundAlloc(allocator);
1004 typename Traits::pointer p = reboundAlloc.allocate(1);
1009 Traits::construct(reboundAlloc,
1015 reboundAlloc.deallocate(p, 1);
1022template <
class t_TYPE>
1030template <
class t_TYPE>
Definition bslma_bslallocator.h:580
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1126
Definition bslma_memoryresource.h:441
BSLS_ANNOTATION_NODISCARD void * allocate(size_t bytes, size_t alignment=k_MAX_ALIGN)
Definition bslma_memoryresource.h:541
void deallocate(void *p, size_t bytes, size_t alignment=k_MAX_ALIGN)
Definition bslma_memoryresource.h:547
Definition bslma_polymorphicallocator.h:452
memory_resource * resource() const
Return the address of the memory resource supplied on construction.
Definition bslma_polymorphicallocator.h:1048
Definition bslma_allocator.h:457
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
static const bool value
Definition bslmf_integralconstant.h:258
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_COMPILERFEATURES_FORWARD_REF(T)
Definition bsls_compilerfeatures.h:2012
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2018
#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)
Definition bsls_ident.h:195
#define BSLS_UTIL_ADDRESSOF(OBJ)
Definition bsls_util.h:289
Definition balxml_encoderoptions.h:68
void swap(TYPE &a, TYPE &b)
Definition bslma_allocatortraits.h:1061
Definition bslmf_conditional.h:120
Definition bslmf_enableif.h:525
Definition bslmf_integralconstant.h:244
Definition bslmf_isconst.h:144
Definition bslmf_isconvertible.h:867
Definition bslmf_issame.h:146
Definition bslmf_isvolatile.h:144
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:658
BSLMF_ASSERT(! bsl::is_volatile< t_TYPE >::value)
BSLMF_ASSERT(! bsl::is_const< t_TYPE >::value)
Namespace for utility functions on allocators.
Definition bslma_allocatorutil.h:405
static bsl::enable_if<!IsDerivedFromBslAllocator< t_ALLOC >::value, t_ALLOC >::type adapt(const t_ALLOC &from)
Definition bslma_allocatorutil.h:856
static AllocatorUtil_Traits< t_ALLOCATOR, t_TYPE >::pointer newObject(const t_ALLOCATOR &allocator)
Definition bslma_allocatorutil.h:945
static AllocatorUtil_Traits< t_ALLOCATOR, t_TYPE >::pointer allocateObject(const t_ALLOCATOR &allocator, std::size_t n=1)
Definition bslma_allocatorutil.h:885
static void swap(t_TYPE *pa, t_TYPE *pb, bsl::false_type allowed)
Definition bslma_allocatorutil.h:1024
static void deallocateBytes(const t_ALLOCATOR &allocator, typename AllocatorUtil_Traits< t_ALLOCATOR >::void_pointer p, std::size_t nbytes, std::size_t alignment=k_MAX_ALIGNMENT)
Definition bslma_allocatorutil.h:911
static void deleteObject(const t_ALLOCATOR &allocator, t_POINTER p)
Definition bslma_allocatorutil.h:936
static AllocatorUtil_Traits< t_ALLOCATOR >::void_pointer allocateBytes(const t_ALLOCATOR &allocator, std::size_t nbytes, std::size_t alignment=k_MAX_ALIGNMENT)
Definition bslma_allocatorutil.h:871
static t_TYPE & assign(t_TYPE *lhs, const t_TYPE &rhs, bsl::true_type allowed)
Definition bslma_allocatorutil.h:903
static void deallocateObject(const t_ALLOCATOR &allocator, t_POINTER p, std::size_t n=1)
Definition bslma_allocatorutil.h:926
AlignmentImpPriorityToType< PRIORITY >::Type Type
Definition bsls_alignmenttotype.h:394
@ BSLS_MAX_ALIGNMENT
Definition bsls_alignmentutil.h:275