annotate src/lib/mmap-anon.c @ 858:8f4bd02461e0 HEAD

Old implementation was overly complex and stupid.
author Timo Sirainen <tss@iki.fi>
date Fri, 27 Dec 2002 17:39:10 +0200
parents 5ac361acb316
children 8ecfe2c250ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /*
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2 Copyright (c) 2002 Timo Sirainen
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 Permission is hereby granted, free of charge, to any person obtaining
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 a copy of this software and associated documentation files (the
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 "Software"), to deal in the Software without restriction, including
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 without limitation the rights to use, copy, modify, merge, publish,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 distribute, sublicense, and/or sell copies of the Software, and to
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 permit persons to whom the Software is furnished to do so, subject to
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 the following conditions:
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 The above copyright notice and this permission notice shall be
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 included in all copies or substantial portions of the Software.
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 606
diff changeset
24 /* @UNSAFE: whole file */
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 606
diff changeset
25
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 #include "lib.h"
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 #include "mmap-util.h"
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 #include <fcntl.h>
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 # define MAP_ANONYMOUS MAP_ANON
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 #endif
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 #ifndef HAVE_LINUX_MREMAP
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 #include <sys/mman.h>
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 /* MMAP_BASE_MOVE may be negative as well */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 #if SSIZE_T_MAX >= LLONG_MAX
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 /* 64bit or more */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 # define MMAP_BASE_MOVE (1024ULL*1024ULL*1024ULL*128ULL) /* 128GB */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 #else
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 /* 32bit most likely */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 # define MMAP_BASE_MOVE (1024UL*1024UL*128UL) /* 128M */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 #endif
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 #define MMAP_SIGNATURE 0xdeadbeef
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 #define PAGE_ALIGN(size) \
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
51 (((size) + (size_t)page_size-1) & ~(size_t)(page_size-1))
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 struct movable_header {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 unsigned int signature;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 size_t size;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 };
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 static int page_size = 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 static int header_size = 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 static void *movable_mmap_base = NULL;
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
61 static void *mmap_top_limit = NULL;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 static void movable_mmap_init(void)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 {
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
65 char x;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
67 page_size = getpagesize();
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
68 header_size = page_size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
69
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
70 /* keep our allocations far below stack. assumes the stack is
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
71 growing down. */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
72 mmap_top_limit = &x - (1024*1024*256);
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 static int anon_mmap_fixed(void *address, size_t length)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 void *base;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 #ifdef MAP_ANONYMOUS
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 base = mmap(address, length, PROT_READ | PROT_WRITE,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 #else
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 int fd;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 /* mmap()ing /dev/zero should be the same with some platforms */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 fd = open("/dev/zero", O_RDWR);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 if (fd == -1)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 i_fatal("Can't open /dev/zero for creating anonymous mmap");
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 base = mmap(address, length, PROT_READ | PROT_WRITE,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 MAP_FIXED | MAP_PRIVATE, fd, 0);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 (void)close(fd);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 #endif
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 if (base != MAP_FAILED && base != address) {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 /* shouldn't happen with MAP_FIXED, but who knows.. */
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 if (munmap(base, length) < 0)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 i_panic("munmap() failed: %m");
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 base = MAP_FAILED;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 errno = EINVAL;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 return base == MAP_FAILED ? -1 : 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 void *mmap_anon(size_t length)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 struct movable_header *hdr;
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
110 void *next_mmap_base;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
111 ssize_t offset;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 int ret;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 if (header_size == 0)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 movable_mmap_init();
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 /* we need extra page to store the pieces which construct
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 the full mmap. also allocate only page-aligned mmap sizes. */
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
119 length = PAGE_ALIGN(length + header_size);
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 if (movable_mmap_base == NULL) {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 /* this is fully guessing */
242
015910e16bfa portability fixes
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
123 movable_mmap_base = (char *) NULL +
405
576967cbd064 bugfix/cleanup
Timo Sirainen <tss@iki.fi>
parents: 347
diff changeset
124 PAGE_ALIGN((size_t)((char *)mmap_anon - (char *)NULL));
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
127 offset = MMAP_BASE_MOVE;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
128 for (;;) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
129 next_mmap_base = (char *) movable_mmap_base + offset;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
130 if ((char *) next_mmap_base < (char *) movable_mmap_base) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
131 /* we're wrapping, fix the offset a bit so we won't
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
132 just loop with same addresses.. */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
133 offset /= 2;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
134 if (offset/10 < page_size) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
135 /* enough tries */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
136 errno = ENOMEM;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
137 return MAP_FAILED;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
138 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
139 }
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
141 movable_mmap_base = next_mmap_base;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
142
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
143 if ((char *) movable_mmap_base >
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
144 (char *) movable_mmap_base + length) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
145 /* too high, would wrap */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
146 continue;
405
576967cbd064 bugfix/cleanup
Timo Sirainen <tss@iki.fi>
parents: 347
diff changeset
147 }
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
149 if ((char *) movable_mmap_base + length >=
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
150 (char *) mmap_top_limit) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
151 /* too high, stack could grow over it */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
152 continue;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
153 }
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
155 if (movable_mmap_base == NULL)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
156 continue;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
157
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
158 ret = anon_mmap_fixed(movable_mmap_base, length);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
159 if (ret == 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
160 break;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
161
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
162 if (ret < 0 && errno != EINVAL)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
163 return MAP_FAILED;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
164 }
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 /* initialize the header */
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
167 hdr = movable_mmap_base;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 hdr->signature = MMAP_SIGNATURE;
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
169 hdr->size = length - header_size;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 return (char *) hdr + header_size;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
174 static int mremap_try_grow(struct movable_header *hdr, size_t new_size)
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 {
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
176 void *grow_base;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
178 grow_base = (char *) hdr + header_size + hdr->size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
179 if ((char *) grow_base <= (char *) hdr + header_size ||
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
180 (char *) grow_base >= (char *) mmap_top_limit) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
181 /* overflows valid address range */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
182 return 0;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
185 if (anon_mmap_fixed(grow_base, new_size - hdr->size) < 0) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
186 if (errno == EINVAL) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
187 /* can't grow, wanted address space is already in use */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
188 return 0;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
189 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
190
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
191 return -1;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
192 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
193
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
194 hdr->size = new_size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
195 return 1;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
198 static void *mremap_move(struct movable_header *hdr, size_t new_size)
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 {
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
200 void *new_base;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
201 char *p;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
202 size_t block_size, old_size;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 new_base = mmap_anon(new_size - header_size);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 if (new_base == MAP_FAILED)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206 return MAP_FAILED;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
208 /* If we're moving large memory areas, it takes less memory to
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
209 copy the memory pages in smaller blocks. */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
210 old_size = hdr->size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
211 block_size = 1024*1024;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
213 p = (char *) (hdr + header_size + hdr->size);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
214 do {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
215 if (block_size > old_size)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
216 block_size = old_size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
217 p -= block_size;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
219 memcpy((char *) new_base + (p - (char *) hdr), p, block_size);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
220 if (munmap(p, block_size) < 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
221 i_panic("munmap() failed: %m");
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
222 } while (p != (char *) hdr);
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
224 return new_base;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 void *mremap_anon(void *old_address, size_t old_size __attr_unused__,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 size_t new_size, unsigned long flags)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 struct movable_header *hdr;
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
231 int ret;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 if (old_address == NULL || old_address == MAP_FAILED) {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 errno = EINVAL;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235 return MAP_FAILED;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 hdr = (struct movable_header *) ((char *) old_address - header_size);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239 if (hdr->signature != MMAP_SIGNATURE)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240 i_panic("movable_mremap(): Invalid old_address");
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
242 new_size = PAGE_ALIGN(new_size);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
243
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
244 if (new_size > hdr->size) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
245 /* grow */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
246 ret = mremap_try_grow(hdr, new_size);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
247 if (ret > 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
248 return old_address;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
249 if (ret < 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
250 return MAP_FAILED;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
252 if ((flags & MREMAP_MAYMOVE) == 0) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
253 errno = ENOMEM;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
254 return MAP_FAILED;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
255 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
256
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
257 return mremap_move(hdr, new_size);
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
258 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
259
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
260 if (new_size < hdr->size) {
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
261 /* shrink */
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
262 if (munmap((char *) hdr + header_size + new_size,
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
263 hdr->size - new_size) < 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
264 i_panic("munmap() failed: %m");
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
265 hdr->size = new_size;
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
266 }
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
267
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
268 return old_address;
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 int munmap_anon(void *start, size_t length __attr_unused__)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 struct movable_header *hdr;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 if (start == NULL || start == MAP_FAILED) {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 errno = EINVAL;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 return -1;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 hdr = (struct movable_header *) ((char *) start - header_size);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281 if (hdr->signature != MMAP_SIGNATURE)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 i_panic("movable_munmap(): Invalid address");
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283
858
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
284 if (munmap(hdr, hdr->size + header_size) < 0)
8f4bd02461e0 Old implementation was overly complex and stupid.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
285 i_panic("munmap() failed: %m");
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 return 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 #else
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 void *mmap_anon(size_t length)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 return mmap(NULL, length, PROT_READ | PROT_WRITE,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 void *mremap_anon(void *old_address, size_t old_size, size_t new_size,
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 unsigned long flags)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301 return mremap(old_address, old_size, new_size, flags);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 int munmap_anon(void *start, size_t length)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 return munmap(start, length);
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 #endif