2000-02-25 05:45:54 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
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
|
|
|
|
|
|
|
#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>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAS_MMAP)
|
|
|
|
|
2000-03-16 07:39:32 -08:00
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED ((void *) -1)
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
intnat dim[MAX_NUM_DIMS];
|
2006-06-10 07:15:43 -07:00
|
|
|
file_offset currpos, startpos, file_size, data_size;
|
|
|
|
uintnat array_size, page, delta;
|
2000-02-25 05:45:54 -08:00
|
|
|
char c;
|
|
|
|
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);
|
|
|
|
major_dim = flags & BIGARRAY_FORTRAN_LAYOUT ? num_dims - 1 : 0;
|
|
|
|
/* Extract dimensions from Caml array */
|
|
|
|
num_dims = Wosize_val(vdim);
|
|
|
|
if (num_dims < 1 || num_dims > MAX_NUM_DIMS)
|
|
|
|
invalid_argument("Bigarray.mmap: 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 || dim[i] > 0x7FFFFFFFL)
|
|
|
|
invalid_argument("Bigarray.create: negative dimension");
|
|
|
|
}
|
|
|
|
/* Determine file size */
|
|
|
|
currpos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (currpos == -1) sys_error(NO_ARG);
|
|
|
|
file_size = lseek(fd, 0, SEEK_END);
|
|
|
|
if (file_size == -1) sys_error(NO_ARG);
|
|
|
|
/* Determine array size in bytes (or size of array without the major
|
|
|
|
dimension if that dimension wasn't specified) */
|
|
|
|
array_size = bigarray_element_size[flags & BIGARRAY_KIND_MASK];
|
|
|
|
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 */
|
|
|
|
if (file_size < startpos)
|
|
|
|
failwith("Bigarray.mmap: 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)
|
2000-02-25 05:45:54 -08:00
|
|
|
failwith("Bigarray.mmap: file size doesn't match array dimensions");
|
|
|
|
} 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) {
|
|
|
|
if (lseek(fd, startpos + array_size - 1, SEEK_SET) == -1)
|
|
|
|
sys_error(NO_ARG);
|
2000-02-25 05:45:54 -08:00
|
|
|
c = 0;
|
|
|
|
if (write(fd, &c, 1) != 1) sys_error(NO_ARG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Restore original file position */
|
|
|
|
lseek(fd, currpos, SEEK_SET);
|
2006-06-10 07:15:43 -07:00
|
|
|
/* Determine offset so that the mapping starts at the given file pos */
|
|
|
|
page = getpagesize();
|
|
|
|
delta = (uintnat) (startpos % page);
|
2000-02-25 05:45:54 -08:00
|
|
|
/* Do the mmap */
|
|
|
|
shared = Bool_val(vshared) ? MAP_SHARED : MAP_PRIVATE;
|
2006-06-10 07:15:43 -07:00
|
|
|
addr = mmap(NULL, array_size + delta, PROT_READ | PROT_WRITE,
|
|
|
|
shared, fd, startpos - delta);
|
2000-02-29 08:05:36 -08:00
|
|
|
if (addr == (void *) MAP_FAILED) sys_error(NO_ARG);
|
2006-06-10 07:15:43 -07:00
|
|
|
addr = (void *) ((uintnat) addr + delta);
|
2000-02-25 05:45:54 -08:00
|
|
|
/* Build and return the Caml bigarray */
|
|
|
|
return alloc_bigarray(flags | BIGARRAY_MAPPED_FILE, num_dims, addr, dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2006-01-27 06:33:42 -08:00
|
|
|
value caml_ba_map_file(value vfd, value vkind, value vlayout,
|
2006-06-10 07:15:43 -07:00
|
|
|
value vshared, value vdim, value vpos)
|
2000-02-25 05:45:54 -08:00
|
|
|
{
|
|
|
|
invalid_argument("Bigarray.map_file: not supported");
|
|
|
|
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;
|
|
|
|
munmap((void *)((uintnat)addr - delta), len + delta);
|
2000-02-25 05:45:54 -08:00
|
|
|
#endif
|
|
|
|
}
|