Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
there were no difference between __mmalloc_free and mfree anymore; merge them
[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
25 /* Cache the pagesize for the current host machine.  Note that if the host
26    does not readily provide a getpagesize() function, we need to emulate it
27    elsewhere, not clutter up this file with lots of kluges to try to figure
28    it out. */
29
30 static size_t pagesize;
31
32 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
33                                     ~(pagesize - 1))
34
35 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
36    MAP_SHARED.  */
37 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
38                                     ? MAP_PRIVATE \
39                                     : MAP_SHARED)
40
41 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
42 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
43                               ? MAP_ANONYMOUS \
44                               : 0)
45
46 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
47 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
48                               ? -1 \
49                                               : (MDP) -> fd)
50
51 /*  Get core for the memory region specified by MDP, using SIZE as the
52     amount to either add to or subtract from the existing region.  Works
53     like sbrk(), but using mmap(). */
54
55 void *mmorecore(struct mdesc *mdp, int size)
56 {
57   ssize_t test = 0;
58   void *result = NULL;
59   off_t foffset;                /* File offset at which new mapping will start */
60   size_t mapbytes;              /* Number of bytes to map */
61   void *moveto;                 /* Address where we wish to move "break value" to */
62   void *mapto;                  /* Address we actually mapped to */
63   char buf = 0;                 /* Single byte to write to extend mapped file */
64
65   if (pagesize == 0)
66     pagesize = getpagesize();
67
68   if (size == 0) {
69     /* Just return the current "break" value. */
70     result = mdp->breakval;
71
72   } else if (size < 0) {
73     /* We are deallocating memory.  If the amount requested would cause
74        us to try to deallocate back past the base of the mmap'd region
75        then do nothing, and return NULL.  Otherwise, deallocate the
76        memory and return the old break value. */
77     if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
78       result = (void *) mdp->breakval;
79       mdp->breakval = (char *) mdp->breakval + size;
80       moveto = PAGE_ALIGN(mdp->breakval);
81       munmap(moveto,
82              (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
83       mdp->top = moveto;
84     }
85   } else {
86     /* We are allocating memory. Make sure we have an open file
87        descriptor if not working with anonymous memory. */
88     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
89       THROWF(system_error,0,"mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags",mdp->fd);
90       result = NULL;
91     } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
92       /* The request would move us past the end of the currently
93          mapped memory, so map in enough more memory to satisfy
94          the request.  This means we also have to grow the mapped-to
95          file by an appropriate amount, since mmap cannot be used
96          to extend a file. */
97       moveto = PAGE_ALIGN((char *) mdp->breakval + size);
98       mapbytes = (char *) moveto - (char *) mdp->top;
99       foffset = (char *) mdp->top - (char *) mdp->base;
100
101       if (mdp->fd > 0) {
102         /* FIXME:  Test results of lseek() */
103         lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
104         test = write(mdp->fd, &buf, 1);
105         if (test == -1)
106           THROWF(system_error, 0, "write to mmap'ed fd failed! error: %s", strerror(errno));
107       }
108
109       /* Let's call mmap. Note that it is possible that mdp->top
110          is 0. In this case mmap will choose the address for us */
111       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
112                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
113                    MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
114
115       if (mapto == (void *) -1/* That's MAP_FAILED */)
116           THROWF(system_error,0,"mmap returned MAP_FAILED! error: %s",strerror(errno));
117
118       if (mdp->top == 0)
119           mdp->base = mdp->breakval = mapto;
120
121       mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
122       result = (void *) mdp->breakval;
123       mdp->breakval = (char *) mdp->breakval + size;
124     } else {
125       result = (void *) mdp->breakval;
126       mdp->breakval = (char *) mdp->breakval + size;
127     }
128   }
129   return (result);
130 }
131
132 void *__mmalloc_remap_core(xbt_mheap_t mdp)
133 {
134   /* FIXME:  Quick hack, needs error checking and other attention. */
135
136   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
137               PROT_READ | PROT_WRITE | PROT_EXEC,
138               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
139 }
140