home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / mmalloc / mmap-sup.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  4KB  |  145 lines

  1. /* Support for an sbrk-like function that uses mmap.
  2.    Copyright 1992 Free Software Foundation, Inc.
  3.  
  4.    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com
  5.  
  6. This file is part of the GNU C Library.
  7.  
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12.  
  13. The GNU C Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with the GNU C Library; see the file COPYING.LIB.  If
  20. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  21. Cambridge, MA 02139, USA.  */
  22.  
  23. #if defined(HAVE_MMAP)
  24.  
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include <sys/mman.h>
  28.  
  29. #ifndef SEEK_SET
  30. #define SEEK_SET 0
  31. #endif
  32.  
  33. #include "mmalloc.h"
  34.  
  35. extern int munmap PARAMS ((caddr_t, size_t));    /* Not in any header file */
  36.  
  37. /* Cache the pagesize for the current host machine.  Note that if the host
  38.    does not readily provide a getpagesize() function, we need to emulate it
  39.    elsewhere, not clutter up this file with lots of kluges to try to figure
  40.    it out. */
  41.  
  42. static size_t pagesize;
  43. extern int getpagesize PARAMS ((void));
  44.  
  45. #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
  46.                     ~(pagesize - 1))
  47.  
  48. /*  Get core for the memory region specified by MDP, using SIZE as the
  49.     amount to either add to or subtract from the existing region.  Works
  50.     like sbrk(), but using mmap(). */
  51.  
  52. PTR
  53. __mmalloc_mmap_morecore (mdp, size)
  54.   struct mdesc *mdp;
  55.   int size;
  56. {
  57.   PTR result = NULL;
  58.   off_t foffset;    /* File offset at which new mapping will start */
  59.   size_t mapbytes;    /* Number of bytes to map */
  60.   caddr_t moveto;    /* Address where we wish to move "break value" to */
  61.   caddr_t mapto;    /* Address we actually mapped to */
  62.   char buf = 0;        /* Single byte to write to extend mapped file */
  63.  
  64.   if (pagesize == 0)
  65.     {
  66.       pagesize = getpagesize ();
  67.     }
  68.   if (size == 0)
  69.     {
  70.       /* Just return the current "break" value. */
  71.       result = mdp -> breakval;
  72.     }
  73.   else if (size < 0)
  74.     {
  75.       /* We are deallocating memory.  If the amount requested would cause
  76.      us to try to deallocate back past the base of the mmap'd region
  77.      then do nothing, and return NULL.  Otherwise, deallocate the
  78.      memory and return the old break value. */
  79.       if (mdp -> breakval + size >= mdp -> base)
  80.     {
  81.       result = (PTR) mdp -> breakval;
  82.       mdp -> breakval += size;
  83.       moveto = PAGE_ALIGN (mdp -> breakval);
  84.       munmap (moveto, (size_t) (mdp -> top - moveto));
  85.       mdp -> top = moveto;
  86.     }
  87.     }
  88.   else
  89.     {
  90.       /* We are allocating memory.  Make sure we have an open file
  91.      descriptor and then go on to get the memory. */
  92.       if (mdp -> fd < 0)
  93.     {
  94.       result = NULL;
  95.     }
  96.       else if (mdp -> breakval + size > mdp -> top)
  97.     {
  98.       /* The request would move us past the end of the currently
  99.          mapped memory, so map in enough more memory to satisfy
  100.          the request.  This means we also have to grow the mapped-to
  101.          file by an appropriate amount, since mmap cannot be used
  102.          to extend a file. */
  103.       moveto = PAGE_ALIGN (mdp -> breakval + size);
  104.       mapbytes = moveto - mdp -> top;
  105.       foffset = mdp -> top - mdp -> base;
  106.       /* FIXME:  Test results of lseek() and write() */
  107.       lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
  108.       write (mdp -> fd, &buf, 1);
  109.       mapto = mmap (mdp -> top, mapbytes, PROT_READ | PROT_WRITE,
  110.             MAP_SHARED | MAP_FIXED, mdp -> fd, foffset);
  111.       if (mapto == mdp -> top)
  112.         {
  113.           mdp -> top = moveto;
  114.           result = (PTR) mdp -> breakval;
  115.           mdp -> breakval += size;
  116.         }
  117.     }
  118.       else
  119.     {
  120.       result = (PTR) mdp -> breakval;
  121.       mdp -> breakval += size;
  122.     }
  123.     }
  124.   return (result);
  125. }
  126.  
  127. PTR
  128. __mmalloc_remap_core (mdp)
  129.   struct mdesc *mdp;
  130. {
  131.   caddr_t base;
  132.  
  133.   /* FIXME:  Quick hack, needs error checking and other attention. */
  134.  
  135.   base = mmap (mdp -> base, mdp -> top - mdp -> base,
  136.            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
  137.            mdp -> fd, 0);
  138.   return ((PTR) base);
  139. }
  140.  
  141. #else    /* defined(HAVE_MMAP) */
  142. /* Prevent "empty translation unit" warnings from the idiots at X3J11. */
  143. static char ansi_c_idiots = 69;
  144. #endif    /* defined(HAVE_MMAP) */
  145.