summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ioriot/Makefile2
-rw-r--r--ioriot/src/datas/btree.c2
-rw-r--r--ioriot/src/datas/ranges.c78
-rw-r--r--ioriot/src/datas/ranges.h92
4 files changed, 173 insertions, 1 deletions
diff --git a/ioriot/Makefile b/ioriot/Makefile
index 15a2f7d..7474d05 100644
--- a/ioriot/Makefile
+++ b/ioriot/Makefile
@@ -1,4 +1,4 @@
-#DEBUG=-g3 -ggdb3 -pg
+DEBUG=-g3 -ggdb3 -pg
NAME=ioriot
LIBS=-pthread
CFLAGS=-Wall -std=gnu99 -pedantic
diff --git a/ioriot/src/datas/btree.c b/ioriot/src/datas/btree.c
index b09ea1c..21b5ab9 100644
--- a/ioriot/src/datas/btree.c
+++ b/ioriot/src/datas/btree.c
@@ -345,4 +345,6 @@ void btree_test(void)
btree_print(b);
btree_destroy(b);
+
+ exit(0);
}
diff --git a/ioriot/src/datas/ranges.c b/ioriot/src/datas/ranges.c
new file mode 100644
index 0000000..5fb6bba
--- /dev/null
+++ b/ioriot/src/datas/ranges.c
@@ -0,0 +1,78 @@
+// Copyright 2018 Mimecast Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "ranges.h"
+
+/**
+ * @brief Callback for printing a range
+ *
+ * @param data The btree key, representing the from offset
+ * @param data The btree value, representing the to offset
+ */
+void _range_print_cb(void *data, void *data2)
+{
+ long from = (long) data;
+ long to = (long) data2;
+
+ Put("\tfrom:'%ld' to:'%ld'", from, to);
+}
+
+range_s *range_new(const long from, const long to)
+{
+ range_s *r = Malloc(range_s);
+
+ r->from = from;
+ r->to = to;
+
+ return r;
+}
+
+void range_destroy(range_s *r)
+{
+ free(r);
+}
+
+ranges_s *ranges_new()
+{
+ ranges_s *r = Malloc(ranges_s);
+ *r = (ranges_s) {
+ .btree = btree_new()
+ };
+ return r;
+}
+
+void ranges_destroy(ranges_s *r)
+{
+ btree_destroy(r->btree);
+ free(r);
+}
+
+void range_ensure(ranges_s* r, long from, long to)
+{
+}
+
+void ranges_print(ranges_s* r)
+{
+ Put("ranges:%p", (void*)r);
+ btree_run_cb2(r->btree, _range_print_cb);
+}
+
+void ranges_test(void)
+{
+ ranges_s *r = ranges_new();
+ range_ensure(r, 0, 10);
+ range_ensure(r, 1032, 2000);
+ ranges_print(r);
+ ranges_destroy(r);
+}
diff --git a/ioriot/src/datas/ranges.h b/ioriot/src/datas/ranges.h
new file mode 100644
index 0000000..ecd6779
--- /dev/null
+++ b/ioriot/src/datas/ranges.h
@@ -0,0 +1,92 @@
+// Copyright 2018 Mimecast Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef RANGES_H
+#define RANGES_H
+
+#include "../defaults.h"
+
+#include "btree.h"
+
+/**
+ * @brief This represents a range
+ */
+typedef struct range_s_ {
+ long from;
+ long to;
+} range_s;
+
+/**
+ * @brief This represents a list of ranges
+ *
+ * A range is used to represent data ranges of a file with holes. Whereas
+ * not all regions of a file have actually allocated data blocks.
+ */
+typedef struct ranges_s_ {
+ btree_s *btree;
+} ranges_s;
+
+/**
+ * @brief Creates a new range object
+ *
+ * @return A new ranges object
+ * @param from Range start offset
+ * @param to Range stop offset
+ */
+range_s* range_new(const long from, const long to);
+
+/**
+ * @brief Destroys the given range object
+ *
+ * @param r The range object to be destroyed
+ */
+void range_destroy(range_s* r);
+
+
+/**
+ * @brief Creates a new ranges object
+ *
+ * @return A new ranges object
+ */
+ranges_s* ranges_new();
+
+/**
+ * @brief Destroys the given ranges object
+ *
+ * @param r The ranges object to be destroyed
+ */
+void ranges_destroy(ranges_s* r);
+
+/**
+ * @brief Ensures that a given range is valid
+ *
+ * @param r The range object
+ * @param from Range start offset
+ * @param to Range stop offset
+ */
+void range_ensure(ranges_s* r, long from, long to);
+
+/**
+ * @brief Prints the range
+ *
+ * @param r The range object to be printed
+ */
+void ranges_print(ranges_s* r);
+
+/**
+ * @brief Unit testing the range object
+ */
+void ranges_test(void);
+
+#endif // RANGES_H