@@ -205,15 +205,15 @@ void LsShm::setFatalErrorHandler( void (*cb)() )
205
205
206
206
void LsShm::tryRecoverBadOffset (LsShmOffset_t offset)
207
207
{
208
- if (x_pShmMap->x_stat .m_iFileSize > offset)
208
+ LsShmSize_t curMaxSize = getMapFileSize ();
209
+ if (curMaxSize > offset)
209
210
{
210
211
remap ();
211
212
return ;
212
213
}
213
214
deleteFile ();
214
215
if (s_fatalErrorCb)
215
216
(*s_fatalErrorCb)();
216
- LsShmSize_t curMaxSize = x_pShmMap->x_stat .m_iFileSize ;
217
217
if ( offset < curMaxSize)
218
218
{
219
219
assert (offset < curMaxSize);
@@ -231,7 +231,8 @@ void LsShm::tryRecoverCorruption()
231
231
232
232
int LsShm::isOffsetValid (LsShmOffset_t offset)
233
233
{
234
- return (offset <= x_pShmMap->x_stat .m_iFileSize );
234
+ LsShmSize_t curMaxSize = getMapFileSize ();
235
+ return (offset <= curMaxSize);
235
236
}
236
237
237
238
@@ -527,8 +528,8 @@ LsShmStatus_t LsShm::newShmMap(LsShmSize_t size, uint64_t id)
527
528
return LSSHM_ERROR;
528
529
x_pShmMap->x_id = id;
529
530
x_pShmMap->x_globalHashOff = 0 ;
530
- x_pShmMap->x_stat .m_iFileSize = size; // x_iMaxSize
531
- x_pShmMap->x_stat .m_iUsedSize = LSSHM_SHM_UNITSIZE; // x_iCurSize
531
+ ls_atomic_set (& x_pShmMap->x_stat .m_iFileSize , size) ; // x_iMaxSize
532
+ ls_atomic_set (& x_pShmMap->x_stat .m_iUsedSize , LSSHM_SHM_UNITSIZE) ; // x_iCurSize
532
533
533
534
if (((x_pShmMap->x_iLockOffset = allocLock ()) == 0 )
534
535
|| (m_pShmLock = offset2pLock (x_pShmMap->x_iLockOffset )) == NULL
@@ -664,21 +665,23 @@ LsShmStatus_t LsShm::initShm(const char *mapName, LsShmXSize_t size,
664
665
m_pFileName, pShmMap->x_id , m_locks.getId ());
665
666
return LSSHM_BADVERSION;
666
667
}
667
-
668
- if (pShmMap-> x_stat . m_iFileSize != mystat.st_size )
668
+ LsShmXSize_t fileSize = ls_atomic_value (&pShmMap-> x_stat . m_iFileSize );
669
+ if (fileSize != mystat.st_size )
669
670
{
670
671
SHM_WARN (" SHM file [%s] size: %lld, does not match x_stat.m_iFileSize: %ld, correct it" ,
671
- m_pFileName, mystat.st_size , (long )pShmMap->x_stat .m_iFileSize );
672
- pShmMap->x_stat .m_iFileSize = mystat.st_size ;
672
+ m_pFileName, mystat.st_size , (long )fileSize);
673
+ ls_atomic_set (&pShmMap->x_stat .m_iFileSize , mystat.st_size );
674
+ fileSize = mystat.st_size ;
673
675
}
674
676
675
677
// expand the file if needed... won't shrink
676
- if (size > pShmMap-> x_stat . m_iFileSize )
678
+ if (size > fileSize )
677
679
{
678
- if (expandFile ((LsShmOffset_t)pShmMap-> x_stat . m_iFileSize ,
679
- (LsShmXSize_t)(size - pShmMap-> x_stat . m_iFileSize )) != LSSHM_OK)
680
+ if (expandFile ((LsShmOffset_t)fileSize ,
681
+ (LsShmXSize_t)(size - fileSize )) != LSSHM_OK)
680
682
return LSSHM_ERROR;
681
- pShmMap->x_stat .m_iFileSize = size;
683
+ fstat (m_iFd, &mystat);
684
+ ls_atomic_set (&pShmMap->x_stat .m_iFileSize , mystat.st_size );
682
685
}
683
686
else
684
687
size = mystat.st_size ;
@@ -712,14 +715,21 @@ LsShmStatus_t LsShm::initShm(const char *mapName, LsShmXSize_t size,
712
715
// will not shrink at the current moment
713
716
LsShmStatus_t LsShm::expand (LsShmXSize_t incrSize)
714
717
{
715
- LsShmXSize_t xsize = x_pShmMap-> x_stat . m_iFileSize ;
716
-
718
+ LsShmXSize_t xsize = getMapFileSize () ;
719
+ struct stat st;
717
720
if (expandFile ((LsShmOffset_t)xsize, incrSize) != LSSHM_OK)
718
721
return LSSHM_ERROR;
719
722
720
723
// unmap();
721
724
xsize += incrSize;
722
- x_pShmMap->x_stat .m_iFileSize = xsize;
725
+ fstat (m_iFd, &st);
726
+ if (st.st_size != xsize)
727
+ {
728
+ SHM_WARN (" LsShm::expand() [%s], after exapnd, expected size: %d, actual size: %lld" ,
729
+ m_pFileName, xsize, st.st_size );
730
+ xsize = st.st_size ;
731
+ }
732
+ ls_atomic_set (&x_pShmMap->x_stat .m_iFileSize , xsize);
723
733
724
734
if (mapAddrMap (xsize) != LSSHM_OK)
725
735
{
@@ -759,12 +769,14 @@ LsShmStatus_t LsShm::remap()
759
769
struct stat mystat;
760
770
if (fstat (m_iFd, &mystat) < 0 )
761
771
return LSSHM_BADMAPFILE;
762
- if (mystat.st_size != x_pShmMap->x_stat .m_iFileSize )
772
+
773
+ LsShmXSize_t xsize = getMapFileSize ();
774
+ if (mystat.st_size != xsize)
763
775
{
764
776
setErrMsg (LSSHM_SYSERROR, " %s: real file size: %lu, SHM stats file size: %lu." ,
765
777
m_pFileName, (unsigned long )mystat.st_size ,
766
- (unsigned long )x_pShmMap-> x_stat . m_iFileSize );
767
- if ( x_pShmMap-> x_stat . m_iFileSize - mystat.st_size > 100 * 1024 * 1024 )
778
+ (unsigned long )xsize );
779
+ if ( xsize - mystat.st_size > 100 * 1024 * 1024 )
768
780
{
769
781
#ifndef NDEBUG
770
782
LsShmMap mapCopy = *x_pShmMap;
@@ -775,10 +787,10 @@ LsShmStatus_t LsShm::remap()
775
787
776
788
assert (mapCopy.x_stat .m_iFileSize > 0 && !" bad file size." );
777
789
}
790
+ xsize = mystat.st_size ;
778
791
}
779
- LsShmXSize_t size = x_pShmMap->x_stat .m_iFileSize ;
780
792
// unmap();
781
- return mapAddrMap (size );
793
+ return mapAddrMap (xsize );
782
794
}
783
795
784
796
@@ -809,36 +821,39 @@ LsShmOffset_t LsShm::allocPage(LsShmSize_t pagesize)
809
821
LsShmXSize_t needSize = 0 ;
810
822
LsShmXSize_t targetSize = 0 ;
811
823
// Allocate from heap space
824
+ LsShmXSize_t used_size = getUsedSize ();
812
825
availSize = avail ();
813
826
LsShmSize_t availAddrSize = m_addrMap.getAvailAddrSpace (
814
- x_pShmMap-> x_stat . m_iUsedSize , pagesize);
827
+ used_size , pagesize);
815
828
if (pagesize > availSize || pagesize > availAddrSize)
816
829
{
817
830
// min 16 unit at a time
818
831
819
832
LS_DBG (" [SHM] [PID:%d] To alloc page: %d bytes at offset: %ld, availAddr: %d\n " ,
820
- getpid (), pagesize, (long )x_pShmMap-> x_stat . m_iUsedSize , availAddrSize);
833
+ getpid (), pagesize, (long )used_size , availAddrSize);
821
834
if (pagesize > availAddrSize)
822
835
needSize = availAddrSize;
823
836
if (pagesize + needSize > availSize)
824
837
needSize += (pagesize + needSize - availSize);
825
838
826
839
if (needSize > 0 )
827
840
{
828
- targetSize = x_pShmMap->x_stat .m_iFileSize + needSize;
841
+ LsShmXSize_t xsize = getMapFileSize ();
842
+ targetSize = xsize + needSize;
829
843
targetSize = (targetSize + (16 * LSSHM_SHM_UNITSIZE - 1 )) &
830
844
~(16 * LSSHM_SHM_UNITSIZE - 1 );
831
845
832
- if (expand (targetSize - x_pShmMap-> x_stat . m_iFileSize ) != LSSHM_OK)
846
+ if (expand (targetSize - xsize ) != LSSHM_OK)
833
847
{
834
848
offset = 0 ;
835
849
goto out;
836
850
}
837
851
}
852
+ used_size = getUsedSize ();
853
+ availAddrSize = m_addrMap.getAvailAddrSpace (used_size, pagesize);
838
854
if (pagesize > availAddrSize)
839
855
{
840
- m_pGPool->addLeftOverPages (x_pShmMap->x_stat .m_iUsedSize ,
841
- availAddrSize);
856
+ m_pGPool->addLeftOverPages (used_size, availAddrSize);
842
857
used (availAddrSize);
843
858
}
844
859
}
0 commit comments