/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  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 <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include       "syswork.h"


namespace cryptrav
{

namespace SYSTEM
{


// System-dependent part...


#ifdef Q_OS_WIN // Windows implementation


#include <windows.h>
#include <winbase.h>

qint64         getAvailPhysMemory(void)
{
 MEMORYSTATUSEX        memoryStatusEx;

 memoryStatusEx.dwLength = sizeof(memoryStatusEx); // must be set - a change from MEMORYSTATUS usage, mentioned in MSDN

 if ( !GlobalMemoryStatusEx(&memoryStatusEx) ) {
   return (-1);
  }

 return memoryStatusEx.ullAvailPhys;
}


#elif defined Q_OS_LINUX // Linux implementation


#include <sys/sysinfo.h>

qint64         getAvailPhysMemory(void)
{
 struct sysinfo        info;

 if (sysinfo(&info) != 0) {
  return 0;
 }

// return info.freeram; // for Linux prior to 2.3.16 - without sysinfo.mem_unit

 return ( ((qint64)info.freeram) * ((qint64)info.mem_unit) ); // for Linux since 2.3.23 (i386), 2.3.48 (all other) - with sysinfo.mem_unit

// (check "man sysinfo" on this subject)
}


#endif // Q_OS_WIN | Q_OS_LINUX


} // namespace "SYSTEM"

} // namespace "cryptrav"
