misc.h revision 35c4bbdf
1/*********************************************************** 2 3Copyright 1987, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. 26 27 All Rights Reserved 28 29Permission to use, copy, modify, and distribute this software and its 30documentation for any purpose and without fee is hereby granted, 31provided that the above copyright notice appear in all copies and that 32both that copyright notice and this permission notice appear in 33supporting documentation, and that the name of Digital not be 34used in advertising or publicity pertaining to distribution of the 35software without specific, written prior permission. 36 37DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 38ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 39DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 40ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 41WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 42ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 43SOFTWARE. 44 45Copyright 1992, 1993 Data General Corporation; 46Copyright 1992, 1993 OMRON Corporation 47 48Permission to use, copy, modify, distribute, and sell this software and its 49documentation for any purpose is hereby granted without fee, provided that the 50above copyright notice appear in all copies and that both that copyright 51notice and this permission notice appear in supporting documentation, and that 52neither the name OMRON or DATA GENERAL be used in advertising or publicity 53pertaining to distribution of the software without specific, written prior 54permission of the party whose name is to be used. Neither OMRON or 55DATA GENERAL make any representation about the suitability of this software 56for any purpose. It is provided "as is" without express or implied warranty. 57 58OMRON AND DATA GENERAL EACH DISCLAIM ALL WARRANTIES WITH REGARD TO THIS 59SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, 60IN NO EVENT SHALL OMRON OR DATA GENERAL BE LIABLE FOR ANY SPECIAL, INDIRECT 61OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 62DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 63TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 64OF THIS SOFTWARE. 65 66******************************************************************/ 67#ifndef MISC_H 68#define MISC_H 1 69/* 70 * X internal definitions 71 * 72 */ 73 74#include <X11/Xosdefs.h> 75#include <X11/Xfuncproto.h> 76#include <X11/Xmd.h> 77#include <X11/X.h> 78#include <X11/Xdefs.h> 79 80#include <stddef.h> 81#include <stdint.h> 82 83#ifndef MAXSCREENS 84#define MAXSCREENS 16 85#endif 86#ifndef MAXGPUSCREENS 87#define MAXGPUSCREENS 16 88#endif 89#define MAXCLIENTS 512 90#define LIMITCLIENTS 256 /* Must be a power of 2 and <= MAXCLIENTS */ 91#define MAXEXTENSIONS 128 92#define MAXFORMATS 8 93#define MAXDEVICES 40 /* input devices */ 94#define GPU_SCREEN_OFFSET 256 95 96/* 128 event opcodes for core + extension events, excluding GE */ 97#define MAXEVENTS 128 98#define EXTENSION_EVENT_BASE 64 99#define EXTENSION_BASE 128 100 101typedef uint32_t ATOM; 102 103#ifndef TRUE 104#define TRUE 1 105#define FALSE 0 106#endif 107 108#ifndef _XTYPEDEF_CALLBACKLISTPTR 109typedef struct _CallbackList *CallbackListPtr; /* also in dix.h */ 110 111#define _XTYPEDEF_CALLBACKLISTPTR 112#endif 113 114typedef struct _xReq *xReqPtr; 115 116#include "os.h" /* for ALLOCATE_LOCAL and DEALLOCATE_LOCAL */ 117#include <X11/Xfuncs.h> /* for bcopy, bzero, and bcmp */ 118 119#define NullBox ((BoxPtr)0) 120#define MILLI_PER_MIN (1000 * 60) 121#define MILLI_PER_SECOND (1000) 122 123 /* this next is used with None and ParentRelative to tell 124 PaintWin() what to use to paint the background. Also used 125 in the macro IS_VALID_PIXMAP */ 126 127#define USE_BACKGROUND_PIXEL 3 128#define USE_BORDER_PIXEL 3 129 130/* byte swap a 32-bit literal */ 131static inline uint32_t 132lswapl(uint32_t x) 133{ 134 return ((x & 0xff) << 24) | 135 ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x >> 24) & 0xff); 136} 137 138/* byte swap a 16-bit literal */ 139static inline uint16_t 140lswaps(uint16_t x) 141{ 142 return (uint16_t)((x & 0xff) << 8) | ((x >> 8) & 0xff); 143} 144 145#undef min 146#undef max 147 148#define min(a, b) (((a) < (b)) ? (a) : (b)) 149#define max(a, b) (((a) > (b)) ? (a) : (b)) 150/* abs() is a function, not a macro; include the file declaring 151 * it in case we haven't done that yet. 152 */ 153#include <stdlib.h> 154#define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0)) 155/* this assumes b > 0 */ 156#define modulus(a, b, d) if (((d) = (a) % (b)) < 0) (d) += (b) 157/* 158 * return the least significant bit in x which is set 159 * 160 * This works on 1's complement and 2's complement machines. 161 * If you care about the extra instruction on 2's complement 162 * machines, change to ((x) & (-(x))) 163 */ 164#define lowbit(x) ((x) & (~(x) + 1)) 165 166/* XXX Not for modules */ 167#include <limits.h> 168#if !defined(MAXSHORT) || !defined(MINSHORT) || \ 169 !defined(MAXINT) || !defined(MININT) 170/* 171 * Some implementations #define these through <math.h>, so preclude 172 * #include'ing it later. 173 */ 174 175#include <math.h> 176#undef MAXSHORT 177#define MAXSHORT SHRT_MAX 178#undef MINSHORT 179#define MINSHORT SHRT_MIN 180#undef MAXINT 181#define MAXINT INT_MAX 182#undef MININT 183#define MININT INT_MIN 184 185#include <assert.h> 186#include <ctype.h> 187#include <stdio.h> /* for fopen, etc... */ 188 189#endif 190 191#ifndef PATH_MAX 192#include <sys/param.h> 193#ifndef PATH_MAX 194#ifdef MAXPATHLEN 195#define PATH_MAX MAXPATHLEN 196#else 197#define PATH_MAX 1024 198#endif 199#endif 200#endif 201 202/** 203 * Calculate the number of bytes needed to hold bits. 204 * @param bits The minimum number of bits needed. 205 * @return The number of bytes needed to hold bits. 206 */ 207static inline int 208bits_to_bytes(const int bits) 209{ 210 return ((bits + 7) >> 3); 211} 212 213/** 214 * Calculate the number of 4-byte units needed to hold the given number of 215 * bytes. 216 * @param bytes The minimum number of bytes needed. 217 * @return The number of 4-byte units needed to hold bytes. 218 */ 219static inline int 220bytes_to_int32(const int bytes) 221{ 222 return (((bytes) + 3) >> 2); 223} 224 225/** 226 * Calculate the number of bytes (in multiples of 4) needed to hold bytes. 227 * @param bytes The minimum number of bytes needed. 228 * @return The closest multiple of 4 that is equal or higher than bytes. 229 */ 230static inline int 231pad_to_int32(const int bytes) 232{ 233 return (((bytes) + 3) & ~3); 234} 235 236/** 237 * Calculate padding needed to bring the number of bytes to an even 238 * multiple of 4. 239 * @param bytes The minimum number of bytes needed. 240 * @return The bytes of padding needed to arrive at the closest multiple of 4 241 * that is equal or higher than bytes. 242 */ 243static inline int 244padding_for_int32(const int bytes) 245{ 246 return ((-bytes) & 3); 247} 248 249 250extern char **xstrtokenize(const char *str, const char *separators); 251extern void FormatInt64(int64_t num, char *string); 252extern void FormatUInt64(uint64_t num, char *string); 253extern void FormatUInt64Hex(uint64_t num, char *string); 254extern void FormatDouble(double dbl, char *string); 255 256/** 257 * Compare the two version numbers comprising of major.minor. 258 * 259 * @return A value less than 0 if a is less than b, 0 if a is equal to b, 260 * or a value greater than 0 261 */ 262static inline int 263version_compare(uint32_t a_major, uint32_t a_minor, 264 uint32_t b_major, uint32_t b_minor) 265{ 266 if (a_major > b_major) 267 return 1; 268 if (a_major < b_major) 269 return -1; 270 if (a_minor > b_minor) 271 return 1; 272 if (a_minor < b_minor) 273 return -1; 274 275 return 0; 276} 277 278/* some macros to help swap requests, replies, and events */ 279 280#define LengthRestB(stuff) \ 281 ((client->req_len << 2) - sizeof(*stuff)) 282 283#define LengthRestS(stuff) \ 284 ((client->req_len << 1) - (sizeof(*stuff) >> 1)) 285 286#define LengthRestL(stuff) \ 287 (client->req_len - (sizeof(*stuff) >> 2)) 288 289#define SwapRestS(stuff) \ 290 SwapShorts((short *)(stuff + 1), LengthRestS(stuff)) 291 292#define SwapRestL(stuff) \ 293 SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff)) 294 295#if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 296void __attribute__ ((error("wrong sized variable passed to swap"))) 297wrong_size(void); 298#else 299static inline void 300wrong_size(void) 301{ 302} 303#endif 304 305#if !(defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))) 306static inline int 307__builtin_constant_p(int x) 308{ 309 return 0; 310} 311#endif 312 313/* byte swap a 64-bit value */ 314static inline void 315swap_uint64(uint64_t *x) 316{ 317 char n; 318 319 n = ((char *) x)[0]; 320 ((char *) x)[0] = ((char *) x)[7]; 321 ((char *) x)[7] = n; 322 323 n = ((char *) x)[1]; 324 ((char *) x)[1] = ((char *) x)[6]; 325 ((char *) x)[6] = n; 326 327 n = ((char *) x)[2]; 328 ((char *) x)[2] = ((char *) x)[5]; 329 ((char *) x)[5] = n; 330 331 n = ((char *) x)[3]; 332 ((char *) x)[3] = ((char *) x)[4]; 333 ((char *) x)[4] = n; 334} 335 336#define swapll(x) do { \ 337 if (sizeof(*(x)) != 8) \ 338 wrong_size(); \ 339 swap_uint64((uint64_t *)(x)); \ 340 } while (0) 341 342/* byte swap a 32-bit value */ 343static inline void 344swap_uint32(uint32_t * x) 345{ 346 char n = ((char *) x)[0]; 347 348 ((char *) x)[0] = ((char *) x)[3]; 349 ((char *) x)[3] = n; 350 n = ((char *) x)[1]; 351 ((char *) x)[1] = ((char *) x)[2]; 352 ((char *) x)[2] = n; 353} 354 355#define swapl(x) do { \ 356 if (sizeof(*(x)) != 4) \ 357 wrong_size(); \ 358 if (__builtin_constant_p((uintptr_t)(x) & 3) && ((uintptr_t)(x) & 3) == 0) \ 359 *(x) = lswapl(*(x)); \ 360 else \ 361 swap_uint32((uint32_t *)(x)); \ 362 } while (0) 363 364/* byte swap a 16-bit value */ 365static inline void 366swap_uint16(uint16_t * x) 367{ 368 char n = ((char *) x)[0]; 369 370 ((char *) x)[0] = ((char *) x)[1]; 371 ((char *) x)[1] = n; 372} 373 374#define swaps(x) do { \ 375 if (sizeof(*(x)) != 2) \ 376 wrong_size(); \ 377 if (__builtin_constant_p((uintptr_t)(x) & 1) && ((uintptr_t)(x) & 1) == 0) \ 378 *(x) = lswaps(*(x)); \ 379 else \ 380 swap_uint16((uint16_t *)(x)); \ 381 } while (0) 382 383/* copy 32-bit value from src to dst byteswapping on the way */ 384#define cpswapl(src, dst) do { \ 385 if (sizeof((src)) != 4 || sizeof((dst)) != 4) \ 386 wrong_size(); \ 387 (dst) = lswapl((src)); \ 388 } while (0) 389 390/* copy short from src to dst byteswapping on the way */ 391#define cpswaps(src, dst) do { \ 392 if (sizeof((src)) != 2 || sizeof((dst)) != 2) \ 393 wrong_size(); \ 394 (dst) = lswaps((src)); \ 395 } while (0) 396 397extern _X_EXPORT void SwapLongs(CARD32 *list, unsigned long count); 398 399extern _X_EXPORT void SwapShorts(short *list, unsigned long count); 400 401extern _X_EXPORT void MakePredeclaredAtoms(void); 402 403extern _X_EXPORT int Ones(unsigned long /*mask */ ); 404 405typedef struct _xPoint *DDXPointPtr; 406typedef struct pixman_box16 *BoxPtr; 407typedef struct _xEvent *xEventPtr; 408typedef struct _xRectangle *xRectanglePtr; 409typedef struct _GrabRec *GrabPtr; 410 411/* typedefs from other places - duplicated here to minimize the amount 412 * of unnecessary junk that one would normally have to include to get 413 * these symbols defined 414 */ 415 416#ifndef _XTYPEDEF_CHARINFOPTR 417typedef struct _CharInfo *CharInfoPtr; /* also in fonts/include/font.h */ 418 419#define _XTYPEDEF_CHARINFOPTR 420#endif 421 422extern _X_EXPORT unsigned long globalSerialNumber; 423extern _X_EXPORT unsigned long serverGeneration; 424 425/* Don't use this directly, use BUG_WARN or BUG_WARN_MSG instead */ 426#define __BUG_WARN_MSG(cond, with_msg, ...) \ 427 do { if (cond) { \ 428 ErrorFSigSafe("BUG: triggered 'if (" #cond ")'\n"); \ 429 ErrorFSigSafe("BUG: %s:%u in %s()\n", \ 430 __FILE__, __LINE__, __func__); \ 431 if (with_msg) ErrorFSigSafe(__VA_ARGS__); \ 432 xorg_backtrace(); \ 433 } } while(0) 434 435#define BUG_WARN_MSG(cond, ...) \ 436 __BUG_WARN_MSG(cond, 1, __VA_ARGS__) 437 438#define BUG_WARN(cond) __BUG_WARN_MSG(cond, 0, NULL) 439 440#define BUG_RETURN(cond) \ 441 do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return; } } while(0) 442 443#define BUG_RETURN_MSG(cond, ...) \ 444 do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return; } } while(0) 445 446#define BUG_RETURN_VAL(cond, val) \ 447 do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return (val); } } while(0) 448 449#define BUG_RETURN_VAL_MSG(cond, val, ...) \ 450 do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return (val); } } while(0) 451 452#endif /* MISC_H */ 453