OSDN Git Service

Move some of osi to use the allocation interfaces
authorZach Johnson <zachoverflow@google.com>
Tue, 26 Aug 2014 06:22:24 +0000 (23:22 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 16 Mar 2015 23:51:30 +0000 (16:51 -0700)
Also changes some tests to ensure all memory freed.

osi/src/fixed_queue.c
osi/src/reactor.c
osi/src/semaphore.c
osi/src/socket.c
osi/src/thread.c
osi/test/reactor_test.cpp
osi/test/thread_test.cpp

index 7db687a..b80f2f2 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <utils/Log.h>
 
+#include "allocator.h"
 #include "fixed_queue.h"
 #include "list.h"
 #include "osi.h"
@@ -42,10 +43,13 @@ typedef struct fixed_queue_t {
 static void internal_dequeue_ready(void *context);
 
 fixed_queue_t *fixed_queue_new(size_t capacity) {
-  fixed_queue_t *ret = calloc(1, sizeof(fixed_queue_t));
+  fixed_queue_t *ret = osi_calloc(sizeof(fixed_queue_t));
   if (!ret)
     goto error;
 
+  pthread_mutex_init(&ret->lock, NULL);
+  ret->capacity = capacity;
+
   ret->list = list_new(NULL);
   if (!ret->list)
     goto error;
@@ -58,19 +62,10 @@ fixed_queue_t *fixed_queue_new(size_t capacity) {
   if (!ret->dequeue_sem)
     goto error;
 
-  pthread_mutex_init(&ret->lock, NULL);
-  ret->capacity = capacity;
-
   return ret;
 
 error:;
-  if (ret) {
-    list_free(ret->list);
-    semaphore_free(ret->enqueue_sem);
-    semaphore_free(ret->dequeue_sem);
-  }
-
-  free(ret);
+  fixed_queue_free(ret, NULL);
   return NULL;
 }
 
@@ -88,7 +83,7 @@ void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb) {
   semaphore_free(queue->enqueue_sem);
   semaphore_free(queue->dequeue_sem);
   pthread_mutex_destroy(&queue->lock);
-  free(queue);
+  osi_free(queue);
 }
 
 bool fixed_queue_is_empty(fixed_queue_t *queue) {
index eb88f75..6340adb 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/eventfd.h>
 #include <utils/Log.h>
 
+#include "allocator.h"
 #include "list.h"
 #include "reactor.h"
 
@@ -59,7 +60,7 @@ static const size_t MAX_EVENTS = 64;
 static const eventfd_t EVENT_REACTOR_STOP = 1;
 
 reactor_t *reactor_new(void) {
-  reactor_t *ret = (reactor_t *)calloc(1, sizeof(reactor_t));
+  reactor_t *ret = (reactor_t *)osi_calloc(sizeof(reactor_t));
   if (!ret)
     return NULL;
 
@@ -107,7 +108,7 @@ void reactor_free(reactor_t *reactor) {
   list_free(reactor->invalidation_list);
   close(reactor->event_fd);
   close(reactor->epoll_fd);
-  free(reactor);
+  osi_free(reactor);
 }
 
 reactor_status_t reactor_start(reactor_t *reactor) {
@@ -133,7 +134,7 @@ reactor_object_t *reactor_register(reactor_t *reactor,
   assert(reactor != NULL);
   assert(fd != INVALID_FD);
 
-  reactor_object_t *object = (reactor_object_t *)calloc(1, sizeof(reactor_object_t));
+  reactor_object_t *object = (reactor_object_t *)osi_calloc(sizeof(reactor_object_t));
   if (!object) {
     ALOGE("%s unable to allocate reactor object: %s", __func__, strerror(errno));
     return NULL;
@@ -157,7 +158,7 @@ reactor_object_t *reactor_register(reactor_t *reactor,
   if (epoll_ctl(reactor->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1) {
     ALOGE("%s unable to register fd %d to epoll set: %s", __func__, fd, strerror(errno));
     pthread_mutex_destroy(&object->lock);
-    free(object);
+    osi_free(object);
     return NULL;
   }
 
@@ -218,7 +219,7 @@ void reactor_unregister(reactor_object_t *obj) {
   pthread_mutex_lock(&obj->lock);
   pthread_mutex_unlock(&obj->lock);
   pthread_mutex_destroy(&obj->lock);
-  free(obj);
+  osi_free(obj);
 }
 
 // Runs the reactor loop for a maximum of |iterations|.
@@ -279,7 +280,7 @@ static reactor_status_t run_reactor(reactor_t *reactor, int iterations) {
 
       if (reactor->object_removed) {
         pthread_mutex_destroy(&object->lock);
-        free(object);
+        osi_free(object);
       }
     }
   }
index be4373c..123469f 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/eventfd.h>
 #include <utils/Log.h>
 
+#include "allocator.h"
 #include "osi.h"
 #include "semaphore.h"
 
@@ -37,7 +38,7 @@ struct semaphore_t {
 };
 
 semaphore_t *semaphore_new(unsigned int value) {
-  semaphore_t *ret = malloc(sizeof(semaphore_t));
+  semaphore_t *ret = osi_malloc(sizeof(semaphore_t));
   if (ret) {
     ret->fd = eventfd(value, EFD_SEMAPHORE);
     if (ret->fd == INVALID_FD) {
@@ -52,7 +53,7 @@ semaphore_t *semaphore_new(unsigned int value) {
 void semaphore_free(semaphore_t *semaphore) {
   if (semaphore->fd != INVALID_FD)
     close(semaphore->fd);
-  free(semaphore);
+  osi_free(semaphore);
 }
 
 void semaphore_wait(semaphore_t *semaphore) {
index 9a7e1ab..687a670 100644 (file)
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <utils/Log.h>
 
+#include "allocator.h"
 #include "osi.h"
 #include "reactor.h"
 #include "socket.h"
@@ -43,7 +44,7 @@ static void internal_read_ready(void *context);
 static void internal_write_ready(void *context);
 
 socket_t *socket_new(void) {
-  socket_t *ret = (socket_t *)calloc(1, sizeof(socket_t));
+  socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t));
   if (!ret) {
     ALOGE("%s unable to allocate memory for socket.", __func__);
     goto error;
@@ -66,14 +67,14 @@ socket_t *socket_new(void) {
 error:;
   if (ret)
     close(ret->fd);
-  free(ret);
+  osi_free(ret);
   return NULL;
 }
 
 socket_t *socket_new_from_fd(int fd) {
   assert(fd != INVALID_FD);
 
-  socket_t *ret = (socket_t *)calloc(1, sizeof(socket_t));
+  socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t));
   if (!ret) {
     ALOGE("%s unable to allocate memory for socket.", __func__);
     return NULL;
@@ -89,7 +90,7 @@ void socket_free(socket_t *socket) {
 
   socket_unregister(socket);
   close(socket->fd);
-  free(socket);
+  osi_free(socket);
 }
 
 bool socket_listen(const socket_t *socket, port_t port) {
@@ -121,7 +122,7 @@ socket_t *socket_accept(const socket_t *socket) {
     return NULL;
   }
 
-  socket_t *ret = (socket_t *)calloc(1, sizeof(socket_t));
+  socket_t *ret = (socket_t *)osi_calloc(sizeof(socket_t));
   if (!ret) {
     close(fd);
     ALOGE("%s unable to allocate memory for socket.", __func__);
index b33c40d..15cb2fe 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <utils/Log.h>
 
+#include "allocator.h"
 #include "fixed_queue.h"
 #include "reactor.h"
 #include "semaphore.h"
@@ -60,7 +61,7 @@ thread_t *thread_new_sized(const char *name, size_t work_queue_capacity) {
   assert(name != NULL);
   assert(work_queue_capacity != 0);
 
-  thread_t *ret = calloc(1, sizeof(thread_t));
+  thread_t *ret = osi_calloc(sizeof(thread_t));
   if (!ret)
     goto error;
 
@@ -95,7 +96,7 @@ error:;
     fixed_queue_free(ret->work_queue, free);
     reactor_free(ret->reactor);
   }
-  free(ret);
+  osi_free(ret);
   return NULL;
 }
 
@@ -112,7 +113,7 @@ void thread_free(thread_t *thread) {
 
   fixed_queue_free(thread->work_queue, free);
   reactor_free(thread->reactor);
-  free(thread);
+  osi_free(thread);
 }
 
 void thread_join(thread_t *thread) {
@@ -135,7 +136,7 @@ bool thread_post(thread_t *thread, thread_fn func, void *context) {
 
   // Queue item is freed either when the queue itself is destroyed
   // or when the item is removed from the queue for dispatch.
-  work_item_t *item = (work_item_t *)malloc(sizeof(work_item_t));
+  work_item_t *item = (work_item_t *)osi_malloc(sizeof(work_item_t));
   if (!item) {
     ALOGE("%s unable to allocate memory: %s", __func__, strerror(errno));
     return false;
@@ -198,7 +199,7 @@ static void *run_thread(void *start_arg) {
   work_item_t *item = fixed_queue_try_dequeue(thread->work_queue);
   while (item && count <= fixed_queue_capacity(thread->work_queue)) {
     item->func(item->context);
-    free(item);
+    osi_free(item);
     item = fixed_queue_try_dequeue(thread->work_queue);
     ++count;
   }
@@ -215,5 +216,5 @@ static void work_queue_read_cb(void *context) {
   fixed_queue_t *queue = (fixed_queue_t *)context;
   work_item_t *item = fixed_queue_dequeue(queue);
   item->func(item->context);
-  free(item);
+  osi_free(item);
 }
index 7959b98..6e3a009 100644 (file)
@@ -4,10 +4,14 @@
 #include <sys/time.h>
 #include <unistd.h>
 
+#include "AllocationTestHarness.h"
+
 extern "C" {
 #include "reactor.h"
 }
 
+class ReactorTest : public AllocationTestHarness {};
+
 static pthread_t thread;
 static volatile bool thread_running;
 
@@ -30,24 +34,24 @@ static void join_reactor_thread() {
   pthread_join(thread, NULL);
 }
 
-TEST(ReactorTest, reactor_new) {
+TEST_F(ReactorTest, reactor_new) {
   reactor_t *reactor = reactor_new();
   EXPECT_TRUE(reactor != NULL);
   reactor_free(reactor);
 }
 
-TEST(ReactorTest, reactor_free_null) {
+TEST_F(ReactorTest, reactor_free_null) {
   reactor_free(NULL);
 }
 
-TEST(ReactorTest, reactor_stop_start) {
+TEST_F(ReactorTest, reactor_stop_start) {
   reactor_t *reactor = reactor_new();
   reactor_stop(reactor);
   reactor_start(reactor);
   reactor_free(reactor);
 }
 
-TEST(ReactorTest, reactor_repeated_stop_start) {
+TEST_F(ReactorTest, reactor_repeated_stop_start) {
   reactor_t *reactor = reactor_new();
   for (int i = 0; i < 10; ++i) {
     reactor_stop(reactor);
@@ -56,7 +60,7 @@ TEST(ReactorTest, reactor_repeated_stop_start) {
   reactor_free(reactor);
 }
 
-TEST(ReactorTest, reactor_start_wait_stop) {
+TEST_F(ReactorTest, reactor_start_wait_stop) {
   reactor_t *reactor = reactor_new();
 
   spawn_reactor_thread(reactor);
@@ -81,7 +85,7 @@ static void unregister_cb(void *context) {
   reactor_stop(arg->reactor);
 }
 
-TEST(ReactorTest, reactor_unregister_from_callback) {
+TEST_F(ReactorTest, reactor_unregister_from_callback) {
   reactor_t *reactor = reactor_new();
 
   int fd = eventfd(0, 0);
@@ -97,7 +101,7 @@ TEST(ReactorTest, reactor_unregister_from_callback) {
   reactor_free(reactor);
 }
 
-TEST(ReactorTest, reactor_unregister_from_separate_thread) {
+TEST_F(ReactorTest, reactor_unregister_from_separate_thread) {
   reactor_t *reactor = reactor_new();
 
   int fd = eventfd(0, 0);
index 32f8c28..d692f5a 100644 (file)
@@ -1,5 +1,7 @@
 #include <gtest/gtest.h>
 
+#include "AllocationTestHarness.h"
+
 extern "C" {
 #include <sys/select.h>
 #include <utils/Log.h>
@@ -10,30 +12,32 @@ extern "C" {
 #include "osi.h"
 }
 
-TEST(ThreadTest, test_new_simple) {
+class ThreadTest : public AllocationTestHarness {};
+
+TEST_F(ThreadTest, test_new_simple) {
   thread_t *thread = thread_new("test_thread");
   ASSERT_TRUE(thread != NULL);
   thread_free(thread);
 }
 
-TEST(ThreadTest, test_free_simple) {
+TEST_F(ThreadTest, test_free_simple) {
   thread_t *thread = thread_new("test_thread");
   thread_free(thread);
 }
 
-TEST(ThreadTest, test_name) {
+TEST_F(ThreadTest, test_name) {
   thread_t *thread = thread_new("test_name");
   ASSERT_STREQ(thread_name(thread), "test_name");
   thread_free(thread);
 }
 
-TEST(ThreadTest, test_long_name) {
+TEST_F(ThreadTest, test_long_name) {
   thread_t *thread = thread_new("0123456789abcdef");
   ASSERT_STREQ("0123456789abcdef", thread_name(thread));
   thread_free(thread);
 }
 
-TEST(ThreadTest, test_very_long_name) {
+TEST_F(ThreadTest, test_very_long_name) {
   thread_t *thread = thread_new("0123456789abcdefg");
   ASSERT_STREQ("0123456789abcdef", thread_name(thread));
   thread_free(thread);
@@ -44,13 +48,13 @@ static void thread_is_self_fn(void *context) {
   EXPECT_TRUE(thread_is_self(thread));
 }
 
-TEST(ThreadTest, test_thread_is_self) {
+TEST_F(ThreadTest, test_thread_is_self) {
   thread_t *thread = thread_new("test_thread");
   thread_post(thread, thread_is_self_fn, thread);
   thread_free(thread);
 }
 
-TEST(ThreadTest, test_thread_is_not_self) {
+TEST_F(ThreadTest, test_thread_is_not_self) {
   thread_t *thread = thread_new("test_thread");
   EXPECT_FALSE(thread_is_self(thread));
   thread_free(thread);