|
@@ -0,0 +1,78 @@
|
|
|
|
+/*
|
|
|
|
+ * Copyright 2012 Institute of Communication and Computer Systems (ICCS)
|
|
|
|
+ *
|
|
|
|
+ * 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.
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @file bibop_free.c
|
|
|
|
+ * @author Ilias Pliotas, Ioannis Koutras
|
|
|
|
+ * @date September 2012
|
|
|
|
+ * @brief free() implementation for BiBoP raw blocks
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include "bibop/bibop_free.h"
|
|
|
|
+#include "bibop/bibop_rb.h"
|
|
|
|
+#include "bibop/bibop_other.h"
|
|
|
|
+
|
|
|
|
+#ifdef HAVE_LOCKS
|
|
|
|
+#include <pthread.h>
|
|
|
|
+#endif /* HAVE_LOCKS */
|
|
|
|
+
|
|
|
|
+/** Frees the memory block inside of a specific BiBoP organized raw block.
|
|
|
|
+ * @param raw_block The pointer of the raw block.
|
|
|
|
+ * @param ptr The pointer of the memory block to be freed.
|
|
|
|
+ */
|
|
|
|
+void bibop_free(raw_block_header_t *raw_block, void *ptr) {
|
|
|
|
+ bibop_rb_t *rb_header;
|
|
|
|
+ chunk_header_t *chunk_header;
|
|
|
|
+ size_t cells_used, cell_no, bmap_index, start_pos;
|
|
|
|
+ BMAP_EL_TYPE mask1, mask2;
|
|
|
|
+
|
|
|
|
+ rb_header = (bibop_rb_t *)((char *)raw_block + sizeof(raw_block_header_t));
|
|
|
|
+ chunk_header = (chunk_header_t *)((char *)ptr - sizeof(chunk_header_t));
|
|
|
|
+
|
|
|
|
+#ifdef HAVE_LOCKS
|
|
|
|
+ pthread_mutex_lock(&raw_block->mutex);
|
|
|
|
+#endif /* HAVE_LOCKS */
|
|
|
|
+
|
|
|
|
+ cells_used = chunk_header->num_of_cells;
|
|
|
|
+
|
|
|
|
+ cell_no = ((char *)chunk_header - ((char *)rb_header + sizeof(bibop_rb_t)))
|
|
|
|
+ / rb_header->bytes_per_cell;
|
|
|
|
+
|
|
|
|
+ bmap_index = cell_no / BMAP_EL_SIZE_BITS;
|
|
|
|
+ start_pos = cell_no % BMAP_EL_SIZE_BITS;
|
|
|
|
+
|
|
|
|
+ // If the sum of the starting position and the cells that are used is more
|
|
|
|
+ // than the available bits of one bitmap array element, then we have to
|
|
|
|
+ // modify two elements.
|
|
|
|
+ if(start_pos + cells_used <= BMAP_EL_SIZE_BITS) {
|
|
|
|
+ mask1 = make_bit_mask(start_pos + 1, cells_used);
|
|
|
|
+
|
|
|
|
+ rb_header->bmap_array[bmap_index] |= mask1;
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ mask1 = make_bit_mask(start_pos + 1, BMAP_EL_SIZE_BITS - start_pos);
|
|
|
|
+ mask2 = make_bit_mask((size_t) 1,
|
|
|
|
+ start_pos + cells_used - BMAP_EL_SIZE_BITS);
|
|
|
|
+
|
|
|
|
+ rb_header->bmap_array[bmap_index] |= mask1;
|
|
|
|
+ rb_header->bmap_array[bmap_index + 1] |= mask2;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+#ifdef HAVE_LOCKS
|
|
|
|
+ pthread_mutex_unlock(&raw_block->mutex);
|
|
|
|
+#endif /* HAVE_LOCKS */
|
|
|
|
+}
|