浏览代码

add support for memalign()

Ioannis Koutras 11 年之前
父节点
当前提交
325b135e28
共有 2 个文件被更改,包括 50 次插入33 次删除
  1. 1 0
      include/dmmlib/memalign.h
  2. 49 33
      src/memalign.c

+ 1 - 0
include/dmmlib/memalign.h

@@ -3,6 +3,7 @@
 
 #include <stddef.h>
 
+void *memalign(size_t alignment, size_t size);
 int posix_memalign(void **memptr, size_t alignment, size_t size);
 
 #endif /* MEMALIGN_H */

+ 49 - 33
src/memalign.c

@@ -45,34 +45,26 @@
 freelist-organized raw blocks.
 #endif /* BITMAP_RB_ONLY */
 
-/** The function posix_memalign() allocates size bytes and places the address of
- * the allocated memory in *memptr. The address of the allocated memory will be
- * a multiple of alignment, which must be a power of two and a multiple of
- * sizeof(void *). If size is 0, then posix_memalign() returns either NULL.
+/** The obsolete function memalign() allocates size bytes and returns a pointer
+ * to the allocated memory.  The memory address will be a multiple of alignment,
+ * which must be a power of two.
  */
-int posix_memalign(void **memptr, size_t alignment, size_t size) {
+void *memalign(size_t alignment, size_t size) {
+    void *memptr;
     DEFAULT_RB_T *encapsulated_rb;
-
-    *memptr = NULL;
-
-    /* Input check */
-    if(size == 0 || (alignment & 1) != 0 || alignment % sizeof(void *) != 0) {
-        return 0;
-    }
-
     raw_block_header_t *raw_block;
 
     /* Search the available freelist-organized raw blocks for a block whose size
      * is size + alignment - 1 */
     SLIST_FOREACH(raw_block, &systemallocator.rb_head, pointers) {
         LOCK_RAW_BLOCK(raw_block);
-        encapsulated_rb = (freelist_rb_t *) 
+        encapsulated_rb = (freelist_rb_t *)
             ((uintptr_t) raw_block + sizeof(raw_block_header_t));
-        *memptr = freelist_memalign(encapsulated_rb, alignment, size);
+        memptr = freelist_memalign(encapsulated_rb, alignment, size);
         UNLOCK_RAW_BLOCK(raw_block);
     }
 
-    if(*memptr == NULL) {
+    if(memptr == NULL) {
 
         if( 2 * (size + alignment - 1)  > SYS_ALLOC_SIZE -
                     sizeof(raw_block_header_t) - sizeof(freelist_rb_t)) {
@@ -94,37 +86,37 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
                 }
             }
 
-            *memptr = (void *)create_raw_block(size + (counter + 1) * alignment
+            memptr = (void *)create_raw_block(size + (counter + 1) * alignment
                     - 1 + sizeof(raw_block_header_t), BIGBLOCK);
-            if(*memptr != NULL) {
+            if(memptr != NULL) {
 
-                *memptr = (void *)((uintptr_t) *memptr +
+                memptr = (void *)((uintptr_t) memptr +
                         sizeof(raw_block_header_t));
 
                 /* Check if alignment is needed */
-                if(((uintptr_t) *memptr) % alignment != 0) {
-                    size_t padding = (- (size_t) *memptr) & (alignment - 1);
+                if(((uintptr_t) memptr) % alignment != 0) {
+                    size_t padding = (- (size_t) memptr) & (alignment - 1);
                     padding += counter * alignment;
 
                     /* Sometimes a deadlock is observed unless the old mutex is
                      * destroyed.
                      */
-                    DESTROY_RAW_BLOCK_LOCK(((raw_block_header_t *) *memptr));
+                    DESTROY_RAW_BLOCK_LOCK(((raw_block_header_t *) memptr));
 
                     /* Copy the raw block's header to the new location */
-                    memcpy((void *)((uintptr_t) *memptr
+                    memcpy((void *)((uintptr_t) memptr
                                 - sizeof(raw_block_header_t) + padding),
-                                (void *)((uintptr_t) *memptr
+                                (void *)((uintptr_t) memptr
                                 - sizeof(raw_block_header_t)),
                                 sizeof(raw_block_header_t)
                            );
 
                     /* Update *memptr */
-                    *memptr = (void *)((uintptr_t) *memptr + padding);
+                    memptr = (void *)((uintptr_t) memptr + padding);
 
                     /* Update big block's size and requested size */
                     raw_block_header_t *aligned_header =
-                        (raw_block_header_t *)((uintptr_t) *memptr -
+                        (raw_block_header_t *)((uintptr_t) memptr -
                                 sizeof(raw_block_header_t));
                     INIT_RAW_BLOCK_LOCK(aligned_header);
                     LOCK_RAW_BLOCK(aligned_header);
@@ -138,12 +130,12 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
 
 #ifdef WITH_DEBUG
                 LOCK_GLOBAL();
-                LOCK_RAW_BLOCK(((raw_block_header_t *) ((uintptr_t) *memptr -
+                LOCK_RAW_BLOCK(((raw_block_header_t *) ((uintptr_t) memptr -
                                 sizeof(raw_block_header_t))));
                 SLIST_INSERT_HEAD(&systemallocator.bb_head,
-                        (raw_block_header_t *) ((uintptr_t) *memptr -
+                        (raw_block_header_t *) ((uintptr_t) memptr -
                             sizeof(raw_block_header_t)), pointers);
-                UNLOCK_RAW_BLOCK(((raw_block_header_t *) ((uintptr_t) *memptr -
+                UNLOCK_RAW_BLOCK(((raw_block_header_t *) ((uintptr_t) memptr -
                                 sizeof(raw_block_header_t))));
                 UNLOCK_GLOBAL();
 #endif /* WITH_DEBUG */
@@ -160,15 +152,15 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
 
                 encapsulated_rb = (DEFAULT_RB_T *)
                     ((uintptr_t) raw_block + sizeof(raw_block_header_t));
-                *memptr = freelist_memalign(encapsulated_rb, alignment, size);
+                memptr = freelist_memalign(encapsulated_rb, alignment, size);
                 UNLOCK_RAW_BLOCK(raw_block);
             }
         }
     }
 
-    if(*memptr != NULL) {
+    if(memptr != NULL) {
         /* Assert that the returned address is a multiple of alignment */
-        assert((uintptr_t) *memptr % alignment == 0);
+        assert((uintptr_t) memptr % alignment == 0);
 
 #ifdef REQUEST_SIZE_INFO
         UPDATE_GLOBAL_STATS(MEMALIGN, size);
@@ -179,5 +171,29 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
 
     MEM_TRACE("dmmlib - ma %p %zu %zu\n", *memptr, alignment, size);
 
-    return 0;
+    return memptr;
+}
+
+/** The function posix_memalign() allocates size bytes and places the address of
+ * the allocated memory in *memptr. The address of the allocated memory will be
+ * a multiple of alignment, which must be a power of two and a multiple of
+ * sizeof(void *). If size is 0, then posix_memalign() returns either NULL, or a
+ * unique pointer value that can later be successfully passed to free().
+ */
+int posix_memalign(void **memptr, size_t alignment, size_t size) {
+    void *ptr = NULL;
+
+    /* Input check */
+    if(size == 0 || (alignment & 1) != 0 || alignment % sizeof(void *) != 0) {
+        return -1;
+    }
+
+    ptr = memalign(alignment, size);
+
+    if(ptr != NULL) {
+        *memptr = ptr;
+        return 0;
+    }
+
+    return -1;
 }