34#ifndef SOMAIO_ALLOCATOR_ALLOCATOR_H
35#define SOMAIO_ALLOCATOR_ALLOCATOR_H
41#include <cartobase/config/cartobase_config.h>
42#include <cartobase/smart/rcptr.h>
67 virtual char *
allocate(
size_t n,
size_t elemsize,
69 virtual void deallocate(
char *ptr,
size_t n,
size_t elemsize )
const = 0;
87 virtual char *
allocate(
size_t n,
size_t elemsize,
89 virtual void deallocate(
char *ptr,
size_t n,
size_t sz )
const;
115 virtual void deallocate(
char *ptr,
size_t n,
size_t sz )
const;
127 std::ostream& operator << ( std::ostream& os,
209 float usefactor = 1 );
213 bool scalefactored =
false,
269 bool isDiskformatOK =
false,
270 float usefactor = 1 );
274 float usefactor = 1 );
277 const std::string & filename,
279 bool isDiskformatOK =
false,
280 float usefactor = 1 );
291 template<
typename T> T*
allocate( T*& ptr,
size_t n )
const;
292 template<
typename T>
void deallocate( T* ptr,
size_t n )
const;
324 mutable bool _allocated;
325 mutable bool _forced;
339 if( !_alloc || !_forced ) {
350 ptr =
reinterpret_cast<T*
>( _alloc->allocate( n,
sizeof( T ), _datasource.get() ) );
352 #warning uninitialized_fill_n not present in Microsoft standard includes
356 std::uninitialized_fill_n( ptr, n, T() );
370#if __cplusplus >= 201100
373 for(
size_t i=0; i<n; ++i )
376 std::_Destroy( ptr, ptr + n );
379 _alloc->deallocate(
reinterpret_cast<char*
>( ptr ), n,
sizeof( T ) );
bool allowsMemoryMapping() const
void deallocate(T *ptr, size_t n) const
AllocatorContext(DataAccess mode, const std::string &filename, offset_t offset=0, bool isDiskformatOK=false, float usefactor=1)
Constructor from a filename.
AllocatorStrategy::DataAccess DataAccess
MappingMode allocatorType() const
void setAllowsMemoryMapping(bool x)
DataAccess accessMode() const
T * allocate(T *&ptr, size_t n) const
return value is the same as modifiable input param ptr
void setDataSourceInfo(carto::rc_ptr< DataSourceInfo > dsi)
AllocatorContext becomes owner od datasourceinfo.
AllocatorContext(const AllocatorContext &)
Constructor by copy.
void setDataSource(carto::rc_ptr< DataSource > datasource)
AllocatorContext becomes owner of datasource.
AllocatorContext(const LowLevelAllocator *alloc)
Constructor from a LLA.
bool canDuplicate() const
AllocatorContext(DataAccess mode, float usefactor)
Constructor without pre-existing DataSource.
AllocatorContext(DataAccess mode, carto::rc_ptr< DataSourceInfo > datasourceinfo, float usefactor=1)
Constructor from a DataSourceInfo.
carto::rc_ptr< DataSourceInfo > dataSourceInfo()
void setAccessMode(DataAccess mode)
static const AllocatorContext & fast()
AllocatorContext(DataAccess mode=AllocatorStrategy::InternalModif, carto::rc_ptr< DataSource > datasource=DataSource::none(), bool isDiskformatOK=false, float usefactor=1)
Constructor from a DataSource.
const carto::rc_ptr< DataSourceInfo > dataSourceInfo() const
carto::rc_ptr< DataSource > dataSource()
const carto::rc_ptr< DataSource > dataSource() const
AllocatorStrategy::MappingMode MappingMode
void setUseFactor(float x)
Determination of the allocation type depending of the buffer size to allocate and the disk format of ...
static bool isMMapCompatible(bool ascii, int byteorder, bool scalefactored=false, int border=0)
helper function, tells if memory mapping is directly possible
static const LowLevelAllocator & lowLevelAllocator(MappingMode m)
static AllocatorContext allocator(MappingMode mode, carto::rc_ptr< DataSource > ds)
static void memSizes(offset_t &ram, offset_t &freeram, offset_t &swap)
same as above but uses a Header attributed object
DataAccess
Data access modes, they describe what you intend to do with data to be read.
@ ReadWrite
Mode for read/write access, the input file will be modified and needs read/write permissions.
@ NotOwner
This value is used for sub-volumes or fake volumes using data that are already allocated by another s...
@ InternalModif
Mode for internal modification.
@ ReadOnly
Mode for read-only access.
static MappingMode mappingMode(DataAccess mode, offset_t buflen, const DataSource *datasource, bool isDiskformatOK, float usefactor=1)
Determines optimal allocator for a data file.
Abstraction layer for various data sources (file, buffer, socket...).
static const carto::rc_ptr< DataSource > none()
An empty ref-counter that is more convenient than calling a constructor of rc_ptr<DataSource> (useful...
Abstract base class for actual allocators types.
virtual char * allocate(size_t n, size_t elemsize, DataSource *datasource) const =0
virtual ~LowLevelAllocator()
virtual bool canDuplicate() const
virtual void deallocate(char *ptr, size_t n, size_t elemsize) const =0
Normal allocation mode: allocation in main memory.
virtual char * allocate(size_t n, size_t elemsize, DataSource *datasource) const
static const MemoryAllocator & singleton()
virtual ~MemoryAllocator()
virtual void deallocate(char *ptr, size_t n, size_t sz) const
This allocator doesn't allocate anything.
static const NullAllocator & singleton()
virtual void deallocate(char *ptr, size_t n, size_t sz) const
virtual char * allocate(size_t n, size_t elemsize, DataSource *datasource) const
unsigned long long offset_t
Offsets are 64 bits if supported.