allocate memory at predefined address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ampeloso@gmail.com

    #1

    allocate memory at predefined address

    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

  • Walter Roberson

    #2
    Re: allocate memory at predefined address

    In article <1160511068.488 299.181920@m7g2 000cwm.googlegr oups.com>,
    <ampeloso@gmail .comwrote:
    >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.
    --
    All is vanity. -- Ecclesiastes

    Comment

    • Ancient_Hacker

      #3
      Re: allocate memory at predefined address


      ampeloso@gmail. com wrote:
      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 * TheFlashMemoryC hip[16384];

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

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

      Comment

      • Jens Thoms Toerring

        #4
        Re: allocate memory at predefined address

        ampeloso@gmail. com wrote:
        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
        --
        \ Jens Thoms Toerring ___ jt@toerring.de
        \______________ ____________ http://toerring.de

        Comment

        • jmcgill

          #5
          Re: allocate memory at predefined address

          ampeloso@gmail. com wrote:
          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).




          Comment

          • Simon Biber

            #6
            Re: allocate memory at predefined address

            Ancient_Hacker wrote:
            Now on a LOT of computers, C pointers correspond to memory addresses,
            so you can OFTEN, but not everywhere:
            >
            volatile char * TheFlashMemoryC hip[16384];
            Are you sure you meant an array of 16384 pointers to char?
            TheFlashMemoryC hip = (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++ ) TheFlashMemoryC hip[ i ] = '!'; // initialize the chip
            Here you try to write integer values (characters) into pointer objects
            without a cast.

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

            --
            Simon.

            Comment

            Working...