diff options
author | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-07-23 16:55:42 +0200 |
---|---|---|
committer | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-07-23 16:55:42 +0200 |
commit | 271a00fe91f06921ac7d04ecbf30dfc268e36669 (patch) | |
tree | 85076a731eec0617bcbdccd7a6e6de414f2f4986 | |
parent | d3d1871cb7246c07f700b55a73283cbcb7b9ba59 (diff) |
Fix some memory leaks
-rw-r--r-- | src/api/api_device.cpp | 6 | ||||
-rw-r--r-- | src/core/compiler.cpp | 2 | ||||
-rw-r--r-- | src/core/cpu/device.cpp | 27 | ||||
-rw-r--r-- | src/core/cpu/device.h | 4 | ||||
-rw-r--r-- | tests/test_commandqueue.cpp | 1 | ||||
-rw-r--r-- | tests/test_context.cpp | 7 | ||||
-rw-r--r-- | tests/test_mem.cpp | 3 |
7 files changed, 42 insertions, 8 deletions
diff --git a/src/api/api_device.cpp b/src/api/api_device.cpp index 05a8b9d..1aa3a4b 100644 --- a/src/api/api_device.cpp +++ b/src/api/api_device.cpp @@ -1,6 +1,8 @@ #include "CL/cl.h" #include <core/cpu/device.h> +static Coal::CPUDevice cpudevice; + cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, @@ -21,8 +23,10 @@ clGetDeviceIDs(cl_platform_id platform, // We currently implement only CPU-based acceleration if (device_type & (CL_DEVICE_TYPE_DEFAULT | CL_DEVICE_TYPE_CPU)) { + cpudevice.init(); + if (devices) - devices[0] = (cl_device_id)(new Coal::CPUDevice()); + devices[0] = (cl_device_id)(&cpudevice); if (num_devices) *num_devices = 1; diff --git a/src/core/compiler.cpp b/src/core/compiler.cpp index b1be9cb..138d680 100644 --- a/src/core/compiler.cpp +++ b/src/core/compiler.cpp @@ -163,7 +163,7 @@ bool Compiler::compile(const std::string &options, // Compile llvm::OwningPtr<clang::CodeGenAction> act( - new clang::EmitLLVMOnlyAction(new llvm::LLVMContext) + new clang::EmitLLVMOnlyAction(&llvm::getGlobalContext()) ); if (!p_compiler.ExecuteAction(*act)) diff --git a/src/core/cpu/device.cpp b/src/core/cpu/device.cpp index 8e5dd87..eb18ec3 100644 --- a/src/core/cpu/device.cpp +++ b/src/core/cpu/device.cpp @@ -24,23 +24,36 @@ using namespace Coal; CPUDevice::CPUDevice() : DeviceInterface(), p_cores(0), p_workers(0), p_stop(false), - p_num_events(0) + p_num_events(0), p_initialized(false) { + +} + +void CPUDevice::init() +{ + if (p_initialized) + return; + // Initialize the locking machinery pthread_cond_init(&p_events_cond, 0); pthread_mutex_init(&p_events_mutex, 0); - // Create 4 worker threads + // Create worker threads p_workers = (pthread_t *)std::malloc(numCPUs() * sizeof(pthread_t)); for (int i=0; i<numCPUs(); ++i) { pthread_create(&p_workers[i], 0, &worker, this); } + + p_initialized = true; } CPUDevice::~CPUDevice() { + if (!p_initialized) + return; + // Terminate the workers and wait for them pthread_mutex_lock(&p_events_mutex); @@ -152,9 +165,16 @@ Event *CPUDevice::getEvent(bool &stop) // single-shot event. pthread_mutex_lock(&p_events_mutex); - while (p_num_events == 0) + while (p_num_events == 0 && !p_stop) pthread_cond_wait(&p_events_cond, &p_events_mutex); + if (p_stop) + { + pthread_mutex_unlock(&p_events_mutex); + stop = true; + return 0; + } + Event *event = p_events.front(); // If the run of this event will finish it, remove it from the list @@ -175,7 +195,6 @@ Event *CPUDevice::getEvent(bool &stop) pthread_mutex_unlock(&p_events_mutex); - stop = false; return event; } diff --git a/src/core/cpu/device.h b/src/core/cpu/device.h index c8807a1..7869476 100644 --- a/src/core/cpu/device.h +++ b/src/core/cpu/device.h @@ -20,6 +20,8 @@ class CPUDevice : public DeviceInterface CPUDevice(); ~CPUDevice(); + void init(); + cl_int info(cl_device_info param_name, size_t param_value_size, void *param_value, @@ -46,7 +48,7 @@ class CPUDevice : public DeviceInterface std::list<Event *> p_events; pthread_cond_t p_events_cond; pthread_mutex_t p_events_mutex; - bool p_stop; + bool p_stop, p_initialized; }; } diff --git a/tests/test_commandqueue.cpp b/tests/test_commandqueue.cpp index aea146e..589430b 100644 --- a/tests/test_commandqueue.cpp +++ b/tests/test_commandqueue.cpp @@ -336,6 +336,7 @@ START_TEST (test_events) clReleaseEvent(write_event); clReleaseEvent(user_event); + clReleaseMemObject(buf); clReleaseCommandQueue(queue); clReleaseContext(ctx); } diff --git a/tests/test_context.cpp b/tests/test_context.cpp index 22ae725..4273a8a 100644 --- a/tests/test_context.cpp +++ b/tests/test_context.cpp @@ -84,6 +84,8 @@ START_TEST (test_create_context) ctx == 0, "errcode_ret can be NULL" ); + + clReleaseContext(ctx); } END_TEST @@ -97,6 +99,8 @@ START_TEST (test_create_context_from_type) result != CL_SUCCESS || ctx == 0, "unable to create a valid context with a device of type default" ); + + clReleaseContext(ctx); } END_TEST @@ -208,6 +212,9 @@ START_TEST (test_get_context_info) context_info.properties.prop_platform != CL_CONTEXT_PLATFORM, "this context must have a valid CL_CONTEXT_PLATFORM property" ); + + clReleaseContext(ctx); + clReleaseContext(ctx); } END_TEST diff --git a/tests/test_mem.cpp b/tests/test_mem.cpp index fee9a2b..b1fd477 100644 --- a/tests/test_mem.cpp +++ b/tests/test_mem.cpp @@ -138,7 +138,7 @@ START_TEST (test_create_sub_buffer) "cannot create a valid sub-buffer" ); - subbuf = clCreateSubBuffer(subbuf, CL_MEM_WRITE_ONLY, + clCreateSubBuffer(subbuf, CL_MEM_WRITE_ONLY, CL_BUFFER_CREATE_TYPE_REGION, (void *)&create_info, &result); fail_if( @@ -240,6 +240,7 @@ START_TEST (test_read_write_subbuf) "the buffer must contain \"Hello, world !\"" ); + clReleaseCommandQueue(queue); clReleaseMemObject(subbuf); clReleaseMemObject(buf); clReleaseContext(ctx); |