Remove unused memory profiling code.
[SXSI/xpathcomp.git] / src / OCamlDriver.cpp
index 146a0df..b93286c 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "XMLTree.h"
 #include "XMLTreeBuilder.h"
-#include "Grammar.h"
 #include "Utils.h"
 #include "common_stub.hpp"
 
@@ -32,8 +31,6 @@
 
 #define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder*>(x))
 
-#define GRAMMAR(x) (Obj_val<Grammar*>(x))
-
 
 #define TREENODEVAL(i) ((treeNode) (Int_val(i)))
 #define TAGVAL(i) ((TagType) (Int_val(i)))
@@ -44,9 +41,19 @@ extern "C" {
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <stdio.h>
+#include <bp-darray.h>
 }
 
 
+extern "C" value caml_clz(value i)
+{
+  return Val_long( ((sizeof(unsigned long)*8) - __builtin_clzl(Long_val(i))) - 1);
+}
+
+extern "C" value caml_leading_bit(value i)
+{
+  return Val_long( ( 1 << (sizeof(unsigned long)*8 - __builtin_clzl(Long_val(i)) - 1)));
+}
 
 /** XMLTreeBuilder bindings
  *
@@ -160,7 +167,7 @@ extern "C"  value caml_xml_tree_load(value fd, value name, value load_tc,value s
   XMLTree * tree;
   try {
 
-    tree = XMLTree::Load(Int_val(fd),Bool_val(load_tc),Int_val(sf), String_val(name));
+    tree = XMLTree::Load(Int_val(fd), Bool_val(load_tc), Int_val(sf), String_val(name));
     result = sxsi_alloc_custom<XMLTree*>();
     Obj_val<XMLTree*>(result) = tree;
     CAMLreturn(result);
@@ -864,28 +871,48 @@ extern "C"  value caml_text_collection_lessthan(value tree,value str){
 
 /** Full reporting into a bit vector
  */
+static std::vector<DocID> sort_results(std::vector<DocID> v)
+{
+  std::vector<DocID> res;
+  std::sort(v.begin(), v.end());
+  DocID prev = NULLT;
+  for(auto i = v.begin(); i != v.end(); ++i){
+    while (prev == *i){
+      ++i;
+      if (i == v.end()) return res;
+    };
+    prev = *i;
+    res.push_back(prev);
+  };
+  return res;
+}
 
 #define BV_QUERY(pref, Pref) \
-  extern "C" value caml_text_collection_## pref ##_bv(value tree, value str){ \
-  CAMLparam2(tree, str);                                               \
-  CAMLlocal3(res, res_bv, res_array);                                  \
-  int j;                                                               \
-  uchar * cstr = (uchar *) strdup(String_val(str));                    \
-  std::vector<DocID> results = XMLTREE(tree)->Pref(cstr);              \
-  res_bv = caml_alloc_string((XMLTREE(tree)->Size() / 4) + 2);         \
-  unsigned long slen = caml_string_length(res_bv);                     \
-  memset(&(Byte(res_bv,0)), 0, slen);                                  \
-  res_array = caml_alloc_shr(results.size(), 0);                       \
-  for (unsigned int i = 0; i < results.size(); ++i) {                  \
-    j = XMLTREE(tree)->ParentNode(results[i]);                         \
-    Byte(res_bv, j >> 3) |=   (1 << (j & 7));                          \
-    caml_initialize(&Field(res_array, i), Val_int(j));                 \
-  };                                                                   \
-  free(cstr);                                                          \
-  res = caml_alloc(2, 0);                                              \
-  Store_field(res, 0, res_bv);                                         \
-  Store_field(res, 1, res_array);                                      \
-  CAMLreturn(res);                                                     \
+  extern "C" value caml_text_collection_## pref ##_bv(value tree, value str, value dobvv){ \
+    CAMLparam3(tree, str, dobvv);                                              \
+    CAMLlocal3(res, res_bv, res_array);                                        \
+    int j;                                                             \
+    uchar * cstr = (uchar *) strdup(String_val(str));                  \
+    std::vector<DocID> uresults = XMLTREE(tree)->Pref(cstr);           \
+    std::vector<DocID> results = sort_results(uresults);                \
+    bool dobv = Bool_val(dobvv);                                       \
+    res_bv = caml_alloc_string(dobv ? ((XMLTREE(tree)->Size() / 4) + 2) : 0); \
+    unsigned long slen = caml_string_length(res_bv);                   \
+    if (dobv)                                                          \
+      memset(&(Byte(res_bv,0)), 0, slen);                              \
+    res_array = caml_alloc_shr(results.size(), 0);                     \
+    for (unsigned int i = 0; i < results.size(); ++i) {                        \
+      j = XMLTREE(tree)->ParentNode(results[i]);                       \
+      if (dobv)        {                                                       \
+       Byte(res_bv, j >> 3) |=   (1 << (j & 7));                       \
+      };                                                               \
+      caml_initialize(&Field(res_array, i), Val_int(j));               \
+    };                                                                 \
+    free(cstr);                                                                \
+    res = caml_alloc(2, 0);                                            \
+    Store_field(res, 0, res_bv);                                       \
+    Store_field(res, 1, res_array);                                    \
+    CAMLreturn(res);                                                   \
   }                                                                    \
 
 
@@ -896,162 +923,6 @@ BV_QUERY(contains, Contains)
 BV_QUERY(lessthan, LessThan)
 
 
-
-//////////////////////////////////////////// Grammar stuff
-
-extern "C" value caml_grammar_load(value file, value load_bp)
-{
-  CAMLparam2(file, load_bp);
-  CAMLlocal1(result);
-  Grammar *grammar;
-  int f1 = Int_val(file);
-  int f2 = dup(f1);
-  FILE * fd = fdopen(f2, "r");
-  if (fd == NULL)
-    CAMLRAISEMSG("Error opening grammar file");
-  grammar = Grammar::load(fd, Bool_val(load_bp));
-  fclose(fd);
-  result = sxsi_alloc_custom<Grammar*>();
-  Obj_val<Grammar*>(result) = grammar;
-  CAMLreturn(result);
-}
-
-extern "C" value caml_grammar_get_symbol_at(value grammar, value symbol, value preorder)
-{
-  CAMLparam3(grammar, symbol, preorder);
-  CAMLreturn(Val_long(GRAMMAR(grammar)->getSymbolAt(Long_val(symbol), Int_val(preorder))));
-}
-
-extern "C" value caml_grammar_first_child(value grammar, value rule, value pos)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->firstChild(Long_val(rule), Int_val(pos))));
-}
-
-extern "C" value caml_grammar_next_sibling(value grammar, value rule, value pos)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->nextSibling(Long_val(rule), Int_val(pos))));
-}
-
-extern "C" value caml_grammar_start_first_child(value grammar, value pos)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->startFirstChild(Int_val(pos))));
-}
-
-extern "C" value caml_grammar_start_next_sibling(value grammar, value pos)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->startNextSibling(Int_val(pos))));
-}
-
-extern "C" value caml_grammar_is_nil(value grammar, value rule)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_bool(GRAMMAR(grammar)->isNil(Long_val(rule))));
-}
-
-extern "C" value caml_grammar_get_tag(value grammar, value tag)
-{
-  CAMLparam1(grammar);
-  CAMLlocal1(res);
-  const char * s = (GRAMMAR(grammar)->getTagName(Long_val(tag))).c_str();
-  res = caml_copy_string(s);
-  CAMLreturn(res);
-}
-
-extern "C" value caml_grammar_get_id1(value grammar, value rule)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_long(GRAMMAR(grammar)->getID1(Long_val(rule))));
-}
-
-extern "C" value caml_grammar_get_id2(value grammar, value rule)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_long(GRAMMAR(grammar)->getID2(Long_val(rule))));
-}
-
-extern "C" value caml_grammar_get_param_pos(value grammar, value rule)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->getParamPos(Long_val(rule))));
-}
-
-extern "C" value caml_grammar_translate_tag(value grammar, value tag)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->translateTag(Int_val(tag))));
-}
-
-extern "C" value caml_grammar_register_tag(value grammar, value str)
-{
-  CAMLparam2(grammar, str);
-  char * s = String_val(str);
-  CAMLreturn(Val_int(GRAMMAR(grammar)->getTagID(s)));
-}
-
-extern "C" value caml_grammar_nil_id(value grammar)
-{
-  CAMLparam1(grammar);
-  CAMLreturn(Val_long((GRAMMAR(grammar)->getNiltagid()) * 4 + 1));
-}
-
-extern "C" {
-extern char *caml_young_end;
-extern char *caml_young_start;
-typedef char * addr;
-#define Is_young(val) \
-  ((addr)(val) < (addr)caml_young_end && (addr)(val) > (addr)caml_young_start)
-
-}
-extern "C" value caml_custom_is_young(value a){
-  return Val_bool(Is_young(a));
-}
-
-extern "C" value caml_custom_array_blit(value a1, value ofs1, value a2, value ofs2,
-                     value n)
-{
-  value * src, * dst;
-  intnat count;
-
-  if (Is_young(a2)) {
-    /* Arrays of values, destination is in young generation.
-       Here too we can do a direct copy since this cannot create
-       old-to-young pointers, nor mess up with the incremental major GC.
-       Again, memmove takes care of overlap. */
-    memmove(&Field(a2, Long_val(ofs2)),
-            &Field(a1, Long_val(ofs1)),
-            Long_val(n) * sizeof(value));
-    return Val_unit;
-  }
-  /* Array of values, destination is in old generation.
-     We must use caml_modify.  */
-  count = Long_val(n);
-  if (a1 == a2 && Long_val(ofs1) < Long_val(ofs2)) {
-    /* Copy in descending order */
-    for (dst = &Field(a2, Long_val(ofs2) + count - 1),
-           src = &Field(a1, Long_val(ofs1) + count - 1);
-         count > 0;
-         count--, src--, dst--) {
-      caml_modify(dst, *src);
-    }
-  } else {
-    /* Copy in ascending order */
-    for (dst = &Field(a2, Long_val(ofs2)), src = &Field(a1, Long_val(ofs1));
-         count > 0;
-         count--, src++, dst++) {
-      caml_modify(dst, *src);
-    }
-  }
-  /* Many caml_modify in a row can create a lot of old-to-young refs.
-     Give the minor GC a chance to run if it needs to. */
-  //caml_check_urgent_gc(Val_unit);
-  return Val_unit;
-}
-
-
 ////////////////////// BP
 
 extern "C" value caml_bitmap_create(value size)
@@ -1070,7 +941,6 @@ extern "C" value caml_bitmap_resize(value bitmap, value nsize)
   CAMLparam2(bitmap, nsize);
   size_t bits = Long_val(nsize);
   size_t bytes = (bits / (8 * sizeof(unsigned int)) + 1 ) * sizeof(unsigned int);
-  fprintf(stderr, "Growing to: %lu bytes\n", (bits / (8 * sizeof(unsigned int)) + 1 ) * sizeof(unsigned int));
   unsigned int * buffer = (unsigned int*) realloc((void *) bitmap, bytes);
   if (buffer == NULL)
     CAMLRAISEMSG("BP: cannot reallocate memory");
@@ -1148,7 +1018,6 @@ extern "C" value caml_bp_save(value b, value file)
   int f1 = Int_val(file);
   int f2 = dup(f1);
   FILE * fd = fdopen(f2, "a");
-  fprintf(stderr, "Writing %i %p bytes\n", ((B->n+D-1)/D)*8, B );
   fflush(stderr);
   if (fd == NULL)
     CAMLRAISEMSG("Error saving bp file");
@@ -1157,3 +1026,8 @@ extern "C" value caml_bp_save(value b, value file)
   CAMLreturn(Val_unit);
 }
 
+extern "C" value caml_bp_alloc_stats(value unit)
+{
+  CAMLparam1(unit);
+  CAMLreturn (Val_long(bp_get_alloc_stats()));
+}