Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further little clarifications in mmalloc comments
[simgrid.git] / src / xbt / mmalloc / mmorecore.c
1 /* Support for an sbrk-like function that uses mmap.
2    Copyright 1992, 2000 Free Software Foundation, Inc.
3
4    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com */
5
6 /* Copyright (c) 2010. The SimGrid Team.
7  * All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #ifndef MAP_ANONYMOUS
13 #define MAP_ANONYMOUS MAP_ANON
14 #endif
15
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>             /* Prototypes for lseek */
18 #endif
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <sys/mman.h>
22
23 #include "mmprivate.h"
24 #include "xbt/ex.h"
25
26 /* Cache the pagesize for the current host machine.  Note that if the host
27    does not readily provide a getpagesize() function, we need to emulate it
28    elsewhere, not clutter up this file with lots of kluges to try to figure
29    it out. */
30
31 static size_t pagesize;
32
33 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
34                                     ~(pagesize - 1))
35
36 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
37    MAP_SHARED.  */
38 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
39                                     ? MAP_PRIVATE \
40                                     : MAP_SHARED)
41
42 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
43 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
44                               ? MAP_ANONYMOUS \
45                               : 0)
46
47 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
48 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
49                               ? -1 \
50                                               : (MDP) -> fd)
51
52 /*  Get core for the memory region specified by MDP, using SIZE as the
53     amount to either add to or subtract from the existing region.  Works
54     like sbrk(), but using mmap(). */
55
56 void *mmorecore(struct mdesc *mdp, int size)
57 {
58   ssize_t test = 0;
59   void *result = NULL;
60   off_t foffset;                /* File offset at which new mapping will start */
61   size_t mapbytes;              /* Number of bytes to map */
62   void *moveto;                 /* Address where we wish to move "break value" to */
63   void *mapto;                  /* Address we actually mapped to */
64   char buf = 0;                 /* Single byte to write to extend mapped file */
65
66   if (pagesize == 0)
67     pagesize = getpagesize();
68
69   if (size == 0) {
70     /* Just return the current "break" value. */
71     result = mdp->breakval;
72
73   } else if (size < 0) {
74     /* We are deallocating memory.  If the amount requested would cause
75        us to try to deallocate back past the base of the mmap'd region
76        then do nothing, and return NULL.  Otherwise, deallocate the
77        memory and return the old break value. */
78     if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
79       result = (void *) mdp->breakval;
80       mdp->breakval = (char *) mdp->breakval + size;
81       moveto = PAGE_ALIGN(mdp->breakval);
82       munmap(moveto,
83              (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
84       mdp->top = moveto;
85     }
86   } else {
87     /* We are allocating memory. Make sure we have an open file
88        descriptor if not working with anonymous memory. */
89     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
90       THROWF(system_error,0,"mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags",mdp->fd);
91       result = NULL;
92     } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
93       /* The request would move us past the end of the currently
94          mapped memory, so map in enough more memory to satisfy
95          the request.  This means we also have to grow the mapped-to
96          file by an appropriate amount, since mmap cannot be used
97          to extend a file. */
98       moveto = PAGE_ALIGN((char *) mdp->breakval + size);
99       mapbytes = (char *) moveto - (char *) mdp->top;
100       foffset = (char *) mdp->top - (char *) mdp->base;
101
102       if (mdp->fd > 0) {
103         /* FIXME:  Test results of lseek() */
104         lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
105         test = write(mdp->fd, &buf, 1);
106         if (test == -1)
107           THROWF(system_error, 0, "write to mmap'ed fd failed! error: %s", strerror(errno));
108       }
109
110       /* Let's call mmap. Note that it is possible that mdp->top
111          is 0. In this case mmap will choose the address for us */
112       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
113                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
114                    MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
115
116       if (mapto == (void *) -1/* That's MAP_FAILED */)
117           THROWF(system_error,0,"mmap returned MAP_FAILED! error: %s",strerror(errno));
118
119       if (mdp->top == 0)
120           mdp->base = mdp->breakval = mapto;
121
122       mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
123       result = (void *) mdp->breakval;
124       mdp->breakval = (char *) mdp->breakval + size;
125     } else {
126       result = (void *) mdp->breakval;
127       mdp->breakval = (char *) mdp->breakval + size;
128     }
129   }
130   return (result);
131 }
132
133 void *__mmalloc_remap_core(xbt_mheap_t mdp)
134 {
135   /* FIXME:  Quick hack, needs error checking and other attention. */
136
137   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
138               PROT_READ | PROT_WRITE | PROT_EXEC,
139               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
140 }
141