Also changes some tests to ensure all memory freed.
#include <stdlib.h>
#include <utils/Log.h>
+#include "allocator.h"
#include "fixed_queue.h"
#include "list.h"
#include "osi.h"
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;
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;
}
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) {
#include <sys/eventfd.h>
#include <utils/Log.h>
+#include "allocator.h"
#include "list.h"
#include "reactor.h"
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;
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) {
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;
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;
}
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|.
if (reactor->object_removed) {
pthread_mutex_destroy(&object->lock);
- free(object);
+ osi_free(object);
}
}
}
#include <sys/eventfd.h>
#include <utils/Log.h>
+#include "allocator.h"
#include "osi.h"
#include "semaphore.h"
};
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) {
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) {
#include <unistd.h>
#include <utils/Log.h>
+#include "allocator.h"
#include "osi.h"
#include "reactor.h"
#include "socket.h"
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;
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;
socket_unregister(socket);
close(socket->fd);
- free(socket);
+ osi_free(socket);
}
bool socket_listen(const socket_t *socket, port_t port) {
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__);
#include <sys/types.h>
#include <utils/Log.h>
+#include "allocator.h"
#include "fixed_queue.h"
#include "reactor.h"
#include "semaphore.h"
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;
fixed_queue_free(ret->work_queue, free);
reactor_free(ret->reactor);
}
- free(ret);
+ osi_free(ret);
return NULL;
}
fixed_queue_free(thread->work_queue, free);
reactor_free(thread->reactor);
- free(thread);
+ osi_free(thread);
}
void thread_join(thread_t *thread) {
// 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;
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;
}
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);
}
#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;
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);
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);
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);
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);
#include <gtest/gtest.h>
+#include "AllocationTestHarness.h"
+
extern "C" {
#include <sys/select.h>
#include <utils/Log.h>
#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);
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);