allocate memory at predefined address

A

ampeloso

Hello,
I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
Thanks
Mike
 
W

Walter Roberson

I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?

There is nothing in the Standard that would definitely allow you to
do this, and there is nothing in the Standard Library that allows
you to request that dynamic memory be allocated (or memory
permissions be granted to it) at any specific address.

There is, in other words, no fully portable method of doing what you ask.

However, it is within the terms of the Standard to -allow- implementations
to support casting integers to become pointers; what happens when you
do so is implementation defined.

For example, it is allowed for implementations to make meaningful

NVram_ptr = (unsigned char *)0x03E00000;
strcpy( &NVram_ptr[10], "nOW iS tHE tIME" );

but what this actually does would be up to the implementation.

(It wouldn't necessarily write at physical location 0x03E0000A --
it would be legitimate for an implementation to interpret
casting 0x03E00000 as a pointer to mean something like
"the address is at offset 0xE00000 from segment register #3". Or worse.)


Some operating systems provide tools that allow extern variables to
be placed at particular locations; for anything like that you would
need to look closely at the documentation of your linker.
 
A

Ancient_Hacker

Hello,
I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
Thanks
Mike

wait a sec, maybe you dont want what you're asking for.

you don't really want to malloc() anything, as malloc() uses the heap
in RAM memory.

You may be saying in the address space of your target system there is
some ROM (more likely flash memory) which is mapped into the address
space at some fixed, or findable address.

Now on a LOT of computers, C pointers correspond to memory addresses,
so you can OFTEN, but not everywhere:

volatile char * TheFlashMemoryChip[16384];

TheFlashMemoryChip = (volatile char *) 0x20000000; // chip select on
addr line 30

for( i = 0 ; i < 16384 ; i++ ) TheFlashMemoryChip[ i ] = '!'; //
initialize the chip
 
J

Jens Thoms Toerring

I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?

Why would you like to allocate memory when you already have memory
to which you're allowed to write to and you know its address? Just
get yourself a pointer, set it to the physical address and use it
as you need to. Like in

int main( void )
{
char *rom = 0xDEADBEEF; /* pysical address to write to */
char *data = "Does this end up in ROM?";

while ( *rom++ = *data++ )
/* empty */ ;
return 0;
}

Of course, this only works if you're allowed to write directly to
physical memory (and you're not too concerned about portability;-)
but on an embedded device that probably wouldn't be that uncommon.
The question is only how you are supposed to write to ROM - wouldn't
you need WOM for that?
Regards, Jens
 
J

jmcgill

Can this be done and how?

Explore the memory management capabilities and/or the memory map of your
hardware, and/or your OS, and/or your system monitor.

If you can represent a value that is guaranteed to be a specific memory
address as constrained by your system, as a C pointer, than you can do
what you suggest. You may be forced to do this in assembly, and even
then, on many operating systems, you have to build something like a
kernel driver in order to allocate anything other than virtual memory.

That said, memory mapped I/O is still quite common. but memory-mapped
I/O bound to a specific hardware address space is really not something
you can talk about strictly in the context of C. This is a question for
comp.arch, or for some forum devoted to your hardware or OS platform.

There are linux kernel drivers that deal with memory-mapped I/O in all
kinds of devices, and might not be too hard to analyze. I'd look at
some of the ISA drive controllers or old sound cards for likely places
to find examples (but I am just guessing).
 
S

Simon Biber

Ancient_Hacker said:
Now on a LOT of computers, C pointers correspond to memory addresses,
so you can OFTEN, but not everywhere:

volatile char * TheFlashMemoryChip[16384];

Are you sure you meant an array of 16384 pointers to char?
TheFlashMemoryChip = (volatile char *) 0x20000000; // chip select on addr line 30

This code is invalid. You can't assign to an array.
for( i = 0 ; i < 16384 ; i++ ) TheFlashMemoryChip[ i ] = '!'; // initialize the chip

Here you try to write integer values (characters) into pointer objects
without a cast.

I think defining TheFlashMemoryChip as:
volatile char * TheFlashMemoryChip;
would make a lot more sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top