@ -75,30 +75,19 @@ RequestAddinShmemSpace(Size size)
total_addin_request = add_size ( total_addin_request , size ) ;
}
/*
* CreateSharedMemoryAndSemaphores
* Creates and initializes shared memory and semaphores .
* CalculateShmemSize
* Calculates the amount of shared memory and number of semaphores needed .
*
* This is called by the postmaster or by a standalone backend .
* It is also called by a backend forked from the postmaster in the
* EXEC_BACKEND case . In the latter case , the shared memory segment
* already exists and has been physically attached to , but we have to
* initialize pointers in local memory that reference the shared structures ,
* because we didn ' t inherit the correct pointer values from the postmaster
* as we do in the fork ( ) scenario . The easiest way to do that is to run
* through the same code as before . ( Note that the called routines mostly
* check IsUnderPostmaster , rather than EXEC_BACKEND , to detect this case .
* This is a bit code - wasteful and could be cleaned up . )
* If num_semaphores is not NULL , it will be set to the number of semaphores
* required .
*
* Note that this function freezes the additional shared memory request size
* from loadable modules .
*/
void
CreateSharedMemoryAndSemaphores ( void )
Size
CalculateShmemSize ( int * num_semaphores )
{
PGShmemHeader * shim = NULL ;
if ( ! IsUnderPostmaster )
{
PGShmemHeader * seghdr ;
Size size ;
int numSemas ;
@ -106,14 +95,18 @@ CreateSharedMemoryAndSemaphores(void)
numSemas = ProcGlobalSemas ( ) ;
numSemas + = SpinlockSemas ( ) ;
/* Return the number of semaphores if requested by the caller */
if ( num_semaphores )
* num_semaphores = numSemas ;
/*
* Size of the Postgres shared - memory block is estimated via
* moderately - accurate estimates for the big hogs , plus 100 K for the
* stuff that ' s too small to bother with estimating .
* Size of the Postgres shared - memory block is estimated via moderately -
* accurate estimates for the big hogs , plus 100 K for the stuff that ' s too
* small to bother with estimating .
*
* We take some care during this phase to ensure that the total size
* request doesn ' t overflow size_t . If this gets through , we don ' t
* need to be so careful during the actual allocation phase .
* We take some care to ensure that the total size request doesn ' t
* overflow size_t . If this gets through , we don ' t need to be so careful
* during the actual allocation phase .
*/
size = 100000 ;
size = add_size ( size , PGSemaphoreShmemSize ( numSemas ) ) ;
@ -161,6 +154,37 @@ CreateSharedMemoryAndSemaphores(void)
/* might as well round it off to a multiple of a typical page size */
size = add_size ( size , 8192 - ( size % 8192 ) ) ;
return size ;
}
/*
* CreateSharedMemoryAndSemaphores
* Creates and initializes shared memory and semaphores .
*
* This is called by the postmaster or by a standalone backend .
* It is also called by a backend forked from the postmaster in the
* EXEC_BACKEND case . In the latter case , the shared memory segment
* already exists and has been physically attached to , but we have to
* initialize pointers in local memory that reference the shared structures ,
* because we didn ' t inherit the correct pointer values from the postmaster
* as we do in the fork ( ) scenario . The easiest way to do that is to run
* through the same code as before . ( Note that the called routines mostly
* check IsUnderPostmaster , rather than EXEC_BACKEND , to detect this case .
* This is a bit code - wasteful and could be cleaned up . )
*/
void
CreateSharedMemoryAndSemaphores ( void )
{
PGShmemHeader * shim = NULL ;
if ( ! IsUnderPostmaster )
{
PGShmemHeader * seghdr ;
Size size ;
int numSemas ;
/* Compute the size of the shared-memory block */
size = CalculateShmemSize ( & numSemas ) ;
elog ( DEBUG3 , " invoking IpcMemoryCreate(size=%zu) " , size ) ;
/*