Finish adapting to new libxml-tree API
[SXSI/xpathcomp.git] / src / bp_stub.cpp
1 #include <cstdlib>
2 extern "C" {
3 #include <unistd.h>
4 }
5 #include <bp/bp.h>
6 #include "common_stub.hpp"
7
8 extern "C" value caml_bitmap_create(value size)
9 {
10   CAMLparam1(size);
11   size_t bits = Long_val(size);
12   size_t words = bits / (8*sizeof(unsigned int));
13   unsigned int *buffer = (unsigned int*) calloc(words+1, sizeof(unsigned int));
14   if (buffer == NULL)
15     sxsi_raise_msg("BP: cannot allocate memory");
16   CAMLreturn( (value) buffer);
17 }
18
19 extern "C" value caml_bitmap_resize(value bitmap, value nsize)
20 {
21   CAMLparam2(bitmap, nsize);
22   size_t bits = Long_val(nsize);
23   size_t bytes = (bits / (8 * sizeof(unsigned int)) + 1 ) * sizeof(unsigned int);
24   unsigned int * buffer = (unsigned int*) realloc((void *) bitmap, bytes);
25   if (buffer == NULL)
26     sxsi_raise_msg("BP: cannot reallocate memory");
27   CAMLreturn((value) buffer);
28 }
29
30 extern "C" value caml_bitmap_setbit(value bitmap, value i, value b)
31 {
32   CAMLparam3(bitmap, i, b);
33   unsigned int j = Int_val(i);
34   unsigned int x = Bool_val(b);
35   bp_setbit ((unsigned int*) bitmap, j, x);
36   CAMLreturn(Val_unit);
37 }
38
39 extern "C" void caml_bp_delete(value b)
40 {
41   CAMLparam1(b);
42   bp * B = Obj_val<bp*>(b);
43   bp_delete(B);
44   CAMLreturn0;
45 }
46
47 extern "C" value caml_bp_construct(value bitmap, value npar)
48 {
49   CAMLparam2(bitmap, npar);
50   CAMLlocal1(res);
51   bp * b = bp_construct(Int_val(npar), (unsigned int *) bitmap, OPT_DEGREE);
52   res = sxsi_alloc_custom<bp*>(caml_bp_delete);
53   Obj_val<bp*>(res) = b;
54   CAMLreturn(res);
55 }
56
57 extern "C" value caml_bp_first_child(value b, value idx)
58 {
59   CAMLparam2(b, idx);
60   CAMLreturn (Val_int( bp_first_child(Obj_val<bp*>(b), Int_val(idx))));
61 }
62
63
64 extern "C" value caml_bp_next_sibling(value b, value idx)
65 {
66   CAMLparam2(b, idx);
67   CAMLreturn (Val_int(bp_next_sibling(Obj_val<bp*>(b), Int_val(idx))));
68 }
69
70 extern "C" value caml_bp_preorder_rank(value b, value idx)
71 {
72   CAMLparam2(b, idx);
73   CAMLreturn (Val_int(bp_preorder_rank(Obj_val<bp*>(b), Int_val(idx)) - 1));
74 }
75
76
77 extern "C" value caml_bp_load(value file)
78 {
79   CAMLparam1(file);
80   CAMLlocal1(result);
81   bp *B;
82   int f1 = Int_val(file);
83   int f2 = dup(f1);
84   FILE * fd = fdopen(f2, "r");
85   if (fd == NULL)
86     sxsi_raise_msg("Error opening bp file");
87   B = loadTree(fd);
88   fclose(fd);
89   result = sxsi_alloc_custom<bp*>(caml_bp_delete);
90   Obj_val<bp*>(result) = B;
91   CAMLreturn(result);
92 }
93
94 extern "C" value caml_bp_save(value b, value file)
95 {
96   CAMLparam2(b, file);
97   bp *B = Obj_val<bp*>(b);
98   int f1 = Int_val(file);
99   int f2 = dup(f1);
100   FILE * fd = fdopen(f2, "a");
101   fflush(stderr);
102   if (fd == NULL)
103     sxsi_raise_msg("Error saving bp file");
104   saveTree(B, fd);
105   fclose(fd);
106   CAMLreturn(Val_unit);
107 }
108
109 extern "C" value caml_bp_alloc_stats(value unit)
110 {
111   CAMLparam1(unit);
112   CAMLreturn (Val_long(bp_get_alloc_stats()));
113 }