| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* mmap.d - mapping of binary files |
| 6 |
* |
| 7 |
* Copyright (c) 2011 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <ecl/ecl.h> |
| 14 |
#ifdef HAVE_SYS_MMAN_H |
| 15 |
# include <sys/mman.h> |
| 16 |
#endif |
| 17 |
#include <ecl/ecl-inl.h> |
| 18 |
#include <ecl/internal.h> |
| 19 |
|
| 20 |
@(defun ext::mmap (filename |
| 21 |
&key |
| 22 |
(length ECL_NIL) |
| 23 |
(offset ecl_make_fixnum(0)) |
| 24 |
(direction @':input') |
| 25 |
(element_type @'base-char') |
| 26 |
(if_exists @':new-version') |
| 27 |
(if_does_not_exist @':error') |
| 28 |
(external_format @':default')) |
| 29 |
@ |
| 30 |
#ifdef HAVE_SYS_MMAN_H |
| 31 |
{ |
| 32 |
cl_object output, stream; |
| 33 |
int c_prot, c_flags, fd; |
| 34 |
size_t len; |
| 35 |
void *pa; |
| 36 |
if (direction == @':input') |
| 37 |
c_prot = PROT_READ; |
| 38 |
else if (direction == @':output') |
| 39 |
c_prot = PROT_WRITE; |
| 40 |
else if (direction == @':io') |
| 41 |
c_prot = PROT_READ | PROT_WRITE; |
| 42 |
else |
| 43 |
c_prot = PROT_NONE; |
| 44 |
if (Null(filename)) { |
| 45 |
c_flags = MAP_ANON | MAP_PRIVATE; |
| 46 |
fd = -1; |
| 47 |
len = ecl_to_unsigned_integer(length); |
| 48 |
stream = ECL_NIL; |
| 49 |
} else { |
| 50 |
c_flags = MAP_SHARED; |
| 51 |
stream = cl_open(13, filename, |
| 52 |
@':direction', direction, |
| 53 |
@':element-type', element_type, |
| 54 |
@':if-exists', if_exists, |
| 55 |
@':if-does-not-exist', if_does_not_exist, |
| 56 |
@':external-format', external_format, |
| 57 |
@':cstream', ECL_NIL); |
| 58 |
fd = ecl_to_int(si_file_stream_fd(stream)); |
| 59 |
if (Null(length)) |
| 60 |
len = ecl_to_unsigned_integer(ecl_file_length(stream)); |
| 61 |
else |
| 62 |
len = ecl_to_unsigned_integer(length); |
| 63 |
} |
| 64 |
output = si_make_vector(element_type, ecl_make_fixnum(0), ECL_NIL, |
| 65 |
ECL_NIL, ECL_NIL, ECL_NIL); |
| 66 |
pa = mmap(0, len, c_prot, c_flags, fd, |
| 67 |
ecl_integer_to_off_t(offset)); |
| 68 |
if (pa == MAP_FAILED) { |
| 69 |
FElibc_error("EXT::MMAP failed.", 0); |
| 70 |
} else { |
| 71 |
output->base_string.self = pa; |
| 72 |
output->base_string.dim = |
| 73 |
output->base_string.fillp = len; |
| 74 |
} |
| 75 |
@(return CONS(output, stream)); |
| 76 |
} |
| 77 |
#else |
| 78 |
{ |
| 79 |
cl_object output, vector; |
| 80 |
if (Null(filename)) { |
| 81 |
output = si_make_vector(element_type, length, ECL_NIL, |
| 82 |
ECL_NIL, ECL_NIL, ECL_NIL); |
| 83 |
} else { |
| 84 |
cl_object stream = cl_open(13, filename, |
| 85 |
@':direction', direction, |
| 86 |
@':element-type', element_type, |
| 87 |
@':if-exists', if_exists, |
| 88 |
@':if-does-not-exist', if_does_not_exist, |
| 89 |
@':external-format', @':pass-through', |
| 90 |
@':cstream', ECL_T); |
| 91 |
if (Null(length)) |
| 92 |
length = ecl_file_length(stream); |
| 93 |
output = si_make_vector(element_type, length, ECL_NIL, |
| 94 |
ECL_NIL, ECL_NIL, ECL_NIL); |
| 95 |
cl_read_sequence(2, output, stream); |
| 96 |
cl_close(1, stream); |
| 97 |
} |
| 98 |
@(return output); |
| 99 |
} |
| 100 |
#endif |
| 101 |
@) |
| 102 |
|
| 103 |
cl_object |
| 104 |
si_mmap_array(cl_object map) |
| 105 |
{ |
| 106 |
#ifdef HAVE_SYS_MMAN_H |
| 107 |
@(return cl_car(map)); |
| 108 |
#else |
| 109 |
@(return map); |
| 110 |
#endif |
| 111 |
} |
| 112 |
|
| 113 |
cl_object |
| 114 |
si_munmap(cl_object map) |
| 115 |
{ |
| 116 |
#ifdef HAVE_SYS_MMAN_H |
| 117 |
cl_object array = cl_car(map); |
| 118 |
cl_object stream = cl_cdr(map); |
| 119 |
int code = munmap(array->base_string.self, array->base_string.dim); |
| 120 |
if (code < 0) { |
| 121 |
FElibc_error("Error when unmapping file.", 0); |
| 122 |
} |
| 123 |
cl_close(1, stream); |
| 124 |
#endif |
| 125 |
@(return ECL_NIL); |
| 126 |
} |