//------------------------------------------------------------------------------
//! @file BufferManager.cc
//! @author Elvin-Alin Sindrilaru - CERN
//------------------------------------------------------------------------------
/************************************************************************
* EOS - the CERN Disk Storage System *
* Copyright (C) 2021 CERN/Switzerland *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see .*
************************************************************************/
#include "common/BufferManager.hh"
EOSCOMMONNAMESPACE_BEGIN
//------------------------------------------------------------------------------
// Get the nearest power of 2 value bigger then the given input but always
// greater than min
//------------------------------------------------------------------------------
uint32_t GetPowerCeil(const uint32_t input, const uint32_t min)
{
uint32_t power = min;
while (input > power) {
power <<= 1;
}
return power;
}
//------------------------------------------------------------------------------
// Get amount of system memory
//------------------------------------------------------------------------------
uint64_t GetSystemMemorySize()
{
static uint64_t total_size = 0ull;
if (!total_size) {
uint64_t pages = sysconf(_SC_PHYS_PAGES);
uint64_t page_size = sysconf(_SC_PAGE_SIZE);
total_size = pages * page_size;
}
return total_size;
}
//------------------------------------------------------------------------------
// Get OS page size aligned buffer
//------------------------------------------------------------------------------
std::unique_ptr
GetAlignedBuffer(const size_t size)
{
static long os_pg_size = sysconf(_SC_PAGESIZE);
char* raw_buffer = nullptr;
std::unique_ptr buffer
((char*) raw_buffer, [](void* ptr) {
if (ptr) {
free(ptr);
}
});
if (posix_memalign((void**) &raw_buffer, os_pg_size, size)) {
return buffer;
}
buffer.reset(raw_buffer);
return buffer;
}
EOSCOMMONNAMESPACE_END