aboutsummaryrefslogtreecommitdiffstats
path: root/inc/utility.h
blob: 5e374a07b8ce67088de7f8aab222e23f62ec4a91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef   __utility_h__
#define   __utility_h__

#include <stddef.h>

/**********************************************************/
/* This object contain some useful functions,  constants  */
/* and types.                                             */
/*   Enums:                                               */
/*     ErrorType    contain kinds of errors.              */
/*                                                        */
/*   Structures:                                          */
/*                                                        */
/*   Types:                                               */
/*     uint         unsigned int                          */
/*     pchar        char*                                 */
/*     ppchar       char**                                */
/*     pvoid        void*                                 */
/*     ppvoid       void**                                */
/*     cchar        const char                            */
/*     pcchar       (const char)*                         */
/*     ppcchar      (const char)**                        */
/*     cvoid        const void                            */
/*     pcvoid       (cosnt void)*                         */
/*     ppcvoid      (const void)**                        */
/*     ... etc                                            */
/*                                                        */
/*  Type transform:                                       */
/*    Char(), pChar(), ppChar()                           */
/*    Void(), pVoid(), ppVoid()                           */
/*    cChar(), pcChar(), ppcChar()                        */
/*    cVoid(), pcVoid(), ppcVoid()                        */
/*    ... etc                                             */
/*                                                        */
/*  Functions:                                            */
/*     ctl_malloc   like malloc(), but will exit on error */
/*     ctl_realloc  like realloc(), but will exit on erro */
/*     ctl_free     like free(), but will return NULL     */
/*     ctl_die      print some message and exit()         */
/*     ctl_swap     swap two elements with given type     */
/*     ctl_max      return the max of the two arguments   */
/*     ctl_min      return the min of the two arguments   */
/*                                                        */
/**********************************************************/

typedef enum{
    CTL_MEMORY = 0x01,
    CTL_USER   = 0x02
} CtlErrorType;

//                         int 
typedef          int*     pint;
typedef unsigned int      uint;
typedef         uint*    puint;
typedef    const int      cint;
typedef         cint*    pcint;
typedef   const uint     cuint;
typedef        cuint*   pcuint;
#define    Int(X) ((   int)(X))
#define   pInt(X) ((  pint)(X))
#define   uInt(X) ((  uint)(X))
#define  puInt(X) (( puint)(X))
#define   cInt(X) ((  cint)(X))
#define  pcInt(X) (( pcint)(X))
#define  cuInt(X) (( cuint)(X))
#define pcuInt(X) ((pcuint)(X))

typedef          short*     pshort;
typedef unsigned short      ushort;
typedef         ushort*    pushort;
typedef    const short      cshort;
typedef         cshort*    pcshort;
typedef   const ushort     cushort;
typedef        cushort*   pcushort;
#define    Short(X) ((   short)(X))
#define   pShort(X) ((  pshort)(X))
#define   uShort(X) ((  ushort)(X))
#define  puShort(X) (( pushort)(X))
#define   cShort(X) ((  cshort)(X))
#define  pcShort(X) (( pcshort)(X))
#define  cuShort(X) (( cushort)(X))
#define pcuShort(X) ((pcushort)(X))

//                        void 
typedef         void*    pvoid;
typedef        pvoid*   ppvoid;
typedef   const void     cvoid;
typedef        cvoid*   pcvoid;
typedef       pcvoid*  ppcvoid;
#define    Void(X) ((   void)(X))
#define   pVoid(X) ((  pvoid)(X))
#define  ppVoid(X) (( ppvoid)(X))
#define   cVoid(X) ((  cvoid)(X))
#define  pcVoid(X) (( pcvoid)(X))
#define ppcVoid(X) ((ppcvoid)(X))

//                         char 
typedef          char*    pchar;
typedef         pchar*   ppchar;
typedef unsigned char     uchar;
typedef         uchar*   puchar;
typedef        puchar*  ppuchar;
typedef    const char     cchar;
typedef         cchar*   pcchar;
typedef        pcchar*  ppcchar;
typedef   const uchar    cuchar;
typedef        cuchar*  pcuchar;
typedef       pcuchar* ppcuchar;
#define     Char(X) ((    char)(X))
#define    pChar(X) ((   pchar)(X))
#define   ppChar(X) ((  ppchar)(X))
#define    uChar(X) ((   uchar)(X))
#define   puChar(X) ((  puchar)(X))
#define  ppuChar(X) (( ppuchar)(X))
#define    cChar(X) ((   cchar)(X))
#define   pcChar(X) ((  pcchar)(X))
#define  ppcChar(X) (( ppcchar)(X))
#define   cuChar(X) ((  cuchar)(X))
#define  pcuChar(X) (( pcuchar)(X))
#define ppcuChar(X) ((ppcuchar)(X))

//
typedef int (*ctl_cmp_func)(pcvoid,pcvoid);

#define ctl_priv static inline 


pvoid ctl_malloc (size_t size);
pvoid ctl_realloc(pvoid ptr, size_t size);
pvoid ctl_free   (pvoid ptr);
void  ctl_die    (CtlErrorType e);
int   ctl_strcmpL(pcchar a, pcchar b);

#define ctl_swap(X,Y,Z) do{X zzzztmp=(Y);(Y)=(Z);(Z)=zzzztmp;}while(0)
#define ctl_max(X,Y)    ((X) > (Y) ? (X) : (Y))
#define ctl_min(X,Y)    ((X) < (Y) ? (X) : (Y))

#endif /* __utility_h__ */