From fa937b5e6bbf86f83b78dd6387b63eb6a6ea1694 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Thu, 23 Jan 1997 18:15:29 +0000
Subject: [PATCH] Remove SB_PAD.  Compute padding at compile time.

---
 src/backend/storage/buffer/buf_init.c | 10 +++++++-
 src/include/config.h                  | 13 ----------
 src/include/storage/buf_internals.h   | 34 +++++++++++++++++++++------
 3 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index 46329d080e2..c16988fc119 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.4 1997/01/16 08:04:30 vadim Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.5 1997/01/23 18:14:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -135,6 +135,14 @@ InitBufferPool(IPCKey key)
     bool foundBufs,foundDescs;
     int i;
     
+    /* check padding of BufferDesc and BufferHdr */
+    if (sizeof(struct sbufdesc) != PADDED_SBUFDESC_SIZE)
+    	elog(WARN,"Internal error:  sbufdesc does not have the proper size, "
+				"contact the Postgres developers");
+    if (sizeof(struct sbufdesc_unpadded) <= PADDED_SBUFDESC_SIZE/2)
+    	elog(WARN,"Internal error:  sbufdesc is greatly over-sized, "
+				"contact the Postgres developers");
+
     Data_Descriptors = NBuffers;
     Free_List_Descriptor = Data_Descriptors;
     Lookup_List_Descriptor = Data_Descriptors + 1;
diff --git a/src/include/config.h b/src/include/config.h
index 0ac869315c0..c66477e2be8 100644
--- a/src/include/config.h
+++ b/src/include/config.h
@@ -28,7 +28,6 @@
 #  define NEED_SYS_SELECT_H
 #  define HAVE_TZSET
 #  define HAVE_ANSI_CPP
-#  define SB_PAD 44
 #  define HAS_TEST_AND_SET
    typedef unsigned int slock_t;
 #endif
@@ -40,7 +39,6 @@
 #  define NEED_ISINF 
 #  define HAS_LONG_LONG
 #  define NEED_UNION_SEMUN 
-#  define SB_PAD 40
 #  define HAS_TEST_AND_SET
 #  include <sys/mman.h>  /* for msemaphore */
    typedef msemaphore slock_t;
@@ -51,7 +49,6 @@
 #  define USE_POSIX_TIME
 #  define NEED_CBRT
 #  define NEED_I386_TAS_ASM
-#  define SB_PAD 56
 #  define HAS_TEST_AND_SET
 #  if defined(__mips__)
 #    undef HAS_TEST_AND_SET
@@ -62,11 +59,9 @@
 #if defined(bsdi)
 #  if defined(i386)
 #    define NEED_I386_TAS_ASM
-#    define SB_PAD 56
 #  endif
 #  if defined(sparc)
 #    define NEED_SPARC_TAS_ASM
-#    define SB_PAD 56
 #  endif
 #  if defined(PRE_BSDI_2_1)
 #    define NEED_UNION_SEMUN 
@@ -92,7 +87,6 @@
 #  define NEED_CBRT
 #  define NEED_RINT
 #  define NEED_UNION_SEMUN 
-#  define SB_PAD 44
 #  define HAS_TEST_AND_SET
    typedef struct { int sem[4]; } slock_t;
 #endif
@@ -107,7 +101,6 @@
 #  define HAVE_TZSET
 #  define NEED_UNION_SEMUN 
 #  define SYSV_DIRENT
-#  define SB_PAD 56
 #  define HAS_TEST_AND_SET
    typedef unsigned char slock_t;
 #endif
@@ -120,7 +113,6 @@
 #  define NO_VFORK
 #  define HAVE_TZSET
 #  define SYSV_DIRENT
-#  define SB_PAD 44
 #  define HAS_TEST_AND_SET
 #  include <abi_mutex.h>
    typedef abilock_t slock_t;
@@ -138,7 +130,6 @@
 #  define HAVE_TZSET
 #  define NEED_CBRT
 #  define NEED_I386_TAS_ASM
-#  define SB_PAD 56
 #  define HAS_TEST_AND_SET
    typedef unsigned char slock_t;
 #endif
@@ -163,7 +154,6 @@
 
 # define USE_LIMITS_H
 # define JMP_BUF
-# define SB_PAD 56
 # define NO_WAITPID
   typedef struct mutex slock_t;
 #endif
@@ -183,14 +173,12 @@
 #  define HAVE_TZSET
 #  define NEED_UNION_SEMUN 
 #  define SYSV_DIRENT
-#  define SB_PAD 56
 #  define HAS_TEST_AND_SET
 typedef unsigned char slock_t;
 #endif
 
 #if defined(sunos4)
 #  define USE_POSIX_TIME
-#  define SB_PAD 56
 #  undef HAVE_MEMMOVE
 #endif
 
@@ -225,7 +213,6 @@ typedef unsigned char slock_t;
 #  define USE_POSIX_TIME
 #  define NEED_UNION_SEMUN 
 #  define NEED_STRDUP
-#  define SB_PAD 60
 #endif
 
 
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index 2672d3a44f0..e5e8927e60f 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: buf_internals.h,v 1.9 1997/01/20 04:06:13 vadim Exp $
+ * $Id: buf_internals.h,v 1.10 1997/01/23 18:15:29 momjian Exp $
  *
  * NOTE
  *	If BUFFERPAGE0 is defined, then 0 will be used as a
@@ -100,6 +100,30 @@ struct buftag{
  *	to put the buffer, for all storage managers.
  */
 
+#define PADDED_SBUFDESC_SIZE 	128
+
+/* DO NOT CHANGE THIS NEXT STRUCTURE:
+   It is used only to get padding information for the real sbufdesc structure
+   It should match the sbufdesc structure exactly except for a missing sb_pad
+*/
+struct sbufdesc_unpadded {
+    Buffer		freeNext;
+    Buffer		freePrev;
+    SHMEM_OFFSET	data;
+    BufferTag		tag;
+    int			buf_id;
+    BufFlags		flags;
+    int16		bufsmgr;
+    unsigned		refcount;
+    char sb_dbname[NAMEDATALEN+1];
+    char sb_relname[NAMEDATALEN+1];
+#ifdef HAS_TEST_AND_SET
+    slock_t	io_in_progress_lock;
+#endif /* HAS_TEST_AND_SET */
+    /* NOTE NO sb_pad HERE */
+};
+
+/* THE REAL STRUCTURE - the structure above must match it, minus sb_pad */
 struct sbufdesc {
     Buffer		freeNext;	/* link for freelist chain */
     Buffer		freePrev;
@@ -121,7 +145,7 @@ struct sbufdesc {
 #endif /* HAS_TEST_AND_SET */
 
     /*
-     * I padded this structure to a power of 2 (128 bytes on a MIPS) because
+     * I padded this structure to a power of 2 (PADDED_SBUFDESC_SIZE) because
      * BufferDescriptorGetBuffer is called a billion times and it does an
      * C pointer subtraction (i.e., "x - y" -> array index of x relative
      * to y, which is calculated using division by struct size).  Integer
@@ -131,11 +155,7 @@ struct sbufdesc {
      * going to make some of these types bigger soon anyway... -pma 1/2/93
      */
 
-    /*
-     * NOTE: This is now defined in the ..../include/config.h file!
-     */
-
-    char		sb_pad[SB_PAD];
+    char	sb_pad[PADDED_SBUFDESC_SIZE-sizeof(struct sbufdesc_unpadded)];
 };
 
 /*
-- 
GitLab