This patch adds the framework for including C code in bitstring.
authorRichard W.M. Jones <rich@annexia.org>
Tue, 26 Aug 2008 08:20:41 +0000 (08:20 +0000)
committerRichard W.M. Jones <rich@annexia.org>
Tue, 26 Aug 2008 08:20:41 +0000 (08:20 +0000)
MANIFEST
Makefile.in
bitstring.ml
bitstring_c.c [new file with mode: 0644]

index 688dd07..06cf900 100644 (file)
--- 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
index 808146d..f3fed47 100644 (file)
@@ -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
index fda9ad6..610c2b5 100644 (file)
@@ -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 (file)
index 0000000..6faba8b
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+
+#include <caml/mlvalues.h>
+#include <caml/fail.h>
+
+/* 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");
+}