ocaml/otherlibs/win32unix/mmap.c

169 lines
6.0 KiB
C
Raw Normal View History

/**************************************************************************/
/* */
/* OCaml */
/* */
/* Manuel Serrano and Xavier Leroy, INRIA Rocquencourt */
/* */
/* Copyright 2000 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#include <stddef.h>
#include "caml/alloc.h"
#include "caml/bigarray.h"
#include "caml/fail.h"
#include "caml/io.h"
#include "caml/mlvalues.h"
#include "caml/signals.h"
#include "caml/sys.h"
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
#include "caml/osdeps.h"
#include "unixsupport.h"
#define uerror(func, arg) \
do { win32_maperr(GetLastError()); uerror(func, arg); } while(0)
/* Defined in [mmap_ba.c] */
2020-09-10 05:55:38 -07:00
extern value caml_unix_mapped_alloc(int, int, void *, intnat *);
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER (-1)
#endif
static __int64 caml_set_file_pointer(HANDLE h, __int64 dist, DWORD mode)
{
LARGE_INTEGER i;
DWORD err;
i.QuadPart = dist;
i.LowPart = SetFilePointer(h, i.LowPart, &i.HighPart, mode);
if (i.LowPart == INVALID_SET_FILE_POINTER) return -1;
return i.QuadPart;
}
CAMLprim value caml_unix_map_file(value vfd, value vkind, value vlayout,
value vshared, value vdim, value vstart)
{
HANDLE fd, fmap;
int flags, major_dim, mode, perm;
intnat num_dims, i;
intnat dim[CAML_BA_MAX_NUM_DIMS];
__int64 currpos, startpos, file_size, data_size;
uintnat array_size, page, delta;
char c;
void * addr;
LARGE_INTEGER li;
SYSTEM_INFO sysinfo;
fd = Handle_val(vfd);
flags = Caml_ba_kind_val(vkind) | Caml_ba_layout_val(vlayout);
startpos = Int64_val(vstart);
num_dims = Wosize_val(vdim);
major_dim = flags & CAML_BA_FORTRAN_LAYOUT ? num_dims - 1 : 0;
/* Extract dimensions from OCaml array */
num_dims = Wosize_val(vdim);
if (num_dims < 1 || num_dims > CAML_BA_MAX_NUM_DIMS)
caml_invalid_argument("Unix.map_file: bad number of dimensions");
for (i = 0; i < num_dims; i++) {
dim[i] = Long_val(Field(vdim, i));
if (dim[i] == -1 && i == major_dim) continue;
if (dim[i] < 0)
caml_invalid_argument("Unix.map_file: negative dimension");
}
/* Determine file size */
currpos = caml_set_file_pointer(fd, 0, FILE_CURRENT);
if (currpos == -1) uerror("map_file", Nothing);
file_size = caml_set_file_pointer(fd, 0, FILE_END);
if (file_size == -1) uerror("map_file", Nothing);
/* Determine array size in bytes (or size of array without the major
dimension if that dimension wasn't specified) */
array_size = caml_ba_element_size[flags & CAML_BA_KIND_MASK];
for (i = 0; i < num_dims; i++)
if (dim[i] != -1) array_size *= dim[i];
/* Check if the first/last dimension is unknown */
if (dim[major_dim] == -1) {
/* Determine first/last dimension from file size */
if (file_size < startpos)
caml_failwith("Unix.map_file: file position exceeds file size");
data_size = file_size - startpos;
dim[major_dim] = (uintnat) (data_size / array_size);
array_size = dim[major_dim] * array_size;
if (array_size != data_size)
caml_failwith("Unix.map_file: file size doesn't match array dimensions");
}
/* Restore original file position */
caml_set_file_pointer(fd, currpos, FILE_BEGIN);
/* Create the file mapping */
if (Bool_val(vshared)) {
perm = PAGE_READWRITE;
mode = FILE_MAP_WRITE;
} else {
perm = PAGE_READONLY; /* doesn't work under Win98 */
mode = FILE_MAP_COPY;
}
li.QuadPart = startpos + array_size;
fmap = CreateFileMapping(fd, NULL, perm, li.HighPart, li.LowPart, NULL);
if (fmap == NULL) uerror("map_file", Nothing);
/* Determine offset so that the mapping starts at the given file pos */
GetSystemInfo(&sysinfo);
delta = (uintnat) (startpos % sysinfo.dwAllocationGranularity);
/* Map the mapping in memory */
li.QuadPart = startpos - delta;
addr =
MapViewOfFile(fmap, mode, li.HighPart, li.LowPart, array_size + delta);
if (addr == NULL) uerror("map_file", Nothing);
addr = (void *) ((uintnat) addr + delta);
/* Close the file mapping */
CloseHandle(fmap);
/* Build and return the OCaml bigarray */
return caml_unix_mapped_alloc(flags, num_dims, addr, dim);
}
CAMLprim value caml_unix_map_file_bytecode(value * argv, int argn)
{
return caml_unix_map_file(argv[0], argv[1], argv[2],
argv[3], argv[4], argv[5]);
}
void caml_ba_unmap_file(void * addr, uintnat len)
{
SYSTEM_INFO sysinfo;
uintnat delta;
GetSystemInfo(&sysinfo);
delta = (uintnat) addr % sysinfo.dwAllocationGranularity;
UnmapViewOfFile((void *)((uintnat)addr - delta));
}
#ifdef IN_OCAML_BIGARRAY
/* This function reports a Win32 error as a Sys_error exception.
It is included for backward compatibility with the old
Bigarray.*.map_file implementation. */
static void caml_ba_sys_error(void)
{
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
wchar_t buffer[512];
DWORD errnum;
errnum = GetLastError();
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errnum,
0,
buffer,
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
sizeof(buffer)/sizeof(wchar_t),
NULL))
2017-08-12 13:24:41 -07:00
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t),
L"Unknown error %ld\n", errnum);
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
caml_raise_sys_error(caml_copy_string_of_utf16(buffer));
}
#endif