From: George Foot Newsgroups: comp.os.msdos.djgpp Subject: Re: How can I calculate the amount of free memory? Date: 23 Apr 1998 02:45:39 GMT Organization: Oxford University, England Lines: 56 Message-ID: <6hma0j$6bf$1@news.ox.ac.uk> References: NNTP-Posting-Host: sable.ox.ac.uk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Mon, 20 Apr 1998 22:33:59 +0900 in comp.os.msdos.djgpp SangGyu Nam wrote: : I'm making an image handling program using DPMI. ... : Whenever an image displayed, I traced the free amount of : free memory by using DPMI function 0x0500. : The function returns correct value when I allocate memory : but after free it, I could not get valid information. : Only the amount before free() was returned. ... : In other words, free() does not return memory to DPMI : automatically. So if I want to get correct information, ... : How can I get the realtime free amount of DPMI memory? I think you're chasing pigeons. When `free' is called, the block is then available for `malloc' to allocate -- so the memory is not lost. That block can only be used for a whole allocation, though; it can't put part of an allocation in that block, and the rest somewhere else. If you allocate a large block, and no previously freed blocks are big enough for the allocation, it will have to come from the pooled DPMI memory. So you should restrict your allocations to that amount anyway. There isn't really a way to get an accurate figure for how much memory you'll be able to use. The DPMI server can change its mind about how much it will give you at any time; if you ask it how much free physical memory there is, and then allocate it all (and it lets you), then ask again, it might choose to offer you some more memory. Don't forget that most DPMI servers also provide virtual memory, so the chances of physically running out are quite small really, unless you go overboard. However, different DPMI servers respond to different requests in different ways. Just because they say there's X megabytes of free memory, that doesn't mean you can allocate it all. Sometimes they won't let you allocate Y bytes but they will let you allocate two blocks of Y/2 bytes. The only reliable way to find out whether you'll be allowed to create some complex structure is to actually try to create it, and watch out for failed mallocs. If you organise the allocation function properly you should be able to catch this and exit the function cleanly, freeing up all the blocks you've already allocated which can't be used now that later blocks could not be allocated. You could of course try to allocate a large block, then subdivide that for further allocations; that should always work, if you can allocate the block. Note what I said above, though, about some DPMI servers not letting you allocate very large chunks all at once. -- george DOT foot AT merton DOT oxford DOT ac DOT uk [sb91salaPlbP2la+PlbP]sb91salaPlbP2la+PlbP