TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: CAREY BLOODWORTH
from: DARIN MCBRIDE
date: 1998-04-29 19:19:00
subject: FREE MEMORY SPACE CALC?

 CB> I'm needing some suggestions on how to reasonably portable calculate the
 CB> amount of free memory left.
OS/2: Use the API.  Mind you, there's much more information than you need 
here... ;-)
Sample code (I hope this is explicit enough)... explicitly public domain (if 
you find a use for it...)
#define INCL_DOS
#include 
#include 
#include 
int main()
{
    const unsigned long start = QSV_TOTPHYSMEM;
    const unsigned long last  = QSV_MAXSHMEM;
    unsigned long aulVals[last - start + 1];
    unsigned long ulRC = DosQuerySysInfo(start,
                                         last,
                                         aulVals,
                                         sizeof(aulVals));
    if (0 == ulRC)
    {
        printf("All values in bytes.\n");
        printf("Total Physical    Memory: %lu\n", aulVals[0]);
        printf("Total Resident    Memory: %lu\n", aulVals[1]);
        printf("Total Available   Memory: %lu\n", aulVals[2]);
        printf("Available Private Memory: %lu\n", aulVals[3]);
        printf("Available Shared  Memory: %lu\n", aulVals[4]);
        {
            const unsigned long size = aulVals[0] - 1024;
            void *p = malloc(size);
            if (NULL != p)
            {
                printf("Allocated %lu bytes: memory at %p\n", size, p);
                {
                    void *p2 = malloc(2048);
                    if (NULL != p2)
                    {
                        printf("Allocated another 2048 bytes\n");
                        free(p2);
                    }
                    free(p);
                }
            }
        }
    }
    else
    {
        printf("Error querying system info: %lu", ulRC);
    }
    return 0;
}
Sample run:
All values in bytes.
Total Physical    Memory: 167378944
Total Resident    Memory: 11730944
Total Available   Memory: 262316032
Available Private Memory: 260571136
Available Shared  Memory: 310247424
Allocated 167377920 bytes: memory at 0x2a40040
Allocated another 2048 bytes
Hmmm... looks like I managed to allocate more memory than is resident.  
(Note: the run-time of this program is nil - OS/2 didn't actually allocate 
any memory, merely marked a few blocks as "in use, not committed.")  I 
couldn't allocated the available private memory, however - that was too high. 
 And since I was merely using malloc rather than the OS/2 api's, I couldn't 
access shared memory directly.
 CB> Rather than each place checking the pointer for success, it just uses a
 CB> very simple minded wrapper that aborts the entire program if
 CB> unsuccessful.  That's not a very good strategy!
It probably is - how often does your code run out of memory?  ;-)
 CB> But what about those compilers that don't.  I don't think I should just
 CB> lie and say you still have MAX_INT left.
unsigned long freememory()
{
#ifdef 32_BITS
  return megs_on_megs_on_megs;
#else
  return 32 * 1024;
#endif
}
:-)
 CB> and then write my own malloc/free clones, but that seems a bit drastic
 CB> and I don't know how some OSs might like having a dozen or so megs of
 CB> memory allocated that isn't really being used.  However, that method
OS/2 doesn't know/care.  Actually, the reason I couldn't allocate all the 
available memory is probably 'cuz gcc allocates 32 meg of stack space for the 
primary thread...
 CB> The real problem, I guess, is the possibility of running out of memory
 CB> abruptly, without giving the user fair warning.
"If this program aborts, get more memory, or try the DPMI version."
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.