diff options
author | cathook <cat.hook31894@gmail.com> | 2013-11-25 00:37:05 +0800 |
---|---|---|
committer | cathook <cat.hook31894@gmail.com> | 2013-11-25 00:37:05 +0800 |
commit | 3f420e030428bef9327b458154386dfc62ca6319 (patch) | |
tree | c94fab9a1569608af4ed3c1bd62e91746ecade0a | |
parent | a582b6f1cb1f0c3052acec762c267daeb8d1c7ee (diff) | |
parent | a95b870df24dc01ebaa3e7def25ae2fd716c779f (diff) | |
download | ctl-3f420e030428bef9327b458154386dfc62ca6319.tar.gz ctl-3f420e030428bef9327b458154386dfc62ca6319.tar.zst ctl-3f420e030428bef9327b458154386dfc62ca6319.zip |
Merge branch 'feature-utility'
-rw-r--r-- | include/utility.h | 96 | ||||
-rw-r--r-- | src/utility.c | 17 |
2 files changed, 101 insertions, 12 deletions
diff --git a/include/utility.h b/include/utility.h index 7f41382..2e4330d 100644 --- a/include/utility.h +++ b/include/utility.h @@ -3,20 +3,104 @@ #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)** */ +/* */ +/* Type transform: */ +/* Char(), pChar(), ppChar() */ +/* Void(), pVoid(), ppVoid() */ +/* cChar(), pcChar(), ppcChar() */ +/* cVoid(), pcVoid(), ppcVoid() */ +/* */ +/* Functions: */ +/* ctl_malloc like malloc(), but will exit on error */ +/* ctl_realloc like realloc(), but will exit on erro */ +/* ctl_die print some message and exit() */ +/* ctl_swap swap two elements with given type */ +/* */ +/**********************************************************/ + typedef enum{ BAD_MEMORY = 0x01 } ErrorType; -typedef unsigned int uint; +// 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)) -#define pVOID(X) ((void* )X) -#define ppVOID(X) ((void**)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)) -#define pCHAR(X) ((char* )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)) -void *ctl_malloc (size_t size); -void *ctl_realloc(void *ptr, size_t size); +pvoid ctl_malloc (size_t size); +pvoid ctl_realloc(pvoid *ptr, size_t size); void ctl_die (ErrorType e); +#define ctl_swap(X,Y,Z) do{X zzzztmp=(Y);(Y)=(Z);(Z)=zzzztmp;}while(0) #endif /* __utility_h__ */ diff --git a/src/utility.c b/src/utility.c index 7000c02..bcc2500 100644 --- a/src/utility.c +++ b/src/utility.c @@ -4,26 +4,31 @@ #include <stdlib.h> #include <stddef.h> -void *ctl_malloc(size_t size){ +/*********** like malloc(), but will exit on error ********/ +pvoid ctl_malloc(size_t size){ void *ptr = malloc(size); if(ptr == NULL){ - ctl_die(1); + ctl_die(BAD_MEMORY); } return ptr; } -void *ctl_realloc(void *ptr, size_t size){ + +/********** like realloc(), but will exit on error ********/ +pvoid ctl_realloc(pvoid ptr, size_t size){ ptr = realloc(ptr, size); if(ptr == NULL){ - ctl_die(1); + ctl_die(BAD_MEMORY); } return ptr; } +/********* print some message on STDERR and exit() ********/ void ctl_die(ErrorType e){ + fprintf(stderr, "exit(%d): ", (int)e); switch(e){ case BAD_MEMORY: - fprintf(stderr, "exit: bad memory mananger\n"); + fprintf(stderr, "bad memory mananger\n"); break; } - exit(e); + exit((int)e); } |