00001
00002
00003
00004
00005
00006
00007
00008 #ifndef MACROS_H
00009 #define MACROS_H
00010
00011 #include <global.h>
00012
00013 #ifdef HAVE_SYS_PARAM_H
00014 #include <sys/param.h>
00015 #endif
00016
00017 #include "pike_memory.h"
00018
00019 #define PTR_TO_INT(PTR) ((size_t) ((char *) (PTR) - (char *) NULL))
00020
00021 #define OFFSETOF(str_type, field) \
00022 PTR_TO_INT(& (((struct str_type *)NULL)->field))
00023 #define BASEOF(ptr, str_type, field) \
00024 ((struct str_type *)((char*)ptr - OFFSETOF(str_type, field)))
00025 #ifdef __cplusplus
00026 extern "C++" {
00027 template<typename T> static inline int low_alignof_(T *ignored)
00028 {
00029 struct { char x; T y;} *bar = NULL;
00030 return PTR_TO_INT(&bar->y);
00031 }
00032 };
00033 #define ALIGNOF(X) low_alignof_((X*)NULL)
00034 #else
00035 #define ALIGNOF(X) OFFSETOF({ char ignored_; X fooo_;}, fooo_)
00036 #endif
00037
00038
00039 #define NELEM(a) (sizeof (a) / sizeof ((a)[0]))
00040 #define ALLOC_STRUCT(X) ( (struct X *)xalloc(sizeof(struct X)) )
00041
00042 #define MINIMUM(X,Y) ((X)<(Y)?(X):(Y))
00043 #define MAXIMUM(X,Y) ((X)>(Y)?(X):(Y))
00044
00045
00046 PMOD_EXPORT extern const char Pike_is8bitalnum_vector[];
00047 #define is8bitalnum(X) (Pike_is8bitalnum_vector[((unsigned)(X))&0xff] == '1')
00048
00049 #define isidchar(X) is8bitalnum(X)
00050
00051 #ifndef HAVE_ISGRAPH
00052 #define isgraph(X) (ispunct(X) || isupper(X) || islower(X) || isdigit(X))
00053 #endif
00054
00055
00056
00057 #define DO_ALIGN(X,Y) (((size_t)(X)+((Y)-1)) & -(Y))
00058 #define CONSTANT_STRLEN(X) (sizeof(X) - sizeof(""))
00059
00060 #define SET_NEXT_AND_FREE(p,free_item) do{ \
00061 next=p->next; \
00062 while(p->refs == 1 && (next=p->next)) \
00063 { \
00064 add_ref(next); \
00065 free_item(p); \
00066 p=next; \
00067 } \
00068 free_item(p); \
00069 }while(0)
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #define FREE_AND_GET_REFERENCED(p, item_type, free_item) do { \
00081 item_type *next; \
00082 while (1) { \
00083 if (p->refs > 1) { \
00084 free_item(p); \
00085 break; \
00086 } \
00087 if (!(next = p->next)) { \
00088 free_item(p); \
00089 p = 0; \
00090 break; \
00091 } \
00092 add_ref(next); \
00093 free_item(p); \
00094 p = next; \
00095 } \
00096 } while (0)
00097
00098 #define DOUBLELINK(first_object, o) do { \
00099 debug_malloc_touch(o); \
00100 o->next=first_object; \
00101 o->prev=0; \
00102 if(first_object) first_object->prev=o; \
00103 first_object=o; \
00104 }while(0)
00105
00106 #define DOUBLEUNLINK(first_object,o) do{ \
00107 debug_malloc_touch(o); \
00108 if(o->prev) { \
00109 o->prev->next=o->next; \
00110 }else { \
00111 DO_IF_DEBUG( \
00112 if(first_object != o) { \
00113 describe(o); \
00114 Pike_fatal("Linked in wrong list!\n"); \
00115 } \
00116 ) \
00117 first_object=o->next; \
00118 } \
00119 \
00120 if(o->next) o->next->prev=o->prev; \
00121 }while(0)
00122
00123
00124 #define PIKE_XCONCAT(X,Y) PIKE_CONCAT(X,Y)
00125 #define PIKE_XCONCAT3(X,Y,Z) PIKE_CONCAT(X,Y,Z)
00126 #define PIKE_XCONCAT4(X,Y,Z,Q) PIKE_CONCAT(X,Y,Z,Q)
00127
00128
00129 #define COMMA ,
00130
00131
00132 PMOD_EXPORT int my_log2(size_t x);
00133
00134 #endif