//定义了与内存状态和系统运行时间有关的平台无关的接口
//作者:刘成彬
//适合平台:WindowsXP, WindowsCE, Linux, Android
#ifndef _MINI_SUN_PLATFORM_MEM_TIME_H_
#define _MINI_SUN_PLATFORM_MEM_TIME_H_
//对Android平台,也认为是linux
#ifdef ANDROID
#define linux ANDROID
#endif
//包含头文件
#ifdef _WIN32
#include
#include
#endif
#ifdef linux
#include
#include
#include
#endif
//内存状态信息
typedef struct MEMINFO
{
unsigned long dwTotalPhys;//所有可用RAM大小
unsigned long dwAvailPhys;//未使用的内存
unsigned long dwTotalPageFile;//交换空间的总大小
unsigned long dwAvailPageFile;//未被使用交换空间的大小
unsigned long dwTotalVirtual; // user bytes of address space
unsigned long dwAvailVirtual; // free user bytes
}MEMINFO,*LPMEMINFO;
//获取内存状态
void AGlobalMemoryStatus(LPMEMINFO meminfo)
{
FILE *fh;
char text[1024] = {'\0'};
char key[32] = {'\0'};
char val[32] = {'\0'};
if ( (fh = fopen( "/proc/meminfo", "r" ))== NULL )
{
return;
}
while( !feof(fh) && fgets(text, 1024, fh) )
{
sscanf( text, "%[^:]: %s", key, val );
if( strcmp(key, "MemTotal") == 0 ) { meminfo->dwTotalPhys = atol( val );}
else if( strcmp(key, "MemFree") == 0 ){ meminfo->dwAvailPhys = atol(val);}
else if( strcmp(key, "SwapTotal") == 0 ){ meminfo->dwTotalPageFile = atol(val);}
else if( strcmp(key, "SwapFree") == 0 ){ meminfo->dwAvailPageFile = atol(val);}
else if( strcmp(key, "LowFree") == 0 ){ meminfo->dwTotalVirtual= atol(val);}
else if( strcmp(key, "LowTotal") == 0 ){ meminfo->dwAvailVirtual = atol(val);}
}
fclose(fh);
}
//获取系统运行时间
unsigned long AGetTickCount()
{
FILE *fh;
char text[1024] = {'\0'};
double key;
double val;
if ( (fh = fopen( "/proc/uptime", "r" ))== NULL )
{
return;
}
fgets(text, 1024, fh);
sscanf( text, "%lf %lf", &key, &val );
fclose(fh);
return key * 1000;
}
//内存状态函数参数类型:TypeMemArg
#ifdef _WIN32
typedef MEMORYSTATUS TypeMemArg;
#endif
#ifdef linux
typedef MEMINFO TypeMemArg;
#endif
//运行时间函数返回值类型:TypeTimeReturn
#ifdef _WIN32
typedef DWORD TypeTimeReturn;
#endif
#ifdef linux
typedef unsigned long TypeTimeReturn;
#endif
//获取内存状态方法
//VOID GlobalMemoryStatus(TypeMemArg b ); // 返回值存放在结构体
#ifdef _WIN32
#define MEMSTATUS(b) (GlobalMemoryStatus(&(b)))
#endif
#ifdef linux
#define MEMSTATUS(b) (AGlobalMemoryStatus(&(b)))
#endif
//获取系统运行时间方法
//DWORD GetTickCount(VOID);
#ifdef _WIN32
#define GetTickCount() (GetTickCount())
#endif
#ifdef linux
#define GetTickCount() (AGetTickCount())
#endif
#endif //_MINI_SUN_PLATFORM_MEM_TIME_H_
阅读(862) | 评论(0) | 转发(0) |