From: Richard W.M. Jones Date: Tue, 26 Aug 2008 08:20:41 +0000 (+0000) Subject: This patch adds the framework for including C code in bitstring. X-Git-Url: http://git.annexia.org/?p=ocaml-bitstring.git;a=commitdiff_plain;h=42545798e1ada7e47d7ba56e1c9c2e32bc0e7129 This patch adds the framework for including C code in bitstring. --- diff --git a/MANIFEST b/MANIFEST index 688dd07..06cf900 100644 --- a/MANIFEST +++ b/MANIFEST @@ -3,6 +3,7 @@ benchmarks/parse_ext3_superblock.ml bitmatch.ml bitstring.ml bitstring.mli +bitstring_c.c bitstring_config.ml.in bitstring_objinfo.ml bitstring_persistent.ml diff --git a/Makefile.in b/Makefile.in index 808146d..f3fed47 100644 --- a/Makefile.in +++ b/Makefile.in @@ -27,6 +27,8 @@ INSTALL = @INSTALL@ TIME = @TIME@ GPROF = @GPROF@ +OCAMLLIB = @OCAMLLIB@ + pkg_cil = @pkg_cil@ pkg_extlib = @pkg_extlib@ @@ -37,6 +39,14 @@ OCAMLOPTPACKAGES = OCAMLDOCFLAGS = -html -sort +CC = @CC@ +CFLAGS = @CFLAGS@ -Wall -Werror -fPIC -I$(OCAMLLIB) + +LIBRARY_PATH = @abs_top_builddir@ +LD_LIBRARY_PATH = @abs_top_builddir@ +export LIBRARY_PATH +export LD_LIBRARY_PATH + SUBDIRS = ifneq ($(pkg_cil),no) ifneq ($(pkg_extlib),no) @@ -56,9 +66,9 @@ all: bitstring.cma bitstring_persistent.cma \ bitstring-objinfo @for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done -bitstring.cma: bitstring_types.cmo bitstring_config.cmo bitstring.cmo \ - bitmatch.cmo - $(OCAMLFIND) ocamlc -a -o $@ $^ +bitstring.cma: bitstring_types.cmo bitstring_config.cmo \ + bitstring_c.o bitstring.cmo bitmatch.cmo + $(OCAMLMKLIB) -o bitstring $^ bitstring_persistent.cma: bitstring_persistent.cmo $(OCAMLFIND) ocamlc -a -o $@ $^ @@ -67,9 +77,9 @@ bitstring_persistent.cmo: bitstring_persistent.ml $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) \ -I +camlp4 -pp camlp4of -c $< -bitstring.cmxa: bitstring_types.cmx bitstring_config.cmx bitstring.cmx \ - bitmatch.cmx - $(OCAMLFIND) ocamlopt -a -o $@ $^ +bitstring.cmxa: bitstring_types.cmx bitstring_config.cmx \ + bitstring_c.o bitstring.cmx bitmatch.cmx + $(OCAMLMKLIB) -o bitstring $^ bitstring_persistent.cmxa: bitstring_persistent.cmx $(OCAMLFIND) ocamlopt -a -o $@ $^ @@ -207,7 +217,7 @@ endif # Install. install: - ocamlfind install bitstring META *.mli *.cmx *.cma *.cmxa *.a \ + ocamlfind install bitstring META *.mli *.cmx *.cma *.cmxa *.a *.so \ bitstring.cmi \ bitstring_persistent.cmi \ pa_bitstring.cmo diff --git a/bitstring.ml b/bitstring.ml index fda9ad6..610c2b5 100644 --- a/bitstring.ml +++ b/bitstring.ml @@ -157,16 +157,8 @@ module I = struct let minus_one = -1 let ff = 0xff - (* Create a mask so many bits wide. *) - let mask bits = - if bits < 30 then - pred (one << bits) - else if bits = 30 then - max_int - else if bits = 31 then - minus_one - else - invalid_arg "Bitstring.I.mask" + (* Create a mask 0-31 bits wide. *) + external mask : int -> int = "ocaml_bitstring_I_mask" "noalloc" (* Byte swap an int of a given size. *) let byteswap v bits = diff --git a/bitstring_c.c b/bitstring_c.c new file mode 100644 index 0000000..6faba8b --- /dev/null +++ b/bitstring_c.c @@ -0,0 +1,42 @@ +/* Bitstring library. + * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version, + * with the OCaml linking exception described in COPYING.LIB. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * $Id: bitstring.ml 146 2008-08-20 16:58:33Z richard.wm.jones $ + */ + +/* This file contains hand-coded, optimized C implementations of + * certain very frequently used functions. + */ + +#include +#include + +#include +#include + +/* Return a mask of 0-31 bits wide. */ +CAMLprim value +ocaml_bitstring_I_mask (value bitsv) +{ + int bits = Int_val (bitsv); + + if (bits <= 31) + return Val_int ((1 << bits) - 1); + else + caml_invalid_argument ("Bitstring.I.mask"); +}