home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / ddx-mips.zip / MIPSMAP.C < prev    next >
C/C++ Source or Header  |  1992-07-31  |  2KB  |  70 lines

  1. /*
  2.  * $XConsortium$
  3.  *
  4.  * Copyright 1991 MIPS Computer Systems, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of MIPS not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  MIPS makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * MIPS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL MIPS
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #ident    "$Header: mipsMap.c,v 1.5 91/07/12 11:43:58 dd Exp $"
  24.  
  25. #include <sysv/sys/types.h>
  26. #include <sysv/sys/sysmacros.h>
  27. #include <sysv/sys/ipc.h>
  28. #include <sysv/sys/shm.h>
  29.  
  30. extern int shmget();
  31. extern char *shmat();
  32. extern int shmdt();
  33.  
  34. char *
  35. mipsMapit(addr, key, size)
  36.     char *addr;
  37.     int key;
  38.     int size;
  39. {
  40.     char *new;
  41.     int shmid;
  42.     static int first;
  43.     int dummy;
  44.     extern char *sbrk();
  45.  
  46.     /*
  47.      * If we use an address of zero, the 4.5x kernel will only give
  48.      * us 2M of headroom over the highest existing segment, which
  49.      * doesn't allow enough room for heap growth.  So, the first time
  50.      * we map something, pick an address mid-way between the heap and
  51.      * the stack.
  52.      */
  53.     if (!first && !addr) {
  54.         first = 1;
  55.         addr = (char *) (((int) &dummy - (int) sbrk(0) / 2) &
  56.             ~(SHMLBA - 1));
  57.     }
  58.  
  59.     if ((shmid = shmget(key, size, 0666)) < 0 ||
  60.         (new = shmat(shmid, addr, 0)) == (char *) -1)
  61.         return 0;
  62.  
  63.     if (addr && new != addr) {
  64.         (void) shmdt(new);
  65.         return 0;
  66.     }
  67.  
  68.     return new;
  69. }
  70.