Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
srn_mmap.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <lxsameer@lxsameer.com>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library 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.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
20
21#if PMAP_WIN32
22
23# include <io.h>
24
25# include "serene/rt/context.h"
26# include "serene/rt/errors.h"
27
28static DWORD get_allocation_granularity(void) {
29 static DWORD granularity;
30 SYSTEM_INFO sys;
31
32 if (!granularity) {
33 GetSystemInfo(&sys);
34 granularity = sys.dwAllocationGranularity;
35 }
36
37 return granularity;
38}
39
41 srn_context_t *ctx, srn_mmap_buffer_t *out, size_t len, int prot, int flags, int fd,
42 off64_t offset
43) {
44 HANDLE fh = (HANDLE)_get_osfhandle(fd);
45 DWORD alignment = get_allocation_granularity();
46 DWORD fmap_prot = 0;
47 DWORD view_prot = 0;
48 DWORD off_low = 0;
49 DWORD off_hi = 0;
50 off64_t page_start;
51 off64_t page_offset;
52
53 SRN_MMAP_VALIDATE(out, len, prot, flags);
54
55 out->data = NULL;
56 out->len = 0;
57 out->fmh = NULL;
58
59 if (fh == INVALID_HANDLE_VALUE) {
60 return ERR(ctx, MEMORY_MAP_FAILED, "failed to mmap. Invalid handle value");
61 }
62
63 if (prot & SRN_PROT_WRITE) {
64 fmap_prot |= PAGE_READWRITE;
65 } else if (prot & SRN_PROT_READ) {
66 fmap_prot |= PAGE_READONLY;
67 }
68
69 if (prot & SRN_PROT_WRITE) {
70 view_prot |= FILE_MAP_WRITE;
71 }
72 if (prot & SRN_PROT_READ) {
73 view_prot |= FILE_MAP_READ;
74 }
75
76 page_start = (offset / alignment) * alignment;
77 page_offset = offset - page_start;
78
79 if (page_offset != 0) { // offset must be a multiple of the allocation granularity
80 return ERR(
81 ctx, MEMORY_MAP_FAILED, "failed to mmap. Offset must be a multiple of allocation granularity"
82 );
83 }
84
85 out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
86 if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
87 out->fmh = NULL;
88 return ERR(ctx, MEMORY_MAP_FAILED, "failed to mmap. Could not create file mapping");
89 }
90
91 off_low = (DWORD)(page_start);
92 off_hi = (DWORD)(page_start >> 32);
93 out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
94 if (!out->data) {
95 CloseHandle(out->fmh);
96 out->fmh = NULL;
97 return ERR(ctx, MEMORY_MAP_FAILED, "failed to mmap. Could not map view of file");
98 }
99 out->len = len;
100
101 return nullptr;
102}
103
105 int error = 0;
106
107 PANIC_IF_NULL(map);
108
109 if (map->data) {
110 if (!UnmapViewOfFile(map->data)) {
111 error = -1;
112 }
113 map->data = NULL;
114 }
115
116 if (map->fmh) {
117 if (!CloseHandle(map->fmh)) {
118 error = -1;
119 }
120 map->fmh = NULL;
121 }
122
123 map->len = 0;
124
125 return error;
126}
127
128#endif
Error handling for the runtime.
@ MEMORY_MAP_FAILED
Definition errors.h:138
#define ERR(ctx, err, msg)
Definition errors.h:170
The fiber facing IO API: blocking looking calls a fiber uses to do IO.
int srn_munmap(srn_mmap_buffer_t *map)
Definition srn_mmap.c:66
srn_error_t * srn_mmap(srn_context_t *ctx, srn_mmap_buffer_t *out, size_t len, int prot, int flags, int fd, off64_t offset)
Definition srn_mmap.c:30
#define SRN_PROT_WRITE
Definition srn_mmap.h:40
#define SRN_PROT_READ
Definition srn_mmap.h:39
#define SRN_MMAP_VALIDATE(out, len, prot, flags)
Definition srn_mmap.h:68
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:147
Memory mapped buffer.
Definition srn_mmap.h:56
void * data
data bytes
Definition srn_mmap.h:58
size_t len
data length
Definition srn_mmap.h:60
#define PANIC_IF_NULL(ptr)
Definition utils.h:66