Home arrow BrainDump arrow Page 2 - Using mmap() for Advanced File I/O

Resizing a Mapping - BrainDump

In this fourth part of a seven-part series on Linux I/O file system calls, you will learn how to use mmap(). It is excerpted from chapter four of the book Linux System Programming: Talking Directly to the Kernel and C Library, written by Robert Love (O'Reilly, 2007; ISBN: 0596009585). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Using mmap() for Advanced File I/O
  2. Resizing a Mapping
  3. Changing the Protection of a Mapping
  4. Synchronizing a File with a Mapping
  5. Giving Advice on a Mapping
By: O'Reilly Media
Rating: starstarstarstarstar / 5
December 18, 2008

print this article



Linux provides the mremap() system call for expanding or shrinking the size of a given mapping. This function is Linux-specific:

  #define _GNU_SOURCE

  #include <unistd.h>
  #include <sys/mman.h>

  void * mremap (void *addr, size_t old_size,
                 size_t new_size, unsigned long flags);

A call to mremap() expands or shrinks mapping in the region [addr,addr+old_size) to the new size new_size. The kernel can potentially move the mapping at the same time, depending on the availability of space in the processí address space and the value of flags.

The opening[in[addr,addr+old_size)indicates that the region starts with (and includes) the low address, whereas the closing)indicates that the region stops just before (does not include) the high address. This convention is known as interval notation.

Theflags parameter can be either0orMREMAP_MAYMOVE, which specifies that the kernel is free to move the mapping, if required, in order to perform the requested resizing. A large resizing is more likely to succeed if the kernel can move the mapping.

On success, mremap() returns a pointer to the newly resized memory mapping. On failure, it returns MAP_FAILED, and sets errnoto one of the following:


   The memory region is locked, and cannot be resized.

   Some pages in the given range are not valid pages in
   the processí address space, or there was a problem
   remapping the given pages.

   An argument was invalid.

   The given range cannot be expanded without moving
   (andMREMAP_MAYMOVEwas not given), or there is
   not enough free space in the processí address space.

Libraries such as glibc often usemremap()to implement an efficientrealloc(), which is an interface for resizing a block of memory originally obtained viamalloc(). For example:

  void * realloc (void *addr, size_t len)
size_t old_size = look_up_mapping_size (addr);
void *p;

          p = mremap (addr, old_size, len, MREMAP_MAYMOVE);
          if (p == MAP_FAILED)
                  return NULL;
          return p;

This would only work if allmalloc()allocations were unique anonymous mappings; nonetheless, it stands as a useful example of the performance gains to be had. The example assumes the programmer has written alook_up_mapping_size()function.

The GNU C library does usemmap()and family for performing some memory allocations. We will look that topic in depth in Chapter 8.

>>> More BrainDump Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates


Dev Shed Tutorial Topics: