[ruby/prism] Use `_POSIX_MAPPED_FILES` and `_WIN32` to know if memory map interface is available in the target platform

https://github.com/ruby/prism/commit/88e2ff52d4
This commit is contained in:
HASUMI Hitoshi 2024-01-22 15:17:40 +09:00 коммит произвёл git
Родитель c22cb960cf
Коммит 15b53e901c
4 изменённых файлов: 23 добавлений и 3 удалений

Просмотреть файл

@ -99,4 +99,13 @@
# define PM_STATIC_ASSERT(line, condition, message) typedef char PM_CONCATENATE(static_assert_, line)[(condition) ? 1 : -1]
#endif
/**
* In general, libc for embedded systems does not support memory-mapped files.
* If the target platform is POSIX or Windows, we can map a file in memory and
* read it in a more efficient manner.
*/
#if defined(_POSIX_MAPPED_FILES) || defined(_WIN32)
# define PRISM_HAS_MMAP
#endif
#endif

Просмотреть файл

@ -41,9 +41,11 @@ pm_serialize_string(pm_parser_t *parser, pm_string_t *string, pm_buffer_t *buffe
pm_buffer_append_bytes(buffer, pm_string_source(string), length);
break;
}
#ifdef PRISM_HAS_MMAP
case PM_STRING_MAPPED:
assert(false && "Cannot serialize mapped strings.");
break;
#endif
}
}

Просмотреть файл

@ -102,7 +102,7 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
*string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
return true;
#else
#elif defined(_POSIX_MAPPED_FILES)
// Open the file for reading
int fd = open(filepath, O_RDONLY);
if (fd == -1) {
@ -135,6 +135,11 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
close(fd);
*string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
return true;
#else
(void) string;
(void) filepath;
perror("pm_string_mapped_init is not implemented for this platform");
return false;
#endif
}
@ -213,11 +218,13 @@ pm_string_free(pm_string_t *string) {
if (string->type == PM_STRING_OWNED) {
free(memory);
#ifdef PRISM_HAS_MMAP
} else if (string->type == PM_STRING_MAPPED && string->length) {
#if defined(_WIN32)
UnmapViewOfFile(memory);
#else
#elif defined(_POSIX_MAPPED_FILES)
munmap(memory, string->length);
#endif
#endif /* PRISM_HAS_MMAP */
}
}

Просмотреть файл

@ -17,7 +17,7 @@
// The following headers are necessary to read files using demand paging.
#ifdef _WIN32
#include <windows.h>
#else
#elif defined(_POSIX_MAPPED_FILES)
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
@ -45,8 +45,10 @@ typedef struct {
/** This string owns its memory, and should be freed using `pm_string_free`. */
PM_STRING_OWNED,
#ifdef PRISM_HAS_MMAP
/** This string is a memory-mapped file, and should be freed using `pm_string_free`. */
PM_STRING_MAPPED
#endif
} type;
} pm_string_t;