summaryrefslogtreecommitdiffstats
path: root/libdimension/progress.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-10-26 00:25:21 -0400
committerTavian Barnes <tavianator@gmail.com>2009-10-26 00:25:21 -0400
commitc6612fb215d71ac2bea3b614786cf585cd1a6c74 (patch)
treefa8a4d6eb0bc83ba112b8c1b03b212641669c002 /libdimension/progress.c
parente4e3ea4fd58cbbe462368abde728f9443f01de19 (diff)
downloaddimension-c6612fb215d71ac2bea3b614786cf585cd1a6c74.tar.xz
Clean up some dmnsn_new_*() functions.
Rather than special-case every failed memory allocation, just make dmnsn_delete_*() more robust and call it.
Diffstat (limited to 'libdimension/progress.c')
-rw-r--r--libdimension/progress.c70
1 files changed, 21 insertions, 49 deletions
diff --git a/libdimension/progress.c b/libdimension/progress.c
index 18c462f..b466111 100644
--- a/libdimension/progress.c
+++ b/libdimension/progress.c
@@ -37,71 +37,43 @@ dmnsn_new_progress()
progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element));
dmnsn_array_push(progress->elements, &element);
- /* Allocate space for the rwlock, condition variable, and mutex */
+ /* Initialize the rwlock, condition variable, and mutex */
+
+ progress->rwlock = NULL;
+ progress->mutex = NULL;
+ progress->cond = NULL;
progress->rwlock = malloc(sizeof(pthread_rwlock_t));
if (!progress->rwlock) {
- dmnsn_delete_array(progress->elements);
- free(progress);
+ dmnsn_delete_progress(progress);
+ return NULL;
+ }
+ if (pthread_rwlock_init(progress->rwlock, NULL) != 0) {
+ dmnsn_delete_progress(progress);
return NULL;
}
progress->cond = malloc(sizeof(pthread_cond_t));
if (!progress->cond) {
- free(progress->rwlock);
- dmnsn_delete_array(progress->elements);
- free(progress);
+ dmnsn_delete_progress(progress);
return NULL;
}
-
- progress->mutex = malloc(sizeof(pthread_mutex_t));
- if (!progress->mutex) {
- free(progress->rwlock);
- free(progress->cond);
- dmnsn_delete_array(progress->elements);
- free(progress);
+ if (pthread_cond_init(progress->cond, NULL) != 0) {
+ dmnsn_delete_progress(progress);
return NULL;
}
- /* Initialize the rwlock, condition variable, and mutex */
-
- if (pthread_rwlock_init(progress->rwlock, NULL) != 0) {
- free(progress->rwlock);
- free(progress->mutex);
- free(progress->cond);
- dmnsn_delete_array(progress->elements);
- free(progress);
- return NULL;
- }
- if (pthread_cond_init(progress->cond, NULL) != 0) {
- if (pthread_rwlock_destroy(progress->rwlock) != 0) {
- dmnsn_error(DMNSN_SEVERITY_LOW,
- "Leaking rwlock in failed allocation.");
- }
- free(progress->rwlock);
- free(progress->mutex);
- free(progress->cond);
- dmnsn_delete_array(progress->elements);
- free(progress);
+ progress->mutex = malloc(sizeof(pthread_mutex_t));
+ if (!progress->mutex) {
+ dmnsn_delete_progress(progress);
return NULL;
}
if (pthread_mutex_init(progress->mutex, NULL) != 0) {
- if (pthread_rwlock_destroy(progress->rwlock) != 0) {
- dmnsn_error(DMNSN_SEVERITY_LOW,
- "Leaking rwlock in failed allocation.");
- }
- if (pthread_cond_destroy(progress->cond) != 0) {
- dmnsn_error(DMNSN_SEVERITY_LOW,
- "Leaking condition variable in failed allocation.");
- }
- free(progress->rwlock);
- free(progress->mutex);
- free(progress->cond);
- dmnsn_delete_array(progress->elements);
- free(progress);
+ dmnsn_delete_progress(progress);
return NULL;
}
}
+
return progress;
}
@@ -111,13 +83,13 @@ void
dmnsn_delete_progress(dmnsn_progress *progress)
{
if (progress) {
- if (pthread_rwlock_destroy(progress->rwlock) != 0) {
+ if (progress->rwlock && pthread_rwlock_destroy(progress->rwlock) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking rwlock.");
}
- if (pthread_mutex_destroy(progress->mutex) != 0) {
+ if (progress->mutex && pthread_mutex_destroy(progress->mutex) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking mutex.");
}
- if (pthread_cond_destroy(progress->cond) != 0) {
+ if (progress->cond && pthread_cond_destroy(progress->cond) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking condition variable.");
}