ソースを参照

bitmap_realloc: Local implementation of memmove

Ioannis Koutras 12 年 前
コミット
7fedc886e7
共有1 個のファイルを変更した46 個の追加4 個の削除を含む
  1. 46 4
      src/bitmap/bitmap_realloc.c

+ 46 - 4
src/bitmap/bitmap_realloc.c

@@ -25,12 +25,54 @@
 #include "bitmap/bitmap.h"
 #include "dmmlib/dmmlib.h"
 
-#include <string.h>
-
 #ifdef HAVE_LOCKS
 #include <pthread.h>
 #endif /* HAVE_LOCKS */
 
+/** 
+ * Implementation of memmove()
+ *
+ * Original code at:
+ * http://discuss.joelonsoftware.com/default.asp?interview.11.437419.13
+ */
+void * data_move(void *destination, void *source, size_t count);
+
+void * data_move(void *destination, void *source, size_t count) {
+    size_t i = 0;
+
+    char *src = (char *)source;
+    char *dest = (char *)destination;
+    void * ptr = dest;
+
+    char * src_beg = src;
+    char * src_end = src_beg + count;
+
+    char * dest_beg = dest;
+    char * dest_end = dest_beg + count;
+    if(src == dest) {
+        return ptr;
+    }
+
+    if((dest_beg >= src_beg && dest_beg <= src_end) || (dest_end >= src_beg && dest_end <= src_end)) {
+
+        //Copy from higher addresses to lower addresses
+        for( i = count ; i > 0 ; i--)
+        {
+            *(dest + i) = *(src + i);
+        }
+
+        return ptr;
+    }
+
+    //No overlap, copy from lower addresses to higher addresses
+    for( i = 0 ; i < count ; i++)
+    {
+        *(dest + i) = *(src + i);
+    }
+
+    return ptr;
+}
+
 /**
  * Reallocates a memory block from a bitmap-organized raw block
  *
@@ -51,10 +93,10 @@ void * bitmap_realloc(bitmap_rb_t *raw_block, void * ptr,
 
     if(req_size > chunk_header->num_of_cells * raw_block->bytes_per_cell -
             CHUNK_HDR_SIZE) {
-        ret = memmove(ret, ptr, chunk_header->num_of_cells *
+        ret = data_move(ret, ptr, chunk_header->num_of_cells *
                 raw_block->bytes_per_cell - CHUNK_HDR_SIZE);
     } else {
-        ret = memmove(ret, ptr, req_size);
+        ret = data_move(ret, ptr, req_size);
     }
 
 #ifdef HAVE_LOCKS