2000-02-25 05:45:54 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
2000-02-25 05:45:54 -08:00
|
|
|
/* */
|
|
|
|
/* 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 */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
2000-02-25 05:45:54 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2012-07-26 12:21:54 -07:00
|
|
|
/* Needed (under Linux at least) to get pwrite's prototype in unistd.h.
|
|
|
|
Must be defined before the first system .h is included. */
|
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
|
2000-02-25 05:45:54 -08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "bigarray.h"
|
|
|
|
#include "custom.h"
|
|
|
|
#include "fail.h"
|
2006-06-10 07:15:43 -07:00
|
|
|
#include "io.h"
|
2000-02-25 05:45:54 -08:00
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "sys.h"
|
2011-07-20 02:17:07 -07:00
|
|
|
#include "signals.h"
|
2000-02-25 05:45:54 -08:00
|
|
|
|
2006-01-27 06:33:42 -08:00
|
|
|
extern int caml_ba_element_size[]; /* from bigarray_stubs.c */
|
2000-02-25 05:45:54 -08:00
|
|
|
|
2012-04-04 05:12:02 -07:00
|
|
|
#include <errno.h>
|
2000-02-25 05:45:54 -08:00
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAS_MMAP
|
2000-02-28 07:46:17 -08:00
|
|
|
#include <sys/types.h>
|
2000-02-25 05:45:54 -08:00
|
|
|
#include <sys/mman.h>
|
2012-04-04 05:12:02 -07:00
|
|
|
#include <sys/stat.h>
|
2000-02-25 05:45:54 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAS_MMAP)
|
|
|
|
|
2000-03-16 07:39:32 -08:00
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED ((void *) -1)
|
|
|
|
#endif
|
|
|
|
|
2012-04-04 05:12:02 -07:00
|
|
|
/* [caml_grow_file] function contributed by Gerd Stolpmann (PR#5543). */
|
|
|
|
|
|
|
|
static int caml_grow_file(int fd, file_offset size)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
/* First use pwrite for growing - it is a conservative method, as it
|
|
|
|
can never happen that we shrink by accident
|
|
|
|
*/
|
|
|
|
#ifdef HAS_PWRITE
|
|
|
|
c = 0;
|
|
|
|
p = pwrite(fd, &c, 1, size - 1);
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Emulate pwrite with lseek. This should only be necessary on ancient
|
|
|
|
systems nowadays
|
|
|
|
*/
|
|
|
|
file_offset currpos;
|
|
|
|
currpos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (currpos != -1) {
|
|
|
|
p = lseek(fd, size - 1, SEEK_SET);
|
|
|
|
if (p != -1) {
|
|
|
|
c = 0;
|
|
|
|
p = write(fd, &c, 1);
|
2012-07-30 11:04:46 -07:00
|
|
|
if (p != -1)
|
2012-04-04 05:12:02 -07:00
|
|
|
p = lseek(fd, currpos, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else p=-1;
|
|
|
|
#endif
|
|
|
|
#ifdef HAS_TRUNCATE
|
|
|
|
if (p == -1 && errno == ESPIPE) {
|
|
|
|
/* Plan B. Check if at least ftruncate is possible. There are
|
|
|
|
some non-seekable descriptor types that do not support pwrite
|
|
|
|
but ftruncate, like shared memory. We never get into this case
|
|
|
|
for real files, so there is no danger of truncating persistent
|
|
|
|
data by accident
|
|
|
|
*/
|
|
|
|
p = ftruncate(fd, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-27 06:33:42 -08:00
|
|
|
CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
|
2006-06-10 07:15:43 -07:00
|
|
|
value vshared, value vdim, value vstart)
|
2000-02-25 05:45:54 -08:00
|
|
|
{
|
|
|
|
int fd, flags, major_dim, shared;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat num_dims, i;
|
2008-01-04 07:01:48 -08:00
|
|
|
intnat dim[CAML_BA_MAX_NUM_DIMS];
|
2012-04-04 05:12:02 -07:00
|
|
|
file_offset startpos, file_size, data_size;
|
|
|
|
struct stat st;
|
2006-06-10 07:15:43 -07:00
|
|
|
uintnat array_size, page, delta;
|
2000-02-25 05:45:54 -08:00
|
|
|
void * addr;
|
|
|
|
|
|
|
|
fd = Int_val(vfd);
|
|
|
|
flags = Int_val(vkind) | Int_val(vlayout);
|
2006-06-10 07:15:43 -07:00
|
|
|
startpos = File_offset_val(vstart);
|
2000-02-25 05:45:54 -08:00
|
|
|
num_dims = Wosize_val(vdim);
|
2008-01-04 07:01:48 -08:00
|
|
|
major_dim = flags & CAML_BA_FORTRAN_LAYOUT ? num_dims - 1 : 0;
|
2012-02-10 08:15:24 -08:00
|
|
|
/* Extract dimensions from OCaml array */
|
2000-02-25 05:45:54 -08:00
|
|
|
num_dims = Wosize_val(vdim);
|
2008-01-04 07:01:48 -08:00
|
|
|
if (num_dims < 1 || num_dims > CAML_BA_MAX_NUM_DIMS)
|
|
|
|
caml_invalid_argument("Bigarray.mmap: bad number of dimensions");
|
2000-02-25 05:45:54 -08:00
|
|
|
for (i = 0; i < num_dims; i++) {
|
|
|
|
dim[i] = Long_val(Field(vdim, i));
|
|
|
|
if (dim[i] == -1 && i == major_dim) continue;
|
2008-01-04 07:01:48 -08:00
|
|
|
if (dim[i] < 0)
|
|
|
|
caml_invalid_argument("Bigarray.create: negative dimension");
|
2000-02-25 05:45:54 -08:00
|
|
|
}
|
2012-04-04 05:12:02 -07:00
|
|
|
/* Determine file size. We avoid lseek here because it is fragile,
|
|
|
|
and because some mappable file types do not support it
|
|
|
|
*/
|
2010-04-01 00:36:49 -07:00
|
|
|
caml_enter_blocking_section();
|
2012-04-04 05:12:02 -07:00
|
|
|
if (fstat(fd, &st) == -1) {
|
2010-04-01 00:36:49 -07:00
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_sys_error(NO_ARG);
|
|
|
|
}
|
2012-04-04 05:12:02 -07:00
|
|
|
file_size = st.st_size;
|
2000-02-25 05:45:54 -08:00
|
|
|
/* Determine array size in bytes (or size of array without the major
|
|
|
|
dimension if that dimension wasn't specified) */
|
2008-01-04 07:01:48 -08:00
|
|
|
array_size = caml_ba_element_size[flags & CAML_BA_KIND_MASK];
|
2000-02-25 05:45:54 -08:00
|
|
|
for (i = 0; i < num_dims; i++)
|
|
|
|
if (dim[i] != -1) array_size *= dim[i];
|
2006-06-10 07:15:43 -07:00
|
|
|
/* Check if the major dimension is unknown */
|
2000-02-25 05:45:54 -08:00
|
|
|
if (dim[major_dim] == -1) {
|
2006-06-10 07:15:43 -07:00
|
|
|
/* Determine major dimension from file size */
|
2010-04-01 00:36:49 -07:00
|
|
|
if (file_size < startpos) {
|
|
|
|
caml_leave_blocking_section();
|
2008-01-04 07:01:48 -08:00
|
|
|
caml_failwith("Bigarray.mmap: file position exceeds file size");
|
2010-04-01 00:36:49 -07:00
|
|
|
}
|
2006-06-10 07:15:43 -07:00
|
|
|
data_size = file_size - startpos;
|
|
|
|
dim[major_dim] = (uintnat) (data_size / array_size);
|
|
|
|
array_size = dim[major_dim] * array_size;
|
2010-04-01 00:36:49 -07:00
|
|
|
if (array_size != data_size) {
|
|
|
|
caml_leave_blocking_section();
|
2008-01-04 07:01:48 -08:00
|
|
|
caml_failwith("Bigarray.mmap: file size doesn't match array dimensions");
|
2010-04-01 00:36:49 -07:00
|
|
|
}
|
2000-02-25 05:45:54 -08:00
|
|
|
} else {
|
|
|
|
/* Check that file is large enough, and grow it otherwise */
|
2006-06-10 07:15:43 -07:00
|
|
|
if (file_size < startpos + array_size) {
|
2012-04-04 05:12:02 -07:00
|
|
|
if (caml_grow_file(fd, startpos + array_size) == -1) { /* PR#5543 */
|
2010-04-01 00:36:49 -07:00
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_sys_error(NO_ARG);
|
|
|
|
}
|
2000-02-25 05:45:54 -08:00
|
|
|
}
|
|
|
|
}
|
2006-06-10 07:15:43 -07:00
|
|
|
/* Determine offset so that the mapping starts at the given file pos */
|
|
|
|
page = getpagesize();
|
2012-04-09 03:23:50 -07:00
|
|
|
delta = (uintnat) startpos % page;
|
2000-02-25 05:45:54 -08:00
|
|
|
/* Do the mmap */
|
|
|
|
shared = Bool_val(vshared) ? MAP_SHARED : MAP_PRIVATE;
|
2012-04-09 03:23:50 -07:00
|
|
|
if (array_size > 0)
|
|
|
|
addr = mmap(NULL, array_size + delta, PROT_READ | PROT_WRITE,
|
|
|
|
shared, fd, startpos - delta);
|
|
|
|
else
|
|
|
|
addr = NULL; /* PR#5463 - mmap fails on empty region */
|
2010-04-01 00:36:49 -07:00
|
|
|
caml_leave_blocking_section();
|
2008-01-04 07:01:48 -08:00
|
|
|
if (addr == (void *) MAP_FAILED) caml_sys_error(NO_ARG);
|
2006-06-10 07:15:43 -07:00
|
|
|
addr = (void *) ((uintnat) addr + delta);
|
2012-02-10 08:15:24 -08:00
|
|
|
/* Build and return the OCaml bigarray */
|
2008-01-04 07:01:48 -08:00
|
|
|
return caml_ba_alloc(flags | CAML_BA_MAPPED_FILE, num_dims, addr, dim);
|
2000-02-25 05:45:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-04-09 03:23:50 -07:00
|
|
|
CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
|
|
|
|
value vshared, value vdim, value vpos)
|
2000-02-25 05:45:54 -08:00
|
|
|
{
|
2011-07-20 02:17:07 -07:00
|
|
|
caml_invalid_argument("Bigarray.map_file: not supported");
|
2000-02-25 05:45:54 -08:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-06-10 07:15:43 -07:00
|
|
|
CAMLprim value caml_ba_map_file_bytecode(value * argv, int argn)
|
|
|
|
{
|
|
|
|
return caml_ba_map_file(argv[0], argv[1], argv[2],
|
|
|
|
argv[3], argv[4], argv[5]);
|
|
|
|
}
|
2000-02-25 05:45:54 -08:00
|
|
|
|
2006-01-27 06:33:42 -08:00
|
|
|
void caml_ba_unmap_file(void * addr, uintnat len)
|
2000-02-25 05:45:54 -08:00
|
|
|
{
|
|
|
|
#if defined(HAS_MMAP)
|
2006-06-10 07:15:43 -07:00
|
|
|
uintnat page = getpagesize();
|
|
|
|
uintnat delta = (uintnat) addr % page;
|
2012-04-09 03:23:50 -07:00
|
|
|
if (len == 0) return; /* PR#5463 */
|
|
|
|
addr = (void *)((uintnat)addr - delta);
|
|
|
|
len = len + delta;
|
|
|
|
#if defined(_POSIX_SYNCHRONIZED_IO)
|
|
|
|
msync(addr, len, MS_ASYNC); /* PR#3571 */
|
|
|
|
#endif
|
|
|
|
munmap(addr, len);
|
2000-02-25 05:45:54 -08:00
|
|
|
#endif
|
|
|
|
}
|