Update the usage of fixed_queue.
* Relax non-NULL requirements for fixed_queue by eliminating
some of the asserts.
Now, when semantically possible, fixed_queue - related function
will return the appropriate value even if the queue pointer is NULL.
This reduces clutter in the code where we had to do anyway
"if (queue != NULL)" checks.
* Add non-NULL guards in the few remaining places where
fixed_queue_get_list(). For now, we need to use this function,
hence the extra check.
That function should be eliminated in the future, because all the
code where it is used violates the semantics of using a queue.
Bug: 24723840
Change-Id: I47632a3515f3d27856d4870e18723d345c040d64
diff --git a/osi/include/fixed_queue.h b/osi/include/fixed_queue.h
index 6902d6b..bb066f8 100644
--- a/osi/include/fixed_queue.h
+++ b/osi/include/fixed_queue.h
@@ -45,11 +45,12 @@
// blocked on it) results in undefined behaviour.
void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
-// Returns a value indicating whether the given |queue| is empty. |queue| may
-// not be NULL.
+// Returns a value indicating whether the given |queue| is empty. If |queue|
+// is NULL, the return value is true.
bool fixed_queue_is_empty(fixed_queue_t *queue);
-// Returns the length of the |queue|. |queue| may not be NULL.
+// Returns the length of the |queue|. If |queue| is NULL, the return value
+// is 0.
size_t fixed_queue_length(fixed_queue_t *queue);
// Returns the maximum number of elements this queue may hold. |queue| may
@@ -73,19 +74,18 @@
bool fixed_queue_try_enqueue(fixed_queue_t *queue, void *data);
// Tries to dequeue an element from |queue|. This function will never block
-// the caller. If the queue is empty, this function returns NULL immediately.
-// Otherwise, the next element in the queue is returned. |queue| may not be
-// NULL.
+// the caller. If the queue is empty or NULL, this function returns NULL
+// immediately. Otherwise, the next element in the queue is returned.
void *fixed_queue_try_dequeue(fixed_queue_t *queue);
// Returns the first element from |queue|, if present, without dequeuing it.
// This function will never block the caller. Returns NULL if there are no
-// elements in the queue. |queue| may not be NULL.
+// elements in the queue or |queue| is NULL.
void *fixed_queue_try_peek_first(fixed_queue_t *queue);
// Returns the last element from |queue|, if present, without dequeuing it.
// This function will never block the caller. Returns NULL if there are no
-// elements in the queue. |queue| may not be NULL.
+// elements in the queue or |queue| is NULL.
void *fixed_queue_try_peek_last(fixed_queue_t *queue);
// Tries to remove a |data| element from the middle of the |queue|. This
diff --git a/osi/src/fixed_queue.c b/osi/src/fixed_queue.c
index 03a252c..5db43a5 100644
--- a/osi/src/fixed_queue.c
+++ b/osi/src/fixed_queue.c
@@ -116,7 +116,8 @@
}
bool fixed_queue_is_empty(fixed_queue_t *queue) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return true;
pthread_mutex_lock(&queue->lock);
bool is_empty = list_is_empty(queue->list);
@@ -126,7 +127,8 @@
}
size_t fixed_queue_length(fixed_queue_t *queue) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return 0;
pthread_mutex_lock(&queue->lock);
size_t length = list_length(queue->list);
@@ -185,7 +187,8 @@
}
void *fixed_queue_try_dequeue(fixed_queue_t *queue) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return NULL;
if (!semaphore_try_wait(queue->dequeue_sem))
return NULL;
@@ -201,7 +204,8 @@
}
void *fixed_queue_try_peek_first(fixed_queue_t *queue) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return NULL;
pthread_mutex_lock(&queue->lock);
void *ret = list_is_empty(queue->list) ? NULL : list_front(queue->list);
@@ -211,7 +215,8 @@
}
void *fixed_queue_try_peek_last(fixed_queue_t *queue) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return NULL;
pthread_mutex_lock(&queue->lock);
void *ret = list_is_empty(queue->list) ? NULL : list_back(queue->list);
@@ -221,7 +226,8 @@
}
void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data) {
- assert(queue != NULL);
+ if (queue == NULL)
+ return NULL;
pthread_mutex_lock(&queue->lock);
bool removed = list_remove(queue->list, data);
@@ -236,7 +242,7 @@
assert(queue != NULL);
// NOTE: This function is not thread safe, and there is no point for
- // callint pthread_mutex_lock() / pthread_mutex_unlock()
+ // calling pthread_mutex_lock() / pthread_mutex_unlock()
return queue->list;
}