source: git/Singular/page.c @ 3d124a7

spielwiese
Last change on this file since 3d124a7 was 40edb03, checked in by Olaf Bachmann <obachman@…>, 27 years ago
Fri Apr 25 16:59:31 1997 Olaf Bachmann <obachman@ratchwum.mathematik.uni-kl.de (Olaf Bachmann)> * Various changes to reflect new configure (versions defined in configure.in, changed HAVE_LIBFACTORY into HAVE_FACTORY, data dir is pasted from configure into mod2.h and used from therein feFopen. * Added configure facility, repalced mod2.h by mod2.h.in Makefile by Makefile.in git-svn-id: file:///usr/local/Singular/svn/trunk@189 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.1 KB
Line 
1#include "mod2.h"
2#ifdef PAGE_TEST
3#include <stdlib.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <sys/mman.h>
7#include <stdio.h>
8#include <errno.h>
9#include <string.h>
10#include <stddef.h>
11#include <signal.h>
12#include <sys/types.h>
13#include <sys/times.h>
14
15unsigned long page_tab[2048];
16char          use_tab [2048];
17int           page_tab_ind=2049;
18void *
19Page_Create(size_t size);
20void
21Page_AllowAccess(void * address, size_t size);
22void
23Page_DenyAccess(void * address, size_t size);
24size_t
25Page_Size(void);
26#ifndef  PROT_NONE
27#define  PROT_NONE  0
28#endif
29
30#if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) )
31#define  MAP_ANONYMOUS  MAP_ANON
32#endif
33
34static caddr_t  startAddr = (caddr_t) 0;
35
36extern int  sys_nerr;
37extern char *  sys_errlist[];
38
39static const char *
40stringErrorReport(void)
41{
42  if ( errno > 0 && errno < sys_nerr )
43    return sys_errlist[errno];
44  else
45    return "Unknown error.\n";
46}
47
48/*
49 * Create memory.
50 */
51#if defined(MAP_ANONYMOUS)
52void *
53Page_Create(size_t size)
54{
55  caddr_t    allocation;
56
57  /*
58   * In this version, "startAddr" is a _hint_, not a demand.
59   * When the memory I map here is contiguous with other
60   * mappings, the allocator can coalesce the memory from two
61   * or more mappings into one large contiguous chunk, and thus
62   * might be able to find a fit that would not otherwise have
63   * been possible. I could _force_ it to be contiguous by using
64   * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
65   * generated by other software, etc.
66   */
67  allocation = (caddr_t) mmap(
68   startAddr
69  ,(int)size
70  ,PROT_READ|PROT_WRITE
71  ,MAP_PRIVATE|MAP_ANONYMOUS
72  ,-1
73  ,0);
74
75  /*
76   * Set the "address hint" for the next mmap() so that it will abut
77   * the mapping we just created.
78   *
79   * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes
80   * when given a non-zero address hint, so we'll leave the hint set
81   * to zero on that system. HP recently told me this is now fixed.
82   * Someone please tell me when it is probable to assume that most
83   * of those systems that were running 9.01 have been upgraded.
84   */
85  startAddr = allocation + size;
86
87  if ( allocation == (caddr_t)-1 )
88    printf("mmap() failed: %s", stringErrorReport());
89
90  page_tab[page_tab_ind]=allocation;
91  page_tab_ind++;
92  return (void *)allocation;
93}
94#else
95void *
96Page_Create(size_t size)
97{
98  static int  devZeroFd = -1;
99  caddr_t    allocation;
100
101  if ( devZeroFd == -1 ) {
102    devZeroFd = open("/dev/zero", O_RDWR);
103    if ( devZeroFd < 0 )
104      printf(
105       "open() on /dev/zero failed: %s"
106      ,stringErrorReport());
107  }
108
109  /*
110   * In this version, "startAddr" is a _hint_, not a demand.
111   * When the memory I map here is contiguous with other
112   * mappings, the allocator can coalesce the memory from two
113   * or more mappings into one large contiguous chunk, and thus
114   * might be able to find a fit that would not otherwise have
115   * been possible. I could _force_ it to be contiguous by using
116   * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
117   * generated by other software, etc.
118   */
119  allocation = (caddr_t) mmap(
120   startAddr
121  ,(int)size
122  ,PROT_READ|PROT_WRITE
123  ,MAP_PRIVATE
124  ,devZeroFd
125  ,0);
126
127  startAddr = allocation + size;
128
129  if ( allocation == (caddr_t)-1 )
130    printf("mmap() failed: %s", stringErrorReport());
131
132  page_tab[page_tab_ind]=allocation;
133  page_tab_ind++;
134  return (void *)allocation;
135}
136#endif
137
138static void
139mprotectFailed(void)
140{
141  printf("mprotect() failed: %s", stringErrorReport());
142}
143
144void
145Page_AllowAccess(void * address, size_t size)
146{
147  if ( mprotect((caddr_t)address, size, PROT_READ|PROT_WRITE) < 0 )
148    mprotectFailed();
149}
150
151void
152Page_DenyAccess(void * address, size_t size)
153{
154  if ( mprotect((caddr_t)address, size, PROT_NONE) < 0 )
155    mprotectFailed();
156}
157
158void
159Page_Delete(void * address, size_t size)
160{
161  if ( munmap((caddr_t)address, size) < 0 )
162    Page_DenyAccess(address, size);
163}
164
165#if defined(_SC_PAGESIZE)
166size_t
167Page_Size(void)
168{
169  return (size_t)sysconf(_SC_PAGESIZE);
170}
171#elif defined(_SC_PAGE_SIZE)
172size_t
173Page_Size(void)
174{
175  return (size_t)sysconf(_SC_PAGE_SIZE);
176}
177#else
178/* extern int  getpagesize(); */
179size_t
180Page_Size(void)
181{
182  return getpagesize();
183}
184#endif
185
186#endif
Note: See TracBrowser for help on using the repository browser.