Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/runtime/memory/mem-mmap.c
ViewVC logotype

Annotation of /sml/trunk/src/runtime/memory/mem-mmap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (view) (download) (as text)

1 : monnier 2 /* mem-mmap.c
2 :     *
3 :     * COPYRIGHT (c) 1993 by AT&T Bell Laboratories.
4 :     *
5 :     * Memory sub-system for systems that provide mmap.
6 :     */
7 :    
8 :     #include "ml-unixdep.h"
9 :     #include "ml-osdep.h"
10 :     #include <sys/mman.h>
11 :     #include INCLUDE_FCNTL_H
12 :     #include "ml-base.h"
13 :     #include "memory.h"
14 :    
15 :     #if !(defined(HAS_MMAP) || defined(HAS_ANON_MMAP))
16 :     # error expected HAS_MMAP or HAS_ANON_MMAP
17 :     #endif
18 :    
19 :     /* protection mode for mmap memory */
20 :     #define PROT_ALL (PROT_READ|PROT_WRITE|PROT_EXEC)
21 :    
22 :     /* flags for mmap */
23 :     #ifdef HAS_ANON_MMAP
24 :     # define MMAP_FLGS (MAP_ANONYMOUS|MAP_PRIVATE)
25 :     #else
26 :     # define MMAP_FLGS MAP_PRIVATE
27 :     #endif
28 :    
29 :     #if (defined(HOST_ALPHA32) && (defined(OPSYS_OSF1) || defined(OPSYS_DUNIX)))
30 :     /* To insure that mmap returns a 32-bit address, we need to specify a non-zero
31 :     * address to mmap(). The address 0x2000000 is the location of the text segment,
32 :     * which is a good minimum value since the C stack lives just below this address
33 :     * and mmap will find regions above it.
34 :     */
35 :     # define MMAP_ADDR (caddr_t)0x2000000
36 :     #else
37 :     # define MMAP_ADDR 0
38 :     #endif
39 :    
40 :     struct mem_obj {
41 :     Word_t *base; /* the base address of the object. */
42 :     Addr_t sizeB; /* the object's size (in bytes) */
43 :     #ifdef HAS_PARTIAL_MUNMAP
44 :     # define mapBase base
45 :     # define mapSizeB sizeB
46 :     #else
47 :     Word_t *mapBase; /* base address of the mapped region containing */
48 :     /* the object */
49 :     Addr_t mapSizeB; /* the size of the mapped region containing */
50 :     /* the object */
51 :     #endif
52 :     };
53 :    
54 :     extern int errno;
55 :    
56 :     #define ALLOC_MEMOBJ() NEW_OBJ(mem_obj_t)
57 :     #define FREE_MEMOBJ(p) FREE(p)
58 :    
59 :     #include "mem-common.ins"
60 :    
61 :     /* MEM_InitMemory:
62 :     */
63 :     void MEM_InitMemory ()
64 :     {
65 :     InitMemory();
66 :    
67 :     } /* MEM_InitMemory */
68 :    
69 :    
70 :     /* MapMemory:
71 :     *
72 :     * Map a BIBOP_PAGE_SZB aligned chunk of szb bytes of virtual memory. Return
73 :     * the address of the mapped memory (or NIL on failure).
74 :     */
75 :     PVT status_t MapMemory (mem_obj_t *obj, Addr_t szb)
76 :     {
77 :     int fd;
78 :     Addr_t addr, offset;
79 :    
80 :     #ifdef HAS_ANON_MMAP
81 :     fd = -1;
82 :     #else
83 :     /* Note: we use O_RDONLY, because some OS are configured such that /dev/zero
84 :     * is not writable. This works because we are using MAP_PRIVATE as the
85 :     * mapping mode.
86 :     */
87 :     if ((fd = open("/dev/zero", O_RDONLY)) == -1) {
88 :     Error ("unable to open /dev/zero, errno = %d\n", errno);
89 :     return FAILURE;
90 :     }
91 :     #endif
92 :    
93 :     /* we grab an extra BIBOP_PAGE_SZB bytes to give us some room for alignment */
94 :     addr = (Addr_t) mmap (MMAP_ADDR, szb+BIBOP_PAGE_SZB, PROT_ALL, MMAP_FLGS, fd, 0);
95 :     if (addr == -1) {
96 :     Error ("unable to map %d bytes, errno = %d\n", szb, errno);
97 :     #ifndef HAS_ANON_MMAP
98 :     close (fd); /* NOTE: this call clobbers errno */
99 :     #endif
100 :     return FAILURE;
101 :     }
102 :     #ifndef HAS_ANON_MMAP
103 :     close (fd);
104 :     #endif
105 :    
106 :     /* insure BIBOP_PAGE_SZB alignment */
107 :     offset = BIBOP_PAGE_SZB - (addr & (BIBOP_PAGE_SZB-1));
108 :     #ifdef HAS_PARTIAL_MUNMAP
109 :     if (offset != 0) {
110 :     /* align addr and discard unused portions of memory */
111 :     munmap ((void *)addr, offset);
112 :     addr += offset;
113 :     munmap ((void *)(addr+szb), BIBOP_PAGE_SZB-offset);
114 :     }
115 :     else {
116 :     munmap ((void *)(addr+szb), BIBOP_PAGE_SZB);
117 :     }
118 :     #else
119 :     obj->mapBase = (Word_t *)addr;
120 :     obj->mapSizeB = szb+BIBOP_PAGE_SZB;
121 :     addr += offset;
122 :     #endif
123 :     obj->base = (Word_t *)addr;
124 :     obj->sizeB = szb;
125 :    
126 :     return SUCCESS;
127 :    
128 :     } /* end of MapMemory */
129 :    
130 :     /* UnmapMemory:
131 :     *
132 :     * Unmap a szb byte chunk of virtual memory at addr.
133 :     */
134 :     PVT void UnmapMemory (mem_obj_t *obj)
135 :     {
136 :     if (munmap((caddr_t)(obj->mapBase), obj->mapSizeB) == -1) {
137 :     Die ("error unmapping [%#x, %#x), errno = %d\n",
138 :     obj->mapBase, (Addr_t)(obj->mapBase) + obj->mapSizeB, errno);
139 :     }
140 :    
141 :     } /* end of UnmapMemory */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0