OSDN Git Service

Code cleanup in GKI layer.
authorSharvil Nanavati <sharvil@google.com>
Sun, 7 Sep 2014 09:15:19 +0000 (02:15 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 16 Mar 2015 23:51:33 +0000 (16:51 -0700)
  * move buffer macros into .c from .h
  * delete dead code (e.g. GKI_igetpoolbuf, pool_list variable)
  * define task states as an enum and not a bitfield
  * start renaming fields in GKI control block for future consolidation

gki/common/gki_buffer.c
gki/common/gki_common.h
gki/common/gki_time.c
gki/ulinux/gki_ulinux.c

index bd82df9..66d0d88 100644 (file)
 #error Number of pools out of range (16 Max)!
 #endif
 
-static void gki_add_to_pool_list(UINT8 pool_id);
-static void gki_remove_from_pool_list(UINT8 pool_id);
+#define ALIGN_POOL(pl_size)  ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
+#define BUFFER_HDR_SIZE     (sizeof(BUFFER_HDR_T))                  /* Offset past header */
+#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
+#define MAGIC_NO            0xDDBADDBA
+
+#define BUF_STATUS_FREE     0
+#define BUF_STATUS_UNLINKED 1
+#define BUF_STATUS_QUEUED   2
 
 /*******************************************************************************
 **
@@ -52,13 +58,11 @@ static void gki_init_free_queue (UINT8 id, UINT16 size, UINT16 total, void *p_me
     act_size = (UINT16)(tempsize + BUFFER_PADDING_SIZE);
 
     /* Remember pool start and end addresses */
-// btla-specific ++
     if(p_mem)
     {
         p_cb->pool_start[id] = (UINT8 *)p_mem;
         p_cb->pool_end[id]   = (UINT8 *)p_mem + (act_size * total);
     }
-// btla-specific --
 
     p_cb->pool_size[id]  = act_size;
 
@@ -68,7 +72,6 @@ static void gki_init_free_queue (UINT8 id, UINT16 size, UINT16 total, void *p_me
     p_cb->freeq[id].max_cnt   = 0;
 
     /* Initialize  index table */
-// btla-specific ++
     if(p_mem)
     {
         hdr = (BUFFER_HDR_T *)p_mem;
@@ -87,8 +90,6 @@ static void gki_init_free_queue (UINT8 id, UINT16 size, UINT16 total, void *p_me
         hdr1->p_next = NULL;
         p_cb->freeq[id]._p_last = hdr1;
     }
-// btla-specific --
-    return;
 }
 
 #if !VALGRIND
@@ -98,7 +99,7 @@ static BOOLEAN gki_alloc_free_queue(UINT8 id)
     tGKI_COM_CB *p_cb = &gki_cb.com;
     GKI_TRACE("\ngki_alloc_free_queue in, id:%d \n", (int)id );
 
-    Q = &p_cb->freeq[p_cb->pool_list[id]];
+    Q = &p_cb->freeq[id];
 
     if(Q->_p_first == 0)
     {
@@ -123,7 +124,7 @@ void gki_dealloc_free_queue(void)
     UINT8   i;
     tGKI_COM_CB *p_cb = &gki_cb.com;
 
-    for (i=0; i < p_cb->curr_total_no_of_pools; i++)
+    for (i=0; i < GKI_NUM_FIXED_BUF_POOLS; i++)
     {
         if ( 0 < p_cb->freeq[i].max_cnt )
         {
@@ -191,15 +192,9 @@ void gki_buffer_init(void)
 
     for (int i = 0; i < GKI_NUM_FIXED_BUF_POOLS; ++i) {
       gki_init_free_queue(i, buffer_info[i].size, buffer_info[i].count, NULL);
-      p_cb->pool_list[i] = i;
     }
-
-    p_cb->curr_total_no_of_pools = GKI_NUM_FIXED_BUF_POOLS;
-
-    return;
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_init_q
@@ -213,11 +208,8 @@ void GKI_init_q (BUFFER_Q *p_q)
 {
     p_q->_p_first = p_q->_p_last = NULL;
     p_q->_count = 0;
-
-    return;
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_getbuf
@@ -257,13 +249,13 @@ void *GKI_getbuf (UINT16 size)
     }
 
     /* Find the first buffer pool that is public that can hold the desired size */
-    for (i=0; i < p_cb->curr_total_no_of_pools; i++)
+    for (i=0; i < GKI_NUM_FIXED_BUF_POOLS; i++)
     {
-        if ( size <= p_cb->freeq[p_cb->pool_list[i]].size )
+        if ( size <= p_cb->freeq[i].size )
             break;
     }
 
-    if(i == p_cb->curr_total_no_of_pools)
+    if(i == GKI_NUM_FIXED_BUF_POOLS)
     {
         GKI_exception (GKI_ERROR_BUF_SIZE_TOOBIG, "getbuf: Size is too big");
         return (NULL);
@@ -274,13 +266,13 @@ void *GKI_getbuf (UINT16 size)
 
     /* search the public buffer pools that are big enough to hold the size
      * until a free buffer is found */
-    for ( ; i < p_cb->curr_total_no_of_pools; i++)
+    for ( ; i < GKI_NUM_FIXED_BUF_POOLS; i++)
     {
         /* Only look at PUBLIC buffer pools (bypass RESTRICTED pools) */
-        if (((UINT16)1 << p_cb->pool_list[i]) & p_cb->pool_access_mask)
+        if (((UINT16)1 << i) & p_cb->pool_access_mask)
             continue;
-        if ( size <= p_cb->freeq[p_cb->pool_list[i]].size )
-             Q = &p_cb->freeq[p_cb->pool_list[i]];
+        if ( size <= p_cb->freeq[i].size )
+             Q = &p_cb->freeq[i];
         else
              continue;
 
@@ -442,8 +434,6 @@ void GKI_freebuf (void *p_buf)
         Q->cur_cnt--;
 
     GKI_enable();
-
-    return;
 #endif
 }
 
@@ -563,8 +553,6 @@ void GKI_enqueue (BUFFER_Q *p_q, void *p_buf)
     p_hdr->status = BUF_STATUS_QUEUED;
 
     GKI_enable();
-
-    return;
 }
 
 
@@ -668,7 +656,6 @@ void *GKI_dequeue (BUFFER_Q *p_q)
     return ((UINT8 *)p_hdr + BUFFER_HDR_SIZE);
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_remove_from_queue
@@ -740,7 +727,6 @@ void *GKI_getfirst (BUFFER_Q *p_q)
     return (p_q->_p_first);
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_getlast
@@ -780,8 +766,6 @@ void *GKI_getnext (void *p_buf)
         return (NULL);
 }
 
-
-
 /*******************************************************************************
 **
 ** Function         GKI_queue_is_empty
@@ -805,115 +789,6 @@ UINT16 GKI_queue_length(BUFFER_Q *p_q)
 
 /*******************************************************************************
 **
-** Function         gki_add_to_pool_list
-**
-** Description      Adds pool to the pool list which is arranged in the
-**                  order of size
-**
-** Returns          void
-**
-*******************************************************************************/
-static void gki_add_to_pool_list(UINT8 pool_id)
-{
-
-    INT32 i, j;
-    tGKI_COM_CB *p_cb = &gki_cb.com;
-
-     /* Find the position where the specified pool should be inserted into the list */
-    for(i=0; i < p_cb->curr_total_no_of_pools; i++)
-    {
-
-        if(p_cb->freeq[pool_id].size <= p_cb->freeq[ p_cb->pool_list[i] ].size)
-            break;
-    }
-
-    /* Insert the new buffer pool ID into the list of pools */
-    for(j = p_cb->curr_total_no_of_pools; j > i; j--)
-    {
-        p_cb->pool_list[j] = p_cb->pool_list[j-1];
-    }
-
-    p_cb->pool_list[i] = pool_id;
-
-    return;
-}
-
-/*******************************************************************************
-**
-** Function         gki_remove_from_pool_list
-**
-** Description      Removes pool from the pool list. Called when a pool is deleted
-**
-** Returns          void
-**
-*******************************************************************************/
-static void gki_remove_from_pool_list(UINT8 pool_id)
-{
-    tGKI_COM_CB *p_cb = &gki_cb.com;
-    UINT8 i;
-
-    for(i=0; i < p_cb->curr_total_no_of_pools; i++)
-    {
-        if(pool_id == p_cb->pool_list[i])
-            break;
-    }
-
-    while (i < (p_cb->curr_total_no_of_pools - 1))
-    {
-        p_cb->pool_list[i] = p_cb->pool_list[i+1];
-        i++;
-    }
-
-    return;
-}
-
-/*******************************************************************************
-**
-** Function         GKI_igetpoolbuf
-**
-** Description      Called by an interrupt service routine to get a free buffer from
-**                  a specific buffer pool.
-**
-** Parameters       pool_id - (input) pool ID to get a buffer out of.
-**
-** Returns          A pointer to the buffer, or NULL if none available
-**
-*******************************************************************************/
-void *GKI_igetpoolbuf (UINT8 pool_id)
-{
-    FREE_QUEUE_T  *Q;
-    BUFFER_HDR_T  *p_hdr;
-
-    if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS)
-        return (NULL);
-
-
-    Q = &gki_cb.com.freeq[pool_id];
-    if(Q->cur_cnt < Q->total)
-    {
-        p_hdr = Q->_p_first;
-        Q->_p_first = p_hdr->p_next;
-
-        if (!Q->_p_first)
-            Q->_p_last = NULL;
-
-        if(++Q->cur_cnt > Q->max_cnt)
-            Q->max_cnt = Q->cur_cnt;
-
-        p_hdr->task_id = GKI_get_taskid();
-
-        p_hdr->status  = BUF_STATUS_UNLINKED;
-        p_hdr->p_next  = NULL;
-        p_hdr->Type    = 0;
-
-        return ((void *) ((UINT8 *)p_hdr + BUFFER_HDR_SIZE));
-    }
-
-    return (NULL);
-}
-
-/*******************************************************************************
-**
 ** Function         GKI_poolcount
 **
 ** Description      Called by an application to get the total number of buffers
@@ -1001,4 +876,3 @@ UINT16 GKI_poolutilization (UINT8 pool_id)
 
     return ((Q->cur_cnt * 100) / Q->total);
 }
-
index 83dcd3f..2cda237 100644 (file)
 #include "gki.h"
 #include "dyn_mem.h"
 
-/* Task States: (For OSRdyTbl) */
-#define TASK_DEAD       0   /* b0000 */
-#define TASK_READY      1   /* b0001 */
-#define TASK_WAIT       2   /* b0010 */
-#define TASK_DELAY      4   /* b0100 */
-#define TASK_SUSPEND    8   /* b1000 */
-
+typedef enum {
+  TASK_DEAD,
+  TASK_READY,
+  TASK_WAIT,
+  TASK_DELAY,
+  TASK_SUSPEND,
+} gki_task_state_t;
 
 /********************************************************************
 **  Internal Error codes
 #define GKI_ERROR_GETPOOLBUF_BAD_QID    0xFFF3
 #define GKI_ERROR_TIMER_LIST_CORRUPTED  0xFFF2
 
-
-/********************************************************************
-**  Misc constants
-*********************************************************************/
-
-/********************************************************************
-**  Buffer Management Data Structures
-*********************************************************************/
-
 typedef struct _buffer_hdr
 {
        struct _buffer_hdr *p_next;   /* next buffer in the queue */
@@ -78,26 +69,13 @@ typedef struct _free_queue
        UINT16           max_cnt;          /* maximum number of buffers allocated at any time */
 } FREE_QUEUE_T;
 
-
-/* Buffer related defines
-*/
-#define ALIGN_POOL(pl_size)  ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
-#define BUFFER_HDR_SIZE     (sizeof(BUFFER_HDR_T))                  /* Offset past header */
-#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
-#define MAX_USER_BUF_SIZE   ((UINT16)0xffff - BUFFER_PADDING_SIZE)  /* pool size must allow for header */
-#define MAGIC_NO            0xDDBADDBA
-
-#define BUF_STATUS_FREE     0
-#define BUF_STATUS_UNLINKED 1
-#define BUF_STATUS_QUEUED   2
-
 /* Put all GKI variables into one control block
 */
 typedef struct
 {
-    const char *OSTName[GKI_MAX_TASKS];         /* name of the task */
+    const char *task_name[GKI_MAX_TASKS];         /* name of the task */
+    gki_task_state_t task_state[GKI_MAX_TASKS];   /* current state of the task */
 
-    UINT8   OSRdyTbl[GKI_MAX_TASKS];        /* current state of the task */
     UINT16  OSWaitEvt[GKI_MAX_TASKS];       /* events that have to be processed by the task */
     UINT16  OSWaitForEvt[GKI_MAX_TASKS];    /* events the task is waiting for*/
 
@@ -128,8 +106,6 @@ typedef struct
 
     /* Define the buffer pool access control variables */
     UINT16      pool_access_mask;                   /* Bits are set if the corresponding buffer pool is a restricted pool */
-    UINT8       pool_list[GKI_NUM_TOTAL_BUF_POOLS]; /* buffer pools arranged in the order of size */
-    UINT8       curr_total_no_of_pools;             /* number of fixed buf pools + current number of dynamic pools */
 
     BOOLEAN     timer_nesting;                      /* flag to prevent timer interrupt nesting */
 } tGKI_COM_CB;
@@ -137,7 +113,6 @@ typedef struct
 /* Internal GKI function prototypes
 */
 BOOLEAN   gki_chk_buf_damage(void *);
-BOOLEAN   gki_chk_buf_owner(void *);
 void      gki_buffer_init (void);
 void      gki_timers_init(void);
 void      gki_adjust_timer_count (INT32);
index 5bef9aa..e2783af 100644 (file)
@@ -265,7 +265,7 @@ void GKI_timer_update (INT32 ticks_since_last_update)
             if (gki_cb.com.OSWaitTmr[task_id] <= 0)
             {
                 /* Timer Expired */
-                gki_cb.com.OSRdyTbl[task_id] = TASK_READY;
+                gki_cb.com.task_state[task_id] = TASK_READY;
             }
         }
 
index aca046f..1518d33 100644 (file)
@@ -119,16 +119,16 @@ static void gki_task_entry(UINT32 params)
     gki_pthread_info_t *p_pthread_info = (gki_pthread_info_t *)params;
     gki_cb.os.thread_id[p_pthread_info->task_id] = pthread_self();
 
-    prctl(PR_SET_NAME, (unsigned long)gki_cb.com.OSTName[p_pthread_info->task_id], 0, 0, 0);
+    prctl(PR_SET_NAME, (unsigned long)gki_cb.com.task_name[p_pthread_info->task_id], 0, 0, 0);
 
     ALOGI("gki_task_entry task_id=%i [%s] starting\n", p_pthread_info->task_id,
-                gki_cb.com.OSTName[p_pthread_info->task_id]);
+                gki_cb.com.task_name[p_pthread_info->task_id]);
 
     /* Call the actual thread entry point */
     (p_pthread_info->task_entry)(p_pthread_info->params);
 
     ALOGI("gki_task task_id=%i [%s] terminating\n", p_pthread_info->task_id,
-                gki_cb.com.OSTName[p_pthread_info->task_id]);
+                gki_cb.com.task_name[p_pthread_info->task_id]);
 
     pthread_exit(0);    /* GKI tasks have no return value */
 }
@@ -213,8 +213,8 @@ UINT8 GKI_create_task(TASKPTR task_entry, UINT8 task_id, const char *taskname)
         return (GKI_FAILURE);
     }
 
-    gki_cb.com.OSRdyTbl[task_id]    = TASK_READY;
-    gki_cb.com.OSTName[task_id]     = taskname;
+    gki_cb.com.task_state[task_id]  = TASK_READY;
+    gki_cb.com.task_name[task_id]     = taskname;
     gki_cb.com.OSWaitTmr[task_id]   = 0;
     gki_cb.com.OSWaitEvt[task_id]   = 0;
 
@@ -254,9 +254,9 @@ UINT8 GKI_create_task(TASKPTR task_entry, UINT8 task_id, const char *taskname)
 
 void GKI_destroy_task(UINT8 task_id)
 {
-    if (gki_cb.com.OSRdyTbl[task_id] != TASK_DEAD)
+    if (gki_cb.com.task_state[task_id] != TASK_DEAD)
     {
-        gki_cb.com.OSRdyTbl[task_id] = TASK_DEAD;
+        gki_cb.com.task_state[task_id] = TASK_DEAD;
 
         /* paranoi settings, make sure that we do not execute any mailbox events */
         gki_cb.com.OSWaitEvt[task_id] &= ~(TASK_MBOX_0_EVT_MASK|TASK_MBOX_1_EVT_MASK|
@@ -275,7 +275,7 @@ void GKI_destroy_task(UINT8 task_id)
             ALOGE( "pthread_join() FAILED: result: %d", result );
         }
         GKI_exit_task(task_id);
-        ALOGI( "GKI_shutdown(): task [%s] terminated\n", gki_cb.com.OSTName[task_id]);
+        ALOGI( "GKI_shutdown(): task [%s] terminated\n", gki_cb.com.task_name[task_id]);
     }
 }
 
@@ -308,7 +308,7 @@ void GKI_task_self_cleanup(UINT8 task_id)
         return;
     }
 
-    if (gki_cb.com.OSRdyTbl[task_id] != TASK_DEAD)
+    if (gki_cb.com.task_state[task_id] != TASK_DEAD)
     {
         /* paranoi settings, make sure that we do not execute any mailbox events */
         gki_cb.com.OSWaitEvt[task_id] &= ~(TASK_MBOX_0_EVT_MASK|TASK_MBOX_1_EVT_MASK|
@@ -355,9 +355,9 @@ void GKI_shutdown(void)
      * GKI_exception problem due to btu->hci sleep request events  */
     for (task_id = GKI_MAX_TASKS; task_id > 0; task_id--)
     {
-        if (gki_cb.com.OSRdyTbl[task_id - 1] != TASK_DEAD)
+        if (gki_cb.com.task_state[task_id - 1] != TASK_DEAD)
         {
-            gki_cb.com.OSRdyTbl[task_id - 1] = TASK_DEAD;
+            gki_cb.com.task_state[task_id - 1] = TASK_DEAD;
 
             /* paranoi settings, make sure that we do not execute any mailbox events */
             gki_cb.com.OSWaitEvt[task_id-1] &= ~(TASK_MBOX_0_EVT_MASK|TASK_MBOX_1_EVT_MASK|
@@ -445,7 +445,7 @@ UINT16 GKI_wait (UINT16 flag, UINT32 timeout)
            no need to call GKI_disable() here as we know that we will have some events as we've been waking
            up after condition pending or timeout */
 
-        if (gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
+        if (gki_cb.com.task_state[rtask] == TASK_DEAD)
         {
             gki_cb.com.OSWaitEvt[rtask] = 0;
             /* unlock thread_evt_mutex as pthread_cond_wait() does auto lock when cond is met */
@@ -486,35 +486,19 @@ UINT16 GKI_wait (UINT16 flag, UINT32 timeout)
 
 void GKI_delay (UINT32 timeout)
 {
-    UINT8 rtask = GKI_get_taskid();
     struct timespec delay;
     int err;
 
-    GKI_TRACE("GKI_delay %d %d", (int)rtask, (int)timeout);
-
     delay.tv_sec = timeout / 1000;
-    delay.tv_nsec = 1000 * 1000 * (timeout%1000);
+    delay.tv_nsec = 1000 * 1000 * (timeout % 1000);
 
     /* [u]sleep can't be used because it uses SIGALRM */
 
     do {
         err = nanosleep(&delay, &delay);
-    } while (err < 0 && errno ==EINTR);
-
-    /* Check if task was killed while sleeping */
-
-     /* NOTE : if you do not implement task killing, you do not need this check */
-
-    if (rtask && gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
-    {
-    }
-
-    GKI_TRACE("GKI_delay %d %d done", (int)rtask, (int)timeout);
-
-    return;
+    } while (err == -1 && errno ==EINTR);
 }
 
-
 /*******************************************************************************
 **
 ** Function         GKI_send_event
@@ -614,7 +598,7 @@ const char *GKI_map_taskname(UINT8 task_id) {
   if (task_id == GKI_MAX_TASKS)
     task_id = GKI_get_taskid();
 
-  return gki_cb.com.OSTName[task_id];
+  return gki_cb.com.task_name[task_id];
 }
 
 /*******************************************************************************
@@ -662,19 +646,16 @@ void GKI_disable (void)
 **
 *******************************************************************************/
 
-void GKI_exception (UINT16 code, char *msg)
+void GKI_exception(UINT16 code, char *msg)
 {
-    UINT8 task_id;
-    int i = 0;
-
     ALOGE( "GKI_exception(): Task State Table");
 
-    for(task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
+    for (int task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
     {
         ALOGE( "TASK ID [%d] task name [%s] state [%d]",
                          task_id,
-                         gki_cb.com.OSTName[task_id],
-                         gki_cb.com.OSRdyTbl[task_id]);
+                         gki_cb.com.task_name[task_id],
+                         gki_cb.com.task_state[task_id]);
     }
 
     ALOGE("GKI_exception %d %s", code, msg);
@@ -683,7 +664,6 @@ void GKI_exception (UINT16 code, char *msg)
     ALOGE( "********************************************************************");
 
     GKI_TRACE("GKI_exception %d %s done", code, msg);
-    return;
 }
 
 /*******************************************************************************
@@ -704,7 +684,7 @@ void GKI_exception (UINT16 code, char *msg)
 void GKI_exit_task (UINT8 task_id)
 {
     GKI_disable();
-    gki_cb.com.OSRdyTbl[task_id] = TASK_DEAD;
+    gki_cb.com.task_state[task_id] = TASK_DEAD;
 
     /* Destroy mutex and condition variable objects */
     pthread_mutex_destroy(&gki_cb.os.thread_evt_mutex[task_id]);