[FIX] SWAP buffer review result
authorAlexander Aksenov <a.aksenov@samsung.com>
Fri, 14 Jun 2013 14:11:34 +0000 (18:11 +0400)
committerAlexander Aksenov <a.aksenov@samsung.com>
Fri, 14 Jun 2013 14:11:34 +0000 (18:11 +0400)
buffer/buffer_queue.c
buffer/swap_buffer_errors.h
buffer/swap_buffer_module.c

index bfbc01e..2966cd1 100644 (file)
@@ -55,10 +55,16 @@ struct queue {
 };
 
 /* Write queue */
-struct queue write_queue = {NULL, NULL};
+struct queue write_queue = {
+       .start_ptr = NULL,
+       .end_ptr = NULL
+};
 
 /* Read queue */
-struct queue read_queue = {NULL, NULL};
+struct queue read_queue = {
+       .start_ptr = NULL,
+       .end_ptr = NULL
+};
 
 /* Pointers array. Points to busy buffers */
 static struct swap_buffer **queue_busy = NULL;
@@ -315,8 +321,10 @@ struct swap_subbuffer *get_from_write_list(size_t size, void **ptr_to_write)
        struct swap_subbuffer *result = NULL;
 
        /* Callbacks are called at the end of the function to prevent deadlocks */
-       struct swap_subbuffer *queue_callback_start_ptr = NULL;
-       struct swap_subbuffer *queue_callback_end_ptr = NULL;
+       struct queue callback_queue = {
+               .start_ptr = NULL,
+               .end_ptr = NULL
+       };
        struct swap_subbuffer *tmp_buffer = NULL;
 
        /* Init pointer */
@@ -346,29 +354,28 @@ struct swap_subbuffer *get_from_write_list(size_t size, void **ptr_to_write)
 
                        /* Lock rw sync. Should be unlocked in swap_buffer_write() */
                        sync_lock(&result->buffer_sync);
-
                        break;
                /* This subbuffer is not enough => it goes to read list */
                } else {
 
                        result = write_queue.start_ptr;
+
                        /* If we reached end of the list */
                        if (write_queue.start_ptr == write_queue.end_ptr) {
                                write_queue.end_ptr = NULL;
                        }
+
                        /* Move start write pointer */
                        write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
 
                        /* Add to callback list */
-                       if (!queue_callback_start_ptr)
-                               queue_callback_start_ptr = result;
-
-                       if (queue_callback_end_ptr)
-                               queue_callback_end_ptr->next_in_queue = result;
-
-                       queue_callback_end_ptr = result;
-                       queue_callback_end_ptr->next_in_queue = NULL;
+                       if (!callback_queue.start_ptr)
+                               callback_queue.start_ptr = result;
 
+                       if (callback_queue.end_ptr)
+                               callback_queue.end_ptr->next_in_queue = result;
+                       callback_queue.end_ptr = result;
+                       callback_queue.end_ptr->next_in_queue = NULL;
                        result = NULL;
                }
        }
@@ -378,13 +385,12 @@ get_from_write_list_unlock:
        sync_unlock(&write_queue.queue_sync);
 
        /* Adding buffers to read list and calling callbacks */
-       for (tmp_buffer = NULL; queue_callback_start_ptr; ) {
-
-               if (queue_callback_start_ptr == queue_callback_end_ptr)
-                       queue_callback_end_ptr = NULL;
+       for (tmp_buffer = NULL; callback_queue.start_ptr; ) {
+               if (callback_queue.start_ptr == callback_queue.end_ptr)
+                       callback_queue.end_ptr = NULL;
 
-               tmp_buffer = queue_callback_start_ptr;
-               queue_callback_start_ptr = queue_callback_start_ptr->next_in_queue;
+               tmp_buffer = callback_queue.start_ptr;
+               callback_queue.start_ptr = callback_queue.start_ptr->next_in_queue;
 
                add_to_read_list_with_callback(tmp_buffer);
        }
index d07b147..1faa97f 100644 (file)
 /* SWAP Buffer error codes enumeration */
 
 enum _swap_buffer_errors {
-    E_SB_SUCCESS = 0,               /* Succes */
-    E_SB_UNRELEASED_BUFFERS = -1,   /* There are some unreleased buffers. Mainly
+    E_SB_SUCCESS = 0,               /* Success */
+    E_SB_UNRELEASED_BUFFERS = 1,    /* There are some unreleased buffers. Mainly
                                        returned by swap_buffer_uninit */
-    E_SB_NO_WRITABLE_BUFFERS = -2,  /* No buffers for writing */
-    E_SB_WRONG_DATA_SIZE = -3,      /* Wrong data size: size == 0 or
+    E_SB_NO_WRITABLE_BUFFERS = 2,   /* No buffers for writing */
+    E_SB_WRONG_DATA_SIZE = 3,       /* Wrong data size: size == 0 or
                                        size > subbuffer size */
-    E_SB_IS_STOPPED = -4,           /* Trying to write data after SWAP buffer
+    E_SB_IS_STOPPED = 4,            /* Trying to write data after SWAP buffer
                                        has been stopped. */
-    E_SB_OVERLAP = -5,              /* Memory areas of data to be written and
+    E_SB_OVERLAP = 5,               /* Memory areas of data to be written and
                                        subbuffer itself are overlap */
-    E_SB_NO_READABLE_BUFFERS = -6,  /* No buffers for reading */
-    E_SB_NO_CALLBACK = -7,          /* Callback function ptr == NULL */
-
-    E_SB_NO_MEM_QUEUE_BUSY = -8,    /* Memory for queue_busy wasn't allocated */
-    E_SB_NO_MEM_BUFFER_STRUCT = -9, /* Memory for one of struct swap_buffer
+    E_SB_NO_READABLE_BUFFERS = 6,   /* No buffers for reading */
+    E_SB_NO_CALLBACK = 7,           /* Callback function ptr == NULL */
+    E_SB_NO_MEM_QUEUE_BUSY = 8,     /* Memory for queue_busy wasn't allocated */
+    E_SB_NO_MEM_BUFFER_STRUCT = 9,  /* Memory for one of struct swap_buffer
                                        wasn't allocated */
-    E_SB_NO_MEM_DATA_BUFFER = -10,  /* Memort for data buffer itself wasn't
+    E_SB_NO_MEM_DATA_BUFFER = 10,   /* Memort for data buffer itself wasn't
                                        allocated */
-    E_SB_NO_SUBBUFFER_IN_BUSY = -11 /* No such subbuffer in busy_list */
+    E_SB_NO_SUBBUFFER_IN_BUSY = 11  /* No such subbuffer in busy_list */
 };
index f69ca08..4af0c22 100644 (file)
@@ -104,7 +104,7 @@ int swap_buffer_uninit(void)
 
        /* Checking whether all buffers are released */
        if (get_busy_buffers_count())
-               return E_SB_UNRELEASED_BUFFERS;
+               return -E_SB_UNRELEASED_BUFFERS;
 
        /* Free */
        buffer_queue_free();
@@ -129,16 +129,16 @@ ssize_t swap_buffer_write(size_t size, void *data)
 
        /* Check buffer status */
        if (!(swap_buffer_status & BUFFER_WORK))
-               return E_SB_IS_STOPPED;
+               return -E_SB_IS_STOPPED;
 
        /* Size sanitization */
        if ((size > subbuffers_size) || (size == 0))
-               return E_SB_WRONG_DATA_SIZE;
+               return -E_SB_WRONG_DATA_SIZE;
 
        /* Get next write buffer and occupying semaphore */
        buffer_to_write = get_from_write_list(size, &ptr_to_write);
        if (!buffer_to_write)
-               return E_SB_NO_WRITABLE_BUFFERS;
+               return -E_SB_NO_WRITABLE_BUFFERS;
 
        /* Check for overlapping */
        if (areas_overlap(ptr_to_write, data, size)) {
@@ -170,7 +170,7 @@ int swap_buffer_get(struct swap_subbuffer **subbuffer)
        /* Get next read buffer */
        buffer_to_read = get_from_read_list();
        if (!buffer_to_read)
-               return E_SB_NO_READABLE_BUFFERS;
+               return -E_SB_NO_READABLE_BUFFERS;
 
        /* Add to busy list */
        buffer_to_read->next_in_queue = NULL;
@@ -226,7 +226,7 @@ int swap_buffer_callback(void *buffer)
        int result;
 
        if (!subbuffer_callback) {
-               return E_SB_NO_CALLBACK;
+               return -E_SB_NO_CALLBACK;
        }
 
        result = subbuffer_callback(buffer);
@@ -240,7 +240,7 @@ int swap_buffer_callback(void *buffer)
 static int __init swap_buffer_module_init(void)
 {
        printk(KERN_NOTICE "SWAP_BUFFER : Buffer module initialized\n");
-       return 0;
+       return E_SB_SUCCESS;
 }
 
 static void __exit swap_buffer_module_exit(void)