plibsys 0.0.5
ptypes.h
Go to the documentation of this file.
1/*
2 * The MIT License
3 *
4 * Copyright (C) 2010-2016 Alexander Saprykin <saprykin.spb@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * 'Software'), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
60#if !defined (PLIBSYS_H_INSIDE) && !defined (PLIBSYS_COMPILATION)
61# error "Header files shouldn't be included directly, consider using <plibsys.h> instead."
62#endif
63
64#ifndef PLIBSYS_HEADER_PTYPES_H
65#define PLIBSYS_HEADER_PTYPES_H
66
67#include <plibsysconfig.h>
68#include <pmacros.h>
69
71
73typedef signed char pint8;
75typedef unsigned char puint8;
77typedef signed short pint16;
79typedef unsigned short puint16;
81typedef signed int pint32;
83typedef unsigned int puint32;
84
95#if defined (P_OS_WIN) && (defined (P_CC_MSVC) || defined (P_CC_BORLAND))
96 typedef signed __int64 pint64;
97 typedef unsigned __int64 puint64;
98#else
99# if PLIBSYS_SIZEOF_LONG == 8
100 typedef signed long pint64;
101 typedef unsigned long puint64;
102# else
103 typedef signed long long pint64;
104 typedef unsigned long long puint64;
105# endif
106#endif
107
109typedef void * ppointer;
111typedef const void * pconstpointer;
112
114typedef signed int pboolean;
116typedef char pchar;
118typedef short pshort;
120typedef int pint;
122typedef long plong;
123
125typedef unsigned char puchar;
127typedef unsigned short pushort;
129typedef unsigned int puint;
131typedef unsigned long pulong;
132
134typedef float pfloat;
136typedef double pdouble;
137
191#if PLIBSYS_SIZEOF_SIZE_T == 8
192# if defined (P_OS_WIN) && (defined (P_CC_MSVC) || defined (P_CC_BORLAND))
193 typedef signed __int64 pssize;
194 typedef unsigned __int64 psize;
195 #define PSIZE_MODIFIER "I64"
196 #define PSSIZE_FORMAT "I64d"
197 #define PSIZE_FORMAT "I64u"
198 #define P_MAXSIZE P_MAXUINT64
199 #define P_MINSSIZE P_MININT64
200 #define P_MAXSSIZE P_MAXINT64
201# else
202# if PLIBSYS_SIZEOF_LONG == 8
203 typedef long pssize;
204 typedef unsigned long psize;
205 #define PSIZE_MODIFIER "l"
206 #define PSSIZE_FORMAT "li"
207 #define PSIZE_FORMAT "lu"
208 #define P_MAXSIZE P_MAXULONG
209 #define P_MINSSIZE P_MINLONG
210 #define P_MAXSSIZE P_MAXLONG
211# else
212 typedef long long pssize;
213 typedef unsigned long long psize;
214 #define PSIZE_MODIFIER "ll"
215 #define PSSIZE_FORMAT "lli"
216 #define PSIZE_FORMAT "llu"
217 #define P_MAXSIZE P_MAXUINT64
218 #define P_MINSSIZE P_MININT64
219 #define P_MAXSSIZE P_MAXINT64
220# endif
221# endif
222#else
223 typedef signed int pssize;
224 typedef unsigned int psize;
225 #define PSIZE_MODIFIER ""
226 #define PSSIZE_FORMAT "i"
227 #define PSIZE_FORMAT "u"
228 #define P_MAXSIZE P_MAXUINT
229 #define P_MINSSIZE P_MININT
230 #define P_MAXSSIZE P_MAXINT
231#endif
232
265#if PLIBSYS_SIZEOF_VOID_P == 8
266# if defined (P_OS_WIN) && (defined (P_CC_MSVC) || defined (P_CC_BORLAND))
267 typedef signed __int64 pintptr;
268 typedef unsigned __int64 puintptr;
269 #define PINTPTR_MODIFIER "I64"
270 #define PINTPTR_FORMAT "I64i"
271 #define PUINTPTR_FORMAT "I64u"
272# else
273# if PLIBSYS_SIZEOF_LONG == 8
274 typedef long pintptr;
275 typedef unsigned long puintptr;
276 #define PINTPTR_MODIFIER "l"
277 #define PINTPTR_FORMAT "li"
278 #define PUINTPTR_FORMAT "lu"
279# else
280 typedef long long pintptr;
281 typedef unsigned long long puintptr;
282 #define PINTPTR_MODIFIER "ll"
283 #define PINTPTR_FORMAT "lli"
284 #define PUINTPTR_FORMAT "llu"
285# endif
286# endif
287#else
288 typedef signed int pintptr;
289 typedef unsigned int puintptr;
290 #define PINTPTR_MODIFIER ""
291 #define PINTPTR_FORMAT "i"
292 #define PUINTPTR_FORMAT "u"
293#endif
294
297
298#if PLIBSYS_SIZEOF_VOID_P == 8
299# define P_INT_TO_POINTER(i) ((void *) (long long) (i))
300# define P_POINTER_TO_INT(p) ((int) (long long) (p))
301# define PPOINTER_TO_INT(p) ((pint) ((pint64) (p)))
302# define PPOINTER_TO_UINT(p) ((puint) ((puint64) (p)))
303# define PINT_TO_POINTER(i) ((ppointer) (pint64) (i))
304# define PUINT_TO_POINTER(u) ((ppointer) (puint64) (u))
305#else
306# define P_INT_TO_POINTER(i) ((void *) (long) (i))
307# define P_POINTER_TO_INT(p) ((int) (long) (p))
308# define PPOINTER_TO_INT(p) ((pint) ((plong) (p)))
309# define PPOINTER_TO_UINT(p) ((puint) ((pulong) (p)))
310# define PINT_TO_POINTER(i) ((ppointer) (plong) (i))
311# define PUINT_TO_POINTER(u) ((ppointer) (pulong) (u))
312#endif
313
363#define PSIZE_TO_POINTER(i) ((ppointer) ((psize) (i)))
365#define PPOINTER_TO_PSIZE(p) ((psize) (p))
366
368#define P_MININT8 ((pint8) 0x80)
370#define P_MAXINT8 ((pint8) 0x7F)
372#define P_MAXUINT8 ((puint8) 0xFF)
373
375#define P_MININT16 ((pint16) 0x8000)
377#define P_MAXINT16 ((pint16) 0x7FFF)
379#define P_MAXUINT16 ((puint16) 0xFFFF)
380
382#define P_MININT32 ((pint32) 0x80000000)
384#define P_MAXINT32 ((pint32) 0x7FFFFFFF)
386#define P_MAXUINT32 ((puint32) 0xFFFFFFFF)
387
389#define P_MININT64 ((pint64) 0x8000000000000000LL)
391#define P_MAXINT64 ((pint64) 0x7FFFFFFFFFFFFFFFLL)
393#define P_MAXUINT64 ((puint64) 0xFFFFFFFFFFFFFFFFULL)
394
469#if defined (P_OS_WIN) && (defined (P_CC_MSVC) || defined (P_CC_BORLAND))
470 #define PINT16_MODIFIER "h"
471#else
472 #define PINT16_MODIFIER ""
473#endif
474
475#define PINT16_FORMAT "hi"
476#define PUINT16_FORMAT "hu"
477
478#define PINT32_MODIFIER ""
479#define PINT32_FORMAT "i"
480#define PUINT32_FORMAT "u"
481
482#if defined (P_OS_WIN) && (defined (P_CC_MSVC) || defined (P_CC_BORLAND))
483 #define PINT64_MODIFIER "I64"
484 #define PINT64_FORMAT "I64i"
485 #define PUINT64_FORMAT "I64u"
486#else
487# if PLIBSYS_SIZEOF_LONG == 8
488 #define PINT64_MODIFIER "l"
489 #define PINT64_FORMAT "li"
490 #define PUINT64_FORMAT "lu"
491# else
492 #define PINT64_MODIFIER "ll"
493 #define PINT64_FORMAT "lli"
494 #define PUINT64_FORMAT "llu"
495# endif
496#endif
497
498#define POFFSET_MODIFIER PINT64_MODIFIER
499#define POFFSET_FORMAT PINT64_FORMAT
500
501/* Endian checks, see P_BYTE_ORDER in plibsysconfig.h */
502
504#define P_LITTLE_ENDIAN 1234
506#define P_BIG_ENDIAN 4321
507
700#if P_BYTE_ORDER == P_LITTLE_ENDIAN
701 #define PINT16_TO_LE(val) ((pint16) (val))
702 #define PUINT16_TO_LE(val) ((puint16) (val))
703 #define PINT16_TO_BE(val) ((pint16) PUINT16_SWAP_BYTES (val))
704 #define PUINT16_TO_BE(val) (PUINT16_SWAP_BYTES (val))
705 #define PINT32_TO_LE(val) ((pint32) (val))
706 #define PUINT32_TO_LE(val) ((puint32) (val))
707 #define PINT32_TO_BE(val) ((pint32) PUINT32_SWAP_BYTES (val))
708 #define PUINT32_TO_BE(val) (PUINT32_SWAP_BYTES (val))
709 #define PINT64_TO_LE(val) ((pint64) (val))
710 #define PUINT64_TO_LE(val) ((puint64) (val))
711 #define PINT64_TO_BE(val) ((pint64) PUINT64_SWAP_BYTES (val))
712 #define PUINT64_TO_BE(val) (PUINT64_SWAP_BYTES (val))
713# if PLIBSYS_SIZEOF_LONG == 8
714 #define PLONG_TO_LE(val) ((plong) PINT64_TO_LE (val))
715 #define PULONG_TO_LE(val) ((pulong) PUINT64_TO_LE (val))
716 #define PLONG_TO_BE(val) ((plong) PINT64_TO_BE (val))
717 #define PULONG_TO_BE(val) ((pulong) PUINT64_TO_BE (val))
718# else
719 #define PLONG_TO_LE(val) ((plong) PINT32_TO_LE (val))
720 #define PULONG_TO_LE(val) ((pulong) PUINT32_TO_LE (val))
721 #define PLONG_TO_BE(val) ((plong) PINT32_TO_BE (val))
722 #define PULONG_TO_BE(val) ((pulong) PUINT32_TO_BE (val))
723# endif
724# if PLIBSYS_SIZEOF_SIZE_T == 8
725 #define PSIZE_TO_LE(val) ((psize) PUINT64_TO_LE (val))
726 #define PSSIZE_TO_LE(val) ((pssize) PINT64_TO_LE (val))
727 #define PSIZE_TO_BE(val) ((psize) PUINT64_TO_BE (val))
728 #define PSSIZE_TO_BE(val) ((pssize) PINT64_TO_BE (val))
729# else
730 #define PSIZE_TO_LE(val) ((psize) PUINT32_TO_LE (val))
731 #define PSSIZE_TO_LE(val) ((pssize) PINT32_TO_LE (val))
732 #define PSIZE_TO_BE(val) ((psize) PUINT32_TO_BE (val))
733 #define PSSIZE_TO_BE(val) ((pssize) PINT32_TO_BE (val))
734# endif
735 #define PINT_TO_LE(val) ((pint) PINT32_TO_LE (val))
736 #define PUINT_TO_LE(val) ((puint) PUINT32_TO_LE (val))
737 #define PINT_TO_BE(val) ((pint) PINT32_TO_BE (val))
738 #define PUINT_TO_BE(val) ((puint) PUINT32_TO_BE (val))
739
740#else
741 #define PINT16_TO_LE(val) ((pint16) PUINT16_SWAP_BYTES (val))
742 #define PUINT16_TO_LE(val) (PUINT16_SWAP_BYTES (val))
743 #define PINT16_TO_BE(val) ((pint16) (val))
744 #define PUINT16_TO_BE(val) ((puint16) (val))
745 #define PINT32_TO_LE(val) ((pint32) PUINT32_SWAP_BYTES (val))
746 #define PUINT32_TO_LE(val) (PUINT32_SWAP_BYTES (val))
747 #define PINT32_TO_BE(val) ((pint32) (val))
748 #define PUINT32_TO_BE(val) ((puint32) (val))
749 #define PINT64_TO_LE(val) ((pint64) PUINT64_SWAP_BYTES (val))
750 #define PUINT64_TO_LE(val) (PUINT64_SWAP_BYTES (val))
751 #define PINT64_TO_BE(val) ((pint64) (val))
752 #define PUINT64_TO_BE(val) ((puint64) (val))
753# if PLIBSYS_SIZEOF_LONG == 8
754 #define PLONG_TO_LE(val) ((plong) PINT64_TO_LE (val))
755 #define PULONG_TO_LE(val) ((pulong) PUINT64_TO_LE (val))
756 #define PLONG_TO_BE(val) ((plong) PINT64_TO_BE (val))
757 #define PULONG_TO_BE(val) ((pulong) PUINT64_TO_BE (val))
758# else
759 #define PLONG_TO_LE(val) ((plong) PINT32_TO_LE (val))
760 #define PULONG_TO_LE(val) ((pulong) PUINT32_TO_LE (val))
761 #define PLONG_TO_BE(val) ((plong) PINT32_TO_BE (val))
762 #define PULONG_TO_BE(val) ((pulong) PUINT32_TO_BE (val))
763# endif
764# if PLIBSYS_SIZEOF_SIZE_T == 8
765 #define PSIZE_TO_LE(val) ((psize) PUINT64_TO_LE (val))
766 #define PSSIZE_TO_LE(val) ((pssize) PINT64_TO_LE (val))
767 #define PSIZE_TO_BE(val) ((psize) PUINT64_TO_BE (val))
768 #define PSSIZE_TO_BE(val) ((pssize) PINT64_TO_BE (val))
769# else
770 #define PSIZE_TO_LE(val) ((psize) PUINT32_TO_LE (val))
771 #define PSSIZE_TO_LE(val) ((pssize) PINT32_TO_LE (val))
772 #define PSIZE_TO_BE(val) ((psize) PUINT32_TO_BE (val))
773 #define PSSIZE_TO_BE(val) ((pssize) PINT32_TO_BE (val))
774# endif
775 #define PINT_TO_LE(val) ((pint) PINT32_TO_LE (val))
776 #define PUINT_TO_LE(val) ((puint) PUINT32_TO_LE (val))
777 #define PINT_TO_BE(val) ((pint) PINT32_TO_BE (val))
778 #define PUINT_TO_BE(val) ((puint) PUINT32_TO_BE (val))
779#endif
780
781/* Functions for bit swapping */
782
789#define PUINT16_SWAP_BYTES(val) \
790 ((puint16) (((puint16) (val)) >> 8 | ((puint16) (val)) << 8))
791
798#define PUINT32_SWAP_BYTES(val) ((puint32) ( \
799 (((puint32) (val)) >> 24) | \
800 ((((puint32) (val)) << 8) & ((puint32) 0x00FF0000U)) | \
801 ((((puint32) (val)) >> 8) & ((puint32) 0x0000FF00U)) | \
802 (((puint32) (val)) << 24)))
803
810#define PUINT64_SWAP_BYTES(val) ((puint64) ( \
811 (((puint64) (val)) >> 56) | \
812 ((((puint64) (val)) << 40) & ((puint64) 0x00FF000000000000ULL)) | \
813 ((((puint64) (val)) << 24) & ((puint64) 0x0000FF0000000000ULL)) | \
814 ((((puint64) (val)) << 8) & ((puint64) 0x000000FF00000000ULL)) | \
815 ((((puint64) (val)) >> 8) & ((puint64) 0x00000000FF000000ULL)) | \
816 ((((puint64) (val)) >> 24) & ((puint64) 0x0000000000FF0000ULL)) | \
817 ((((puint64) (val)) >> 40) & ((puint64) 0x000000000000FF00ULL)) | \
818 (((puint64) (val)) << 56)))
819
820/* Functions, similar to ?_TO_? functions */
821
828#define PINT16_FROM_LE(val) (PINT16_TO_LE (val))
829
837#define PUINT16_FROM_LE(val) (PUINT16_TO_LE (val))
838
845#define PINT16_FROM_BE(val) (PINT16_TO_BE (val))
846
854#define PUINT16_FROM_BE(val) (PUINT16_TO_BE (val))
855
856
863#define PINT32_FROM_LE(val) (PINT32_TO_LE (val))
864
872#define PUINT32_FROM_LE(val) (PUINT32_TO_LE (val))
873
880#define PINT32_FROM_BE(val) (PINT32_TO_BE (val))
881
889#define PUINT32_FROM_BE(val) (PUINT32_TO_BE (val))
890
897#define PINT64_FROM_LE(val) (PINT64_TO_LE (val))
898
906#define PUINT64_FROM_LE(val) (PUINT64_TO_LE (val))
907
914#define PINT64_FROM_BE(val) (PINT64_TO_BE (val))
915
923#define PUINT64_FROM_BE(val) (PUINT64_TO_BE (val))
924
931#define PLONG_FROM_LE(val) (PLONG_TO_LE (val))
932
940#define PULONG_FROM_LE(val) (PULONG_TO_LE (val))
941
948#define PLONG_FROM_BE(val) (PLONG_TO_BE (val))
949
957#define PULONG_FROM_BE(val) (PULONG_TO_BE (val))
958
965#define PINT_FROM_LE(val) (PINT_TO_LE (val))
966
973#define PUINT_FROM_LE(val) (PUINT_TO_LE (val))
974
981#define PINT_FROM_BE(val) (PINT_TO_BE (val))
982
989#define PUINT_FROM_BE(val) (PUINT_TO_BE (val))
990
997#define PSIZE_FROM_LE(val) (PSIZE_TO_LE (val))
998
1005#define PSSIZE_FROM_LE(val) (PSSIZE_TO_LE (val))
1006
1013#define PSIZE_FROM_BE(val) (PSIZE_TO_BE (val))
1014
1021#define PSSIZE_FROM_BE(val) (PSSIZE_TO_BE (val))
1022
1023/* Host-network order functions */
1024
1031#define p_ntohl(val) (PUINT32_FROM_BE (val))
1032
1039#define p_ntohs(val) (PUINT16_FROM_BE (val))
1040
1047#define p_htonl(val) (PUINT32_TO_BE (val))
1048
1055#define p_htons(val) (PUINT16_TO_BE (val))
1056
1057#ifndef FALSE
1059# define FALSE (0)
1060#endif
1061
1062#ifndef TRUE
1064# define TRUE (!FALSE)
1065#endif
1066
1070typedef void * P_HANDLE;
1071
1081 ppointer value,
1082 ppointer user_data);
1083
1090typedef void (*PFunc) (ppointer data, ppointer user_data);
1091
1097typedef void (*PDestroyFunc) (ppointer data);
1098
1108
1119
1121
1122#endif /* PLIBSYS_HEADER_PTYPES_H */
Miscellaneous macros.
#define P_BEGIN_DECLS
Starts .h file declarations to be exported as C functions.
Definition pmacros.h:298
#define P_END_DECLS
Closes .h file declarations to be exported as C functions, should be always used after P_BEGIN_DECLS.
Definition pmacros.h:299
void(* PFunc)(ppointer data, ppointer user_data)
General purpose function.
Definition ptypes.h:1090
signed int pssize
Type for a platform independent signed size_t.
Definition ptypes.h:223
unsigned char puint8
Type for unsigned 8 bit.
Definition ptypes.h:75
void(* PDestroyFunc)(ppointer data)
Object destroy notification function.
Definition ptypes.h:1097
signed __int64 pint64
Type for signed 64 bit.
Definition ptypes.h:96
unsigned short puint16
Type for unsigned 16 bit.
Definition ptypes.h:79
short pshort
Type for a short.
Definition ptypes.h:118
char pchar
Type for a char.
Definition ptypes.h:116
unsigned int psize
Type for a platform independent size_t.
Definition ptypes.h:224
pint(* PCompareDataFunc)(pconstpointer a, pconstpointer b, ppointer data)
Compares two values with additional data.
Definition ptypes.h:1118
const void * pconstpointer
Type for a const pointer.
Definition ptypes.h:111
void * P_HANDLE
Platform independent system handle.
Definition ptypes.h:1070
void * ppointer
Type for a pointer.
Definition ptypes.h:109
unsigned short pushort
Type for an unsigned short.
Definition ptypes.h:127
signed char pint8
Type for signed 8 bit.
Definition ptypes.h:73
float pfloat
Type for a float.
Definition ptypes.h:134
unsigned int puint
Type for an unsigned int.
Definition ptypes.h:129
signed int pint32
Type for signed 32 bit.
Definition ptypes.h:81
unsigned int puintptr
Type for a platform independent unsigned pointer represented by an integer.
Definition ptypes.h:289
unsigned long pulong
Type for an unsigned long.
Definition ptypes.h:131
pint64 poffset
Platform independent offset_t definition.
Definition ptypes.h:296
pint(* PCompareFunc)(pconstpointer a, pconstpointer b)
Compares two values.
Definition ptypes.h:1107
long plong
Type for a long.
Definition ptypes.h:122
int pint
Type for an int.
Definition ptypes.h:120
unsigned __int64 puint64
Type for unsigned 64 bit.
Definition ptypes.h:97
signed int pintptr
Type for a platform independent signed pointer represented by an integer.
Definition ptypes.h:288
double pdouble
Type for a double precision float.
Definition ptypes.h:136
unsigned char puchar
Type for an unsigned char.
Definition ptypes.h:125
pboolean(* PTraverseFunc)(ppointer key, ppointer value, ppointer user_data)
Function to traverse through a key-value container.
Definition ptypes.h:1080
signed short pint16
Type for signed 16 bit.
Definition ptypes.h:77
signed int pboolean
Type for a bool.
Definition ptypes.h:114
unsigned int puint32
Type for unsigned 32 bit.
Definition ptypes.h:83