Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
srn_mmap.h
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
19// This implementation is based on libgit2's api
20#pragma once
21
22#include <stddef.h>
23#include <stdio.h>
24
25#include "serene/utils.h"
26
27#if (defined(_WIN32)) && !defined(__CYGWIN__)
28# define PMAP_WIN32 1
29#else
30# define PMAP_WIN32 0
31#endif
32
33#if PMAP_WIN32
34# include <windows.h>
35#endif
36
37/* srn_mmap() prot values */
38#define SRN_PROT_NONE 0x0
39#define SRN_PROT_READ 0x1
40#define SRN_PROT_WRITE 0x2
41#define SRN_PROT_EXEC 0x4
42
43/* srn__mmmap() flags values */
44#define SRN_MAP_FILE 0
45#define SRN_MAP_SHARED 1
46#define SRN_MAP_PRIVATE 2
47#define SRN_MAP_TYPE 0xf
48#define SRN_MAP_FIXED 0x10
49
50typedef struct srn_context_t srn_context_t;
51typedef struct srn_error_t srn_error_t;
52
53/**
54 * Memory mapped buffer
55 */
56typedef struct srn_mmap_buffer_t {
57 /// data bytes
58 void *data;
59 /// data length
60 size_t len;
61
62#if PMAP_WIN32
63 /// file mapping handle
64 HANDLE fmh;
65#endif
67
68#define SRN_MMAP_VALIDATE(out, len, prot, flags) \
69 do { \
70 PANIC_IF( \
71 (out) == nullptr || (len) == 0, \
72 "mmap requires a non-null output buffer and a positive length" \
73 ); \
74 PANIC_IF( \
75 !(((prot) & SRN_PROT_WRITE) || ((prot) & SRN_PROT_READ)), \
76 "mmap requires read or write protection" \
77 ); \
78 PANIC_IF(((flags) & SRN_MAP_FIXED) != 0, "mmap does not support fixed mappings"); \
79 } while (0)
80
81extern srn_error_t *srn_mmap(
82 srn_context_t *ctx, srn_mmap_buffer_t *out, size_t len, int prot, int flags, int fd,
83 off64_t offset
84);
85extern int srn_munmap(srn_mmap_buffer_t *map);
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
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