mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Add a crude facility for dealing with relative pointers.
C doesn't have any sort of built-in understanding of a pointer relative to some arbitrary base address, but dynamic shared memory segments can be mapped at different addresses in different processes, so any sort of shared data structure stored within a dynamic shared memory segment can't use absolute pointers. We could use something like Size to represent a relative pointer, but then the compiler provides no type-checking. Use stupid macro tricks to get some type-checking. Patch originally by me. Concept suggested by Andres Freund. Recently resubmitted as part of Thomas Munro's work on dynamic shared memory allocation. Discussion: 20131205144434.GG12398@alap2.anarazel.de Discussion: CAEepm=1z5WLuNoJ80PaCvz6EtG9dN0j-KuHcHtU6QEfcPP5-qA@mail.gmail.com
This commit is contained in:
		
							
								
								
									
										74
									
								
								src/include/utils/relptr.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/include/utils/relptr.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
				
			|||||||
 | 
					/*-------------------------------------------------------------------------
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * relptr.h
 | 
				
			||||||
 | 
					 *	  This file contains basic declarations for relative pointers.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
 | 
				
			||||||
 | 
					 * Portions Copyright (c) 1994, Regents of the University of California
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * src/include/utils/relptr.h
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *-------------------------------------------------------------------------
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef RELPTR_H
 | 
				
			||||||
 | 
					#define RELPTR_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Relative pointers are intended to be used when storing an address that may
 | 
				
			||||||
 | 
					 * be relative either to the base of the processes address space or some
 | 
				
			||||||
 | 
					 * dynamic shared memory segment mapped therein.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The idea here is that you declare a relative pointer as relptr(type)
 | 
				
			||||||
 | 
					 * and then use relptr_access to dereference it and relptr_store to change
 | 
				
			||||||
 | 
					 * it.  The use of a union here is a hack, because what's stored in the
 | 
				
			||||||
 | 
					 * relptr is always a Size, never an actual pointer.  But including a pointer
 | 
				
			||||||
 | 
					 * in the union allows us to use stupid macro tricks to provide some measure
 | 
				
			||||||
 | 
					 * of type-safety.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define relptr(type)	 union { type *relptr_type; Size relptr_off; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * pgindent gets confused by declarations of the type relptr(type), so it's
 | 
				
			||||||
 | 
					 * useful to give them a name that doesn't include parentheses.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define relptr_declare(type, name) \
 | 
				
			||||||
 | 
						typedef union { type *relptr_type; Size relptr_off; } name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
 | 
				
			||||||
 | 
					#define relptr_access(base, rp) \
 | 
				
			||||||
 | 
						(AssertVariableIsOfTypeMacro(base, char *), \
 | 
				
			||||||
 | 
						 (__typeof__((rp).relptr_type)) ((rp).relptr_off == 0 ? NULL : \
 | 
				
			||||||
 | 
							(base + (rp).relptr_off)))
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * If we don't have __builtin_types_compatible_p, assume we might not have
 | 
				
			||||||
 | 
					 * __typeof__ either.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define relptr_access(base, rp) \
 | 
				
			||||||
 | 
						(AssertVariableIsOfTypeMacro(base, char *), \
 | 
				
			||||||
 | 
						 (void *) ((rp).relptr_off == 0 ? NULL : (base + (rp).relptr_off)))
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define relptr_is_null(rp) \
 | 
				
			||||||
 | 
						((rp).relptr_off == 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
 | 
				
			||||||
 | 
					#define relptr_store(base, rp, val) \
 | 
				
			||||||
 | 
						(AssertVariableIsOfTypeMacro(base, char *), \
 | 
				
			||||||
 | 
						 AssertVariableIsOfTypeMacro(val, __typeof__((rp).relptr_type)), \
 | 
				
			||||||
 | 
						 (rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * If we don't have __builtin_types_compatible_p, assume we might not have
 | 
				
			||||||
 | 
					 * __typeof__ either.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define relptr_store(base, rp, val) \
 | 
				
			||||||
 | 
						(AssertVariableIsOfTypeMacro(base, char *), \
 | 
				
			||||||
 | 
						 (rp).relptr_off = ((val) == NULL ? 0 : ((char *) (val)) - (base)))
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define relptr_copy(rp1, rp2) \
 | 
				
			||||||
 | 
						((rp1).relptr_off = (rp2).relptr_off)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif   /* RELPTR_H */
 | 
				
			||||||
		Reference in New Issue
	
	Block a user