delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/26/10:37:18

Date: Tue, 26 Sep 2000 16:35:23 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Rafael García <rafael AT geninfor DOT com>
cc: djgpp AT delorie DOT com
Subject: RE: negative sbrk(0)
In-Reply-To: <8qprpe$l72$1@diana.bcn.ttd.net>
Message-ID: <Pine.SUN.3.91.1000926162008.13268V@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Tue, 26 Sep 2000, Rafael Garc=EDa wrote:

> > > Well, I have got sbrk(0) to return negative values in some cases, so =
the
> > > function tells I have -1950Kb.
> >
> > sbrk returns an insigned value, not a signed one.  So negative values
> > are actually very large positive ones.  Happens a lot on Windows.
>=20
> So... I cannot use that function I read somewhere to know available memor=
y?

You could use sbrk, but only up to a point: if the DPMI server returns=20
memory in a different area, your calculations will be wrong.  You could=20
work around that by requesting Unixy sbrk algorithm, but that has=20
disadvantages in certain cases (see the docs of the _crt0_startup_flags=20
variable in the libc.info file, for more details).

> > Is there another method?

There are other methods, but they all suffer from a common problem: on=20
any OS except plain DOS, the DPMI server usually lies about available=20
memory, and there's no reliable way of counting the amount of memory used=
=20
by the client program.  You need to be aware that in a virtual memory=20
environment, there's no easy way to know what memory is actually taken by=
=20
a process.

One possible way of tracking memory allocation would be by calling=20
_go32_dpmi_get_free_memory_information or=20
__dpmi_get_free_memory_information.  Look them up in the library docs.

> Perhaps I must try malloc(1.5*filelength(f)) to decide if it is good idea=
 to
> load entire file in ram

Why multiply by 1.5?  Are you going to enlarge the data?

> but I thougth it would be better technique to watch
> available before. I think it is possible a long delay for virtual allocat=
ion
> if I ask for 20 Mb in a 2Mb ram machine

This is false: malloc only reserves the address space, but doesn't=20
actually page in the memory until you access it.  (This is explained in=20
section 15.2 of the DJGPP FAQ list.)  So using malloc is indeed the right=
=20
way of achieving what you want, even on memory-starved machines.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019