summaryrefslogtreecommitdiffstats
path: root/libdimension/progress.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-04-07 14:26:15 -0400
committerTavian Barnes <tavianator@gmail.com>2010-04-07 14:34:52 -0400
commit2b087cb45ae91f90492a935625570d7d42ee3ecb (patch)
treea464213b08d04c8c91c8879a84e534f895c84378 /libdimension/progress.c
parent7d6663eeb68bf9d0a3dff86128827c0c1d85df69 (diff)
downloaddimension-2b087cb45ae91f90492a935625570d7d42ee3ecb.tar.xz
New dmnsn_malloc() function, and friends.
I'm tired of checking for malloc failures everywhere, considering it never happens. So just bail out whenever it does. A lot of stuff is guaranteed to succeed if it returns now.
Diffstat (limited to 'libdimension/progress.c')
-rw-r--r--libdimension/progress.c67
1 files changed, 20 insertions, 47 deletions
diff --git a/libdimension/progress.c b/libdimension/progress.c
index 998af8e..bc8830b 100644
--- a/libdimension/progress.c
+++ b/libdimension/progress.c
@@ -21,7 +21,6 @@
#include "dimension.h"
#include <pthread.h>
#include <errno.h>
-#include <stdlib.h> /* For malloc */
/* For thread synchronization */
static void dmnsn_progress_rdlock(const dmnsn_progress *progress);
@@ -32,70 +31,44 @@ static void dmnsn_progress_unlock(const dmnsn_progress *progress);
dmnsn_progress *
dmnsn_new_progress()
{
- dmnsn_progress_element element = { .progress = 0, .total = 1 };
- dmnsn_progress *progress = malloc(sizeof(dmnsn_progress));
- if (progress) {
- progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element));
- dmnsn_array_push(progress->elements, &element);
+ dmnsn_progress *progress = dmnsn_malloc(sizeof(dmnsn_progress));
+ progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element));
- /* Initialize the rwlock, condition variable, and mutex */
+ dmnsn_progress_element element = { .progress = 0, .total = 1 };
+ dmnsn_array_push(progress->elements, &element);
- progress->rwlock = NULL;
- progress->mutex = NULL;
- progress->cond = NULL;
+ /* Initialize the rwlock, condition variable, and mutex */
- progress->rwlock = malloc(sizeof(pthread_rwlock_t));
- if (!progress->rwlock) {
- dmnsn_delete_progress(progress);
- errno = ENOMEM;
- return NULL;
- }
- if (pthread_rwlock_init(progress->rwlock, NULL) != 0) {
- dmnsn_delete_progress(progress);
- return NULL;
- }
+ progress->rwlock = dmnsn_malloc(sizeof(pthread_rwlock_t));
+ if (pthread_rwlock_init(progress->rwlock, NULL) != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize read-write lock.");
+ }
- progress->cond = malloc(sizeof(pthread_cond_t));
- if (!progress->cond) {
- dmnsn_delete_progress(progress);
- errno = ENOMEM;
- return NULL;
- }
- if (pthread_cond_init(progress->cond, NULL) != 0) {
- dmnsn_delete_progress(progress);
- return NULL;
- }
+ progress->cond = dmnsn_malloc(sizeof(pthread_cond_t));
+ if (pthread_cond_init(progress->cond, NULL) != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize condition variable.");
+ }
- progress->mutex = malloc(sizeof(pthread_mutex_t));
- if (!progress->mutex) {
- dmnsn_delete_progress(progress);
- errno = ENOMEM;
- return NULL;
- }
- if (pthread_mutex_init(progress->mutex, NULL) != 0) {
- dmnsn_delete_progress(progress);
- return NULL;
- }
- } else {
- errno = ENOMEM;
+ progress->mutex = dmnsn_malloc(sizeof(pthread_mutex_t));
+ if (pthread_mutex_init(progress->mutex, NULL) != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH, "Couldn't initialize mutex.");
}
return progress;
}
-/* Delete a dmnsn_progress*, which has not yet been associated with a thread;
- mostly for failed returns from *_async() functions. */
+/* Delete a dmnsn_progress*, which has not yet been associated with a thread */
void
dmnsn_delete_progress(dmnsn_progress *progress)
{
if (progress) {
- if (progress->rwlock && pthread_rwlock_destroy(progress->rwlock) != 0) {
+ if (pthread_rwlock_destroy(progress->rwlock) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking rwlock.");
}
- if (progress->mutex && pthread_mutex_destroy(progress->mutex) != 0) {
+ if (pthread_mutex_destroy(progress->mutex) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking mutex.");
}
- if (progress->cond && pthread_cond_destroy(progress->cond) != 0) {
+ if (pthread_cond_destroy(progress->cond) != 0) {
dmnsn_error(DMNSN_SEVERITY_LOW, "Leaking condition variable.");
}