1 #ifndef _sw_platform_h 2 #define _sw_platform_h 3 4 /* 5 * Copyright 2003, 2004 Porchdog Software, Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without modification, 8 * are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY PORCHDOG SOFTWARE ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE HOWL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 25 * OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * The views and conclusions contained in the software and documentation are those 28 * of the authors and should not be interpreted as representing official policies, 29 * either expressed or implied, of Porchdog Software, Inc. 30 */ 31 32 33 #ifdef __cplusplus 34 extern "C" 35 { 36 #endif 37 38 39 #if defined(__VXWORKS__) 40 41 # define HOWL_API 42 # include <vxworks.h> 43 # include <sysLib.h> 44 45 # define sw_snooze(SECS) taskDelay(sysClkRateGet() * SECS) 46 47 #elif defined(WIN32) 48 49 # define WIN32_LEAN_AND_MEAN 50 # define HOWL_API __stdcall 51 # pragma warning(disable:4127) 52 # include <windows.h> 53 # include <stdlib.h> 54 55 typedef signed char int8_t; 56 typedef unsigned char u_int8_t; 57 typedef signed short int16_t; 58 typedef unsigned short u_int16_t; 59 typedef signed long int32_t; 60 typedef unsigned long u_int32_t; 61 typedef _int64 int64_t; 62 typedef _int64 u_int64_t; 63 64 # define sw_snooze(SECS) Sleep(SECS * 1000) 65 66 #else 67 68 # define HOWL_API 69 # if defined(HOWL_KERNEL) 70 # include <howl_config.h> 71 # endif 72 # include <sys/types.h> 73 # include <stdlib.h> 74 # include <unistd.h> 75 76 # define sw_snooze(SECS) sleep(SECS) 77 78 #endif 79 80 #if defined(__sun) 81 82 # define u_int8_t uint8_t 83 # define u_int16_t uint16_t 84 # define u_int32_t uint32_t 85 # define u_int64_t uint64_t 86 87 #endif 88 89 typedef void * sw_opaque; 90 typedef void * sw_opaque_t; 91 typedef int8_t sw_int8; 92 typedef u_int8_t sw_uint8; 93 typedef u_int8_t sw_bool; 94 typedef int16_t sw_int16; 95 typedef u_int16_t sw_uint16; 96 typedef int32_t sw_int32; 97 typedef u_int32_t sw_uint32; 98 typedef int64_t sw_int64; 99 typedef u_int64_t sw_uint64; 100 typedef char * sw_string; 101 typedef sw_uint8 * sw_octets; 102 #if !defined(__VXWORKS__) || defined(__cplusplus) 103 typedef const char * sw_const_string; 104 typedef const u_int8_t * sw_const_octets; 105 #else 106 typedef char * sw_const_string; 107 typedef u_int8_t * sw_const_octets; 108 #endif 109 typedef size_t sw_size_t; 110 typedef int sw_result; 111 112 113 114 /* -------------------------------------------------------- 115 * 116 * Endian-osity 117 * 118 * SW_ENDIAN is 0 for big endian platforms, 1 119 * for little endian platforms. 120 * 121 * The macro WORDS_BIGENDIAN will be defined 122 * by autoconf. If you are using Howl on 123 * a platform that doesn't have autoconf, define 124 * SW_ENDIAN directly 125 * -------------------------------------------------------- 126 */ 127 128 #if !defined(SW_ENDIAN) 129 130 # if defined(WORDS_BIGENDIAN) && WORDS_BIGENDIAN == 1 131 132 # define SW_ENDIAN 0 133 134 # else 135 136 # define SW_ENDIAN 1 137 138 # endif 139 140 #endif 141 142 143 /* -------------------------------------------------------- 144 * 145 * Strings 146 * 147 * These macros supports cross platform string functions 148 * for the following OSes 149 * 150 * Win32 151 * *NIX 152 * PalmOS 153 * VxWorks 154 * 155 * -------------------------------------------------------- 156 */ 157 158 #if defined(WIN32) 159 160 # include <string.h> 161 162 # define sw_memset(ARG1, ARG2, ARG3) memset((char*) ARG1, ARG2, ARG3) 163 # define sw_memcpy(ARG1, ARG2, ARG3) memmove((char*) ARG1, (char*) ARG2, ARG3) 164 # define sw_memcmp(ARG1, ARG2, ARG3) memcmp((char*) ARG1, ARG2, ARG3) 165 # define sw_strcasecmp(ARG1, ARG2) stricmp(ARG1, ARG2) 166 # define sw_strncasecmp(ARG1, ARG2) strnicmp(ARG1, ARG2) 167 # define sw_strcat(ARG1, ARG2) strcat(ARG1, ARG2) 168 # define sw_strncat(ARG1, ARG2) strncat(ARG1, ARG2) 169 # define sw_strchr(ARG1, ARG2) strchr(ARG1, ARG2) 170 # define sw_strcmp(ARG1, ARG2) strcmp(ARG1, ARG2) 171 # define sw_strncmp(ARG1, ARG2) strncmp(ARG1, ARG2) 172 # define sw_strcoll(ARG1, ARG2) strcoll(ARG1, ARG2) 173 # define sw_strcpy(ARG1, ARG2) (ARG2) ? strcpy(ARG1, ARG2) : strcpy(ARG1, "") 174 # define sw_strncpy(ARG1, ARG2, N) (ARG2) ? strncpy(ARG1, ARG2, N) : strcpy(ARG1, "") 175 # define sw_strcspn(ARG1, ARG2) strcspn(ARG1, ARG2) 176 # define sw_strlen(ARG1) strlen(ARG1) 177 # define sw_strstr(ARG1, ARG2) strstr(ARG1, ARG2) 178 # define sw_strtok_r(ARG1, ARG2, ARG3) strtok_r(ARG1, ARG2, ARG3) 179 180 #elif defined(__VXWORKS__) 181 182 # include <string.h> 183 184 extern sw_int32 185 sw_strcasecmp( 186 sw_const_string arg1, 187 sw_const_string arg2); 188 189 extern sw_int32 190 sw_strncasecmp( 191 sw_const_string arg1, 192 sw_const_string arg2, 193 sw_len n); 194 195 extern sw_string 196 sw_strtok_r( 197 sw_string arg1, 198 sw_const_string arg2, 199 sw_string * lasts); 200 201 # define sw_memset(ARG1, ARG2, ARG3) memset((char*) ARG1, ARG2, ARG3) 202 # define sw_memcpy(ARG1, ARG2, ARG3) memcpy((char*) ARG1, (char*) ARG2, ARG3) 203 # define sw_memcmp(ARG1, ARG2, ARG3) memcmp((char*) ARG1, ARG2, ARG3) 204 # define sw_strcat(ARG1, ARG2) strcat(ARG1, ARG2) 205 # define sw_strncat(ARG1, ARG2) strncat(ARG1, ARG2) 206 # define sw_strchr(ARG1, ARG2) strchr(ARG1, ARG2) 207 # define sw_strcmp(ARG1, ARG2) strcmp(ARG1, ARG2) 208 # define sw_strncmp(ARG1, ARG2) strncmp(ARG1, ARG2) 209 # define sw_strcoll(ARG1, ARG2) strcoll(ARG1, ARG2) 210 # define sw_strcpy(ARG1, ARG2) ARG2 ? strcpy(ARG1, ARG2) : strcpy(ARG1, "") 211 # define sw_strncpy(ARG1, ARG2, N) ARG2 ? strncpy(ARG1, ARG2, N) : strcpy(ARG1, "") 212 # define sw_strcspn(ARG1, ARG2) strcspn(ARG1, ARG2) 213 # define sw_strlen(ARG1) strlen(ARG1) 214 # define sw_strstr(ARG1, ARG2) strstr(ARG1, ARG2) 215 216 #elif defined(__PALMOS__) 217 218 # include <StringMgr.h> 219 220 # define sw_strcasecmp(ARG1, ARG2) strcasecmp(ARG1, ARG2) 221 # define sw_strncasecmp(ARG1, ARG2) strncasecmp(ARG1, ARG2) 222 # define sw_strcat(ARG1, ARG2) StrCat(ARG1, ARG2) 223 # define sw_strncat(ARG1, ARG2) StrNCat(ARG1, ARG2) 224 # define sw_strchr(ARG1, ARG2) StrChr(ARG1, ARG2) 225 # define sw_strcmp(ARG1, ARG2) StrCampare(ARG1, ARG2) 226 # define sw_strncmp(ARG1, ARG2) StrNCompare(ARG1, ARG2) 227 # define sw_strcoll(ARG1, ARG2) strcoll(ARG1, ARG2) 228 # define sw_strcpy(ARG1, ARG2) ARG2 ? StrCopy(ARG1, ARG2) : StrCopy(ARG1, "") 229 # define sw_strncpy(ARG1, ARG2, N) ARG2 ? StrNCopy(ARG1, ARG2, N) : StrCopy(ARG1, "") 230 # define sw_strcspn(ARG1, ARG2) strcspn(ARG1, ARG2) 231 # define sw_strlen(ARG1) StrLen(ARG1) 232 # define sw_strstr(ARG1, ARG2) strstr(ARG1, ARG2) 233 # define sw_strtok_r(ARG1, ARG2, ARG3) strtok_r(ARG1, ARG2, ARG3) 234 235 #else 236 237 # include <string.h> 238 239 # if defined(__Lynx__) 240 char * strchr(char*, int); 241 # endif 242 243 # define sw_memset(ARG1, ARG2, ARG3) memset((char*) ARG1, ARG2, ARG3) 244 # define sw_memcpy(ARG1, ARG2, ARG3) memcpy((char*) ARG1, (char*) ARG2, ARG3) 245 # define sw_memcmp(ARG1, ARG2, ARG3) memcmp((char*) ARG1, ARG2, ARG3) 246 # define sw_strcasecmp(ARG1, ARG2) strcasecmp(ARG1, ARG2) 247 # define sw_strncasecmp(ARG1, ARG2) strncasecmp(ARG1, ARG2) 248 # define sw_strcat(ARG1, ARG2) strcat(ARG1, ARG2) 249 # define sw_strncat(ARG1, ARG2) strncat(ARG1, ARG2) 250 # define sw_strchr(ARG1, ARG2) strchr(ARG1, ARG2) 251 # define sw_strcmp(ARG1, ARG2) strcmp(ARG1, ARG2) 252 # define sw_strncmp(ARG1, ARG2) strncmp(ARG1, ARG2) 253 # define sw_strcoll(ARG1, ARG2) strcoll(ARG1, ARG2) 254 # define sw_strcpy(ARG1, ARG2) ARG2 ? strcpy(ARG1, ARG2) : strcpy(ARG1, "") 255 # define sw_strncpy(ARG1, ARG2, N) ARG2 ? strncpy(ARG1, ARG2, N) : strcpy(ARG1, "") 256 # define sw_strcspn(ARG1, ARG2) strcspn(ARG1, ARG2) 257 # define sw_strlen(ARG1) strlen(ARG1) 258 # define sw_strstr(ARG1, ARG2) strstr(ARG1, ARG2) 259 # define sw_strtok_r(ARG1, ARG2, ARG3) strtok_r(ARG1, ARG2, ARG3) 260 261 #endif 262 263 264 sw_string 265 sw_strdup( 266 sw_const_string str); 267 268 269 /* -------------------------------------------------------- 270 * 271 * Memory 272 * 273 * These macros support cross platform heap functions. 274 * When compiling with DEBUG, some extra checking is 275 * done which can aid in tracking down heap corruption 276 * problems 277 * 278 * -------------------------------------------------------- 279 */ 280 281 #if defined(NDEBUG) 282 283 # define sw_malloc(SIZE) malloc(SIZE) 284 # define sw_realloc(MEM,SIZE) realloc(MEM, SIZE) 285 # define sw_free(MEM) if (MEM) free(MEM) 286 287 #else 288 289 # define sw_malloc(SIZE) _sw_debug_malloc(SIZE, __SW_FUNCTION__, __FILE__, __LINE__) 290 # define sw_realloc(MEM,SIZE) _sw_debug_realloc(MEM, SIZE, __SW_FUNCTION__, __FILE__, __LINE__) 291 # define sw_free(MEM) if (MEM) _sw_debug_free(MEM, __SW_FUNCTION__, __FILE__, __LINE__) 292 293 #endif 294 295 296 sw_opaque HOWL_API 297 _sw_debug_malloc( 298 sw_size_t size, 299 sw_const_string function, 300 sw_const_string file, 301 sw_uint32 line); 302 303 304 sw_opaque HOWL_API 305 _sw_debug_realloc( 306 sw_opaque_t mem, 307 sw_size_t size, 308 sw_const_string function, 309 sw_const_string file, 310 sw_uint32 line); 311 312 313 void HOWL_API 314 _sw_debug_free( 315 sw_opaque_t mem, 316 sw_const_string function, 317 sw_const_string file, 318 sw_uint32 line); 319 320 321 322 /* -------------------------------------------------------- 323 * 324 * Sockets 325 * 326 * These macros and APIs support cross platform socket 327 * calls. I am relying on BSD APIs, but even with those 328 * there are subtle and not so subtle platform differences 329 * 330 * -------------------------------------------------------- 331 */ 332 333 #if defined(__VXWORKS__) 334 335 # include <vxworks.h> 336 # include <hostLib.h> 337 # include <sockLib.h> 338 # include <ioLib.h> 339 # include <inetLib.h> 340 341 typedef int sw_sockdesc_t; 342 typedef socklen_t sw_socklen_t; 343 344 #elif defined(WIN32) 345 346 # include <winsock2.h> 347 348 typedef SOCKET sw_sockdesc_t; 349 typedef int sw_socklen_t; 350 351 # define SW_E_WOULDBLOCK WSAEWOULDBLOCK 352 # define SW_INVALID_SOCKET INVALID_SOCKET 353 # define SW_SOCKET_ERROR SOCKET_ERROR 354 355 # define sw_close_socket(X) closesocket(X) 356 357 #else 358 359 # if defined(sun) 360 361 # include <unistd.h> 362 363 # endif 364 365 # include <sys/types.h> 366 # include <signal.h> 367 368 # if defined(__Lynx__) 369 370 # include <socket.h> 371 372 # else 373 374 # include <sys/socket.h> 375 376 # endif 377 378 # include <netinet/in.h> 379 # include <netinet/tcp.h> 380 # include <netdb.h> 381 # include <arpa/inet.h> 382 # include <stdlib.h> 383 # include <unistd.h> 384 # include <sys/ioctl.h> 385 # include <stdio.h> 386 # include <errno.h> 387 388 typedef sw_int32 sw_sockdesc_t; 389 typedef socklen_t sw_socklen_t; 390 391 # define SW_E_WOULDBLOCK EWOULDBLOCK 392 # define SW_INVALID_SOCKET -1 393 # define SW_SOCKET_ERROR -1 394 395 # define sw_close_socket(X) close(X) 396 397 #endif 398 399 400 /* -------------------------------------------------------- 401 * 402 * strerror() 403 * 404 * This function will print a string rep of a system error 405 * code 406 * 407 * -------------------------------------------------------- 408 */ 409 410 sw_const_string 411 sw_strerror(); 412 413 414 /* 415 * Obsolete types and macros. 416 * 417 * These are here for backwards compatibility, but will 418 * be removed in the future 419 */ 420 #define sw_char sw_int8 421 #define sw_uchar sw_uint8 422 #define sw_octet sw_uint8 423 #define sw_short sw_int16 424 #define sw_ushort sw_uint16 425 #define sw_long sw_int32 426 #define sw_ulong sw_uint32 427 428 429 #define SW_TRY(EXPR) { sw_result result; if ((result = EXPR) != SW_OKAY) return result; } ((void) 0) 430 #define SW_TRY_GOTO(EXPR) { if ((result = EXPR) != SW_OKAY) goto exit; } ((void) 0) 431 432 433 #ifdef __cplusplus 434 } 435 #endif 436 437 438 #endif 439