Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
relptr.h94 linesDownload Raw Back to utils
1/*-------------------------------------------------------------------------2 *3 * relptr.h4 *	  This file contains basic declarations for relative pointers.5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * src/include/utils/relptr.h10 *11 *-------------------------------------------------------------------------12 */13 14#ifndef RELPTR_H15#define RELPTR_H16 17/*18 * Relative pointers are intended to be used when storing an address that may19 * be relative either to the base of the process's address space or some20 * dynamic shared memory segment mapped therein.21 *22 * The idea here is that you declare a relative pointer as relptr(type)23 * and then use relptr_access to dereference it and relptr_store to change24 * it.  The use of a union here is a hack, because what's stored in the25 * relptr is always a Size, never an actual pointer.  But including a pointer26 * in the union allows us to use stupid macro tricks to provide some measure27 * of type-safety.28 */29#define relptr(type)	 union { type *relptr_type; Size relptr_off; }30 31/*32 * pgindent gets confused by declarations that use "relptr(type)" directly,33 * so preferred style is to write34 *		typedef struct ... SomeStruct;35 *		relptr_declare(SomeStruct, RelptrSomeStruct);36 * and then declare pointer variables as "RelptrSomeStruct someptr".37 */38#define relptr_declare(type, relptrtype) \39	typedef relptr(type) relptrtype40 41#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P42#define relptr_access(base, rp) \43	(AssertVariableIsOfTypeMacro(base, char *), \44	 (__typeof__((rp).relptr_type)) ((rp).relptr_off == 0 ? NULL : \45		(base) + (rp).relptr_off - 1))46#else47/*48 * If we don't have __builtin_types_compatible_p, assume we might not have49 * __typeof__ either.50 */51#define relptr_access(base, rp) \52	(AssertVariableIsOfTypeMacro(base, char *), \53	 (void *) ((rp).relptr_off == 0 ? NULL : (base) + (rp).relptr_off - 1))54#endif55 56#define relptr_is_null(rp) \57	((rp).relptr_off == 0)58 59#define relptr_offset(rp) \60	((rp).relptr_off - 1)61 62/* We use this inline to avoid double eval of "val" in relptr_store */63static inline Size64relptr_store_eval(char *base, char *val)65{66	if (val == NULL)67		return 0;68	else69	{70		Assert(val >= base);71		return val - base + 1;72	}73}74 75#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P76#define relptr_store(base, rp, val) \77	(AssertVariableIsOfTypeMacro(base, char *), \78	 AssertVariableIsOfTypeMacro(val, __typeof__((rp).relptr_type)), \79	 (rp).relptr_off = relptr_store_eval((base), (char *) (val)))80#else81/*82 * If we don't have __builtin_types_compatible_p, assume we might not have83 * __typeof__ either.84 */85#define relptr_store(base, rp, val) \86	(AssertVariableIsOfTypeMacro(base, char *), \87	 (rp).relptr_off = relptr_store_eval((base), (char *) (val)))88#endif89 90#define relptr_copy(rp1, rp2) \91	((rp1).relptr_off = (rp2).relptr_off)92 93#endif							/* RELPTR_H */94 
codekingpro/portable-devtools · Team Ai