diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/error.c | 27 | ||||
-rw-r--r-- | libdimension/platform.c | 33 | ||||
-rw-r--r-- | libdimension/platform.h | 11 | ||||
-rw-r--r-- | libdimension/png.c | 3 |
4 files changed, 48 insertions, 26 deletions
diff --git a/libdimension/error.c b/libdimension/error.c index 49b1d8d..bc0e3e2 100644 --- a/libdimension/error.c +++ b/libdimension/error.c @@ -18,15 +18,10 @@ * <http://www.gnu.org/licenses/>. * *************************************************************************/ -#define _GNU_SOURCE -#include <unistd.h> -#include <sys/syscall.h> /* For gettid() where supported */ -#include <execinfo.h> /* For backtrace() etc. */ - -#include "dimension.h" +#include "dimension_impl.h" #include <pthread.h> -#include <stdio.h> /* For fprintf() */ -#include <stdlib.h> /* For exit() */ +#include <stdio.h> /* For fprintf() */ +#include <stdlib.h> /* For exit() */ static void dmnsn_default_fatal_error_fn(); static dmnsn_fatal_error_fn *dmnsn_fatal = &dmnsn_default_fatal_error_fn; @@ -139,18 +134,9 @@ void dmnsn_set_fatal_error_fn(dmnsn_fatal_error_fn *fatal) static void dmnsn_default_fatal_error_fn() { - const unsigned int size = 64; - void *buffer[size]; - - int nptrs = backtrace(buffer, size); - /* buffer + 1 to hide this static function */ - backtrace_symbols_fd(buffer + 1, nptrs - 1, STDERR_FILENO); - -#ifdef SYS_gettid - pid_t pid = getpid(), - tid = syscall(SYS_gettid); + dmnsn_backtrace(stderr); - if (pid == tid) { + if (dmnsn_is_main_thread()) { exit(EXIT_FAILURE); } else { int *ret = malloc(sizeof(int)); /* Don't use dmnsn_malloc */ @@ -158,7 +144,4 @@ dmnsn_default_fatal_error_fn() *ret = 1; pthread_exit(ret); } -#else - exit(EXIT_FAILURE); -#endif } diff --git a/libdimension/platform.c b/libdimension/platform.c index 6ac15c0..be7e7a8 100644 --- a/libdimension/platform.c +++ b/libdimension/platform.c @@ -19,8 +19,37 @@ *************************************************************************/ #include "dimension_impl.h" -#include <unistd.h> /* For sysconf() */ -#include <sched.h> /* For sched_getaffinity() */ +#include <unistd.h> /* For sysconf() */ +#include <arpa/inet.h> /* For htonl() */ +#include <execinfo.h> /* For backtrace() etc. */ +#include <sys/syscall.h> /* For gettid() where supported */ +#include <sched.h> /* For sched_getaffinity() */ + +void +dmnsn_backtrace(FILE *file) +{ + const size_t size = 128; + void *buffer[size]; + + int nptrs = backtrace(buffer, size); + backtrace_symbols_fd(buffer, nptrs, fileno(file)); +} + +bool +dmnsn_is_main_thread() +{ +#ifdef SYS_gettid + return getpid() == syscall(SYS_gettid); +#else + return true; +#endif +} + +bool +dmnsn_is_little_endian() +{ + return htonl(1) != 1; +} size_t dmnsn_ncpus() diff --git a/libdimension/platform.h b/libdimension/platform.h index bda83dd..a2bcd39 100644 --- a/libdimension/platform.h +++ b/libdimension/platform.h @@ -21,7 +21,18 @@ #ifndef DIMENSION_IMPL_UTILITIES_H #define DIMENSION_IMPL_UTILITIES_H +#include <stdbool.h> #include <stddef.h> +#include <stdio.h> + +/* Provide a stack trace if possible */ +void dmnsn_backtrace(FILE *file); + +/* Return whether this is the main execution thread, if we can tell */ +bool dmnsn_is_main_thread(); + +/* Return true if we are little-endian */ +bool dmnsn_is_little_endian(); /* Return the number of CPUs available to dimension */ size_t dmnsn_ncpus(); diff --git a/libdimension/png.c b/libdimension/png.c index 9bece58..7c21475 100644 --- a/libdimension/png.c +++ b/libdimension/png.c @@ -21,7 +21,6 @@ #include "dimension_impl.h" #include <pthread.h> #include <png.h> -#include <arpa/inet.h> #include <errno.h> #include <setjmp.h> #include <stdlib.h> @@ -245,7 +244,7 @@ dmnsn_png_write_canvas_thread(void *ptr) /* Write the info struct */ png_write_info(png_ptr, info_ptr); - if (htonl(1) != 1) { + if (dmnsn_is_little_endian()) { /* We are little-endian; swap the byte order of the pixels */ png_set_swap(png_ptr); } |