Debian Codegraph @

Xpjproject/pjlib/include/pj/types.h

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