proxygen
folly::fbstring_core< Char >::RefCounted Struct Reference

Static Public Member Functions

static constexpr size_t getDataOffset ()
 
static RefCountedfromData (Char *p)
 
static size_t refs (Char *p)
 
static void incrementRefs (Char *p)
 
static void decrementRefs (Char *p)
 
static RefCountedcreate (size_t *size)
 
static RefCountedcreate (const Char *data, size_t *size)
 
static RefCountedreallocate (Char *const data, const size_t currentSize, const size_t currentCapacity, size_t *newCapacity)
 

Public Attributes

std::atomic< size_t > refCount_
 
Char data_ [1]
 

Detailed Description

template<class Char>
struct folly::fbstring_core< Char >::RefCounted

Definition at line 549 of file FBString.h.

Member Function Documentation

template<class Char>
static RefCounted* folly::fbstring_core< Char >::RefCounted::create ( size_t *  size)
inlinestatic

Definition at line 580 of file FBString.h.

References testing::gmock_generated_actions_test::Char(), folly::checkedMalloc(), folly::goodMallocSize(), and folly::fbstring_core< Char >::RefCounted::refCount_.

580  {
581  const size_t allocSize =
582  goodMallocSize(getDataOffset() + (*size + 1) * sizeof(Char));
583  auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
584  result->refCount_.store(1, std::memory_order_release);
585  *size = (allocSize - getDataOffset()) / sizeof(Char) - 1;
586  return result;
587  }
void * checkedMalloc(size_t size)
Definition: Malloc.h:227
static constexpr size_t getDataOffset()
Definition: FBString.h:553
size_t size() const
Definition: FBString.h:493
size_t goodMallocSize(size_t minSize) noexcept
Definition: Malloc.h:201
template<class Char>
static RefCounted* folly::fbstring_core< Char >::RefCounted::create ( const Char *  data,
size_t *  size 
)
inlinestatic

Definition at line 589 of file FBString.h.

References FBSTRING_LIKELY, folly::fbstring_detail::podCopy(), and folly::size().

589  {
590  const size_t effectiveSize = *size;
591  auto result = create(size);
592  if (FBSTRING_LIKELY(effectiveSize > 0)) {
593  fbstring_detail::podCopy(data, data + effectiveSize, result->data_);
594  }
595  return result;
596  }
void podCopy(const Pod *b, const Pod *e, Pod *d)
Definition: FBString.h:174
const Char * data() const
Definition: FBString.h:432
static RefCounted * create(size_t *size)
Definition: FBString.h:580
#define FBSTRING_LIKELY(x)
Definition: FBString.h:74
size_t size() const
Definition: FBString.h:493
template<class Char>
static void folly::fbstring_core< Char >::RefCounted::decrementRefs ( Char *  p)
inlinestatic

Definition at line 571 of file FBString.h.

References FBSTRING_ASSERT, and bm::free().

571  {
572  auto const dis = fromData(p);
573  size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
574  FBSTRING_ASSERT(oldcnt > 0);
575  if (oldcnt == 1) {
576  free(dis);
577  }
578  }
static RefCounted * fromData(Char *p)
Definition: FBString.h:557
void free()
#define FBSTRING_ASSERT(expr)
Definition: FBString.h:64
template<class Char>
static RefCounted* folly::fbstring_core< Char >::RefCounted::fromData ( Char *  p)
inlinestatic

Definition at line 557 of file FBString.h.

557  {
558  return static_cast<RefCounted*>(static_cast<void*>(
559  static_cast<unsigned char*>(static_cast<void*>(p)) -
560  getDataOffset()));
561  }
static constexpr size_t getDataOffset()
Definition: FBString.h:553
template<class Char>
static constexpr size_t folly::fbstring_core< Char >::RefCounted::getDataOffset ( )
inlinestatic

Definition at line 553 of file FBString.h.

553  {
554  return offsetof(RefCounted, data_);
555  }
template<class Char>
static void folly::fbstring_core< Char >::RefCounted::incrementRefs ( Char *  p)
inlinestatic

Definition at line 567 of file FBString.h.

567  {
568  fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
569  }
std::atomic< size_t > refCount_
Definition: FBString.h:550
static RefCounted * fromData(Char *p)
Definition: FBString.h:557
template<class Char>
static RefCounted* folly::fbstring_core< Char >::RefCounted::reallocate ( Char *const  data,
const size_t  currentSize,
const size_t  currentCapacity,
size_t *  newCapacity 
)
inlinestatic

Definition at line 598 of file FBString.h.

References testing::gmock_generated_actions_test::Char(), FBSTRING_ASSERT, folly::goodMallocSize(), and folly::smartRealloc().

602  {
603  FBSTRING_ASSERT(*newCapacity > 0 && *newCapacity > currentSize);
604  const size_t allocNewCapacity =
605  goodMallocSize(getDataOffset() + (*newCapacity + 1) * sizeof(Char));
606  auto const dis = fromData(data);
607  FBSTRING_ASSERT(dis->refCount_.load(std::memory_order_acquire) == 1);
608  auto result = static_cast<RefCounted*>(smartRealloc(
609  dis,
610  getDataOffset() + (currentSize + 1) * sizeof(Char),
611  getDataOffset() + (currentCapacity + 1) * sizeof(Char),
612  allocNewCapacity));
613  FBSTRING_ASSERT(result->refCount_.load(std::memory_order_acquire) == 1);
614  *newCapacity = (allocNewCapacity - getDataOffset()) / sizeof(Char) - 1;
615  return result;
616  }
static constexpr size_t getDataOffset()
Definition: FBString.h:553
const Char * data() const
Definition: FBString.h:432
static RefCounted * fromData(Char *p)
Definition: FBString.h:557
#define FBSTRING_ASSERT(expr)
Definition: FBString.h:64
void * smartRealloc(void *p, const size_t currentSize, const size_t currentCapacity, const size_t newCapacity)
Definition: Malloc.h:261
size_t goodMallocSize(size_t minSize) noexcept
Definition: Malloc.h:201
template<class Char>
static size_t folly::fbstring_core< Char >::RefCounted::refs ( Char *  p)
inlinestatic

Definition at line 563 of file FBString.h.

563  {
564  return fromData(p)->refCount_.load(std::memory_order_acquire);
565  }
std::atomic< size_t > refCount_
Definition: FBString.h:550
static RefCounted * fromData(Char *p)
Definition: FBString.h:557

Member Data Documentation

template<class Char>
Char folly::fbstring_core< Char >::RefCounted::data_[1]

Definition at line 551 of file FBString.h.

template<class Char>
std::atomic<size_t> folly::fbstring_core< Char >::RefCounted::refCount_

Definition at line 550 of file FBString.h.

Referenced by folly::fbstring_core< Char >::RefCounted::create().


The documentation for this struct was generated from the following file: