janwas
0bb0df5b2c
- remove headers always included from PCH - nommgr.h is only included ifdef REDEFINED_NEW (allows leaving out the mmgr stuff) - in lib/*.cpp, moved the corresponding include file to right behind the PCH (catches headers that aren't compilable by themselves) - byte_order no longer depends on SDL - add debug_level (another means of filtering debug output; needed for thesis) - split posix stuff up into subdirs (lib/posix and sysdep/win/wposix). makes including only some of the modules (e.g. sockets, time) much easier. This was SVN commit r4728.
115 lines
3.2 KiB
C++
115 lines
3.2 KiB
C++
/**
|
|
* =========================================================================
|
|
* File : wposix.cpp
|
|
* Project : 0 A.D.
|
|
* Description : emulate a subset of POSIX on Win32.
|
|
*
|
|
* @author Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
* =========================================================================
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2004-2005 Jan Wassenberg
|
|
*
|
|
* Redistribution and/or modification are also permitted under the
|
|
* terms of the GNU General Public License as published by the
|
|
* Free Software Foundation (version 2 or later, at your option).
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include "precompiled.h"
|
|
#include "wposix.h"
|
|
|
|
#include "wposix_internal.h"
|
|
|
|
|
|
long sysconf(int name)
|
|
{
|
|
// used by _SC_*_PAGES
|
|
static DWORD page_size;
|
|
static BOOL (WINAPI *pGlobalMemoryStatusEx)(MEMORYSTATUSEX*);
|
|
|
|
ONCE(
|
|
{
|
|
// get page size
|
|
// (used by _SC_PAGESIZE and _SC_*_PAGES)
|
|
SYSTEM_INFO si;
|
|
GetSystemInfo(&si); // can't fail => page_size always > 0.
|
|
page_size = si.dwPageSize;
|
|
|
|
// import GlobalMemoryStatusEx - it's not defined by the VC6 PSDK.
|
|
// used by _SC_*_PAGES if available (provides better results).
|
|
const HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
|
*(void**)&pGlobalMemoryStatusEx = GetProcAddress(hKernel32Dll, "GlobalMemoryStatusEx");
|
|
FreeLibrary(hKernel32Dll);
|
|
// make sure the reference is released so BoundsChecker
|
|
// doesn't complain. it won't actually be unloaded anyway -
|
|
// there is at least one other reference.
|
|
}
|
|
);
|
|
|
|
|
|
switch(name)
|
|
{
|
|
case _SC_PAGESIZE:
|
|
// note: don't add _SC_PAGE_SIZE - they are different names but
|
|
// have the same value.
|
|
return page_size;
|
|
|
|
case _SC_PHYS_PAGES:
|
|
case _SC_AVPHYS_PAGES:
|
|
{
|
|
u64 total_phys_mem;
|
|
u64 avail_phys_mem;
|
|
|
|
// first try GlobalMemoryStatus - cannot fail.
|
|
// override its results if GlobalMemoryStatusEx is available.
|
|
MEMORYSTATUS ms;
|
|
GlobalMemoryStatus(&ms);
|
|
// can't fail.
|
|
total_phys_mem = ms.dwTotalPhys;
|
|
avail_phys_mem = ms.dwAvailPhys;
|
|
|
|
// newer API is available: use it to report correct results
|
|
// (no overflow or wraparound) on systems with > 4 GB of memory.
|
|
MEMORYSTATUSEX mse = { sizeof(mse) };
|
|
if(pGlobalMemoryStatusEx && pGlobalMemoryStatusEx(&mse))
|
|
{
|
|
total_phys_mem = mse.ullTotalPhys;
|
|
avail_phys_mem = mse.ullAvailPhys;
|
|
}
|
|
// else: not an error, since this isn't available before Win2k / XP.
|
|
// we have results from GlobalMemoryStatus anyway.
|
|
|
|
if(name == _SC_PHYS_PAGES)
|
|
return (long)(round_up((uintptr_t)total_phys_mem, 2*MiB) / page_size);
|
|
// Richter, "Programming Applications for Windows":
|
|
// reported value doesn't include non-paged pool reserved
|
|
// during boot; it's not considered available to kernel.
|
|
// it's 528 KiB on my 512 MiB machine (WinXP and Win2k).
|
|
else
|
|
return (long)(avail_phys_mem / page_size);
|
|
}
|
|
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#ifdef REDEFINED_NEW
|
|
# include "lib/nommgr.h"
|
|
#endif
|
|
char* getcwd(char* buf, size_t buf_size)
|
|
{
|
|
return _getcwd(buf, buf_size);
|
|
}
|
|
#ifdef REDEFINED_NEW
|
|
# include "lib/mmgr.h"
|
|
#endif
|