summaryrefslogtreecommitdiff
path: root/osi
diff options
context:
space:
mode:
authorPavlin Radoslavov <pavlin@google.com>2016-02-02 18:12:08 -0800
committerAndre Eisenbach <eisenbach@google.com>2016-02-04 23:15:46 +0000
commit20524d393e8b3bea4c573f7980cd843500b0e6a4 (patch)
treec22a757569544e8fc9ad4d47af44f3f4069bc3ec /osi
parent1910a80f533ece8669e5f62175422365f8abf270 (diff)
downloadandroid-system-bt-20524d393e8b3bea4c573f7980cd843500b0e6a4.tar.gz
android-system-bt-20524d393e8b3bea4c573f7980cd843500b0e6a4.tar.xz
Refactor usage of osi_free() and osi_freebuf()
* Allow to call osi_freebuf(ptr) on NULL pointers. This simplifies the code: a notable number of "if (foo != NULL)" checks are removed. * Add new function osi_free_and_reset(p_ptr) that frees the buffer, and explicitly resets the pointer to NULL. This prevents unintended usage of free memory. * Add corresponding function osi_freebuf_and_reset(p_ptr) * Minor cleanup around usages of osi_free() and osi_freebuf() Also: * Removed unused function btif_gattc_cleanup() * Replaced usage of the following functions with osi_freebuf_and_reset() - mca_free_buf() - utl_freebuf() - btif_hl_free_buf() * Replaced usage of rc_supported_event_free() with osi_freebuf() * Replaced usage of btif_hl_get_buf() with osi_getbuf() * Eliminate some of the osi_get_buf_size() calls Bug: 22948224 Change-Id: Ife860658b26274da6f228d7353cb0f1531587337
Diffstat (limited to 'osi')
-rw-r--r--osi/include/allocator.h18
-rw-r--r--osi/src/allocator.c17
-rw-r--r--osi/src/data_dispatcher.c5
-rw-r--r--osi/src/wakelock.c12
4 files changed, 38 insertions, 14 deletions
diff --git a/osi/include/allocator.h b/osi/include/allocator.h
index c9e26de..97b700d 100644
--- a/osi/include/allocator.h
+++ b/osi/include/allocator.h
@@ -41,9 +41,16 @@ void *osi_malloc(size_t size);
void *osi_calloc(size_t size);
void osi_free(void *ptr);
+// Free a buffer that was previously allocated with function |osi_malloc|
+// or |osi_calloc| and reset the pointer to that buffer to NULL.
+// |p_ptr| is a pointer to the buffer pointer to be reset.
+// |p_ptr| cannot be NULL.
+void osi_free_and_reset(void **p_ptr);
+
//
-// TODO: Functions osi_getbuf(), osi_freebuf() and osi_get_buf_size() below
-// should be removed.
+// TODO: Function osi_get_buf_size() should be removed.
+// The rest of the functions below should be removed and replaced with the
+// corresponding functions above.
//
// Allocate a buffer of size |size|. Return the allocated buffer if there
@@ -51,7 +58,14 @@ void osi_free(void *ptr);
void *osi_getbuf(uint16_t size);
// Free a buffer that was previously allocated with function |osi_getbuf|.
+// |ptr| can be NULL.
void osi_freebuf(void *ptr);
+// Free a buffer that was previously allocated with function |osi_getbuf|
+// and reset the pointer to that buffer to NULL.
+// |p_ptr| is a pointer to the buffer pointer to be reset.
+// |p_ptr| cannot be NULL.
+void osi_freebuf_and_reset(void **p_ptr);
+
// Get the size of the buffer previously allocated with function |osi_getbuf|
uint16_t osi_get_buf_size(void *ptr);
diff --git a/osi/src/allocator.c b/osi/src/allocator.c
index 3fbe4c8..ed29f9c 100644
--- a/osi/src/allocator.c
+++ b/osi/src/allocator.c
@@ -80,6 +80,13 @@ void osi_free(void *ptr) {
free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
}
+void osi_free_and_reset(void **p_ptr)
+{
+ assert(p_ptr != NULL);
+ osi_free(*p_ptr);
+ *p_ptr = NULL;
+}
+
const allocator_t allocator_calloc = {
osi_calloc,
osi_free
@@ -110,11 +117,21 @@ void *osi_getbuf(uint16_t size)
void osi_freebuf(void *p_buf)
{
+ if (p_buf == NULL)
+ return;
+
BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1;
assert(header->magic_number == MAGIC_NUMBER);
osi_free(header);
}
+void osi_freebuf_and_reset(void **p_ptr)
+{
+ assert(p_ptr != NULL);
+ osi_freebuf(*p_ptr);
+ *p_ptr = NULL;
+}
+
uint16_t osi_get_buf_size(void *p_buf)
{
BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1;
diff --git a/osi/src/data_dispatcher.c b/osi/src/data_dispatcher.c
index 659769b..ebbd674 100644
--- a/osi/src/data_dispatcher.c
+++ b/osi/src/data_dispatcher.c
@@ -69,10 +69,7 @@ void data_dispatcher_free(data_dispatcher_t *dispatcher) {
return;
hash_map_free(dispatcher->dispatch_table);
-
- if (dispatcher->name)
- osi_free(dispatcher->name);
-
+ osi_free(dispatcher->name);
osi_free(dispatcher);
}
diff --git a/osi/src/wakelock.c b/osi/src/wakelock.c
index dd8b8f7..e67f62e 100644
--- a/osi/src/wakelock.c
+++ b/osi/src/wakelock.c
@@ -203,15 +203,11 @@ static void wakelock_initialize_native(void) {
}
void wakelock_cleanup(void) {
- if (wake_lock_path && wake_lock_path != DEFAULT_WAKE_LOCK_PATH) {
- osi_free(wake_lock_path);
- wake_lock_path = NULL;
- }
+ if (wake_lock_path && wake_lock_path != DEFAULT_WAKE_LOCK_PATH)
+ osi_free_and_reset((void **)&wake_lock_path);
- if (wake_unlock_path && wake_unlock_path != DEFAULT_WAKE_UNLOCK_PATH) {
- osi_free(wake_unlock_path);
- wake_unlock_path = NULL;
- }
+ if (wake_unlock_path && wake_unlock_path != DEFAULT_WAKE_UNLOCK_PATH)
+ osi_free_and_reset((void **)&wake_unlock_path);
initialized = PTHREAD_ONCE_INIT;
pthread_mutex_destroy(&monitor);