diff --git a/Makefile.am b/Makefile.am index 239ca2c..1bb7373 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,9 @@ lib_LTLIBRARIES = libumem.la +noinst_PROGRAMS = umem_test + +umem_test_SOURCES = umem_test.c +umem_test_LDADD = -lumem -lpthread -ldl + libumem_la_SOURCES = init_lib.c \ umem_agent_support.c \ umem_fail.c \ @@ -6,13 +11,14 @@ libumem_la_SOURCES = init_lib.c \ umem_update_thread.c \ vmem_mmap.c \ vmem_sbrk.c \ - envvar.o \ - getpcstack.o \ - malloc.o \ - misc.o \ - vmem_base.o \ - umem.o \ - vmem.o + envvar.c \ + getpcstack.c \ + misc.c \ + vmem_base.c \ + umem.c \ + vmem.c + +# malloc.c \ # XXX: Standalone version? # See diff --git a/misc.c b/misc.c index 0097053..aa4d63f 100644 --- a/misc.c +++ b/misc.c @@ -49,7 +49,10 @@ #include #include "misc.h" + +#ifdef ECELERITY #include "util.h" +#endif #define UMEM_ERRFD 2 /* goes to standard error */ #define UMEM_MAX_ERROR_SIZE 4096 /* error messages are truncated to this */ @@ -80,7 +83,9 @@ umem_log_enter(const char *error_str, int serious) char c; looped = 0; +#ifdef ECELERITY mem_printf(serious ? DCRITICAL : DINFO, "umem: %s", error_str); +#endif (void) mutex_lock(&umem_error_lock); diff --git a/sol_compat.h b/sol_compat.h index f369dc1..f419fb6 100644 --- a/sol_compat.h +++ b/sol_compat.h @@ -11,9 +11,15 @@ #include #include -#define INLINE inline -#define THR_RETURN void * -#define THR_API +#ifdef _WIN32 +# define THR_RETURN DWORD +# define THR_API WINAPI +# define INLINE __inline +#else +# define THR_RETURN void * +# define THR_API +# define INLINE inline +#endif #if defined(__MACH__) || defined(_WIN32) #define NO_WEAK_SYMBOLS @@ -98,7 +104,41 @@ static INLINE int thr_create(void *stack_base, # define RTLD_FIRST 0 #endif -/*#include "ec_atomic.h"*/ +#ifdef ECELERITY +# include "ec_atomic.h" +#else +# ifdef _WIN32 +# define ec_atomic_inc(a) InterlockedIncrement(a) +# define ec_atomic_inc64(a) InterlockedIncrement64(a) +# elif (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) +static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp) +{ + uint_t prev; + asm volatile ("lock; cmpxchgl %1, %2" + : "=a" (prev) + : "r" (with), "m" (*(mem)), "0" (cmp) + : "memory"); + return prev; +} +# endif + +# ifndef ec_atomic_inc +static INLINE uint_t ec_atomic_inc(uint_t *mem) +{ + register uint_t last; + do { + last = *mem; + } while (ec_atomic_cas(mem, last+1, last) != last); + return ++last; +} +# endif +# ifndef ec_atomic_inc64 + /* yeah, it's not great. It's only used to bump failed allocation + * counts, so it is not critical right now. */ +# define ec_atomic_inc64(a) (*a)++ +# endif + +#endif #define P2PHASE(x, align) ((x) & ((align) - 1)) #define P2ALIGN(x, align) ((x) & -(align)) @@ -139,4 +179,12 @@ static INLINE int __nthreads(void) # define _LP64 1 #endif +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif +#ifndef MAX +# define MAX(a,b) ((a) > (b) ? (a) : (b)) +#endif + + #endif diff --git a/umem_fail.c b/umem_fail.c index ca110e0..c8b63d0 100644 --- a/umem_fail.c +++ b/umem_fail.c @@ -126,10 +126,11 @@ umem_panic(const char *format, ...) if (format[strlen(format)-1] != '\n') umem_error_enter("\n"); +#ifdef ECELERITY va_start(va, format); - /*ec_debug_vprintf(DCRITICAL, DMEM, format, va);*/ - fvprintf(stderr, format, va); + ec_debug_vprintf(DCRITICAL, DMEM, format, va); va_end(va); +#endif print_stacktrace(); diff --git a/umem_test.c b/umem_test.c new file mode 100644 index 0000000..113139a --- /dev/null +++ b/umem_test.c @@ -0,0 +1,19 @@ +#include "umem.h" + +int main(int argc, char *argv[]) +{ + char *foo; + + umem_startup(NULL, 0, 0, NULL, NULL); + + foo = umem_alloc(32, UMEM_DEFAULT); + + strcpy(foo, "hello there"); + + printf("Hello %s\n", foo); + + umem_free(foo, 32); + + return 0; +} +