summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhhugo <hugo.heuzard@gmail.com>2023-03-07 21:26:28 +0900
committerGitHub <noreply@github.com>2023-03-07 13:26:28 +0100
commitbcffd84a9b0e38a3ca02a1ffd2de54541f3bcb9f (patch)
treed60bf45367559ee22f3847b0ac82803db6ca5df6
parentdcf9181b56f1b08989b878110114dbe184576495 (diff)
downloadocaml-bcffd84a9b0e38a3ca02a1ffd2de54541f3bcb9f.tar.gz
Small refactoring for null terminated values (#12086)
-rw-r--r--.depend2
-rw-r--r--bytecomp/bytelink.ml9
-rw-r--r--bytecomp/dll.ml7
-rw-r--r--bytecomp/dll.mli2
-rw-r--r--bytecomp/symtable.ml24
-rw-r--r--tools/dumpobj.ml11
-rw-r--r--tools/objinfo.ml11
-rw-r--r--utils/misc.ml11
-rw-r--r--utils/misc.mli8
9 files changed, 39 insertions, 46 deletions
diff --git a/.depend b/.depend
index 2416343f7f..5439e23dae 100644
--- a/.depend
+++ b/.depend
@@ -6920,6 +6920,7 @@ tools/dumpobj.cmo : \
bytecomp/symtable.cmi \
tools/opnames.cmi \
bytecomp/opcodes.cmi \
+ utils/misc.cmi \
parsing/location.cmi \
bytecomp/instruct.cmi \
typing/ident.cmi \
@@ -6931,6 +6932,7 @@ tools/dumpobj.cmx : \
bytecomp/symtable.cmx \
tools/opnames.cmx \
bytecomp/opcodes.cmx \
+ utils/misc.cmx \
parsing/location.cmx \
bytecomp/instruct.cmx \
typing/ident.cmx \
diff --git a/bytecomp/bytelink.ml b/bytecomp/bytelink.ml
index 79976dda15..afc1a47efb 100644
--- a/bytecomp/bytelink.ml
+++ b/bytecomp/bytelink.ml
@@ -297,11 +297,6 @@ let output_debug_info oc =
!debug_info;
debug_info := []
-(* Output a list of strings with 0-termination *)
-
-let output_stringlist oc l =
- List.iter (fun s -> output_string oc s; output_byte oc 0) l
-
(* Transform a file name into an absolute file name *)
let make_absolute file =
@@ -385,10 +380,10 @@ let link_bytecode ?final_name tolink exec_name standalone =
(* DLL stuff *)
if standalone then begin
(* The extra search path for DLLs *)
- output_stringlist outchan !Clflags.dllpaths;
+ output_string outchan (concat_null_terminated !Clflags.dllpaths);
Bytesections.record outchan "DLPT";
(* The names of the DLLs *)
- output_stringlist outchan sharedobjs;
+ output_string outchan (concat_null_terminated sharedobjs);
Bytesections.record outchan "DLLS"
end;
(* The names of all primitives *)
diff --git a/bytecomp/dll.ml b/bytecomp/dll.ml
index 1675a6ca21..21023d3799 100644
--- a/bytecomp/dll.ml
+++ b/bytecomp/dll.ml
@@ -164,9 +164,6 @@ let ld_library_path_contents () =
| s ->
Misc.split_path_contents s
-let split_dll_path path =
- Misc.split_path_contents ~sep:'\000' path
-
(* Initialization for separate compilation *)
let init_compile nostdlib =
@@ -176,10 +173,10 @@ let init_compile nostdlib =
(* Initialization for linking in core (dynlink or toplevel) *)
-let init_toplevel dllpath =
+let init_toplevel dllpaths =
search_path :=
ld_library_path_contents() @
- split_dll_path dllpath @
+ dllpaths @
ld_conf_contents();
opened_dlls :=
List.map (fun dll -> "", Execution dll)
diff --git a/bytecomp/dll.mli b/bytecomp/dll.mli
index 5d80e1d4be..f87554ff8f 100644
--- a/bytecomp/dll.mli
+++ b/bytecomp/dll.mli
@@ -66,6 +66,6 @@ val init_compile: bool -> unit
the running program (CAML_LD_LIBRARY_PATH + directories in executable +
contents of ld.conf file). Take note of the DLLs that were opened
when starting the running program. *)
-val init_toplevel: string -> unit
+val init_toplevel: string list -> unit
val reset: unit -> unit
diff --git a/bytecomp/symtable.ml b/bytecomp/symtable.ml
index b977095237..f196ad6ed6 100644
--- a/bytecomp/symtable.ml
+++ b/bytecomp/symtable.ml
@@ -117,12 +117,9 @@ let all_primitives () =
prim
let data_primitive_names () =
- let prim = all_primitives() in
- let b = Buffer.create 512 in
- for i = 0 to Array.length prim - 1 do
- Buffer.add_string b prim.(i); Buffer.add_char b '\000'
- done;
- Buffer.contents b
+ all_primitives()
+ |> Array.to_list
+ |> concat_null_terminated
let output_primitive_names outchan =
output_string outchan (data_primitive_names())
@@ -307,17 +304,14 @@ let init_toplevel () =
(* Locations of globals *)
global_table := (Obj.magic (sect.read_struct "SYMB") : GlobalMap.t);
(* Primitives *)
- let prims = sect.read_string "PRIM" in
+ let prims = Misc.split_null_terminated (sect.read_string "PRIM") in
c_prim_table := PrimMap.empty;
- let pos = ref 0 in
- while !pos < String.length prims do
- let i = String.index_from prims !pos '\000' in
- set_prim_table (String.sub prims !pos (i - !pos));
- pos := i + 1
- done;
+ List.iter set_prim_table prims;
(* DLL initialization *)
- let dllpath = try sect.read_string "DLPT" with Not_found -> "" in
- Dll.init_toplevel dllpath;
+ let dllpaths =
+ try Misc.split_null_terminated (sect.read_string "DLPT")
+ with Not_found -> [] in
+ Dll.init_toplevel dllpaths;
(* Recover CRC infos for interfaces *)
let crcintfs =
try
diff --git a/tools/dumpobj.ml b/tools/dumpobj.ml
index f914b1ea60..004e1af494 100644
--- a/tools/dumpobj.ml
+++ b/tools/dumpobj.ml
@@ -477,18 +477,13 @@ let dump_obj ic =
seek_in ic cu.cu_pos;
print_code ic cu.cu_codesize
-(* Read the primitive table from an executable *)
-
-let read_primitive_table ic len =
- let p = really_input_string ic len in
- String.split_on_char '\000' p |> List.filter ((<>) "") |> Array.of_list
-
(* Print an executable file *)
let dump_exe ic =
Bytesections.read_toc ic;
- let prim_size = Bytesections.seek_section ic "PRIM" in
- primitives := read_primitive_table ic prim_size;
+ (* Read the primitive table from an executable *)
+ let prims = Bytesections.read_section_string ic "PRIM" in
+ primitives := Array.of_list (Misc.split_null_terminated prims);
ignore(Bytesections.seek_section ic "DATA");
let init_data = (input_value ic : Obj.t array) in
globals := Array.make (Array.length init_data) Empty;
diff --git a/tools/objinfo.ml b/tools/objinfo.ml
index 1b723d2018..f4d4188f4b 100644
--- a/tools/objinfo.ml
+++ b/tools/objinfo.ml
@@ -33,17 +33,8 @@ let shape = ref false
module Magic_number = Misc.Magic_number
let input_stringlist ic len =
- let get_string_list sect len =
- let rec fold s e acc =
- if e != len then
- if sect.[e] = '\000' then
- fold (e+1) (e+1) (String.sub sect s (e-s) :: acc)
- else fold s (e+1) acc
- else acc
- in fold 0 0 []
- in
let sect = really_input_string ic len in
- get_string_list sect len
+ split_null_terminated sect
let dummy_crc = String.make 32 '-'
let null_crc = String.make 32 '0'
diff --git a/utils/misc.ml b/utils/misc.ml
index 18fe9bfb1d..557bc63d60 100644
--- a/utils/misc.ml
+++ b/utils/misc.ml
@@ -423,6 +423,17 @@ let find_first_mono =
(* String operations *)
+let split_null_terminated s =
+ let[@tail_mod_cons] rec discard_last_sep = function
+ | [] | [""] -> []
+ | x :: xs -> x :: discard_last_sep xs
+ in
+ discard_last_sep (String.split_on_char '\000' s)
+
+let concat_null_terminated = function
+ | [] -> ""
+ | l -> String.concat "\000" (l @ [""])
+
let chop_extensions file =
let dirname = Filename.dirname file and basename = Filename.basename file in
try
diff --git a/utils/misc.mli b/utils/misc.mli
index 52e2bc5db6..e8c716f8e4 100644
--- a/utils/misc.mli
+++ b/utils/misc.mli
@@ -257,6 +257,14 @@ val protect_writing_to_file
channel. If the function raises an exception then [filename]
will be removed. *)
+val concat_null_terminated : string list -> string
+(** [concat_null_terminated [x1;x2; ... xn]] is
+ [x1 ^ "\000" ^ x2 ^ "\000" ^ ... ^ xn ^ "\000"] *)
+
+val split_null_terminated : string -> string list
+(** [split_null_terminated s] is similar
+ [String.split_on_char '\000'] but ignores the trailing separator, if any *)
+
val chop_extensions: string -> string
(** Return the given file name without its extensions. The extensions
is the longest suffix starting with a period and not including