00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00028 #ifndef _DNXDEBUG_H_
00029 #define _DNXDEBUG_H_
00030
00031 #include "dnxLogging.h"
00032
00033 #include <errno.h>
00034 #include <string.h>
00035 #include <pthread.h>
00036 #include <assert.h>
00037
00038 #if HAVE_CONFIG_H
00039 # include <config.h>
00040 #endif
00041
00042 #if DEBUG_LOCKS && defined(PTHREAD_MUTEX_ERRORCHECK_NP)
00043 extern int pthread_mutexattr_settype(pthread_mutexattr_t * attr, int kind);
00044 # define DNX_PT_MUTEX_INIT(mp) { \
00045 pthread_mutexattr_t attr; \
00046 pthread_mutexattr_init(&attr); \
00047 pthread_mutex_init((mp),&attr); \
00048 pthread_mutexattr_settype((mp), PTHREAD_MUTEX_ERRORCHECK_NP); \
00049 pthread_mutexattr_destroy(&attr); \
00050 dnxDebug(2, "mutex INIT at %s:%d", __FILE__, __LINE__); \
00051 }
00052 # define DNX_PT_MUTEX_DESTROY(mp) \
00053 dnxDebug(2, "mutex DESTROY at %s:%d", __FILE__, __LINE__); \
00054 if (pthread_mutex_destroy(mp) != 0) { \
00055 dnxDebug(1, "mutex_destroy: mutex is in use at %s:%d!", __FILE__, __LINE__); \
00056 assert(0); \
00057 }
00058 # define DNX_PT_MUTEX_LOCK(mp) \
00059 dnxDebug(10, "mutex LOCK at %s:%d", __FILE__, __LINE__); \
00060 if (pthread_mutex_lock(mp) != 0) { \
00061 switch (errno) { \
00062 case EINVAL: dnxDebug(1, "mutex_lock: mutex is not initialized at %s:%d!", __FILE__, __LINE__); break; \
00063 case EDEADLK: dnxDebug(1, "mutex_lock: mutex already locked by this thread at %s:%d!", __FILE__, __LINE__); break; \
00064 default: dnxDebug(1, "mutex_lock: unknown error %d: %s at %s:%d!", errno, strerror(errno), __FILE__, __LINE__); break; \
00065 } assert(0); \
00066 }
00067 # define DNX_PT_MUTEX_UNLOCK(mp) \
00068 dnxDebug(10, "mutex UNLOCK at %s:%d", __FILE__, __LINE__); \
00069 if (pthread_mutex_unlock(mp) != 0) { \
00070 switch (errno) { \
00071 case EINVAL: dnxDebug(1, "mutex_unlock: mutex is not initialized at %s:%d!", __FILE__, __LINE__); break; \
00072 case EPERM: dnxDebug(1, "mutex_unlock: mutex not locked by this thread at %s:%d!", __FILE__, __LINE__); break; \
00073 default: dnxDebug(1, "mutex_unlock: unknown error %d: %s at %s:%d!", errno, strerror(errno), __FILE__, __LINE__); break; \
00074 } assert(0); \
00075 }
00076 #else
00077 # define DNX_PT_MUTEX_INIT(mp) pthread_mutex_init((mp),0)
00078 # define DNX_PT_MUTEX_DESTROY(mp) pthread_mutex_destroy(mp)
00079 # define DNX_PT_MUTEX_LOCK(mp) pthread_mutex_lock(mp)
00080 # define DNX_PT_MUTEX_UNLOCK(mp) pthread_mutex_unlock(mp)
00081 #endif
00082
00083 #if DEBUG_LOCKS
00084 # define DNX_PT_COND_DESTROY(cvp) \
00085 if (pthread_cond_destroy(cvp) != 0) { \
00086 dnxDebug(1, "cond_destroy: condition-variable is in use at %s:%d!", __FILE__, __LINE__); \
00087 assert(0); \
00088 }
00089 #else
00090 # define DNX_PT_COND_DESTROY(cvp) pthread_cond_destroy(cvp)
00091 #endif
00092
00093 #if DEBUG_HEAP
00094 # include "dnxHeap.h"
00095 # define xmalloc(sz) dnxMalloc((sz), __FILE__, __LINE__)
00096 # define xcalloc(n,sz) dnxCalloc((n), (sz), __FILE__, __LINE__)
00097 # define xrealloc(p,sz) dnxRealloc((p), (sz), __FILE__, __LINE__)
00098 # define xstrdup(str) dnxStrdup((str), __FILE__, __LINE__)
00099 # define xfree dnxFree
00100 # define xheapchk dnxCheckHeap
00101 #else
00102 # include <stdlib.h>
00103 # include <string.h>
00104 # define xmalloc malloc
00105 # define xcalloc calloc
00106 # define xrealloc realloc
00107 # define xstrdup strdup
00108 # define xfree free
00109 # define xheapchk()
00110 #endif
00111
00112 #endif
00113