commit feab191ea1be8a75e3bd430a3476f26582d3673c Author: Robert Haas Date: Wed May 25 09:38:48 2016 -0400 Reserve zero as an invalid DSM handle. Previously, the handle for the control segment could not be zero, but some other DSM segment could potentially have a handle value of zero. However, that means that if you want to store a dsm_handle that might or might not be valid, you need a separate boolean to keep track of whether you've got a legal value there. That's annoying, so change things so that no DSM segment can ever have a handle of 0 - or as we call it here, DSM_HANDLE_INVALID. Thomas Munro, reviewed by me. diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c index fae0b00..0dd1ed4 100644 --- a/src/backend/storage/ipc/dsm.c +++ b/src/backend/storage/ipc/dsm.c @@ -181,7 +181,7 @@ dsm_postmaster_startup(PGShmemHeader *shim) Assert(dsm_control_address == NULL); Assert(dsm_control_mapped_size == 0); dsm_control_handle = random(); - if (dsm_control_handle == 0) + if (dsm_control_handle == DSM_HANDLE_INVALID) continue; if (dsm_impl_op(DSM_OP_CREATE, dsm_control_handle, segsize, &dsm_control_impl_private, &dsm_control_address, @@ -475,6 +475,8 @@ dsm_create(Size size, int flags) { Assert(seg->mapped_address == NULL && seg->mapped_size == 0); seg->handle = random(); + if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */ + continue; if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private, &seg->mapped_address, &seg->mapped_size, ERROR)) break; diff --git a/src/include/storage/dsm.h b/src/include/storage/dsm.h index 8be7c9a..bc91be6 100644 --- a/src/include/storage/dsm.h +++ b/src/include/storage/dsm.h @@ -19,6 +19,9 @@ typedef struct dsm_segment dsm_segment; #define DSM_CREATE_NULL_IF_MAXSEGMENTS 0x0001 +/* A sentinel value for an invalid DSM handle. */ +#define DSM_HANDLE_INVALID 0 + /* Startup and shutdown functions. */ struct PGShmemHeader; /* avoid including pg_shmem.h */ extern void dsm_cleanup_using_control_segment(dsm_handle old_control_handle);