home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / lib / msgc / src / unixgc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.3 KB  |  137 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prlock.h"
  20. #include "prlog.h"
  21. #include "prmem.h"
  22. #include "gcint.h"
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <sys/mman.h>
  29.  
  30. #define _PR_GC_VMBASE 0x40000000
  31.  
  32. #if defined(SOLARIS)
  33. #define _MD_MMAP_FLAGS MAP_SHARED
  34. #elif defined(OSF1) || defined(RELIANTUNIX)
  35. #define _MD_MMAP_FLAGS MAP_PRIVATE|MAP_FIXED
  36. #else
  37. #define _MD_MMAP_FLAGS MAP_PRIVATE
  38. #endif
  39.  
  40. static PRInt32 zero_fd = -1;
  41. static PRLock *zero_fd_lock = NULL;
  42.  
  43. void _MD_InitGC(void)
  44. {
  45. #ifdef DEBUG
  46.     /*
  47.      * Disable using mmap(2) if NSPR_NO_MMAP is set
  48.      */
  49.     if (getenv("NSPR_NO_MMAP")) {
  50.         zero_fd = -2;
  51.         return;
  52.     }
  53. #endif
  54.     zero_fd = open("/dev/zero",O_RDWR , 0);
  55.     zero_fd_lock = PR_NewLock();
  56. }
  57.  
  58. /* This static variable is used by _MD_GrowGCHeap and _MD_ExtendGCHeap */
  59. static void *lastaddr = (void*) _PR_GC_VMBASE;
  60.  
  61. void *_MD_GrowGCHeap(PRUint32 *sizep)
  62. {
  63.     void *addr;
  64.     PRUint32 size;
  65.  
  66.     size = *sizep;
  67.  
  68.     PR_Lock(zero_fd_lock);
  69.     if (zero_fd < 0) {
  70.         goto mmap_loses;
  71.     }
  72.  
  73.     /* Extend the mapping */
  74.     addr = mmap(lastaddr, size, PROT_READ|PROT_WRITE|PROT_EXEC,
  75.         _MD_MMAP_FLAGS,
  76.         zero_fd, 0);
  77.     if (addr == (void*)-1) {
  78.         zero_fd = -1;
  79.         goto mmap_loses;
  80.     }
  81.     lastaddr = ((char*)addr + size);
  82. #ifdef DEBUG
  83.     PR_LOG(_pr_msgc_lm, PR_LOG_WARNING,
  84.         ("GC: heap extends from %08x to %08x\n",
  85.         _PR_GC_VMBASE,
  86.         _PR_GC_VMBASE + (char*)lastaddr - (char*)_PR_GC_VMBASE));
  87. #endif
  88.     PR_Unlock(zero_fd_lock);
  89.     return addr;
  90.  
  91. mmap_loses:
  92.     PR_Unlock(zero_fd_lock);
  93.     return PR_MALLOC(size);
  94. }
  95.  
  96. /* XXX - This is disabled.  MAP_FIXED just does not work. */
  97. #if 0
  98. PRBool _MD_ExtendGCHeap(char *base, PRInt32 oldSize, PRInt32 newSize) {
  99.     PRBool rv = PR_FALSE;
  100.     void* addr;
  101.     PRInt32 allocSize = newSize - oldSize;
  102.  
  103.     PR_Lock(zero_fd_lock);
  104.     addr = mmap(base + oldSize, allocSize, PROT_READ|PROT_WRITE|PROT_EXEC,
  105.         _MD_MMAP_FLAGS | MAP_FIXED, zero_fd, 0);
  106.     if (addr == (void*)-1) {
  107.         goto loser;
  108.     }
  109.     if (addr != (void*) (base + oldSize)) {
  110.         munmap(base + oldSize, allocSize);
  111.         goto loser;
  112.     }
  113.     lastaddr = ((char*)base + newSize);
  114.     PR_LOG(_pr_msgc_lm, PR_LOG_ALWAYS,
  115.         ("GC: heap now extends from %p to %p",
  116.         base, base + newSize));
  117.     rv = PR_TRUE;
  118.  
  119. loser:
  120.     PR_Unlock(zero_fd_lock);
  121.     return rv;
  122. }
  123. #else
  124. PRBool _MD_ExtendGCHeap(char *base, PRInt32 oldSize, PRInt32 newSize) {
  125.     return PR_FALSE;
  126. }
  127. #endif
  128.  
  129. void _MD_FreeGCSegment(void *base, PRInt32 len)
  130. {
  131.     if (zero_fd < 0) {
  132.         PR_DELETE(base);
  133.     } else {
  134.         (void) munmap(base, len);
  135.     }
  136. }
  137.