2 * Copyright (c) 2006 OmniTI, Inc. All rights reserved
3 * This header file distributed under the terms of the CDDL.
4 * Portions Copyright 2004 Sun Microsystems, Inc. All Rights reserved.
6 #ifndef _EC_UMEM_SOL_COMPAT_H_
7 #define _EC_UMEM_SOL_COMPAT_H_
14 #ifdef HAVE_SYS_TIME_H
19 # define THR_RETURN DWORD
20 # define THR_API WINAPI
21 # define INLINE __inline
23 # define THR_RETURN void *
25 # define INLINE inline
28 #if defined(__MACH__) || defined(_WIN32)
29 #define NO_WEAK_SYMBOLS
30 #define _umem_cache_alloc(a,b) umem_cache_alloc(a,b)
31 #define _umem_cache_free(a,b) umem_cache_free(a,b)
32 #define _umem_zalloc(a,b) umem_zalloc(a,b)
33 #define _umem_alloc(a,b) umem_alloc(a,b)
34 #define _umem_alloc_align(a,b,c) umem_alloc_align(a,b,c)
35 #define _umem_free(a,b) umem_free(a,b)
36 #define _umem_free_align(a,b) umem_free_align(a,b)
40 #define bcopy(s, d, n) memcpy(d, s, n)
41 #define bzero(m, s) memset(m, 0, s)
44 typedef pthread_t thread_t;
45 typedef pthread_mutex_t mutex_t;
46 typedef pthread_cond_t cond_t;
47 typedef u_int64_t hrtime_t;
48 typedef uint32_t uint_t;
49 typedef unsigned long ulong_t;
50 typedef struct timespec timestruc_t;
51 typedef long long longlong_t;
52 typedef struct timespec timespec_t;
53 static INLINE hrtime_t gethrtime(void) {
55 gettimeofday(&tv, NULL);
56 return (((u_int64_t)tv.tv_sec) << 32) | tv.tv_usec;
58 # define thr_self() pthread_self()
59 static INLINE thread_t _thr_self(void) {
62 #if defined(_MACH_PORT_T)
63 #define CPUHINT() (pthread_mach_thread_np(pthread_self()))
65 # define thr_sigsetmask pthread_sigmask
68 #define THR_DETACHED 2
71 static INLINE int thr_create(void *stack_base,
72 size_t stack_size, THR_RETURN (THR_API *start_func)(void*),
73 void *arg, long flags, thread_t *new_thread_ID)
78 pthread_attr_init(&attr);
80 if (flags & THR_DETACHED) {
81 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
83 ret = pthread_create(new_thread_ID, &attr, start_func, arg);
84 pthread_attr_destroy(&attr);
89 # define mutex_init(mp, type, arg) pthread_mutex_init(mp, NULL)
90 # define mutex_lock(mp) pthread_mutex_lock(mp)
91 # define mutex_unlock(mp) pthread_mutex_unlock(mp)
92 # define mutex_destroy(mp) pthread_mutex_destroy(mp)
93 # define mutex_trylock(mp) pthread_mutex_trylock(mp)
94 # define DEFAULTMUTEX PTHREAD_MUTEX_INITIALIZER
95 # define DEFAULTCV PTHREAD_COND_INITIALIZER
96 # define MUTEX_HELD(mp) 1 /* not really, but only used in an assert */
98 # define cond_init(c, type, arg) pthread_cond_init(c, NULL)
99 # define cond_wait(c, m) pthread_cond_wait(c, m)
100 # define _cond_wait(c, m) pthread_cond_wait(c, m)
101 # define cond_signal(c) pthread_cond_signal(c)
102 # define cond_broadcast(c) pthread_cond_broadcast(c)
103 # define cond_destroy(c) pthread_cond_destroy(c)
104 # define cond_timedwait pthread_cond_timedwait
105 # define _cond_timedwait pthread_cond_timedwait
108 # define RTLD_FIRST 0
112 # include "ec_atomic.h"
115 # define ec_atomic_inc(a) InterlockedIncrement(a)
116 # define ec_atomic_inc64(a) InterlockedIncrement64(a)
117 # elif (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__)
118 static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp)
121 __asm volatile ("lock; cmpxchgl %1, %2"
123 : "r" (with), "m" (*(mem)), "0" (cmp)
127 # elif defined(__sparc__) && defined(__GNUC__)
128 static INLINE uint_t ec_atomic_cas(uint_t *mem, uint_t with, uint_t cmp)
130 __asm volatile ("cas [%3],%2,%0"
131 : "+r"(with), "=m"(*(mem))
132 : "r"(cmp), "r"(mem), "m"(*(mem)) );
137 # ifndef ec_atomic_inc
138 static INLINE uint_t ec_atomic_inc(uint_t *mem)
140 register uint_t last;
143 } while (ec_atomic_cas(mem, last+1, last) != last);
147 # ifndef ec_atomic_inc64
148 /* yeah, it's not great. It's only used to bump failed allocation
149 * counts, so it is not critical right now. */
150 # define ec_atomic_inc64(a) (*a)++
155 #define P2PHASE(x, align) ((x) & ((align) - 1))
156 #define P2ALIGN(x, align) ((x) & -(align))
157 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
158 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
159 #define P2END(x, align) (-(~(x) & -(align)))
160 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
161 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
162 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
163 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
164 #define ISP2(x) (((x) & ((x) - 1)) == 0)
166 /* beware! umem only uses these atomic adds for incrementing by 1 */
167 #define atomic_add_64(lvalptr, delta) ec_atomic_inc64(lvalptr)
168 #define atomic_add_32_nv(a, b) ec_atomic_inc(a)
171 #define NANOSEC 1000000000
175 #define issetugid() 0
176 #elif !defined(__FreeBSD__)
177 #define issetugid() (geteuid() == 0)
180 #define _sysconf(a) sysconf(a)
181 #define __NORETURN __attribute__ ((noreturn))
183 #define EC_UMEM_DUMMY_PCSTACK 1
184 static INLINE int __nthreads(void)
186 /* or more; just to force multi-threaded mode */
190 #if (SIZEOF_VOID_P == 8)
195 # define MIN(a,b) ((a) < (b) ? (a) : (b))
198 # define MAX(a,b) ((a) > (b) ? (a) : (b))