Function set_capacity

Synopsis

#include <include/EASTL/string.h>

void set_capacity(size_type n=npos)

Description

No description yet.

Source

Lines 1385-1440 in include/EASTL/string.h. Line 600 in include/EASTL/string.h.

template <typename T, typename Allocator>
inline void basic_string<T, Allocator>::set_capacity(size_type n)
{
    if(n == npos)
        // If the user wants to set the capacity to equal the current size...
        // '-1' because we pretend that we didn't allocate memory for the terminating 0.
        n = internalLayout().GetSize();
    else if(n < internalLayout().GetSize())
    {
        internalLayout().SetSize(n);
        *internalLayout().EndPtr() = 0;
    }
    if((n < capacity() && internalLayout().IsHeap()) || (n > capacity()))
    {
        // In here the string is transition from heap->heap, heap->sso or sso->heap
        if(EASTL_LIKELY(n))
        {
            if(n <= SSOLayout::SSO_CAPACITY)
            {
                // heap->sso
                // A heap based layout wants to reduce its size to within sso capacity
                // An sso layout wanting to reduce its capacity will not get in here
                pointer pOldBegin = internalLayout().BeginPtr();
                const size_type nOldCap = internalLayout().GetHeapCapacity();
                CharStringUninitializedCopy(pOldBegin, pOldBegin + n, internalLayout().SSOBeginPtr());
                internalLayout().SetSSOSize(n);
                *internalLayout().SSOEndPtr() = 0;
                DoFree(pOldBegin, nOldCap + 1);
                return;
            }
            pointer pNewBegin = DoAllocate(n + 1); // We need the + 1 to accomodate the trailing 0.
            size_type nSavedSize = internalLayout().GetSize(); // save the size in case we transition from sso->heap
            pointer pNewEnd = CharStringUninitializedCopy(internalLayout().BeginPtr(), internalLayout().EndPtr(), pNewBegin);
            *pNewEnd = 0;
            DeallocateSelf();
            internalLayout().SetHeapBeginPtr(pNewBegin);
            internalLayout().SetHeapCapacity(n);
            internalLayout().SetHeapSize(nSavedSize);
        }
        else
        {
            DeallocateSelf();
            AllocateSelf();
        }
    }
}





Add Discussion as Guest

Log in