@@ -302,6 +302,10 @@ tbm_create(long maxbytes, dsa_area *dsa)
302
302
tbm -> maxentries = (int ) nbuckets ;
303
303
tbm -> lossify_start = 0 ;
304
304
tbm -> dsa = dsa ;
305
+ tbm -> dsapagetable = InvalidDsaPointer ;
306
+ tbm -> dsapagetableold = InvalidDsaPointer ;
307
+ tbm -> ptpages = InvalidDsaPointer ;
308
+ tbm -> ptchunks = InvalidDsaPointer ;
305
309
306
310
return tbm ;
307
311
}
@@ -363,20 +367,23 @@ void
363
367
tbm_free_shared_area (dsa_area * dsa , dsa_pointer dp )
364
368
{
365
369
TBMSharedIteratorState * istate = dsa_get_address (dsa , dp );
366
- PTEntryArray * ptbase = dsa_get_address ( dsa , istate -> pagetable ) ;
370
+ PTEntryArray * ptbase ;
367
371
PTIterationArray * ptpages ;
368
372
PTIterationArray * ptchunks ;
369
373
370
- if (pg_atomic_sub_fetch_u32 (& ptbase -> refcount , 1 ) == 0 )
371
- dsa_free (dsa , istate -> pagetable );
372
-
373
- if (istate -> spages )
374
+ if (DsaPointerIsValid (istate -> pagetable ))
375
+ {
376
+ ptbase = dsa_get_address (dsa , istate -> pagetable );
377
+ if (pg_atomic_sub_fetch_u32 (& ptbase -> refcount , 1 ) == 0 )
378
+ dsa_free (dsa , istate -> pagetable );
379
+ }
380
+ if (DsaPointerIsValid (istate -> spages ))
374
381
{
375
382
ptpages = dsa_get_address (dsa , istate -> spages );
376
383
if (pg_atomic_sub_fetch_u32 (& ptpages -> refcount , 1 ) == 0 )
377
384
dsa_free (dsa , istate -> spages );
378
385
}
379
- if (istate -> schunks )
386
+ if (DsaPointerIsValid ( istate -> schunks ) )
380
387
{
381
388
ptchunks = dsa_get_address (dsa , istate -> schunks );
382
389
if (pg_atomic_sub_fetch_u32 (& ptchunks -> refcount , 1 ) == 0 )
@@ -786,7 +793,7 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
786
793
{
787
794
dsa_pointer dp ;
788
795
TBMSharedIteratorState * istate ;
789
- PTEntryArray * ptbase ;
796
+ PTEntryArray * ptbase = NULL ;
790
797
PTIterationArray * ptpages = NULL ;
791
798
PTIterationArray * ptchunks = NULL ;
792
799
@@ -797,7 +804,7 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
797
804
* Allocate TBMSharedIteratorState from DSA to hold the shared members and
798
805
* lock, this will also be used by multiple worker for shared iterate.
799
806
*/
800
- dp = dsa_allocate (tbm -> dsa , sizeof (TBMSharedIteratorState ));
807
+ dp = dsa_allocate0 (tbm -> dsa , sizeof (TBMSharedIteratorState ));
801
808
istate = dsa_get_address (tbm -> dsa , dp );
802
809
803
810
/*
@@ -856,7 +863,7 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
856
863
Assert (npages == tbm -> npages );
857
864
Assert (nchunks == tbm -> nchunks );
858
865
}
859
- else
866
+ else if ( tbm -> status == TBM_ONE_PAGE )
860
867
{
861
868
/*
862
869
* In one page mode allocate the space for one pagetable entry and
@@ -868,8 +875,8 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
868
875
ptpages -> index [0 ] = 0 ;
869
876
}
870
877
871
- pg_atomic_init_u32 ( & ptbase -> refcount , 0 );
872
-
878
+ if ( ptbase != NULL )
879
+ pg_atomic_init_u32 ( & ptbase -> refcount , 0 );
873
880
if (npages > 1 )
874
881
qsort_arg ((void * ) (ptpages -> index ), npages , sizeof (int ),
875
882
tbm_shared_comparator , (void * ) ptbase -> ptentry );
@@ -899,10 +906,11 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
899
906
* increase the refcount by 1 so that while freeing the shared iterator
900
907
* we don't free pagetable and iterator array until its refcount becomes 0.
901
908
*/
902
- pg_atomic_add_fetch_u32 (& ptbase -> refcount , 1 );
903
- if (ptpages )
909
+ if (ptbase != NULL )
910
+ pg_atomic_add_fetch_u32 (& ptbase -> refcount , 1 );
911
+ if (ptpages != NULL )
904
912
pg_atomic_add_fetch_u32 (& ptpages -> refcount , 1 );
905
- if (ptchunks )
913
+ if (ptchunks != NULL )
906
914
pg_atomic_add_fetch_u32 (& ptchunks -> refcount , 1 );
907
915
908
916
/* Initialize the iterator lock */
@@ -1069,9 +1077,16 @@ tbm_shared_iterate(TBMSharedIterator *iterator)
1069
1077
{
1070
1078
TBMIterateResult * output = & iterator -> output ;
1071
1079
TBMSharedIteratorState * istate = iterator -> state ;
1072
- PagetableEntry * ptbase = iterator -> ptbase -> ptentry ;
1073
- int * idxpages = iterator -> ptpages -> index ;
1074
- int * idxchunks = iterator -> ptchunks -> index ;
1080
+ PagetableEntry * ptbase = NULL ;
1081
+ int * idxpages = NULL ;
1082
+ int * idxchunks = NULL ;
1083
+
1084
+ if (iterator -> ptbase != NULL )
1085
+ ptbase = iterator -> ptbase -> ptentry ;
1086
+ if (iterator -> ptpages != NULL )
1087
+ idxpages = iterator -> ptpages -> index ;
1088
+ if (iterator -> ptchunks != NULL )
1089
+ idxchunks = iterator -> ptchunks -> index ;
1075
1090
1076
1091
/* Acquire the LWLock before accessing the shared members */
1077
1092
LWLockAcquire (& istate -> lock , LW_EXCLUSIVE );
@@ -1480,7 +1495,7 @@ tbm_attach_shared_iterate(dsa_area *dsa, dsa_pointer dp)
1480
1495
* Create the TBMSharedIterator struct, with enough trailing space to
1481
1496
* serve the needs of the TBMIterateResult sub-struct.
1482
1497
*/
1483
- iterator = (TBMSharedIterator * ) palloc (sizeof (TBMSharedIterator ) +
1498
+ iterator = (TBMSharedIterator * ) palloc0 (sizeof (TBMSharedIterator ) +
1484
1499
MAX_TUPLES_PER_PAGE * sizeof (OffsetNumber ));
1485
1500
1486
1501
istate = (TBMSharedIteratorState * ) dsa_get_address (dsa , dp );
0 commit comments