Remapping a file using mmap() question - Unix

This is a discussion on Remapping a file using mmap() question - Unix ; Hi I have to write an app that will scan a file containing records in fixed length fields - perfect for memory mapping. However , I'll also from time to time need to write new records onto the end of ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Remapping a file using mmap() question

  1. Remapping a file using mmap() question

    Hi

    I have to write an app that will scan a file containing records in
    fixed length fields - perfect for memory mapping. However , I'll also
    from time to time need to write new records onto the end of the file
    on the fly. My question is - is there a way to remap the file or
    extend the size of the mapped segment to include new fields written
    onto the end WITHOUT invaliding the current memory map pointer? (so I
    don't want to just munmap and mmap again).

    Thanks for any help

    B2003

  2. Re: Remapping a file using mmap() question

    On Mon, 12 May 2008 04:48:00 -0700, Boltar wrote:

    > Hi
    >
    > I have to write an app that will scan a file containing records in fixed
    > length fields - perfect for memory mapping. However , I'll also from
    > time to time need to write new records onto the end of the file on the
    > fly. My question is - is there a way to remap the file or extend the
    > size of the mapped segment to include new fields written onto the end
    > WITHOUT invaliding the current memory map pointer? (so I don't want to
    > just munmap and mmap again).


    mremap() is what you need.
    Note there is *no guarantee* that the pointer remains the same. The
    system _may_ choose another place in your VM-map, if it wants to.
    (similar to realloc())

    So your program should be designed carefully: don't allow any pointers
    inside your mmap()ed area(, or be prepared to abandon or repare them on a
    mremap()). Easyest way is to only keep the base-pointer and the mapped
    size, and use offsets or indexes for addressing the mmapped area, IMHO.

    HTH,
    AvK

  3. Re: Remapping a file using mmap() question


    Boltar wrote:

    > I have to write an app that will scan a file containing records in
    > fixed length fields - perfect for memory mapping. However , I'll also
    > from time to time need to write new records onto the end of the file
    > on the fly. My question is - is there a way to remap the file or
    > extend the size of the mapped segment to include new fields written
    > onto the end WITHOUT invaliding the current memory map pointer? (so I
    > don't want to just munmap and mmap again).


    I don't think you need to do anything special. Just make sure that you
    make the original mapping big enough and don't access the mapping past
    the end of the file.

    DS

+ Reply to Thread