Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #ifndef __PJ_TYPES_H__ |
| 21 | #define __PJ_TYPES_H__ |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @file types.h |
| 26 | * @brief Declaration of basic types and utility. |
| 27 | */ |
| 28 | /** |
| 29 | * @defgroup PJ_BASIC Basic Data Types and Library Functionality. |
| 30 | * @ingroup PJ_DS |
| 31 | * @{ |
| 32 | */ |
| 33 | #include <pj/config.h> |
| 34 | |
| 35 | PJ_BEGIN_DECL |
| 36 | |
| 37 | /* ************************************************************************* */ |
| 38 | |
| 39 | /** Signed 32bit integer. */ |
| 40 | typedef int pj_int32_t; |
| 41 | |
| 42 | /** Unsigned 32bit integer. */ |
| 43 | typedef unsigned int pj_uint32_t; |
| 44 | |
| 45 | /** Signed 16bit integer. */ |
| 46 | typedef short pj_int16_t; |
| 47 | |
| 48 | /** Unsigned 16bit integer. */ |
| 49 | typedef unsigned short pj_uint16_t; |
| 50 | |
| 51 | /** Signed 8bit integer. */ |
| 52 | typedef signed char pj_int8_t; |
| 53 | |
| 54 | /** Unsigned 8bit integer. */ |
| 55 | typedef unsigned char pj_uint8_t; |
| 56 | |
| 57 | /** Large unsigned integer. */ |
| 58 | typedef size_t pj_size_t; |
| 59 | |
| 60 | /** Large signed integer. */ |
| 61 | #if defined(PJ_WIN64) && PJ_WIN64!=0 |
| 62 | typedef pj_int64_t pj_ssize_t; |
| 63 | #else |
| 64 | typedef long pj_ssize_t; |
| 65 | #endif |
| 66 | |
| 67 | /** Status code. */ |
| 68 | typedef int pj_status_t; |
| 69 | |
| 70 | /** Boolean. */ |
| 71 | typedef int pj_bool_t; |
| 72 | |
| 73 | /** Native char type, which will be equal to wchar_t for Unicode |
| 74 | * and char for ANSI. */ |
| 75 | #if defined(PJ_NATIVE_STRING_IS_UNICODE) && PJ_NATIVE_STRING_IS_UNICODE!=0 |
| 76 | typedef wchar_t pj_char_t; |
| 77 | #else |
| 78 | typedef char pj_char_t; |
| 79 | #endif |
| 80 | |
| 81 | /** This macro creates Unicode or ANSI literal string depending whether |
| 82 | * native platform string is Unicode or ANSI. */ |
| 83 | #if defined(PJ_NATIVE_STRING_IS_UNICODE) && PJ_NATIVE_STRING_IS_UNICODE!=0 |
| 84 | # define PJ_T(literal_str) L##literal_str |
| 85 | #else |
| 86 | # define PJ_T(literal_str) literal_str |
| 87 | #endif |
| 88 | |
| 89 | |
| 90 | /** Status is OK. */ |
| 91 | #define PJ_SUCCESS 0 |
| 92 | |
| 93 | /** True value. */ |
| 94 | #define PJ_TRUE 1 |
| 95 | |
| 96 | /** False value. */ |
| 97 | #define PJ_FALSE 0 |
| 98 | |
| 99 | /** |
| 100 | * File offset type. |
| 101 | */ |
| 102 | #if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 |
| 103 | typedef pj_int64_t pj_off_t; |
| 104 | #else |
| 105 | typedef pj_ssize_t pj_off_t; |
| 106 | #endif |
| 107 | |
| 108 | /* ************************************************************************* */ |
| 109 | /* |
| 110 | * Data structure types. |
| 111 | */ |
| 112 | /** |
| 113 | * This type is used as replacement to legacy C string, and used throughout |
| 114 | * the library. By convention, the string is NOT null terminated. |
| 115 | */ |
| 116 | struct pj_str_t |
| 117 | { |
| 118 | /** Buffer pointer, which is by convention NOT null terminated. */ |
| 119 | char *ptr; |
| 120 | |
| 121 | /** The length of the string. */ |
| 122 | pj_ssize_t slen; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * This structure represents high resolution (64bit) time value. The time |
| 127 | * values represent time in cycles, which is retrieved by calling |
| 128 | * #pj_get_timestamp(). |
| 129 | */ |
| 130 | typedef union pj_timestamp |
| 131 | { |
| 132 | struct |
| 133 | { |
| 134 | #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0 |
| 135 | pj_uint32_t lo; /**< Low 32-bit value of the 64-bit value. */ |
| 136 | pj_uint32_t hi; /**< high 32-bit value of the 64-bit value. */ |
| 137 | #else |
| 138 | pj_uint32_t hi; /**< high 32-bit value of the 64-bit value. */ |
| 139 | pj_uint32_t lo; /**< Low 32-bit value of the 64-bit value. */ |
| 140 | #endif |
| 141 | } u32; /**< The 64-bit value as two 32-bit values. */ |
| 142 | |
| 143 | #if PJ_HAS_INT64 |
| 144 | pj_uint64_t u64; /**< The whole 64-bit value, where available. */ |
| 145 | #endif |
| 146 | } pj_timestamp; |
| 147 | |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * The opaque data type for linked list, which is used as arguments throughout |
| 152 | * the linked list operations. |
| 153 | */ |
| 154 | typedef void pj_list_type; |
| 155 | |
| 156 | /** |
| 157 | * List. |
| 158 | */ |
| 159 | typedef struct pj_list pj_list; |
| 160 | |
| 161 | /** |
| 162 | * Opaque data type for hash tables. |
| 163 | */ |
| 164 | typedef struct pj_hash_table_t pj_hash_table_t; |
| 165 | |
| 166 | /** |
| 167 | * Opaque data type for hash entry (only used internally by hash table). |
| 168 | */ |
| 169 | typedef struct pj_hash_entry pj_hash_entry; |
| 170 | |
| 171 | /** |
| 172 | * Data type for hash search iterator. |
| 173 | * This structure should be opaque, however applications need to declare |
| 174 | * concrete variable of this type, that's why the declaration is visible here. |
| 175 | */ |
| 176 | typedef struct pj_hash_iterator_t |
| 177 | { |
| 178 | pj_uint32_t index; /**< Internal index. */ |
| 179 | pj_hash_entry *entry; /**< Internal entry. */ |
| 180 | } pj_hash_iterator_t; |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Forward declaration for memory pool factory. |
| 185 | */ |
| 186 | typedef struct pj_pool_factory pj_pool_factory; |
| 187 | |
| 188 | /** |
| 189 | * Opaque data type for memory pool. |
| 190 | */ |
| 191 | typedef struct pj_pool_t pj_pool_t; |
| 192 | |
| 193 | /** |
| 194 | * Forward declaration for caching pool, a pool factory implementation. |
| 195 | */ |
| 196 | typedef struct pj_caching_pool pj_caching_pool; |
| 197 | |
| 198 | /** |
| 199 | * This type is used as replacement to legacy C string, and used throughout |
| 200 | * the library. |
| 201 | */ |
| 202 | typedef struct pj_str_t pj_str_t; |
| 203 | |
| 204 | /** |
| 205 | * Opaque data type for I/O Queue structure. |
| 206 | */ |
| 207 | typedef struct pj_ioqueue_t pj_ioqueue_t; |
| 208 | |
| 209 | /** |
| 210 | * Opaque data type for key that identifies a handle registered to the |
| 211 | * I/O queue framework. |
| 212 | */ |
| 213 | typedef struct pj_ioqueue_key_t pj_ioqueue_key_t; |
| 214 | |
| 215 | /** |
| 216 | * Opaque data to identify timer heap. |
| 217 | */ |
| 218 | typedef struct pj_timer_heap_t pj_timer_heap_t; |
| 219 | |
| 220 | /** |
| 221 | * Opaque data type for atomic operations. |
| 222 | */ |
| 223 | typedef struct pj_atomic_t pj_atomic_t; |
| 224 | |
| 225 | /** |
| 226 | * Value type of an atomic variable. |
| 227 | */ |
| 228 | typedef PJ_ATOMIC_VALUE_TYPE pj_atomic_value_t; |
| 229 | |
| 230 | /* ************************************************************************* */ |
| 231 | |
| 232 | /** Thread handle. */ |
| 233 | typedef struct pj_thread_t pj_thread_t; |
| 234 | |
| 235 | /** Lock object. */ |
| 236 | typedef struct pj_lock_t pj_lock_t; |
| 237 | |
| 238 | /** Group lock */ |
| 239 | typedef struct pj_grp_lock_t pj_grp_lock_t; |
| 240 | |
| 241 | /** Mutex handle. */ |
| 242 | typedef struct pj_mutex_t pj_mutex_t; |
| 243 | |
| 244 | /** Semaphore handle. */ |
| 245 | typedef struct pj_sem_t pj_sem_t; |
| 246 | |
| 247 | /** Event object. */ |
| 248 | typedef struct pj_event_t pj_event_t; |
| 249 | |
| 250 | /** Unidirectional stream pipe object. */ |
| 251 | typedef struct pj_pipe_t pj_pipe_t; |
| 252 | |
| 253 | /** Operating system handle. */ |
| 254 | typedef void *pj_oshandle_t; |
| 255 | |
| 256 | /** Socket handle. */ |
| 257 | #if defined(PJ_WIN64) && PJ_WIN64!=0 |
| 258 | typedef pj_int64_t pj_sock_t; |
| 259 | #else |
| 260 | typedef long pj_sock_t; |
| 261 | #endif |
| 262 | |
| 263 | /** Generic socket address. */ |
| 264 | typedef void pj_sockaddr_t; |
| 265 | |
| 266 | /** Forward declaration. */ |
| 267 | typedef struct pj_sockaddr_in pj_sockaddr_in; |
| 268 | |
| 269 | /** Color type. */ |
| 270 | typedef unsigned int pj_color_t; |
| 271 | |
| 272 | /** Exception id. */ |
| 273 | typedef int pj_exception_id_t; |
| 274 | |
| 275 | /* ************************************************************************* */ |
| 276 | |
| 277 | /** Utility macro to compute the number of elements in static array. */ |
| 278 | #define PJ_ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) |
| 279 | |
| 280 | /** Maximum value for signed 32-bit integer. */ |
| 281 | #define PJ_MAXINT32 0x7FFFFFFFL |
| 282 | |
| 283 | /** |
| 284 | * Length of object names. |
| 285 | */ |
| 286 | #define PJ_MAX_OBJ_NAME 32 |
| 287 | |
| 288 | /* ************************************************************************* */ |
| 289 | /* |
| 290 | * General. |
| 291 | */ |
| 292 | /** |
| 293 | * Initialize the PJ Library. |
| 294 | * This function must be called before using the library. The purpose of this |
| 295 | * function is to initialize static library data, such as character table used |
| 296 | * in random string generation, and to initialize operating system dependent |
| 297 | * functionality (such as WSAStartup() in Windows). |
| 298 | * |
| 299 | * Apart from calling pj_init(), application typically should also initialize |
| 300 | * the random seed by calling pj_srand(). |
| 301 | * |
| 302 | * @return PJ_SUCCESS on success. |
| 303 | */ |
| 304 | PJ_DECL(pj_status_t) pj_init(void); |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Shutdown PJLIB. |
| 309 | */ |
| 310 | PJ_DECL(void) pj_shutdown(void); |
| 311 | |
| 312 | /** |
| 313 | * Type of callback to register to pj_atexit(). |
| 314 | */ |
| 315 | typedef void (*pj_exit_callback)(void); |
| 316 | |
| 317 | /** |
| 318 | * Register cleanup function to be called by PJLIB when pj_shutdown() is |
| 319 | * called. |
| 320 | * |
| 321 | * @param func The function to be registered. |
| 322 | * |
| 323 | * @return PJ_SUCCESS on success. |
| 324 | */ |
| 325 | PJ_DECL(pj_status_t) pj_atexit(pj_exit_callback func); |
| 326 | |
| 327 | |
| 328 | |
| 329 | /** |
| 330 | * Swap the byte order of an 16bit data. |
| 331 | * |
| 332 | * @param val16 The 16bit data. |
| 333 | * |
| 334 | * @return An 16bit data with swapped byte order. |
| 335 | */ |
| 336 | PJ_INLINE(pj_int16_t) pj_swap16(pj_int16_t val16) |
| 337 | { |
| 338 | pj_uint8_t *p = (pj_uint8_t*)&val16; |
| 339 | pj_uint8_t tmp = *p; |
| 340 | *p = *(p+1); |
| 341 | *(p+1) = tmp; |
| 342 | return val16; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Swap the byte order of an 32bit data. |
| 347 | * |
| 348 | * @param val32 The 32bit data. |
| 349 | * |
| 350 | * @return An 32bit data with swapped byte order. |
| 351 | */ |
| 352 | PJ_INLINE(pj_int32_t) pj_swap32(pj_int32_t val32) |
| 353 | { |
| 354 | pj_uint8_t *p = (pj_uint8_t*)&val32; |
| 355 | pj_uint8_t tmp = *p; |
| 356 | *p = *(p+3); |
| 357 | *(p+3) = tmp; |
| 358 | tmp = *(p+1); |
| 359 | *(p+1) = *(p+2); |
| 360 | *(p+2) = tmp; |
| 361 | return val32; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /** |
| 366 | * @} |
| 367 | */ |
| 368 | /** |
| 369 | * @addtogroup PJ_TIME Time Data Type and Manipulation. |
| 370 | * @ingroup PJ_MISC |
| 371 | * @{ |
| 372 | */ |
| 373 | |
| 374 | /** |
| 375 | * Representation of time value in this library. |
| 376 | * This type can be used to represent either an interval or a specific time |
| 377 | * or date. |
| 378 | */ |
| 379 | typedef struct pj_time_val |
| 380 | { |
| 381 | /** The seconds part of the time. */ |
| 382 | long sec; |
| 383 | |
| 384 | /** The miliseconds fraction of the time. */ |
| 385 | long msec; |
| 386 | |
| 387 | } pj_time_val; |
| 388 | |
| 389 | /** |
| 390 | * Normalize the value in time value. |
| 391 | * @param t Time value to be normalized. |
| 392 | */ |
| 393 | PJ_DECL(void) pj_time_val_normalize(pj_time_val *t); |
| 394 | |
| 395 | /** |
| 396 | * Get the total time value in miliseconds. This is the same as |
| 397 | * multiplying the second part with 1000 and then add the miliseconds |
| 398 | * part to the result. |
| 399 | * |
| 400 | * @param t The time value. |
| 401 | * @return Total time in miliseconds. |
| 402 | * @hideinitializer |
| 403 | */ |
| 404 | #define PJ_TIME_VAL_MSEC(t) ((t).sec * 1000 + (t).msec) |
| 405 | |
| 406 | /** |
| 407 | * This macro will check if \a t1 is equal to \a t2. |
| 408 | * |
| 409 | * @param t1 The first time value to compare. |
| 410 | * @param t2 The second time value to compare. |
| 411 | * @return Non-zero if both time values are equal. |
| 412 | * @hideinitializer |
| 413 | */ |
| 414 | #define PJ_TIME_VAL_EQ(t1, t2) ((t1).sec==(t2).sec && (t1).msec==(t2).msec) |
| 415 | |
| 416 | /** |
| 417 | * This macro will check if \a t1 is greater than \a t2 |
| 418 | * |
| 419 | * @param t1 The first time value to compare. |
| 420 | * @param t2 The second time value to compare. |
| 421 | * @return Non-zero if t1 is greater than t2. |
| 422 | * @hideinitializer |
| 423 | */ |
| 424 | #define PJ_TIME_VAL_GT(t1, t2) ((t1).sec>(t2).sec || \ |
| 425 | ((t1).sec==(t2).sec && (t1).msec>(t2).msec)) |
| 426 | |
| 427 | /** |
| 428 | * This macro will check if \a t1 is greater than or equal to \a t2 |
| 429 | * |
| 430 | * @param t1 The first time value to compare. |
| 431 | * @param t2 The second time value to compare. |
| 432 | * @return Non-zero if t1 is greater than or equal to t2. |
| 433 | * @hideinitializer |
| 434 | */ |
| 435 | #define PJ_TIME_VAL_GTE(t1, t2) (PJ_TIME_VAL_GT(t1,t2) || \ |
| 436 | PJ_TIME_VAL_EQ(t1,t2)) |
| 437 | |
| 438 | /** |
| 439 | * This macro will check if \a t1 is less than \a t2 |
| 440 | * |
| 441 | * @param t1 The first time value to compare. |
| 442 | * @param t2 The second time value to compare. |
| 443 | * @return Non-zero if t1 is less than t2. |
| 444 | * @hideinitializer |
| 445 | */ |
| 446 | #define PJ_TIME_VAL_LT(t1, t2) (!(PJ_TIME_VAL_GTE(t1,t2))) |
| 447 | |
| 448 | /** |
| 449 | * This macro will check if \a t1 is less than or equal to \a t2. |
| 450 | * |
| 451 | * @param t1 The first time value to compare. |
| 452 | * @param t2 The second time value to compare. |
| 453 | * @return Non-zero if t1 is less than or equal to t2. |
| 454 | * @hideinitializer |
| 455 | */ |
| 456 | #define PJ_TIME_VAL_LTE(t1, t2) (!PJ_TIME_VAL_GT(t1, t2)) |
| 457 | |
| 458 | /** |
| 459 | * Add \a t2 to \a t1 and store the result in \a t1. Effectively |
| 460 | * |
| 461 | * this macro will expand as: (\a t1 += \a t2). |
| 462 | * @param t1 The time value to add. |
| 463 | * @param t2 The time value to be added to \a t1. |
| 464 | * @hideinitializer |
| 465 | */ |
| 466 | #define PJ_TIME_VAL_ADD(t1, t2) do { \ |
| 467 | (t1).sec += (t2).sec; \ |
| 468 | (t1).msec += (t2).msec; \ |
| 469 | pj_time_val_normalize(&(t1)); \ |
| 470 | } while (0) |
| 471 | |
| 472 | |
| 473 | /** |
| 474 | * Substract \a t2 from \a t1 and store the result in \a t1. Effectively |
| 475 | * this macro will expand as (\a t1 -= \a t2). |
| 476 | * |
| 477 | * @param t1 The time value to subsctract. |
| 478 | * @param t2 The time value to be substracted from \a t1. |
| 479 | * @hideinitializer |
| 480 | */ |
| 481 | #define PJ_TIME_VAL_SUB(t1, t2) do { \ |
| 482 | (t1).sec -= (t2).sec; \ |
| 483 | (t1).msec -= (t2).msec; \ |
| 484 | pj_time_val_normalize(&(t1)); \ |
| 485 | } while (0) |
| 486 | |
| 487 | |
| 488 | /** |
| 489 | * This structure represent the parsed representation of time. |
| 490 | * It is acquired by calling #pj_time_decode(). |
| 491 | */ |
| 492 | typedef struct pj_parsed_time |
| 493 | { |
| 494 | /** This represents day of week where value zero means Sunday */ |
| 495 | int wday; |
| 496 | |
| 497 | /* This represents day of the year, 0-365, where zero means |
| 498 | * 1st of January. |
| 499 | */ |
| 500 | /*int yday; */ |
| 501 | |
| 502 | /** This represents day of month: 1-31 */ |
| 503 | int day; |
| 504 | |
| 505 | /** This represents month, with the value is 0 - 11 (zero is January) */ |
| 506 | int mon; |
| 507 | |
| 508 | /** This represent the actual year (unlike in ANSI libc where |
| 509 | * the value must be added by 1900). |
| 510 | */ |
| 511 | int year; |
| 512 | |
| 513 | /** This represents the second part, with the value is 0-59 */ |
| 514 | int sec; |
| 515 | |
| 516 | /** This represents the minute part, with the value is: 0-59 */ |
| 517 | int min; |
| 518 | |
| 519 | /** This represents the hour part, with the value is 0-23 */ |
| 520 | int hour; |
| 521 | |
| 522 | /** This represents the milisecond part, with the value is 0-999 */ |
| 523 | int msec; |
| 524 | |
| 525 | } pj_parsed_time; |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * @} // Time Management |
| 530 | */ |
| 531 | |
| 532 | /* ************************************************************************* */ |
| 533 | /* |
| 534 | * Terminal. |
| 535 | */ |
| 536 | /** |
| 537 | * Color code combination. |
| 538 | */ |
| 539 | enum { |
| 540 | PJ_TERM_COLOR_R = 2, /**< Red */ |
| 541 | PJ_TERM_COLOR_G = 4, /**< Green */ |
| 542 | PJ_TERM_COLOR_B = 1, /**< Blue. */ |
| 543 | PJ_TERM_COLOR_BRIGHT = 8 /**< Bright mask. */ |
| 544 | }; |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | PJ_END_DECL |
| 550 | |
| 551 | |
| 552 | #endif /* __PJ_TYPES_H__ */ |
| 553 | |