本文整理汇总了C++中std::allocator类的典型用法代码示例。如果您正苦于以下问题:C++ allocator类的具体用法?C++ allocator怎么用?C++ allocator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了allocator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int, char**)
{
const std::allocator<int> a;
std::size_t M = a.max_size();
assert(M > 0xFFFF && M <= (std::numeric_limits<std::size_t>::max() / sizeof(int)));
return 0;
}
示例2: test_address
void test_address()
{
T* tp = new T();
const T* ctp = tp;
const std::allocator<T> a;
assert(a.address(*tp) == tp);
assert(a.address(*ctp) == tp);
delete tp;
}
示例3: PSFastMemFree
namespace NL3D
{
#ifdef PS_FAST_ALLOC
uint NumPSAlloc = 0;
uint NumDealloc = 0;
NLMISC::CContiguousBlockAllocator *PSBlockAllocator= NULL;
static std::allocator<uint8> PSStdAllocator;
//
typedef NLMISC::CContiguousBlockAllocator *TBlocAllocPtr;
//
struct CPSAllocInfo
{
size_t NumAllocatedBytes;
TBlocAllocPtr BlocAllocator; // may be NULL if was allocated from stl allocator
};
//
void *PSFastMemAlloc(uint numBytes)
{
NL_PS_FUNC(PSFastMemAlloc)
CPSAllocInfo *result;
// if a block allocator is available, use it
if (PSBlockAllocator)
{
result = (CPSAllocInfo *) PSBlockAllocator->alloc(numBytes + sizeof(CPSAllocInfo));
result->BlocAllocator = PSBlockAllocator; // mark as a block from block allocator
}
else
{
result = (CPSAllocInfo *) PSStdAllocator.allocate(numBytes + sizeof(CPSAllocInfo));
result->BlocAllocator = NULL;
}
result->NumAllocatedBytes = numBytes;
return (void *) (result + 1); // usable space starts after header
}
void PSFastMemFree(void *block)
{
NL_PS_FUNC(PSFastMemFree)
uint8 *realAddress = (uint8 *) ((uint8 *) block - sizeof(CPSAllocInfo));
CPSAllocInfo *ai = (CPSAllocInfo *) realAddress;
if (ai->BlocAllocator)
{
// block comes from a block allocator
ai->BlocAllocator->free((void *) realAddress, ai->NumAllocatedBytes + sizeof(CPSAllocInfo));
}
else
{
// block comes from the stl allocator
PSStdAllocator.deallocate((uint8 *) realAddress, ai->NumAllocatedBytes + sizeof(CPSAllocInfo));
}
}
#endif
}
示例4: sizeof
void *PSFastMemAlloc(uint numBytes)
{
NL_PS_FUNC(PSFastMemAlloc)
CPSAllocInfo *result;
// if a block allocator is available, use it
if (PSBlockAllocator)
{
result = (CPSAllocInfo *) PSBlockAllocator->alloc(numBytes + sizeof(CPSAllocInfo));
result->BlocAllocator = PSBlockAllocator; // mark as a block from block allocator
}
else
{
result = (CPSAllocInfo *) PSStdAllocator.allocate(numBytes + sizeof(CPSAllocInfo));
result->BlocAllocator = NULL;
}
result->NumAllocatedBytes = numBytes;
return (void *) (result + 1); // usable space starts after header
}
示例5: free
void String::free()
{
if (elements)
{
std::for_each(elements,end,
[this](char& c) {alloc.destroy(&c);});
alloc.deallocate(elements,end-elements);
}
}
示例6: free
/**
* @brief destroy the elements and deallocate the space previously allocated.
*/
void StrVec::free()
{
if (element) // if not nullptr
{
//! destory it in reverse order.
for (auto p = first_free; p != element; /* empty */)
alloc.destroy(--p);
alloc.deallocate(element, capacity());
}
}
示例7: free
void free()
{
// may not pass deallocate() a nullptr.
if (element_)
{
// move backward and use "--p" to delete [element_, free)
// call type dtor, which is string dtor.
for (auto p = free_; p != element_; /* empty */ )
alloc.destroy(--p);
alloc.deallocate(element_, cap_ - element_);
}
}
示例8: reallocate
void str_vec::reallocate()
{
auto new_capacity = size()>1?size()*2:1;
auto new_data = alloc.allocate(new_capacity);
auto dest = new_data;
auto elem = elements;
for(std::size_t i = 0;i<size();++i)
alloc.construct(dest++,std::move(*elem++));
free();
elements = new_data;
first_free = dest;
std::cout << cap - elements << std::endl;
cap = new_data+new_capacity;
}
示例9:
static void *l_realloc_func(void *b, int os, int s)
{
if (os == s) return b;
void *newB = l_stlAlloc.allocate(s);
memcpy(newB, b, std::min(os, s));
l_free_func(b, os);
return newB;
}
示例10:
std::pair<std::string *, std::string *>
alloc_and_copy(const std::string *begin, const std::string *end)
{
// allocate `unconstructed` space to hold elements
// *cxx-iter-arithmetic*
auto data = alloc.allocate(end - begin);
// uninitialized_copy()
// * construct copies of given elements in uninitialized space
// * returns the position after the last initialized element.
return {data, uninitialized_copy(begin, end, data)};
}
示例11:
/**
* @brief allocate new space for the given range and copy them into it
* @param b
* @param e
* @return a pair of pointers pointing to [first element , one past the last) in the new space
*/
std::pair<std::string *, std::string *>
StrVec::alloc_n_copy(std::string *b, std::string *e)
{
//! calculate the size needed and allocate space accordingly
std::string* data = alloc.allocate(e - b);
return{ data, std::uninitialized_copy(b, e, data) };
//! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! which copies the range [first,last) into the space of which
//! the starting address p_data is pointing to.
//! This function returns a pointer pointing to one past the last element.
}
示例12: init
/**
@brief �ͷ�ԭ�ȵĿռ䲢���·���
�൱�ڰ��³ߴ����¹���һ���� AutoGrownMemIO
����Ҫ�Ѿ����ݿ������µ�ַ
*/
void AutoGrownMemIO::init(size_t newsize)
{
// if (m_beg)
// ::free(m_beg);
// m_pos = m_beg = m_end = 0;
// m_beg = (byte*)::malloc(newsize);
size_t oldSize = size();
try {
if (m_beg) {
G_byteAlloc.deallocate(m_beg, oldSize);
m_pos = m_beg = m_end = 0;
}
m_beg = G_byteAlloc.allocate(newsize);
}
catch (const std::exception& exp)
{
std::ostringstream oss;
oss << "at " << BOOST_CURRENT_FUNCTION << ": size=" << oldSize << ", newsize=" << newsize
<< ", nested-exption[, type=" << typeid(exp).name() << ", what=" << exp.what() << "]";
throw std::runtime_error(oss.str());
}
if (0 == m_beg)
{
#ifdef _MSC_VER
std::ostringstream oss;
oss << "alloc failed in \"" << BOOST_CURRENT_FUNCTION
<< "\", with capacity=" << newsize
<< ", [this=" << (void*)(this)
<< "] was partly mutated and is not in consistent state!";
throw std::bad_alloc(oss.str().c_str());
#else
throw std::bad_alloc();
#endif
}
m_pos = m_beg;
m_end = m_beg + newsize;
}
示例13: wy_alloc_n_move
/**
* @brief allocate memory for spicified number of elements
* @param n
* @note it's user's responsibility to ensure that @param n is greater than
* the current capacity.
*/
void StrVec::wy_alloc_n_move(std::size_t n)
{
std::size_t newCapacity = n;
std::string*
newData = alloc.allocate(newCapacity);
std::string*
dest = newData;
std::string*
elem = element;
//! move the old to newly allocated space.
for (std::size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
//! update data structure
element = newData;
first_free = dest;
cap = element + newCapacity;
}
示例14: resize
/**
@brief �ı� buffer �ߴ�
���ı� buffer �е��Ѵ����ݣ����ı� pos
@note must m_pos <= newsize
*/
void AutoGrownMemIO::resize(size_t newsize)
{
assert(tell() <= newsize);
// byte* newbeg = (byte*)::realloc(m_beg, newsize);
byte* newbeg = 0;
try {
newbeg = G_byteAlloc.allocate(newsize);
}
catch (const std::exception& exp)
{
std::ostringstream oss;
oss << "at " << BOOST_CURRENT_FUNCTION << ": size=" << size() << ", newsize=" << newsize
<< ", nested-exption[, type=" << typeid(exp).name() << ", what=" << exp.what() << "]";
throw std::runtime_error(oss.str());
}
if (newbeg)
{
memcpy(newbeg, m_beg, size());
G_byteAlloc.deallocate(m_beg, size() );
m_pos = newbeg + (m_pos - m_beg);
m_beg = newbeg;
m_end = newbeg + newsize;
}
else
{
#ifdef _MSC_VER
std::ostringstream oss;
oss << "realloc failed in \"void AutoGrownMemIO::resize(newsize=" << newsize
<< ")\", the AutoGrownMemIO object is not mutated!";
throw std::bad_alloc(oss.str().c_str());
#else
throw std::bad_alloc();
#endif
}
}
示例15: runtime_error
AutoGrownMemIO::AutoGrownMemIO(size_t size)
{
// m_beg = 0 == size ? 0 : (byte*)::malloc(size);
try {
m_beg = 0 == size ? 0 : G_byteAlloc.allocate(size);
}
catch (const std::exception& exp)
{
std::ostringstream oss;
oss << "at " << BOOST_CURRENT_FUNCTION << ": size=" << size
<< ", nested-exption[, type=" << typeid(exp).name() << ", what=" << exp.what() << "]";
throw std::runtime_error(oss.str());
}
m_end = m_beg + size;
m_pos = m_beg;
}