00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if defined(WIN32) || defined(_WIN32_WCE)
00023 #include "ortp-config-win32.h"
00024 #else
00025 #include "ortp-config.h"
00026 #endif
00027 #include "ortp/port.h"
00028 #include "ortp/ortp.h"
00029 #include "utils.h"
00030
00031 #if defined(_WIN32) && !defined(_WIN32_WCE)
00032 #include <process.h>
00033 #endif
00034
00035 static void *ortp_libc_malloc(size_t sz){
00036 return malloc(sz);
00037 }
00038
00039 static void *ortp_libc_realloc(void *ptr, size_t sz){
00040 return realloc(ptr,sz);
00041 }
00042
00043 static void ortp_libc_free(void*ptr){
00044 free(ptr);
00045 }
00046
00047 static bool_t allocator_used=FALSE;
00048
00049 static OrtpMemoryFunctions ortp_allocator={
00050 ortp_libc_malloc,
00051 ortp_libc_realloc,
00052 ortp_libc_free
00053 };
00054
00055 void ortp_set_memory_functions(OrtpMemoryFunctions *functions){
00056 if (allocator_used){
00057 ortp_fatal("ortp_set_memory_functions() must be called before "
00058 "first use of ortp_malloc or ortp_realloc");
00059 return;
00060 }
00061 ortp_allocator=*functions;
00062 }
00063
00064 void* ortp_malloc(size_t sz){
00065 allocator_used=TRUE;
00066 return ortp_allocator.malloc_fun(sz);
00067 }
00068
00069 void* ortp_realloc(void *ptr, size_t sz){
00070 allocator_used=TRUE;
00071 return ortp_allocator.realloc_fun(ptr,sz);
00072 }
00073
00074 void ortp_free(void* ptr){
00075 ortp_allocator.free_fun(ptr);
00076 }
00077
00078 void * ortp_malloc0(size_t size){
00079 void *ptr=ortp_malloc(size);
00080 memset(ptr,0,size);
00081 return ptr;
00082 }
00083
00084 char * ortp_strdup(const char *tmp){
00085 size_t sz=strlen(tmp)+1;
00086 char *ret=(char*)ortp_malloc(sz);
00087 strcpy(ret,tmp);
00088 ret[sz-1]='\0';
00089 return ret;
00090 }
00091
00092
00093
00094
00095
00096
00097 int set_non_blocking_socket (ortp_socket_t sock)
00098 {
00099
00100
00101 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00102 return fcntl (sock, F_SETFL, O_NONBLOCK);
00103 #else
00104 unsigned long nonBlock = 1;
00105 return ioctlsocket(sock, FIONBIO , &nonBlock);
00106 #endif
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 int close_socket(ortp_socket_t sock){
00116 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00117 return close (sock);
00118 #else
00119 return closesocket(sock);
00120 #endif
00121 }
00122
00123
00124
00125 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00126
00127 #else
00128 int inet_aton (const char * cp, struct in_addr * addr)
00129 {
00130 unsigned long retval;
00131
00132 retval = inet_addr (cp);
00133
00134 if (retval == INADDR_NONE)
00135 {
00136 return -1;
00137 }
00138 else
00139 {
00140 addr->S_un.S_addr = retval;
00141 return 1;
00142 }
00143 }
00144 #endif
00145
00146 char *ortp_strndup(const char *str,int n){
00147 int min=MIN((int)strlen(str),n)+1;
00148 char *ret=(char*)ortp_malloc(min);
00149 strncpy(ret,str,n);
00150 ret[min-1]='\0';
00151 return ret;
00152 }
00153
00154 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00155 int __ortp_thread_join(ortp_thread_t thread, void **ptr){
00156 int err=pthread_join(thread,ptr);
00157 if (err!=0) {
00158 ortp_error("pthread_join error: %s",strerror(err));
00159 }
00160 return err;
00161 }
00162
00163 int __ortp_thread_create(pthread_t *thread, pthread_attr_t *attr, void * (*routine)(void*), void *arg){
00164 pthread_attr_t my_attr;
00165 pthread_attr_init(&my_attr);
00166 if (attr)
00167 my_attr = *attr;
00168 #ifdef ORTP_DEFAULT_THREAD_STACK_SIZE
00169 if (ORTP_DEFAULT_THREAD_STACK_SIZE!=0)
00170 pthread_attr_setstacksize(&my_attr, ORTP_DEFAULT_THREAD_STACK_SIZE);
00171 #endif
00172 return pthread_create(thread, &my_attr, routine, arg);
00173 }
00174
00175 #endif
00176 #if defined(_WIN32) || defined(_WIN32_WCE)
00177
00178 int WIN_mutex_init(ortp_mutex_t *mutex, void *attr)
00179 {
00180 *mutex=CreateMutex(NULL, FALSE, NULL);
00181 return 0;
00182 }
00183
00184 int WIN_mutex_lock(ortp_mutex_t * hMutex)
00185 {
00186 WaitForSingleObject(*hMutex, INFINITE);
00187 return 0;
00188 }
00189
00190 int WIN_mutex_unlock(ortp_mutex_t * hMutex)
00191 {
00192 ReleaseMutex(*hMutex);
00193 return 0;
00194 }
00195
00196 int WIN_mutex_destroy(ortp_mutex_t * hMutex)
00197 {
00198 CloseHandle(*hMutex);
00199 return 0;
00200 }
00201
00202 typedef struct thread_param{
00203 void * (*func)(void *);
00204 void * arg;
00205 }thread_param_t;
00206
00207 static unsigned WINAPI thread_starter(void *data){
00208 thread_param_t *params=(thread_param_t*)data;
00209 void *ret=params->func(params->arg);
00210 ortp_free(data);
00211 return (DWORD)ret;
00212 }
00213
00214 #if defined _WIN32_WCE
00215 # define _beginthreadex CreateThread
00216 # define _endthreadex ExitThread
00217 #endif
00218
00219 int WIN_thread_create(ortp_thread_t *th, void *attr, void * (*func)(void *), void *data)
00220 {
00221 thread_param_t *params=ortp_new(thread_param_t,1);
00222 params->func=func;
00223 params->arg=data;
00224 *th=(HANDLE)_beginthreadex( NULL, 0, thread_starter, params, 0, NULL);
00225 return 0;
00226 }
00227
00228 int WIN_thread_join(ortp_thread_t thread_h, void **unused)
00229 {
00230 if (thread_h!=NULL)
00231 {
00232 WaitForSingleObject(thread_h, INFINITE);
00233 CloseHandle(thread_h);
00234 }
00235 return 0;
00236 }
00237
00238 int WIN_cond_init(ortp_cond_t *cond, void *attr)
00239 {
00240 *cond=CreateEvent(NULL, FALSE, FALSE, NULL);
00241 return 0;
00242 }
00243
00244 int WIN_cond_wait(ortp_cond_t* hCond, ortp_mutex_t * hMutex)
00245 {
00246
00247 WIN_mutex_unlock(hMutex);
00248 WaitForSingleObject(*hCond, INFINITE);
00249 WIN_mutex_lock(hMutex);
00250 return 0;
00251 }
00252
00253 int WIN_cond_signal(ortp_cond_t * hCond)
00254 {
00255 SetEvent(*hCond);
00256 return 0;
00257 }
00258
00259 int WIN_cond_broadcast(ortp_cond_t * hCond)
00260 {
00261 WIN_cond_signal(hCond);
00262 return 0;
00263 }
00264
00265 int WIN_cond_destroy(ortp_cond_t * hCond)
00266 {
00267 CloseHandle(*hCond);
00268 return 0;
00269 }
00270
00271
00272 #if defined(_WIN32_WCE)
00273 #include <time.h>
00274
00275 int
00276 gettimeofday (struct timeval *tv, void *tz)
00277 {
00278 DWORD timemillis = GetTickCount();
00279 tv->tv_sec = timemillis/1000;
00280 tv->tv_usec = (timemillis - (tv->tv_sec*1000)) * 1000;
00281 return 0;
00282 }
00283
00284 #else
00285
00286 int gettimeofday (struct timeval *tv, void* tz)
00287 {
00288 union
00289 {
00290 __int64 ns100;
00291 FILETIME fileTime;
00292 } now;
00293
00294 GetSystemTimeAsFileTime (&now.fileTime);
00295 tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
00296 tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
00297 return (0);
00298 }
00299
00300 #endif
00301
00302 const char *getWinSocketError(int error)
00303 {
00304 static char buf[80];
00305
00306 switch (error)
00307 {
00308 case WSANOTINITIALISED: return "Windows sockets not initialized : call WSAStartup";
00309 case WSAEADDRINUSE: return "Local Address already in use";
00310 case WSAEADDRNOTAVAIL: return "The specified address is not a valid address for this machine";
00311 case WSAEINVAL: return "The socket is already bound to an address.";
00312 case WSAENOBUFS: return "Not enough buffers available, too many connections.";
00313 case WSAENOTSOCK: return "The descriptor is not a socket.";
00314 case WSAECONNRESET: return "Connection reset by peer";
00315
00316 default :
00317 sprintf(buf, "Error code : %d", error);
00318 return buf;
00319 break;
00320 }
00321
00322 return buf;
00323 }
00324
00325 #ifdef _WORKAROUND_MINGW32_BUGS
00326 char * WSAAPI gai_strerror(int errnum){
00327 return (char*)getWinSocketError(errnum);
00328 }
00329 #endif
00330
00331 #endif
00332