Main Page | Class List | Directories | File List | Class Members | File Members

pike_macros.h

Go to the documentation of this file.
00001 /*
00002 || This file is part of Pike. For copyright information see COPYRIGHT.
00003 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
00004 || for more information.
00005 || $Id: pike_macros.h,v 1.39 2005/03/15 09:58:35 grubba Exp $
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 /* #define ALIGNOF(X) PTR_TO_INT(&(((struct { char ignored_ ; X fooo_; } *)NULL)->fooo_)) */
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 /* !HAVE_ISGRAPH */
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 /* This variant never leaves p pointing at a deallocated block, as the
00072  * one above can do. I.e. it frees a ref to the item p points at, and
00073  * sets p to the same or next item with references, or sets it to
00074  * zero. */
00075 /* how can SET_NEXT_AND_FREE leave *next* pointing to a deallocated block?
00076  * -Hubbe
00077  */
00078 /* Afaik it doesn't, but it leaves no value that is usable for the
00079  * purposes this macro was made for. /mast */
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 /* Useful to get a literal comma in an argument to a macro. */
00129 #define COMMA ,
00130 
00131 /* Needed for fsort_template.h */
00132 PMOD_EXPORT int my_log2(size_t x);
00133 
00134 #endif

Generated on Fri Jul 22 23:44:26 2005 for Pike by  doxygen 1.3.9.1