You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.1 KiB

// Copyright 2020 Siemens Digital Industries Software
// ==================================================
// Copyright 2013.
// Siemens Product Lifecycle Management Software Inc.
// All Rights Reserved.
// ==================================================
// Copyright 2020 Siemens Digital Industries Software
/**
@file
This file contains an STL allocator class that uses SM (StorageManagement module from NX) persistent memory
as its underlying storage area.
<br>One of the key features of SM is that allocated memory is returned to the Operating System once deallocated. This means
that the memory footprint of the application just not increases, but can also decrease significantly.
*/
/* */
/* */
#ifndef SM_ALLOCATOR_HXX_INCLUDED
#define SM_ALLOCATOR_HXX_INCLUDED
#include <base_utils/Mem.h>
#include <limits.h>
#ifdef SGI
#include <new.h>
#define SGISTATIC static
#else
#include <new>
#define SGISTATIC
#endif //SGI
// USE_REBIND_CLASS used to be conditional in ifdef blocks for each platform.
// However, since ALL platforms are now covered, removed all ifdef blocks to
// make it ONE SINGLE statement.
#define USE_REBIND_CLASS
#ifdef SOLARIS
#define OVERLOAD_MAX_SIZE
#endif
// Disable spurious warning C4100: 'p' unreferenced formal parameter
// from 'void destroy(pointer p)'
#if defined(WNT) || defined(WNT32)
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma warning(push)
#endif /* _MSC_VER >= 1200 */
#pragma warning(disable:4100)
#endif /* _MSC_VER */
/**
@brief Provides a way to leverage NX Storage Management (SM) for memory allocation in STL library.
One of the key features of SM is that allocated memory is returned to the Operating System once deallocated. This means
that the memory footprint of the application just not increases, but can also decrease significantly.
@code
class MyClass
{
public:
MyClass(const std::string& iParameter);
virtual ~MyClass();
[...]
};
typedef std::vector< MyClass, sm_allocator<MyClass> > MyClassVector;
[...]
MyClassVector.push_back( MyClassString("InstanceI") );
@endcode
None of the methods defined in this class is of use to customizers.
*/
template<class T>
class sm_allocator
{
public:
#ifdef USE_REBIND_CLASS
template<class U>
class rebind
{
public:
typedef sm_allocator<U> other;
};
template <class U> sm_allocator(const sm_allocator<U>&)
{
}
#endif
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
sm_allocator()
{
}
sm_allocator<T>& operator=(const sm_allocator<T>&)
{
return *this;
}
SGISTATIC pointer allocate(size_type n, const void * /*hint*/ = NULL)
{
size_type alloc_size = n * sizeof(T);
/* On Solaris, the parameter "n" is the number of bytes and not the number of elements, */
/* so don't multiply it by the sizeof(T) -- someone else has already done that. */
/*
#if defined(SOLARIS)
alloc_size = n;
#endif
*/
return (T *)MEM_alloc(alloc_size);
}
char *_Charalloc(size_type n)
{
return (char *)MEM_alloc(n);
}
SGISTATIC void deallocate(pointer p, size_type /*n*/ = 0)
{
MEM_free(p);
}
void construct(pointer p, const T& val)
{
new (p) T(val);
}
void destroy(pointer p)
{
p->~T();
}
size_type max_size() const
{
return UINT_MAX / sizeof(T);
}
#ifdef OVERLOAD_MAX_SIZE
size_type max_size(size_type size) const
{
return 1 > UINT_MAX/size ? size_type(1) : size_type(UINT_MAX/size);
}
#endif
bool operator == (const sm_allocator &/*other*/) const
{
return true;
}
bool operator != (const sm_allocator &/*other*/) const
{
return false;
}
};
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma warning(pop)
#endif /* _MSC_VER */
#endif