From 1874938c0d35990f95e494a7fef066bf97001d8f Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 29 Jun 2010 21:17:39 -0600 Subject: Move platform-specific stuff in libdimension into platform.c. --- libdimension/error.c | 27 +++++---------------------- libdimension/platform.c | 33 +++++++++++++++++++++++++++++++-- libdimension/platform.h | 11 +++++++++++ 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 @@ * . * *************************************************************************/ -#define _GNU_SOURCE -#include -#include /* For gettid() where supported */ -#include /* For backtrace() etc. */ - -#include "dimension.h" +#include "dimension_impl.h" #include -#include /* For fprintf() */ -#include /* For exit() */ +#include /* For fprintf() */ +#include /* 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 /* For sysconf() */ -#include /* For sched_getaffinity() */ +#include /* For sysconf() */ +#include /* For htonl() */ +#include /* For backtrace() etc. */ +#include /* For gettid() where supported */ +#include /* 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 #include +#include + +/* 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 #include -#include #include #include #include @@ -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); } -- cgit v1.2.3