* Re: weird behavior of malloc()
2001-01-10 22:43 weird behavior of malloc() rwgk
@ 2001-01-11 0:09 ` Terrance Richard Boyes
2001-01-11 2:28 ` Larry Zimmerman
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Terrance Richard Boyes @ 2001-01-11 0:09 UTC (permalink / raw)
rwgk@my-deja.com wrote:
> System:
> Red Hat Linux release 6.2 (Zoot)
> Kernel 2.2.14-6.0smp on a 2-processor alpha
> On this system, malloc() returns pointers to places in memory that
> are very far apart. For example, one call returns 0x125f77510,
> and the next one 0x20000002010 (the first call only allocates
> 16 bytes or so, the second one about 160000 bytes).
Memory fragmentation. And possibly an attempt to minimise it ?
--
<URL:http://www.pierrot.co.uk/faq/dtm.faq> Team AMIGA
An egg has the shortest sex-life of all: if gets laid once; it gets
eaten once. It also has to come in a box with 11 others, and the only
person who will sit on its face is its mother.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: weird behavior of malloc()
2001-01-10 22:43 weird behavior of malloc() rwgk
2001-01-11 0:09 ` Terrance Richard Boyes
@ 2001-01-11 2:28 ` Larry Zimmerman
2001-01-11 16:38 ` Greg Lindahl
2001-01-11 16:07 ` Pierre Asselin
2001-01-11 18:25 ` T. Daniel Crawford
3 siblings, 1 reply; 6+ messages in thread
From: Larry Zimmerman @ 2001-01-11 2:28 UTC (permalink / raw)
In article <93iohn$m3c$1@nnrp1.deja.com>,
rwgk@my-deja.com writes:
> On this system, malloc() returns pointers to places in memory that
> are very far apart. For example, one call returns 0x125f77510,
> and the next one 0x20000002010 (the first call only allocates
> 16 bytes or so, the second one about 160000 bytes).
>
> Unfortunately, this behavior is causing us problems. For use in
> a Fortran program, we compute the difference between the pointers
> returned by malloc() and a pointer to a Fortran reference array.
> The difference is divided by the size of Fortran INTEGER, and
> that result passed back from C to Fortran as a Fortran INTEGER.
> If the difference is too large for INTEGER, our program crashes.
This problem was discussed in the "axp-list" some time ago. Malloc
gets memory at low addresses for small chunks of data and at large
addresses for large chunks of data. I'm not sure why you would try
the address difference method to get memory size, but I'm pretty sure
there is no reliable way to get this to work on Alpha with GNU tools.
> .... Why does malloc() on
> the Alpha return pointers to places in memory that are so far
> apart? Could this be changed?
I think one of the Digital engineers who worked on the Linux Alpha port
explained this, but I don't recall the details. I don't think it would
be easy to change in gcc or g77. Are you using Compaq's compilers - they
may have a way of working around this problem.
Larry Z.
--
.signature withheld pending a patent/copyright search.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: weird behavior of malloc()
2001-01-11 2:28 ` Larry Zimmerman
@ 2001-01-11 16:38 ` Greg Lindahl
0 siblings, 0 replies; 6+ messages in thread
From: Greg Lindahl @ 2001-01-11 16:38 UTC (permalink / raw)
laz@gonefishing.sk.ca (Larry Zimmerman) writes:
> This problem was discussed in the "axp-list" some time ago.
Same guy asked the question. And here are the answers:
> I'm not sure why you would try
> the address difference method to get memory size,
Because his e-vile(tm) Fortran code uses a 4-byte integer to store
address differences, and that is often hard to change.
> but I'm pretty sure
> there is no reliable way to get this to work on Alpha with GNU tools.
taso does the trick.
-- greg
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: weird behavior of malloc()
2001-01-10 22:43 weird behavior of malloc() rwgk
2001-01-11 0:09 ` Terrance Richard Boyes
2001-01-11 2:28 ` Larry Zimmerman
@ 2001-01-11 16:07 ` Pierre Asselin
2001-01-11 18:25 ` T. Daniel Crawford
3 siblings, 0 replies; 6+ messages in thread
From: Pierre Asselin @ 2001-01-11 16:07 UTC (permalink / raw)
rwgk@my-deja.com wrote:
>
> System:
> Red Hat Linux release 6.2 (Zoot)
> Kernel 2.2.14-6.0smp on a 2-processor alpha
>
> On this system, malloc() returns pointers to places in memory that
> are very far apart. For example, one call returns 0x125f77510,
> and the next one 0x20000002010 (the first call only allocates
> 16 bytes or so, the second one about 160000 bytes).
If I remember correctly, Gnu malloc uses brk(2) for small chunks and
mmap(2) for big chunks. You can change the threshold with
mallopt(M_MMAP_THRESHOLD, <value>).
> Unfortunately, this behavior is causing us problems. For use in
> a Fortran program, we compute the difference between the pointers
> returned by malloc() and a pointer to a Fortran reference array.
> The difference is divided by the size of Fortran INTEGER, and
> that result passed back from C to Fortran as a Fortran INTEGER.
> If the difference is too large for INTEGER, our program crashes.
You're between a rock and a hard place. Try mallopt(), but even
it it works you'll be sitting on a time bomb. Otherwise, a quick
fix would be to replace all INTEGER declarations by INTEGER*8,
without bothering at first to check which integers hold memory
offsets. If some of those integers are in common, expect trouble.
If some are equivalenced, expect big trouble.
Another way would be to write your own malloc replacement and use the
preprocessor to make your C code use it. The C library (e.g. stdio)
would still use the real malloc, but presumable it doesn't pass
offsets back to Fortran.
> Our program is very large (>100000 lines) and it is practically
> impossible to find and replace all INTEGER that store offsets for
> dynamic memory allocation. Also, the program is running fine
> on another 64-bit architecture (IRIX64). Why does malloc() on
> the Alpha return pointers to places in memory that are so far
> apart? Could this be changed?
On True64, the compilers have a `-taso' flag to force everything in a
32-bit address space, but that requires support from the OS and is not
available on Linux. Don't know about Irix.
--
Pierre Asselin
Westminster, Colorado
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: weird behavior of malloc()
2001-01-10 22:43 weird behavior of malloc() rwgk
` (2 preceding siblings ...)
2001-01-11 16:07 ` Pierre Asselin
@ 2001-01-11 18:25 ` T. Daniel Crawford
3 siblings, 0 replies; 6+ messages in thread
From: T. Daniel Crawford @ 2001-01-11 18:25 UTC (permalink / raw)
rwgk@my-deja.com wrote:
> System:
> Red Hat Linux release 6.2 (Zoot)
> Kernel 2.2.14-6.0smp on a 2-processor alpha
>
> On this system, malloc() returns pointers to places in memory that
> are very far apart. For example, one call returns 0x125f77510,
> and the next one 0x20000002010 (the first call only allocates
> 16 bytes or so, the second one about 160000 bytes).
>
> Unfortunately, this behavior is causing us problems. For use in
> a Fortran program, we compute the difference between the pointers
> returned by malloc() and a pointer to a Fortran reference array.
> The difference is divided by the size of Fortran INTEGER, and
> that result passed back from C to Fortran as a Fortran INTEGER.
> If the difference is too large for INTEGER, our program crashes.
>
> Our program is very large (>100000 lines) and it is practically
> impossible to find and replace all INTEGER that store offsets for
> dynamic memory allocation. Also, the program is running fine
> on another 64-bit architecture (IRIX64). Why does malloc() on
> the Alpha return pointers to places in memory that are so far
> apart? Could this be changed?
>
> Thanks for any comments,
> Ralf
>
> Sent via Deja.com
> http://www.deja.com/
I asked a nearly identical question on the axp-list just last week. The
very helpful folks on that list informed me that answer is the -taso
(Truncated Address Space Option) flag. This flag will make sure that
all addresses returned by malloc() routines will fall below the 2^31
threshold, and you will then be able to store adress differences in a
32-bit integer.
If you are using the Compaq Fortran compiler, you'll need to add the
flag "-Wl,-taso" to the linker flags so that the -taso option will be
passed to the GNU linker. Make certain you have a recent version of the
GNU binutils.
Good luck!
-Daniel
--
T. Daniel Crawford Department of Chemistry
crawdad@vt.edu Virginia Tech
www.chem.vt.edu/chem-dept/crawford Voice: 540-231-7760 FAX: 540-231-3255
^ permalink raw reply [flat|nested] 6+ messages in thread