cleanup: remove MemSet() casts - Mailing list pgsql-patches

From Neil Conway
Subject cleanup: remove MemSet() casts
Date
Msg-id 42805138.5010805@samurai.com
Whole thread Raw
Responses Re: cleanup: remove MemSet() casts
List pgsql-patches
There were various locations in the code that were casting the first
argument to MemSet() or occasionally memset() to a char *. This is not
necessary, per C89: memset's first argument is a void * to begin with,
and there is an implicit conversion between void * and any other pointer
type.

I also noticed a bit of unclear / inconsistent code in MemSet() itself:
it's not obvious whether unary * or postfix ++ has higher precedence
(the latter does), so I added some parentheses to make this clear.

Barring any objections, I'll apply this to HEAD tomorrow.

-Neil
Index: src/backend/access/hash/hashovfl.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/access/hash/hashovfl.c,v
retrieving revision 1.45
diff -c -r1.45 hashovfl.c
*** src/backend/access/hash/hashovfl.c    31 Dec 2004 21:59:13 -0000    1.45
--- src/backend/access/hash/hashovfl.c    10 May 2005 05:37:49 -0000
***************
*** 509,515 ****

      /* set all of the bits to 1 */
      freep = HashPageGetBitmap(pg);
!     MemSet((char *) freep, 0xFF, BMPGSZ_BYTE(metap));

      /* write out the new bitmap page (releasing write lock and pin) */
      _hash_wrtbuf(rel, buf);
--- 509,515 ----

      /* set all of the bits to 1 */
      freep = HashPageGetBitmap(pg);
!     MemSet(freep, 0xFF, BMPGSZ_BYTE(metap));

      /* write out the new bitmap page (releasing write lock and pin) */
      _hash_wrtbuf(rel, buf);
Index: src/backend/access/hash/hashpage.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/access/hash/hashpage.c,v
retrieving revision 1.47
diff -c -r1.47 hashpage.c
*** src/backend/access/hash/hashpage.c    31 Dec 2004 21:59:13 -0000    1.47
--- src/backend/access/hash/hashpage.c    10 May 2005 05:32:24 -0000
***************
*** 295,302 ****
      metap->hashm_maxbucket = metap->hashm_lowmask = 1;    /* nbuckets - 1 */
      metap->hashm_highmask = 3;    /* (nbuckets << 1) - 1 */

!     MemSet((char *) metap->hashm_spares, 0, sizeof(metap->hashm_spares));
!     MemSet((char *) metap->hashm_mapp, 0, sizeof(metap->hashm_mapp));

      metap->hashm_spares[1] = 1; /* the first bitmap page is only spare */
      metap->hashm_ovflpoint = 1;
--- 295,302 ----
      metap->hashm_maxbucket = metap->hashm_lowmask = 1;    /* nbuckets - 1 */
      metap->hashm_highmask = 3;    /* (nbuckets << 1) - 1 */

!     MemSet(metap->hashm_spares, 0, sizeof(metap->hashm_spares));
!     MemSet(metap->hashm_mapp, 0, sizeof(metap->hashm_mapp));

      metap->hashm_spares[1] = 1; /* the first bitmap page is only spare */
      metap->hashm_ovflpoint = 1;
Index: src/backend/access/hash/hashutil.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/access/hash/hashutil.c,v
retrieving revision 1.41
diff -c -r1.41 hashutil.c
*** src/backend/access/hash/hashutil.c    31 Dec 2004 21:59:13 -0000    1.41
--- src/backend/access/hash/hashutil.c    10 May 2005 05:33:30 -0000
***************
*** 56,62 ****
          (sizeof(HashItemData) - sizeof(IndexTupleData));

      hitem = (HashItem) palloc(nbytes_hitem);
!     memcpy((char *) &(hitem->hash_itup), (char *) itup, tuplen);

      return hitem;
  }
--- 56,62 ----
          (sizeof(HashItemData) - sizeof(IndexTupleData));

      hitem = (HashItem) palloc(nbytes_hitem);
!     memcpy(&(hitem->hash_itup), itup, tuplen);

      return hitem;
  }
Index: src/backend/storage/lmgr/lock.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/storage/lmgr/lock.c,v
retrieving revision 1.150
diff -c -r1.150 lock.c
*** src/backend/storage/lmgr/lock.c    29 Apr 2005 22:28:24 -0000    1.150
--- src/backend/storage/lmgr/lock.c    10 May 2005 05:53:17 -0000
***************
*** 551,558 ****
          ProcQueueInit(&(lock->waitProcs));
          lock->nRequested = 0;
          lock->nGranted = 0;
!         MemSet((char *) lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
!         MemSet((char *) lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
          LOCK_PRINT("LockAcquire: new", lock, lockmode);
      }
      else
--- 551,558 ----
          ProcQueueInit(&(lock->waitProcs));
          lock->nRequested = 0;
          lock->nGranted = 0;
!         MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
!         MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
          LOCK_PRINT("LockAcquire: new", lock, lockmode);
      }
      else
Index: src/backend/utils/cache/relcache.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/utils/cache/relcache.c,v
retrieving revision 1.222
diff -c -r1.222 relcache.c
*** src/backend/utils/cache/relcache.c    6 May 2005 17:24:54 -0000    1.222
--- src/backend/utils/cache/relcache.c    10 May 2005 05:54:30 -0000
***************
*** 297,303 ****
      /*
       * clear all fields of reldesc
       */
!     MemSet((char *) relation, 0, sizeof(RelationData));
      relation->rd_targblock = InvalidBlockNumber;

      /* make sure relation is marked as having no open file yet */
--- 297,303 ----
      /*
       * clear all fields of reldesc
       */
!     MemSet(relation, 0, sizeof(RelationData));
      relation->rd_targblock = InvalidBlockNumber;

      /* make sure relation is marked as having no open file yet */
***************
*** 315,321 ****
       */
      relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);

!     memcpy((char *) relationForm, (char *) relp, CLASS_TUPLE_SIZE);

      /* initialize relation tuple form */
      relation->rd_rel = relationForm;
--- 315,321 ----
       */
      relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);

!     memcpy(relationForm, relp, CLASS_TUPLE_SIZE);

      /* initialize relation tuple form */
      relation->rd_rel = relationForm;
Index: src/backend/utils/cache/syscache.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/utils/cache/syscache.c,v
retrieving revision 1.98
diff -c -r1.98 syscache.c
*** src/backend/utils/cache/syscache.c    14 Apr 2005 20:03:26 -0000    1.98
--- src/backend/utils/cache/syscache.c    10 May 2005 05:55:04 -0000
***************
*** 455,461 ****

      Assert(!CacheInitialized);

!     MemSet((char *) SysCache, 0, sizeof(SysCache));

      for (cacheId = 0; cacheId < SysCacheSize; cacheId++)
      {
--- 455,461 ----

      Assert(!CacheInitialized);

!     MemSet(SysCache, 0, sizeof(SysCache));

      for (cacheId = 0; cacheId < SysCacheSize; cacheId++)
      {
Index: src/backend/utils/fmgr/dfmgr.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/utils/fmgr/dfmgr.c,v
retrieving revision 1.79
diff -c -r1.79 dfmgr.c
*** src/backend/utils/fmgr/dfmgr.c    31 Dec 2004 22:01:31 -0000    1.79
--- src/backend/utils/fmgr/dfmgr.c    10 May 2005 05:55:52 -0000
***************
*** 126,132 ****
                      (errcode(ERRCODE_OUT_OF_MEMORY),
                       errmsg("out of memory")));

!         MemSet((char *) file_scanner, 0, sizeof(DynamicFileList));
          strcpy(file_scanner->filename, fullname);
          file_scanner->device = stat_buf.st_dev;
  #ifndef WIN32
--- 126,132 ----
                      (errcode(ERRCODE_OUT_OF_MEMORY),
                       errmsg("out of memory")));

!         MemSet(file_scanner, 0, sizeof(DynamicFileList));
          strcpy(file_scanner->filename, fullname);
          file_scanner->device = stat_buf.st_dev;
  #ifndef WIN32
Index: src/include/c.h
===================================================================
RCS file: /var/lib/cvs/pgsql/src/include/c.h,v
retrieving revision 1.182
diff -c -r1.182 c.h
*** src/include/c.h    28 Apr 2005 21:47:17 -0000    1.182
--- src/include/c.h    10 May 2005 06:08:39 -0000
***************
*** 630,636 ****
  #define MemSet(start, val, len) \
      do \
      { \
!         int32 * _start = (int32 *) (start); \
          int        _val = (val); \
          Size    _len = (len); \
  \
--- 630,636 ----
  #define MemSet(start, val, len) \
      do \
      { \
!         int32  *_start = (int32 *) (start); \
          int        _val = (val); \
          Size    _len = (len); \
  \
***************
*** 639,650 ****
              _val == 0 && \
              _len <= MEMSET_LOOP_LIMIT) \
          { \
!             int32 * _stop = (int32 *) ((char *) _start + _len); \
              while (_start < _stop) \
!                 *_start++ = 0; \
          } \
          else \
!             memset((char *) _start, _val, _len); \
      } while (0)

  #define MEMSET_LOOP_LIMIT  1024
--- 639,650 ----
              _val == 0 && \
              _len <= MEMSET_LOOP_LIMIT) \
          { \
!             int32 *_stop = (int32 *) ((char *) _start + _len); \
              while (_start < _stop) \
!                 *(_start++) = 0;   \
          } \
          else \
!             memset(_start, _val, _len); \
      } while (0)

  #define MEMSET_LOOP_LIMIT  1024
***************
*** 658,664 ****
  #define MemSetAligned(start, val, len) \
      do \
      { \
!         int32 * _start = (int32 *) (start); \
          int        _val = (val); \
          Size    _len = (len); \
  \
--- 658,664 ----
  #define MemSetAligned(start, val, len) \
      do \
      { \
!         int32  *_start = (int32 *) (start); \
          int        _val = (val); \
          Size    _len = (len); \
  \
***************
*** 668,677 ****
          { \
              int32 * _stop = (int32 *) ((char *) _start + _len); \
              while (_start < _stop) \
!                 *_start++ = 0; \
          } \
          else \
!             memset((char *) _start, _val, _len); \
      } while (0)


--- 668,677 ----
          { \
              int32 * _stop = (int32 *) ((char *) _start + _len); \
              while (_start < _stop) \
!                 *(_start++) = 0;   \
          } \
          else \
!             memset(_start, _val, _len); \
      } while (0)


Index: src/interfaces/libpq/fe-protocol2.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/interfaces/libpq/fe-protocol2.c,v
retrieving revision 1.16
diff -c -r1.16 fe-protocol2.c
*** src/interfaces/libpq/fe-protocol2.c    31 Dec 2004 22:03:50 -0000    1.16
--- src/interfaces/libpq/fe-protocol2.c    10 May 2005 06:01:09 -0000
***************
*** 592,598 ****
      {
          result->attDescs = (PGresAttDesc *)
              pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
!         MemSet((char *) result->attDescs, 0, nfields * sizeof(PGresAttDesc));
      }

      /* get type info */
--- 592,598 ----
      {
          result->attDescs = (PGresAttDesc *)
              pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
!         MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc));
      }

      /* get type info */
***************
*** 667,673 ****
              pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
          if (conn->curTuple == NULL)
              goto outOfMemory;
!         MemSet((char *) conn->curTuple, 0, nfields * sizeof(PGresAttValue));

          /*
           * If it's binary, fix the column format indicators.  We assume
--- 667,673 ----
              pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
          if (conn->curTuple == NULL)
              goto outOfMemory;
!         MemSet(conn->curTuple, 0, nfields * sizeof(PGresAttValue));

          /*
           * If it's binary, fix the column format indicators.  We assume
***************
*** 1409,1415 ****
      if (!startpacket)
          return NULL;

!     MemSet((char *) startpacket, 0, sizeof(StartupPacket));

      startpacket->protoVersion = htonl(conn->pversion);

--- 1409,1415 ----
      if (!startpacket)
          return NULL;

!     MemSet(startpacket, 0, sizeof(StartupPacket));

      startpacket->protoVersion = htonl(conn->pversion);


pgsql-patches by date:

Previous
From: Neil Conway
Date:
Subject: pgstat: reduce message header
Next
From: Neil Conway
Date:
Subject: Re: cleanup: remove MemSet() casts