@@ -1250,12 +1250,38 @@ void SharedMemoryBase::internalUnmap()
12501250 }
12511251}
12521252
1253+
1254+ ULONG SharedMemoryBase::getSystemPageSize (CheckStatusWrapper* statusVector)
1255+ {
1256+ // Get system page size as this is the unit of mapping.
1257+
1258+ #ifdef SOLARIS
1259+ const long ps = sysconf (_SC_PAGESIZE);
1260+ if (ps == -1 )
1261+ {
1262+ error (statusVector, " sysconf" , errno);
1263+ return 0 ;
1264+ }
1265+ #else
1266+ const int ps = getpagesize ();
1267+ if (ps == -1 )
1268+ {
1269+ error (statusVector, " getpagesize" , errno);
1270+ return 0 ;
1271+ }
1272+ #endif
1273+ return ps;
1274+ }
1275+
1276+
12531277SharedMemoryBase::SharedMemoryBase (const TEXT * filename, ULONG length, IpcObject* callback, bool skipLock)
12541278 :
12551279#ifdef HAVE_SHARED_MUTEX_SECTION
12561280 sh_mem_mutex (0 ),
12571281#endif
1258- sh_mem_length_mapped (0 ), sh_mem_header (NULL ),
1282+ sh_mem_length_mapped (0 ),
1283+ sh_mem_increment (0 ),
1284+ sh_mem_header (NULL ),
12591285 sh_mem_callback (callback)
12601286{
12611287/* *************************************
@@ -1286,6 +1312,14 @@ SharedMemoryBase::SharedMemoryBase(const TEXT* filename, ULONG length, IpcObject
12861312 TEXT init_filename[MAXPATHLEN ];
12871313 iscPrefixLock (init_filename, INIT_FILE , true );
12881314
1315+ if (length)
1316+ {
1317+ sh_mem_increment = length = FB_ALIGN (length, getSystemPageSize (&statusVector));
1318+
1319+ if (statusVector.hasData ())
1320+ status_exception::raise (&statusVector);
1321+ }
1322+
12891323 const bool trunc_flag = (length != 0 );
12901324
12911325 // open the init lock file
@@ -1569,8 +1603,17 @@ void SharedMemoryBase::internalUnmap()
15691603 unlinkFile ();
15701604}
15711605
1606+
1607+ ULONG SharedMemoryBase::getSystemPageSize (CheckStatusWrapper* /* statusVector*/ )
1608+ {
1609+ SYSTEM_INFO sys_info;
1610+ GetSystemInfo (&sys_info);
1611+ return sys_info.dwAllocationGranularity ;
1612+ }
1613+
1614+
15721615SharedMemoryBase::SharedMemoryBase (const TEXT * filename, ULONG length, IpcObject* cb, bool /* skipLock*/ )
1573- : sh_mem_mutex (0 ), sh_mem_length_mapped (0 ),
1616+ : sh_mem_mutex (0 ), sh_mem_length_mapped (0 ), sh_mem_increment ( 0 ),
15741617 sh_mem_handle (INVALID_HANDLE_VALUE ), sh_mem_object (0 ), sh_mem_interest (0 ), sh_mem_hdr_object (0 ),
15751618 sh_mem_hdr_address (0 ), sh_mem_header (NULL ), sh_mem_callback (cb), sh_mem_unlink (false )
15761619{
@@ -1598,6 +1641,17 @@ SharedMemoryBase::SharedMemoryBase(const TEXT* filename, ULONG length, IpcObject
15981641 bool init_flag = false ;
15991642 DWORD err = 0 ;
16001643
1644+ if (length)
1645+ {
1646+ LocalStatus ls;
1647+ CheckStatusWrapper statusVector (&ls);
1648+
1649+ sh_mem_increment = length = FB_ALIGN (length, getSystemPageSize (&statusVector));
1650+
1651+ if (statusVector.hasData ())
1652+ status_exception::raise (&statusVector);
1653+ }
1654+
16011655 // retry to attach to mmapped file if the process initializing dies during initialization.
16021656
16031657 retry:
@@ -1925,24 +1979,9 @@ UCHAR* SharedMemoryBase::mapObject(CheckStatusWrapper* statusVector, ULONG objec
19251979 *
19261980 **************************************/
19271981
1928- // Get system page size as this is the unit of mapping.
1929-
1930- #ifdef SOLARIS
1931- const long ps = sysconf (_SC_PAGESIZE);
1932- if (ps == -1 )
1933- {
1934- error (statusVector, " sysconf" , errno);
1982+ const ULONG page_size = getSystemPageSize (statusVector);
1983+ if (!page_size)
19351984 return NULL ;
1936- }
1937- #else
1938- const int ps = getpagesize ();
1939- if (ps == -1 )
1940- {
1941- error (statusVector, " getpagesize" , errno);
1942- return NULL ;
1943- }
1944- #endif
1945- const ULONG page_size = (ULONG ) ps;
19461985
19471986 // Compute the start and end page-aligned offsets which contain the object being mapped.
19481987
@@ -1980,24 +2019,9 @@ void SharedMemoryBase::unmapObject(CheckStatusWrapper* statusVector, UCHAR** obj
19802019 * Zero the object pointer after a successful unmap.
19812020 *
19822021 **************************************/
1983- // Get system page size as this is the unit of mapping.
1984-
1985- #ifdef SOLARIS
1986- const long ps = sysconf (_SC_PAGESIZE);
1987- if (ps == -1 )
1988- {
1989- error (statusVector, " sysconf" , errno);
1990- return ;
1991- }
1992- #else
1993- const int ps = getpagesize ();
1994- if (ps == -1 )
1995- {
1996- error (statusVector, " getpagesize" , errno);
2022+ const size_t page_size = getSystemPageSize (statusVector);
2023+ if (!page_size)
19972024 return ;
1998- }
1999- #endif
2000- const size_t page_size = (ULONG ) ps;
20012025
20022026 // Compute the start and end page-aligned addresses which contain the mapped object.
20032027
@@ -2035,9 +2059,7 @@ UCHAR* SharedMemoryBase::mapObject(CheckStatusWrapper* statusVector,
20352059 *
20362060 **************************************/
20372061
2038- SYSTEM_INFO sys_info;
2039- GetSystemInfo (&sys_info);
2040- const ULONG page_size = sys_info.dwAllocationGranularity ;
2062+ const ULONG page_size = getSystemPageSize (statusVector);
20412063
20422064 // Compute the start and end page-aligned offsets which
20432065 // contain the object being mapped.
@@ -2047,6 +2069,8 @@ UCHAR* SharedMemoryBase::mapObject(CheckStatusWrapper* statusVector,
20472069 const ULONG length = end - start;
20482070 const HANDLE handle = sh_mem_object;
20492071
2072+ fb_assert (end <= sh_mem_length_mapped);
2073+
20502074 UCHAR * address = (UCHAR *) MapViewOfFile (handle, FILE_MAP_WRITE , 0 , start, length);
20512075
20522076 if (address == NULL )
@@ -2075,9 +2099,7 @@ void SharedMemoryBase::unmapObject(CheckStatusWrapper* statusVector,
20752099 * Zero the object pointer after a successful unmap.
20762100 *
20772101 **************************************/
2078- SYSTEM_INFO sys_info;
2079- GetSystemInfo (&sys_info);
2080- const size_t page_size = sys_info.dwAllocationGranularity ;
2102+ const size_t page_size = getSystemPageSize (statusVector);
20812103
20822104 // Compute the start and end page-aligned offsets which
20832105 // contain the object being mapped.
@@ -2497,6 +2519,8 @@ bool SharedMemoryBase::remapFile(CheckStatusWrapper* statusVector, ULONG new_len
24972519
24982520 if (flag)
24992521 {
2522+ new_length = FB_ALIGN (new_length, getSystemPageSize (statusVector));
2523+
25002524 FB_UNUSED (os_utils::ftruncate (mainLock->getFd (), new_length));
25012525
25022526 if (new_length > sh_mem_length_mapped)
@@ -2553,6 +2577,8 @@ bool SharedMemoryBase::remapFile(CheckStatusWrapper* statusVector,
25532577
25542578 if (flag)
25552579 {
2580+ new_length = FB_ALIGN (new_length, getSystemPageSize (statusVector));
2581+
25562582 LARGE_INTEGER offset;
25572583 offset.QuadPart = new_length;
25582584
0 commit comments