* Create a new dynamic shared memory segment.
*/
dsm_segment *
-dsm_create(Size size)
+dsm_create(Size size, int flags)
{
- dsm_segment *seg = dsm_create_descriptor();
+ dsm_segment *seg;
uint32 i;
uint32 nitems;
if (!dsm_init_done)
dsm_backend_startup();
+ /*
+ * If we've been instructed to return NULL when it's not possible to
+ * register another segment, check whether we seem to be at the limit.
+ * This allows us to avoid the overhead of creating a new segment only to
+ * immediately destroy it again. Since we don't take the lock here, the
+ * value we read might be slightly stale, but the remote possibility of
+ * an unnecessary failure here shouldn't trouble anyone too much.
+ */
+ if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0
+ && dsm_control->nitems >= dsm_control->maxitems)
+ return NULL;
+
+ /* Create a new segment descriptor. */
+ seg = dsm_create_descriptor();
+
/* Loop until we find an unused segment identifier. */
for (;;)
{
/* Verify that we can support an additional mapping. */
if (nitems >= dsm_control->maxitems)
+ {
+ if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
+ {
+ dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
+ &seg->mapped_address, &seg->mapped_size, WARNING);
+ if (seg->resowner != NULL)
+ ResourceOwnerForgetDSM(seg->resowner, seg);
+ dlist_delete(&seg->node);
+ pfree(seg);
+ return NULL;
+ }
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
errmsg("too many dynamic shared memory segments")));
+ }
/* Enter the handle into a new array slot. */
dsm_control->item[nitems].handle = seg->handle;
typedef struct dsm_segment dsm_segment;
+#define DSM_CREATE_NULL_IF_MAXSEGMENTS 0x0001
+
/* Startup and shutdown functions. */
struct PGShmemHeader; /* avoid including pg_shmem.h */
extern void dsm_cleanup_using_control_segment(dsm_handle old_control_handle);
#endif
/* Functions that create, update, or remove mappings. */
-extern dsm_segment *dsm_create(Size size);
+extern dsm_segment *dsm_create(Size size, int flags);
extern dsm_segment *dsm_attach(dsm_handle h);
extern void *dsm_resize(dsm_segment *seg, Size size);
extern void *dsm_remap(dsm_segment *seg);
segsize = shm_toc_estimate(&e);
/* Create the shared memory segment and establish a table of contents. */
- seg = dsm_create(shm_toc_estimate(&e));
+ seg = dsm_create(shm_toc_estimate(&e), 0);
toc = shm_toc_create(PG_TEST_SHM_MQ_MAGIC, dsm_segment_address(seg),
segsize);