Przeglądaj źródła

move memmove() out of bitmap_realloc.c

Ioannis Koutras 12 lat temu
rodzic
commit
c8a0916bfc
4 zmienionych plików z 104 dodań i 47 usunięć
  1. 32 0
      private-include/memmove.h
  2. 1 0
      src/CMakeLists.txt
  3. 4 47
      src/bitmap/bitmap_realloc.c
  4. 67 0
      src/memmove.c

+ 32 - 0
private-include/memmove.h

@@ -0,0 +1,32 @@
+/*
+ *   Copyright 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   memmove.h
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   October 2012
+ * @brief  memmove function
+ */
+
+#ifndef MEMMOVE_H
+#define MEMMOVE_H
+
+#include <stddef.h>
+
+void * memmove(void *destination, void *source, size_t count);
+
+#endif /* MEMMOVE_H */

+ 1 - 0
src/CMakeLists.txt

@@ -103,6 +103,7 @@ elseif(RAW_BLOCKS_TYPE STREQUAL "bitmap")
     bitmap/bitmap_malloc.c
     bitmap/bitmap_free.c
     bitmap/bitmap_realloc.c
+    memmove.c
     )
 
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")

+ 4 - 47
src/bitmap/bitmap_realloc.c

@@ -24,55 +24,12 @@
 
 #include "bitmap/bitmap.h"
 #include "dmmlib/dmmlib.h"
+#include "memmove.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
  *
@@ -93,10 +50,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 = data_move(ret, ptr, chunk_header->num_of_cells *
-                raw_block->bytes_per_cell - CHUNK_HDR_SIZE);
+        memmove(ret, ptr, chunk_header->num_of_cells * raw_block->bytes_per_cell
+                - CHUNK_HDR_SIZE);
     } else {
-        ret = data_move(ret, ptr, req_size);
+        memmove(ret, ptr, req_size);
     }
 
 #ifdef HAVE_LOCKS

+ 67 - 0
src/memmove.c

@@ -0,0 +1,67 @@
+/*
+ *   Copyright 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   memmove.c
+ * @author Ioannis Koutras
+ * @date   October 2012
+ * @brief  memmove() implementation
+ */
+
+#include "memmove.h"
+
+/** Copies n bytes from string s2 to string s1. The two strings may overlap; the
+ * copy is always done in a non-destructive manner.
+ * 
+ * Original code at:
+ * http://discuss.joelonsoftware.com/default.asp?interview.11.437419.13
+ */
+void * memmove(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;
+}