[Date Prev][Date Next][Thread Prev][Thread Next][Thread Index]

Re: [XaraXtreme-dev] [patch] Have implemented a Linux port of GetMemoryStatus ("Function to find available RAM")



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
 
Hello everyone,

I have read and signed the Contributor's Agreement, and sent it to
developers@xxxxxxxxx

I am attaching the patch that implements the Linux version of
GetMemoryStatus, made against the SVN repository with diff -dup.

ChangeLog entry:

2006-05-27  Israel G. Lugo  <ilugo@xxxxxxxxxxxxxxxxxx>

    * memory.cpp (GetMemoryStatus): Implemented Linux version.


Regards,
Israel

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
 
iD8DBQFEeLDPgn9msFnMGkcRAt6wAJsEB5nis158zcKLZ56Ypw1W80HCigCgtdZw
Mh8EztHc89AVBqrTK9a09Gc=
=eIse
-----END PGP SIGNATURE-----

Index: memory.cpp
===================================================================
--- memory.cpp	(revision 1199)
+++ memory.cpp	(working copy)
@@ -118,6 +118,10 @@
 #include "ralphcri.h"
 #endif
 
+#if !defined(__WXMSW__) && !defined(__WXMAC__)
+#  include <stdio.h>
+#endif
+
 DECLARE_SOURCE("$Revision$");
 
 
@@ -1023,10 +1027,43 @@
 		if (pLoadPercent) *pLoadPercent = memStatus.dwMemoryLoad;
 		if (pPhysRam) *pPhysRam = memStatus.dwTotalPhys;
 	}
-#else
-	PORTNOTETRACE("other", "GetMemoryStatus is not implemented on this architecture");
+#elif defined(__WXMAC__)
+	PORTNOTETRACE("other", "GetMemoryStatus is not implemented for WXMAC");
 	if (pPhysRam) *pPhysRam = 512L * 1024 * 1024;	// Guess 512M
 	if (pLoadPercent) *pLoadPercent = 50;			// Guess 50% free
+#else
+	/* Linux: read memory information from the kernel's /proc/meminfo interface */
+	FILE *fp;
+	unsigned long memTotalKb, memFreeKb;
+	int haveMemTotal, haveMemFree;
+	char lineBuf[256];
+
+	fp = fopen("/proc/meminfo", "r");
+
+	if (fp != NULL)
+	{
+		while (!haveMemTotal || !haveMemFree)
+		{
+			if (fgets(lineBuf, 256, fp) == NULL)
+				break;
+
+			if (sscanf(lineBuf, "MemTotal: %lu", &memTotalKb) == 1)
+				haveMemTotal = 1;
+			if (sscanf(lineBuf, "MemFree: %lu", &memFreeKb) == 1)
+				haveMemFree = 1;
+		}
+		fclose(fp);
+	}
+
+	if (!haveMemTotal)
+		memTotalKb = 512UL * 1024;	/* guess 512MB */
+	if (!haveMemFree)
+		memFreeKb = memTotalKb / 2;	/* guess 50% free */
+
+	if (pPhysRam != NULL)
+		*pPhysRam = (UINT64)memTotalKb * 1024;
+	if (pLoadPercent != NULL)
+		*pLoadPercent = (UINT32)(100UL - ((memFreeKb * 100UL) / memTotalKb));
 #endif
 }