Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromocaml/ocaml

Commit5ed7200

Browse files
Deprecate Bigarray.*.map_file and add Unix.map_file (ocaml#997)
To break the circular dependency between Bigarray and Unix, a CamlinternalBigarray module was added to the stdlib. This module defines all the types used by the compiler to produce optimized code for bigarrays.Thanks to David Allsopp for fixing Windows tests.
1 parent5abc3a8 commit5ed7200

File tree

22 files changed

+391
-237
lines changed

22 files changed

+391
-237
lines changed

‎Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Next version (4.05.0):
116116
it previously raised an EPIPE error, it now returns 0 like other OSes
117117
(Jonathan Protzenko)
118118

119+
- GPR#997: Deprecate Bigarray.*.map_file and add Unix.map_file as a
120+
first step towards moving Bigarray to the stdlib
121+
(Jeremie Dimino)
122+
119123
### Bytecode debugger (ocamldebug):
120124

121125
- GPR#977: Catch Out_of_range in "list" command

‎bytecomp/typeopt.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let array_pattern_kind pat = array_type_kind pat.pat_env pat.pat_type
118118
letbigarray_decode_typeenvtytbldfl=
119119
match scrape env tywith
120120
|Tconstr(Pdot(Pident mod_id, type_name, _),[], _)
121-
whenIdent.name mod_id="Bigarray" ->
121+
whenIdent.name mod_id="CamlinternalBigarray" ->
122122
begintryList.assoc type_name tblwithNot_found -> dflend
123123
|_ ->
124124
dfl

‎otherlibs/bigarray/bigarray.ml

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,7 @@ external init : unit -> unit = "caml_ba_init"
1919

2020
let _= init()
2121

22-
typefloat32_elt=Float32_elt
23-
typefloat64_elt=Float64_elt
24-
typeint8_signed_elt=Int8_signed_elt
25-
typeint8_unsigned_elt=Int8_unsigned_elt
26-
typeint16_signed_elt=Int16_signed_elt
27-
typeint16_unsigned_elt=Int16_unsigned_elt
28-
typeint32_elt=Int32_elt
29-
typeint64_elt=Int64_elt
30-
typeint_elt=Int_elt
31-
typenativeint_elt=Nativeint_elt
32-
typecomplex32_elt=Complex32_elt
33-
typecomplex64_elt=Complex64_elt
34-
35-
type('a, 'b) kind=
36-
Float32 : (float,float32_elt)kind
37-
|Float64 : (float,float64_elt)kind
38-
|Int8_signed : (int,int8_signed_elt)kind
39-
|Int8_unsigned : (int,int8_unsigned_elt)kind
40-
|Int16_signed : (int,int16_signed_elt)kind
41-
|Int16_unsigned : (int,int16_unsigned_elt)kind
42-
|Int32 : (int32,int32_elt)kind
43-
|Int64 : (int64,int64_elt)kind
44-
|Int : (int,int_elt)kind
45-
|Nativeint : (nativeint,nativeint_elt)kind
46-
|Complex32 : (Complex.t,complex32_elt)kind
47-
|Complex64 : (Complex.t,complex64_elt)kind
48-
|Char : (char,int8_unsigned_elt)kind
22+
includeCamlinternalBigarray
4923

5024
(* Keep those constants in sync with the caml_ba_kind enumeration
5125
in bigarray.h*)
@@ -79,21 +53,14 @@ let kind_size_in_bytes : type a b. (a, b) kind -> int = function
7953
|Complex64 ->16
8054
|Char ->1
8155

82-
typec_layout=C_layout_typ
83-
typefortran_layout=Fortran_layout_typ
84-
85-
type'a layout=
86-
C_layout:c_layoutlayout
87-
|Fortran_layout:fortran_layoutlayout
88-
8956
(* Keep those constants in sync with the caml_ba_layout enumeration
9057
in bigarray.h*)
9158

9259
let c_layout=C_layout
9360
let fortran_layout=Fortran_layout
9461

9562
moduleGenarray=struct
96-
type('a, 'b, 'c) t
63+
type('a, 'b, 'c) t= ('a,'b,'c)genarray
9764
externalcreate: ('a,'b)kind ->'clayout ->intarray -> ('a,'b,'c)t
9865
="caml_ba_create"
9966
externalget: ('a,'b,'c)t ->intarray ->'a
@@ -133,8 +100,12 @@ module Genarray = struct
133100
externalmap_internal:Unix.file_descr -> ('a,'b)kind ->'clayout ->
134101
bool ->intarray ->int64 -> ('a,'b,'c)t
135102
="caml_ba_map_file_bytecode""caml_ba_map_file"
136-
letmap_filefd?(pos =0L)kindlayoutshareddims=
137-
map_internal fd kind layout shared dims pos
103+
let()=Unix.map_file_impl:= {Unix.map_file_impl= map_internal }
104+
letmap_filefd?poskindlayoutshareddims=
105+
try
106+
Unix.map_file fd ?pos kind layout shared dims
107+
withUnix.Unix_error (error,_,_) ->
108+
raise (Sys_error (Unix.error_message error))
138109
end
139110

140111
moduleArray0=struct

‎otherlibs/bigarray/bigarray.mli

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,23 @@
5959
of abstract types for technical injectivity reasons).
6060
*)
6161

62-
typefloat32_elt=Float32_elt
63-
typefloat64_elt=Float64_elt
64-
typeint8_signed_elt=Int8_signed_elt
65-
typeint8_unsigned_elt=Int8_unsigned_elt
66-
typeint16_signed_elt=Int16_signed_elt
67-
typeint16_unsigned_elt=Int16_unsigned_elt
68-
typeint32_elt=Int32_elt
69-
typeint64_elt=Int64_elt
70-
typeint_elt=Int_elt
71-
typenativeint_elt=Nativeint_elt
72-
typecomplex32_elt=Complex32_elt
73-
typecomplex64_elt=Complex64_elt
74-
75-
type('a, 'b) kind=
62+
typefloat32_elt=CamlinternalBigarray.float32_elt=Float32_elt
63+
typefloat64_elt=CamlinternalBigarray.float64_elt=Float64_elt
64+
typeint8_signed_elt=CamlinternalBigarray.int8_signed_elt=Int8_signed_elt
65+
typeint8_unsigned_elt=CamlinternalBigarray.int8_unsigned_elt=
66+
Int8_unsigned_elt
67+
typeint16_signed_elt=CamlinternalBigarray.int16_signed_elt=
68+
Int16_signed_elt
69+
typeint16_unsigned_elt=CamlinternalBigarray.int16_unsigned_elt=
70+
Int16_unsigned_elt
71+
typeint32_elt=CamlinternalBigarray.int32_elt=Int32_elt
72+
typeint64_elt=CamlinternalBigarray.int64_elt=Int64_elt
73+
typeint_elt=CamlinternalBigarray.int_elt=Int_elt
74+
typenativeint_elt=CamlinternalBigarray.nativeint_elt=Nativeint_elt
75+
typecomplex32_elt=CamlinternalBigarray.complex32_elt=Complex32_elt
76+
typecomplex64_elt=CamlinternalBigarray.complex64_elt=Complex64_elt
77+
78+
type('a, 'b) kind= ('a,'b)CamlinternalBigarray.kind=
7679
Float32 : (float,float32_elt)kind
7780
|Float64 : (float,float64_elt)kind
7881
|Int8_signed : (int,int8_signed_elt)kind
@@ -178,10 +181,11 @@ val kind_size_in_bytes : ('a, 'b) kind -> int
178181

179182
(** {6 Array layouts}*)
180183

181-
typec_layout=C_layout_typ(**)
184+
typec_layout=CamlinternalBigarray.c_layout=C_layout_typ(**)
182185
(** See {!Bigarray.fortran_layout}.*)
183186

184-
typefortran_layout=Fortran_layout_typ(**)
187+
typefortran_layout=CamlinternalBigarray.fortran_layout=
188+
Fortran_layout_typ(**)
185189
(** To facilitate interoperability with existing C and Fortran code,
186190
this library supports two different memory layouts for big arrays,
187191
one compatible with the C conventions,
@@ -212,7 +216,7 @@ type fortran_layout = Fortran_layout_typ (**)
212216
re-exported as values below for backward-compatibility reasons.
213217
*)
214218

215-
type'a layout=
219+
type'a layout='aCamlinternalBigarray.layout=
216220
C_layout:c_layoutlayout
217221
|Fortran_layout:fortran_layoutlayout
218222

@@ -224,7 +228,7 @@ val fortran_layout : fortran_layout layout
224228

225229
moduleGenarray :
226230
sig
227-
type('a, 'b, 'c) t
231+
type('a, 'b, 'c) t= ('a,'b,'c)CamlinternalBigarray.genarray
228232
(** The type [Genarray.t] is the type of big arrays with variable
229233
numbers of dimensions. Any number of dimensions between 0 and 16
230234
is supported.
@@ -437,53 +441,10 @@ module Genarray :
437441
valmap_file:
438442
Unix.file_descr ->?pos:int64 -> ('a,'b)kind ->'clayout ->
439443
bool ->intarray -> ('a,'b,'c)t
440-
(** Memory mapping of a file as a big array.
441-
[Genarray.map_file fd kind layout shared dims]
442-
returns a big array of kind [kind], layout [layout],
443-
and dimensions as specified in [dims]. The data contained in
444-
this big array are the contents of the file referred to by
445-
the file descriptor [fd] (as opened previously with
446-
[Unix.openfile], for example). The optional [pos] parameter
447-
is the byte offset in the file of the data being mapped;
448-
it defaults to 0 (map from the beginning of the file).
449-
450-
If [shared] is [true], all modifications performed on the array
451-
are reflected in the file. This requires that [fd] be opened
452-
with write permissions. If [shared] is [false], modifications
453-
performed on the array are done in memory only, using
454-
copy-on-write of the modified pages; the underlying file is not
455-
affected.
456-
457-
[Genarray.map_file] is much more efficient than reading
458-
the whole file in a big array, modifying that big array,
459-
and writing it afterwards.
460-
461-
To adjust automatically the dimensions of the big array to
462-
the actual size of the file, the major dimension (that is,
463-
the first dimension for an array with C layout, and the last
464-
dimension for an array with Fortran layout) can be given as
465-
[-1]. [Genarray.map_file] then determines the major dimension
466-
from the size of the file. The file must contain an integral
467-
number of sub-arrays as determined by the non-major dimensions,
468-
otherwise [Failure] is raised.
469-
470-
If all dimensions of the big array are given, the file size is
471-
matched against the size of the big array. If the file is larger
472-
than the big array, only the initial portion of the file is
473-
mapped to the big array. If the file is smaller than the big
474-
array, the file is automatically grown to the size of the big array.
475-
This requires write permissions on [fd].
476-
477-
Array accesses are bounds-checked, but the bounds are determined by
478-
the initial call to [map_file]. Therefore, you should make sure no
479-
other process modifies the mapped file while you're accessing it,
480-
or a SIGBUS signal may be raised. This happens, for instance, if the
481-
file is shrunk.
482-
483-
This function raises [Sys_error] in the case of any errors from the
484-
underlying system calls. [Invalid_argument] or [Failure] may be
485-
raised in cases where argument validation fails.*)
486-
444+
[@@ocaml.deprecated"\
445+
Use Unix.map_file instead.\n\
446+
Note that Bigarray.Genarray.map_file raises Sys_error while\n\
447+
Unix.map_file raises Unix_error."]
487448
end
488449

489450
(** {6 Zero-dimensional arrays}*)
@@ -610,8 +571,10 @@ module Array1 : sig
610571

611572
valmap_file:Unix.file_descr ->?pos:int64 -> ('a,'b)kind ->'clayout ->
612573
bool ->int -> ('a,'b,'c)t
613-
(** Memory mapping of a file as a one-dimensional big array.
614-
See {!Bigarray.Genarray.map_file} for more details.*)
574+
[@@ocaml.deprecated"\
575+
Use [array1_of_genarray (Unix.map_file ...)] instead.\n\
576+
Note that Bigarray.Array1.map_file raises Sys_error while\n\
577+
Unix.map_file raises Unix_error."]
615578

616579
externalunsafe_get: ('a,'b,'c)t ->int ->'a="%caml_ba_unsafe_ref_1"
617580
(** Like {!Bigarray.Array1.get}, but bounds checking is not always performed.
@@ -721,8 +684,10 @@ module Array2 :
721684

722685
valmap_file:Unix.file_descr ->?pos:int64 -> ('a,'b)kind ->'clayout ->
723686
bool ->int ->int -> ('a,'b,'c)t
724-
(** Memory mapping of a file as a two-dimensional big array.
725-
See {!Bigarray.Genarray.map_file} for more details.*)
687+
[@@ocaml.deprecated"\
688+
Use [array2_of_genarray (Unix.map_file ...)] instead.\n\
689+
Note that Bigarray.Array2.map_file raises Sys_error while\n\
690+
Unix.map_file raises Unix_error."]
726691

727692
externalunsafe_get: ('a,'b,'c)t ->int ->int ->'a
728693
="%caml_ba_unsafe_ref_2"
@@ -855,8 +820,10 @@ module Array3 :
855820

856821
valmap_file:Unix.file_descr ->?pos:int64 -> ('a,'b)kind ->'clayout ->
857822
bool ->int ->int ->int -> ('a,'b,'c)t
858-
(** Memory mapping of a file as a three-dimensional big array.
859-
See {!Bigarray.Genarray.map_file} for more details.*)
823+
[@@ocaml.deprecated"\
824+
Use [array3_of_genarray (Unix.map_file ...)] instead.\n\
825+
Note that Bigarray.Array3.map_file raises Sys_error while\n\
826+
Unix.map_file raises Unix_error."]
860827

861828
externalunsafe_get: ('a,'b,'c)t ->int ->int ->int ->'a
862829
="%caml_ba_unsafe_ref_3"

‎otherlibs/bigarray/mmap_unix.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include"caml/mlvalues.h"
2929
#include"caml/sys.h"
3030
#include"caml/signals.h"
31+
#include"unixsupport.h"
3132

3233
externintcaml_ba_element_size[];/* from bigarray_stubs.c */
3334

@@ -125,7 +126,7 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
125126
caml_enter_blocking_section();
126127
if (fstat(fd,&st)==-1) {
127128
caml_leave_blocking_section();
128-
caml_sys_error(NO_ARG);
129+
uerror("fstat",Nothing);
129130
}
130131
file_size=st.st_size;
131132
/* Determine array size in bytes (or size of array without the major
@@ -152,7 +153,7 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
152153
if (file_size<startpos+array_size) {
153154
if (caml_grow_file(fd,startpos+array_size)==-1) {/* PR#5543 */
154155
caml_leave_blocking_section();
155-
caml_sys_error(NO_ARG);
156+
uerror("caml_grow_file",Nothing);
156157
}
157158
}
158159
}
@@ -167,7 +168,7 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
167168
else
168169
addr=NULL;/* PR#5463 - mmap fails on empty region */
169170
caml_leave_blocking_section();
170-
if (addr== (void*)MAP_FAILED)caml_sys_error(NO_ARG);
171+
if (addr== (void*)MAP_FAILED)uerror("mmap",Nothing);
171172
addr= (void*) ((uintnat)addr+delta);
172173
/* Build and return the OCaml bigarray */
173174
returncaml_ba_alloc(flags |CAML_BA_MAPPED_FILE,num_dims,addr,dim);

‎otherlibs/bigarray/mmap_win32.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
externintcaml_ba_element_size[];/* from bigarray_stubs.c */
2828

29-
staticvoidcaml_ba_sys_error(void);
30-
3129
#ifndefINVALID_SET_FILE_POINTER
3230
#defineINVALID_SET_FILE_POINTER (-1)
3331
#endif
@@ -74,9 +72,9 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
7472
}
7573
/* Determine file size */
7674
currpos=caml_ba_set_file_pointer(fd,0,FILE_CURRENT);
77-
if (currpos==-1)caml_ba_sys_error();
75+
if (currpos==-1)uerror("SetFilePointer",Nothing);
7876
file_size=caml_ba_set_file_pointer(fd,0,FILE_END);
79-
if (file_size==-1)caml_ba_sys_error();
77+
if (file_size==-1)uerror("SetFilePointer",Nothing);
8078
/* Determine array size in bytes (or size of array without the major
8179
dimension if that dimension wasn't specified) */
8280
array_size=caml_ba_element_size[flags&CAML_BA_KIND_MASK];
@@ -105,15 +103,15 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
105103
}
106104
li.QuadPart=startpos+array_size;
107105
fmap=CreateFileMapping(fd,NULL,perm,li.HighPart,li.LowPart,NULL);
108-
if (fmap==NULL)caml_ba_sys_error();
106+
if (fmap==NULL)uerror("CreateFileMapping",Nothing);
109107
/* Determine offset so that the mapping starts at the given file pos */
110108
GetSystemInfo(&sysinfo);
111109
delta= (uintnat) (startpos %sysinfo.dwAllocationGranularity);
112110
/* Map the mapping in memory */
113111
li.QuadPart=startpos-delta;
114112
addr=
115113
MapViewOfFile(fmap,mode,li.HighPart,li.LowPart,array_size+delta);
116-
if (addr==NULL)caml_ba_sys_error();
114+
if (addr==NULL)uerror("MapViewOfFile",Nothing);;
117115
addr= (void*) ((uintnat)addr+delta);
118116
/* Close the file mapping */
119117
CloseHandle(fmap);
@@ -136,20 +134,3 @@ void caml_ba_unmap_file(void * addr, uintnat len)
136134
delta= (uintnat)addr %sysinfo.dwAllocationGranularity;
137135
UnmapViewOfFile((void*)((uintnat)addr-delta));
138136
}
139-
140-
staticvoidcaml_ba_sys_error(void)
141-
{
142-
charbuffer[512];
143-
DWORDerrnum;
144-
145-
errnum=GetLastError();
146-
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
147-
NULL,
148-
errnum,
149-
0,
150-
buffer,
151-
sizeof(buffer),
152-
NULL))
153-
sprintf(buffer,"Unknown error %ld\n",errnum);
154-
caml_raise_sys_error(caml_copy_string(buffer));
155-
}

‎otherlibs/threads/unix.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ module LargeFile =
317317
externalfstat :file_descr ->stats="unix_fstat_64"
318318
end
319319

320+
typemap_file_impl=
321+
{ map_file_impl
322+
: 'a 'b 'c. file_descr
323+
-> ('a, 'b)CamlinternalBigarray.kind
324+
-> 'cCamlinternalBigarray.layout
325+
->bool
326+
->intarray
327+
->int64
328+
-> ('a, 'b, 'c)CamlinternalBigarray.genarray
329+
}
330+
let map_file_impl=
331+
ref { map_file_impl=fun______ -> failwith"Bigarray not initialized!" }
332+
letmap_filefd?(pos=0L)kindlayoutshareddims=
333+
!map_file_impl.map_file_impl fd kind layout shared dims pos
334+
320335
typeaccess_permission=
321336
R_OK
322337
|W_OK

‎otherlibs/unix/unix.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ module LargeFile =
341341
externalfstat :file_descr ->stats="unix_fstat_64"
342342
end
343343

344+
typemap_file_impl=
345+
{ map_file_impl
346+
: 'a 'b 'c. file_descr
347+
-> ('a, 'b)CamlinternalBigarray.kind
348+
-> 'cCamlinternalBigarray.layout
349+
->bool
350+
->intarray
351+
->int64
352+
-> ('a, 'b, 'c)CamlinternalBigarray.genarray
353+
}
354+
let map_file_impl=
355+
ref { map_file_impl=fun______ -> failwith"Bigarray not initialized!" }
356+
letmap_filefd?(pos=0L)kindlayoutshareddims=
357+
!map_file_impl.map_file_impl fd kind layout shared dims pos
358+
344359
typeaccess_permission=
345360
R_OK
346361
|W_OK

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp