Browse Source

bibop_free + minor changes in bibop raw blocks

Ioannis Koutras 12 years ago
parent
commit
af4d78f303

+ 32 - 0
private-include/bibop/bibop_free.h

@@ -0,0 +1,32 @@
+/*
+ *   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.h
+ * @author  Ilias Pliotas (hpliotas@gmail.com), Ioannis Koutras
+ * (joko@microlab.ntua.gr)
+ * @brief   Memory de-allocation for BiBoP-organized raw blocks
+ */
+
+#ifndef BIBOP_FREE_H
+#define BIBOP_FREE_H
+
+#include "dmmlib/raw_block.h"
+
+void bibop_free(raw_block_header_t *raw_block, void *ptr);
+
+#endif /* BIBOP_FREE_H */

+ 1 - 1
private-include/bibop/bibop_other.h

@@ -36,6 +36,6 @@ void add_arrays(BMAP_EL_TYPE *array1, BMAP_EL_TYPE *array2);
 
 
 size_t prev_pow2(size_t n);
 size_t prev_pow2(size_t n);
 
 
-BMAP_EL_TYPE make_bit_mask(unsigned int start_pos, size_t length);
+BMAP_EL_TYPE make_bit_mask(size_t start_pos, size_t length);
 
 
 #endif /* BIBOP_OTHER_H */
 #endif /* BIBOP_OTHER_H */

+ 3 - 1
private-include/bibop/bibop_rb.h

@@ -34,6 +34,7 @@
 
 
 #define BMAP_INDEX_NUM 20
 #define BMAP_INDEX_NUM 20
 
 
+/** BiBoP raw block header data structure */
 typedef struct bibop_rb_s {
 typedef struct bibop_rb_s {
     BMAP_EL_TYPE bmap_array[BMAP_INDEX_NUM];
     BMAP_EL_TYPE bmap_array[BMAP_INDEX_NUM];
     BMAP_EL_TYPE bmap_array2[BMAP_INDEX_NUM];
     BMAP_EL_TYPE bmap_array2[BMAP_INDEX_NUM];
@@ -42,8 +43,9 @@ typedef struct bibop_rb_s {
                               fixed-sized, this is also fixed */
                               fixed-sized, this is also fixed */
 } bibop_rb_t;
 } bibop_rb_t;
 
 
+/** Chunk header data structure */
 typedef struct chunk_header_s {
 typedef struct chunk_header_s {
-    size_t num_of_cells;
+    size_t num_of_cells; /**< The number of occupied cells. */
 } chunk_header_t;
 } chunk_header_t;
 
 
 #endif /* BIBOP_RB_H */
 #endif /* BIBOP_RB_H */

+ 1 - 0
src/CMakeLists.txt

@@ -88,6 +88,7 @@ elseif(RAW_BLOCKS_TYPE STREQUAL "bibop")
     ${dmmlib_SRCS}
     ${dmmlib_SRCS}
     bibop/bibop_other.c
     bibop/bibop_other.c
     bibop/bibop_malloc.c
     bibop/bibop_malloc.c
+    bibop/bibop_free.c
     )
     )
 
 
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")

+ 78 - 0
src/bibop/bibop_free.c

@@ -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 */
+}

+ 8 - 7
src/bibop/bibop_malloc.c

@@ -33,15 +33,15 @@
 /**
 /**
  * Returns a memory block from a BiBoP-organized raw block
  * Returns a memory block from a BiBoP-organized raw block
  *
  *
- * @param  size_t The requested memory size.
- * @retval        The address to serve the request.
- * @retval NULL   No available memory space.
+ * @param  raw_block The pointer of the raw block.
+ * @param  req_size  The requested memory size.
+ * @retval           The address to serve the request.
+ * @retval NULL      No available memory space.
  */
  */
 void * bibop_malloc(raw_block_header_t *raw_block, size_t req_size) { 
 void * bibop_malloc(raw_block_header_t *raw_block, size_t req_size) { 
     bibop_rb_t *rb_header;
     bibop_rb_t *rb_header;
-    size_t cells, stop, i;
+    size_t cells, stop, i, found;
     void *ret;
     void *ret;
-    unsigned int found;
     BMAP_EL_TYPE mask1, mask2;
     BMAP_EL_TYPE mask1, mask2;
     chunk_header_t *chunk_address;
     chunk_header_t *chunk_address;
 
 
@@ -76,7 +76,7 @@ void * bibop_malloc(raw_block_header_t *raw_block, size_t req_size) {
         add_arrays(rb_header->bmap_array2, rb_header->bmap_array3);
         add_arrays(rb_header->bmap_array2, rb_header->bmap_array3);
     }
     }
 
 
-    found = 0 ;
+    found = 0;
     ret = NULL;
     ret = NULL;
 
 
     for(i = 0; i < BMAP_INDEX_NUM; ++i) {
     for(i = 0; i < BMAP_INDEX_NUM; ++i) {
@@ -88,7 +88,8 @@ void * bibop_malloc(raw_block_header_t *raw_block, size_t req_size) {
                 rb_header->bmap_array[i] &= mask1;
                 rb_header->bmap_array[i] &= mask1;
             } else {
             } else {
                 mask1 = ~make_bit_mask(found, BMAP_EL_SIZE_BITS - found + 1);
                 mask1 = ~make_bit_mask(found, BMAP_EL_SIZE_BITS - found + 1);
-                mask2 = ~make_bit_mask(1, cells - (BMAP_EL_SIZE_BITS - found) - 1);
+                mask2 = ~make_bit_mask((size_t) 1,
+                        cells - (BMAP_EL_SIZE_BITS - found) - 1);
                 rb_header->bmap_array[i] &= mask1;
                 rb_header->bmap_array[i] &= mask1;
                 rb_header->bmap_array[i + 1] &= mask2;
                 rb_header->bmap_array[i + 1] &= mask2;
             }
             }

+ 2 - 1
src/bibop/bibop_other.c

@@ -75,6 +75,7 @@ void add_arrays(BMAP_EL_TYPE *array1, BMAP_EL_TYPE *array2) {
     }
     }
 }
 }
 
 
+/** Compute the power of 2 which is <= n */
 size_t prev_pow2(size_t n) {
 size_t prev_pow2(size_t n) {
 	n |= n >> 1;
 	n |= n >> 1;
 	n |= n >> 2;
 	n |= n >> 2;
@@ -89,7 +90,7 @@ size_t prev_pow2(size_t n) {
  * @param start_pos The starting position of 1's
  * @param start_pos The starting position of 1's
  * @param length    The amount of 1's
  * @param length    The amount of 1's
  */
  */
-BMAP_EL_TYPE make_bit_mask(unsigned int start_pos , size_t length) { 
+BMAP_EL_TYPE make_bit_mask(size_t start_pos, size_t length) { 
     BMAP_EL_TYPE init;
     BMAP_EL_TYPE init;
     
     
     init = (BMAP_EL_TYPE) 1;	
     init = (BMAP_EL_TYPE) 1;