@@ -166,7 +166,7 @@ _PG_fini(void)
166
166
static void
167
167
ispell_shmem_startup ()
168
168
{
169
- bool found = FALSE ;
169
+ bool found = false ;
170
170
char * segment ;
171
171
172
172
if (prev_shmem_startup_hook )
@@ -177,27 +177,19 @@ ispell_shmem_startup()
177
177
*/
178
178
LWLockAcquire (AddinShmemInitLock , LW_EXCLUSIVE );
179
179
180
- segment = ShmemInitStruct (SEGMENT_NAME ,
181
- max_ispell_mem_size (),
182
- & found );
180
+ segment = ShmemInitStruct (SEGMENT_NAME , max_ispell_mem_size (), & found );
183
181
segment_info = (SegmentInfo * ) segment ;
184
182
185
183
/* Was the shared memory segment already initialized? */
186
184
if (!found )
187
185
{
188
- if (segment == NULL ) {
189
- ereport (ERROR ,
190
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
191
- errmsg ("Cannot acquire %d kB of shared memory" ,
192
- max_ispell_mem_size_kb )));
193
- }
194
186
memset (segment , 0 , max_ispell_mem_size ());
195
187
196
- #if PG_VERSION_NUM >= 90600
188
+ #if PG_VERSION_NUM >= 90600
197
189
segment_info -> lock = & (GetNamedLWLockTranche ("shared_ispell" ))-> lock ;
198
- #else
190
+ #else
199
191
segment_info -> lock = LWLockAssign ();
200
- #endif
192
+ #endif
201
193
segment_info -> firstfree = segment + MAXALIGN (sizeof (SegmentInfo ));
202
194
segment_info -> available = max_ispell_mem_size ()
203
195
- (int )(segment_info -> firstfree - segment );
@@ -291,11 +283,15 @@ clean_dict_affix(IspellDict *dict)
291
283
* of the shared memory (using SegmentInfo->lock).
292
284
*/
293
285
static void
294
- init_shared_dict (DictInfo * info , char * dictFile , char * affFile , char * stopFile )
286
+ init_shared_dict (DictInfo * info , MemoryContext infoCntx ,
287
+ char * dictFile , char * affFile , char * stopFile )
295
288
{
296
- int size ;
289
+ int size ;
297
290
SharedIspellDict * shdict = NULL ;
298
- SharedStopList * shstop = NULL ;
291
+ SharedStopList * shstop = NULL ;
292
+ MemoryContext oldctx ;
293
+
294
+ oldctx = MemoryContextSwitchTo (infoCntx );
299
295
300
296
/* DICTIONARY + AFFIXES */
301
297
@@ -315,7 +311,7 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
315
311
/* load the dictionary (word list) if not yet defined */
316
312
if (shdict == NULL )
317
313
{
318
- IspellDict * dict ;
314
+ IspellDict * dict ;
319
315
320
316
dict = (IspellDict * ) palloc0 (sizeof (IspellDict ));
321
317
@@ -337,11 +333,13 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
337
333
*/
338
334
if (info -> dict .useFlagAliases )
339
335
{
340
- int i ;
336
+ int i ;
337
+
341
338
dict -> useFlagAliases = true;
342
339
dict -> lenAffixData = info -> dict .lenAffixData ;
343
340
dict -> nAffixData = info -> dict .nAffixData ;
344
341
dict -> AffixData = (char * * ) palloc0 (dict -> nAffixData * sizeof (char * ));
342
+
345
343
for (i = 0 ; i < dict -> nAffixData ; i ++ )
346
344
{
347
345
dict -> AffixData [i ] = palloc0 (strlen (info -> dict .AffixData [i ]) + 1 );
@@ -419,8 +417,9 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
419
417
else
420
418
memset (info -> stopFile , 0 , sizeof (info -> stopFile ));
421
419
420
+ MemoryContextSwitchTo (oldctx );
422
421
/* save current context as long-lived */
423
- info -> saveCntx = CurrentMemoryContext ;
422
+ info -> infoCntx = infoCntx ;
424
423
}
425
424
426
425
Datum dispell_init (PG_FUNCTION_ARGS );
@@ -582,7 +581,15 @@ dispell_init(PG_FUNCTION_ARGS)
582
581
/* search if the dictionary is already initialized */
583
582
LWLockAcquire (segment_info -> lock , LW_EXCLUSIVE );
584
583
585
- init_shared_dict (info , dictFile , affFile , stopFile );
584
+ /*
585
+ * Current context is a long lived context. Create child context to store
586
+ * DictInfo internal data.
587
+ */
588
+ info -> infoCntx = AllocSetContextCreate (CurrentMemoryContext ,
589
+ "shared_ispell context" ,
590
+ ALLOCSET_DEFAULT_SIZES );
591
+
592
+ init_shared_dict (info , info -> infoCntx , dictFile , affFile , stopFile );
586
593
587
594
LWLockRelease (segment_info -> lock );
588
595
@@ -611,8 +618,7 @@ dispell_lexize(PG_FUNCTION_ARGS)
611
618
/* do we need to reinit the dictionary? was the dict reset since the lookup */
612
619
if (timestamp_cmp_internal (info -> lookup , segment_info -> lastReset ) < 0 )
613
620
{
614
- DictInfo saveInfo = * info ;
615
- MemoryContext ctx ;
621
+ DictInfo saveInfo = * info ;
616
622
617
623
/* relock in exclusive mode */
618
624
LWLockRelease (segment_info -> lock );
@@ -623,15 +629,11 @@ dispell_lexize(PG_FUNCTION_ARGS)
623
629
* info here
624
630
*/
625
631
626
- MemoryContextResetAndDeleteChildren (saveInfo .saveCntx );
627
- ctx = MemoryContextSwitchTo (saveInfo .saveCntx );
628
-
632
+ MemoryContextResetAndDeleteChildren (saveInfo .infoCntx );
629
633
MemSet (info , 0 , sizeof (* info ));
630
634
631
- init_shared_dict (info , saveInfo .dictFile ,
635
+ init_shared_dict (info , saveInfo .infoCntx , saveInfo . dictFile ,
632
636
saveInfo .affixFile , saveInfo .stopFile );
633
-
634
- MemoryContextSwitchTo (ctx );
635
637
}
636
638
637
639
res = NINormalizeWord (& (info -> dict ), txt );
@@ -721,8 +723,8 @@ shstrcpy(char *str)
721
723
static SPNode *
722
724
copySPNode (SPNode * node )
723
725
{
724
- int i ;
725
- SPNode * copy = NULL ;
726
+ int i ;
727
+ SPNode * copy = NULL ;
726
728
727
729
if (node == NULL )
728
730
return NULL ;
@@ -739,8 +741,8 @@ copySPNode(SPNode *node)
739
741
static int
740
742
sizeSPNode (SPNode * node )
741
743
{
742
- int i ;
743
- int size = 0 ;
744
+ int i ;
745
+ int size = 0 ;
744
746
745
747
if (node == NULL )
746
748
return 0 ;
@@ -852,9 +854,9 @@ sizeIspellDict(IspellDict *dict, char *dictFile, char *affixFile)
852
854
Datum
853
855
dispell_list_dicts (PG_FUNCTION_ARGS )
854
856
{
855
- FuncCallContext * funcctx ;
856
- TupleDesc tupdesc ;
857
- SharedIspellDict * dict ;
857
+ FuncCallContext * funcctx ;
858
+ TupleDesc tupdesc ;
859
+ SharedIspellDict * dict ;
858
860
859
861
/* init on the first call */
860
862
if (SRF_IS_FIRSTCALL ())
0 commit comments