3 Copyright (C) 2021-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2021. */
23 #include "immutable.h"
29 #if IMMUTABLE_EFFECTIVE
30 /* Real implementation. */
35 /* Get intptr_t, uintptr_t. */
40 # if defined _WIN32 && !defined __CYGWIN__
42 /* Declare VirtualAlloc(), GetSystemInfo. */
43 # define WIN32_LEAN_AND_MEAN
44 # define WIN32_EXTRA_LEAN
47 /* Don't assume that UNICODE is not defined. */
48 # undef CreateFileMapping
49 # define CreateFileMapping CreateFileMappingA
53 /* Declare getpagesize(). */
55 /* On HP-UX, getpagesize exists, but it is not declared in <unistd.h> even if
56 the compiler options -D_HPUX_SOURCE -D_XOPEN_SOURCE=600 are used. */
62 int getpagesize (void);
65 /* Declare mmap(), mprotect(). */
66 # include <sys/types.h>
67 # include <sys/mman.h>
76 # define PATH_MAX 4096
79 # include "glthread/once.h"
84 /* ================= Back end of the malloc implementation ================= */
86 /* The memory page size.
87 Once it is initialized, a power of 2. Typically 4096 or 8192. */
88 static uintptr_t pagesize
;
90 /* Initializes pagesize. */
94 /* Simultaneous execution of this initialization in multiple threads is OK. */
95 # if defined _WIN32 && !defined __CYGWIN__
97 <https://msdn.microsoft.com/en-us/library/ms724381.aspx>
98 <https://msdn.microsoft.com/en-us/library/ms724958.aspx> */
100 GetSystemInfo (&info
);
101 pagesize
= info
.dwPageSize
;
103 pagesize
= getpagesize ();
108 # if defined _WIN32 && !defined __CYGWIN__
111 init_mmap_file (void)
117 /* Variables needed for obtaining memory pages via mmap(). */
119 static long file_length
;
121 /* Initialization of these variables. */
123 do_init_mmap_file (void)
125 /* Use TMPDIR, except if it is too long. */
126 const char *tmpdir
= getenv ("TMPDIR");
127 if (tmpdir
== NULL
|| strlen (tmpdir
) > PATH_MAX
)
129 /* Now strlen (tmpdir) <= PATH_MAX. */
131 char filename
[PATH_MAX
+ 1 + 41 + 1];
132 sprintf (filename
, "%s/glimmdata-%d-%ld", tmpdir
, getpid (), random ());
134 file_fd
= open (filename
, O_CREAT
| O_TRUNC
| O_RDWR
| O_CLOEXEC
, 0700);
137 fprintf (stderr
, "glimm: Cannot open %s!\n", filename
);
141 /* Remove the file from the file system as soon as possible, to make
142 sure there is no leftover after this process terminates or crashes. */
148 /* Once-only initializer for these variables. */
149 gl_once_define (static, for_mmap_once
)
152 init_mmap_file (void)
154 /* Use a once-only initializer here, since simultaneous execution of
155 do_init_mmap_file() in multiple threads must be avoided. */
156 gl_once (for_mmap_once
, do_init_mmap_file
);
162 /* Size of the (page-aligned) header that links the writable mapping
163 and the read-only mapping together. */
164 # define SHARED_LINK_HEADER_SIZE \
165 (INTPTR_WIDTH / CHAR_BIT) /* = sizeof (void *) */
167 /* Allocates a contiguous set of pages of memory.
168 size > 0, must be a multiple of pagesize.
169 Returns a multiple of PAGESIZE, or 0 upon failure. */
171 alloc_pages (size_t size
)
173 # if defined _WIN32 && !defined __CYGWIN__
174 /* Allocate pages from the system paging file.
176 <https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga> */
178 CreateFileMapping (INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_COMMIT
,
179 size
>> 16 >> 16, size
& 0xFFFFFFFFU
, NULL
);
183 "glimm: Cannot allocate file mapping. GetLastError() = 0x%08X\n",
184 (unsigned int) GetLastError ());
188 <https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile> */
189 char *mem_w
= (char *) MapViewOfFile (h
, FILE_MAP_WRITE
, 0, 0, size
);
190 char *mem_r
= (char *) MapViewOfFile (h
, FILE_MAP_READ
, 0, 0, size
);
191 if (mem_w
== NULL
|| mem_r
== NULL
)
194 <https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile> */
196 UnmapViewOfFile (mem_w
);
198 UnmapViewOfFile (mem_r
);
201 /* It is OK to call CloseHandle before UnmapViewOfFile. The file mapping
202 object gets really closed only once all its views are unmapped. */
203 if (!CloseHandle (h
))
205 UnmapViewOfFile (mem_w
);
206 UnmapViewOfFile (mem_r
);
211 /* Extend the file by size/pagesize pages. */
212 long new_file_length
= file_length
+ size
;
213 if (ftruncate (file_fd
, new_file_length
) < 0)
215 fprintf (stderr
, "glimm: Cannot extend backing file!\n");
218 /* Create separate writable mapping and read-only mapping. */
219 char *mem_w
= (char *) mmap (NULL
, size
, PROT_READ
| PROT_WRITE
,
220 MAP_SHARED
, file_fd
, file_length
);
221 char *mem_r
= (char *) mmap (NULL
, size
, PROT_READ
,
222 MAP_SHARED
, file_fd
, file_length
);
223 if (mem_w
== (char *)(-1) || mem_r
== (char *)(-1))
225 if (mem_w
!= (char *)(-1))
226 munmap (mem_w
, size
);
227 if (mem_r
!= (char *)(-1))
228 munmap (mem_r
, size
);
231 file_length
= new_file_length
;
234 /* Link the two memory areas together. */
235 ((intptr_t *) mem_w
)[0] = mem_r
- mem_w
;
236 return (uintptr_t) mem_w
;
239 /* Frees a contiguous set of pages of memory, returned by alloc_pages.
240 size > 0, must be a multiple of pagesize. */
242 free_pages (uintptr_t pages
, size_t size
)
244 pages
-= SHARED_LINK_HEADER_SIZE
;
245 if ((pages
& (pagesize
- 1)) != 0)
247 char *mem_w
= (char *) pages
;
248 char *mem_r
= mem_w
+ ((intptr_t *) mem_w
)[0];
249 # if defined _WIN32 && !defined __CYGWIN__
250 if (!UnmapViewOfFile (mem_w
))
252 if (!UnmapViewOfFile (mem_r
))
255 if (munmap (mem_w
, size
) < 0)
257 if (munmap (mem_r
, size
) < 0)
262 /* Cygwin defines PAGESIZE in <limits.h>. */
265 /* ======================= Instantiate the front end ======================= */
267 # define PAGESIZE pagesize
268 /* On Cygwin and Linux/PowerPC, PAGESIZE is 65536. On macOS 11, it is 16384.
269 On all other platforms, it is either 4096 or 8192. */
270 # if defined __CYGWIN__ || (defined __linux__ && defined __powerpc__)
271 # define PAGESIZE_MAX 65536
273 # define PAGESIZE_MAX 16384
276 # define ALLOC_PAGES alloc_pages
277 # define FREE_PAGES free_pages
278 # define ALIGNMENT sizeof (void *)
279 # define PAGE_RESERVED_HEADER_SIZE SHARED_LINK_HEADER_SIZE
281 # include "ssfmalloc.h"
285 immmalloc (size_t size
)
287 /* Initializations. */
294 void *writable_pointer
= (void *) allocate_block (size
);
295 if (writable_pointer
== NULL
)
297 return writable_pointer
;
301 immfreeze (void *writable_pointer
)
303 uintptr_t mem_w
= (uintptr_t) writable_pointer
& -(intptr_t)pagesize
;
304 return (void *) ((uintptr_t) writable_pointer
+ ((intptr_t *) mem_w
)[0]);
308 immfree (const void *readonly_pointer
)
310 uintptr_t mem_r
= (uintptr_t) readonly_pointer
& -(intptr_t)pagesize
;
311 free_block ((uintptr_t) readonly_pointer
- ((intptr_t *) mem_r
)[0]);
315 /* Dummy implementation. */
318 immmalloc (size_t size
)
320 void *p
= malloc (size
);
327 immfreeze (void *writable_pointer
)
329 return writable_pointer
;
333 immfree (const void *readonly_pointer
)
335 void *writable_pointer
= (void *) readonly_pointer
;
336 free (writable_pointer
);
343 immstrdup (const char *string
)
345 size_t size
= strlen (string
) + 1;
346 void *wp
= immmalloc (size
);
349 memcpy (wp
, string
, size
);
350 return (const char *) immfreeze (wp
);