@@ -294,13 +294,9 @@ static void
294
294
init_shared_dict (DictInfo * info , char * dictFile , char * affFile , char * stopFile )
295
295
{
296
296
int size ;
297
-
298
297
SharedIspellDict * shdict = NULL ;
299
298
SharedStopList * shstop = NULL ;
300
299
301
- IspellDict * dict ;
302
- StopList stoplist ;
303
-
304
300
/* DICTIONARY + AFFIXES */
305
301
306
302
/* TODO This should probably check that the filenames are not NULL, and maybe that
@@ -319,6 +315,8 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
319
315
/* load the dictionary (word list) if not yet defined */
320
316
if (shdict == NULL )
321
317
{
318
+ IspellDict * dict ;
319
+
322
320
dict = (IspellDict * ) palloc0 (sizeof (IspellDict ));
323
321
324
322
NIStartBuild (dict );
@@ -389,6 +387,8 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
389
387
/* load the stopwords if not yet defined */
390
388
if (shstop == NULL )
391
389
{
390
+ StopList stoplist ;
391
+
392
392
readstoplist (stopFile , & stoplist , lowerstr );
393
393
394
394
size = sizeStopList (& stoplist , stopFile );
@@ -418,6 +418,9 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
418
418
memcpy (info -> stopFile , stopFile , strlen (stopFile ) + 1 );
419
419
else
420
420
memset (info -> stopFile , 0 , sizeof (info -> stopFile ));
421
+
422
+ /* save current context as long-lived */
423
+ info -> saveCntx = CurrentMemoryContext ;
421
424
}
422
425
423
426
Datum dispell_init (PG_FUNCTION_ARGS );
@@ -504,6 +507,9 @@ dispell_mem_used(PG_FUNCTION_ARGS)
504
507
* The StopWords parameter is optional, the two other are required.
505
508
*
506
509
* If any of the filenames are incorrect, the call to init_shared_dict will fail.
510
+ *
511
+ * Do not call it directly - it saves current memory context as long-lived
512
+ * context.
507
513
*/
508
514
Datum
509
515
dispell_init (PG_FUNCTION_ARGS )
@@ -605,11 +611,27 @@ dispell_lexize(PG_FUNCTION_ARGS)
605
611
/* do we need to reinit the dictionary? was the dict reset since the lookup */
606
612
if (timestamp_cmp_internal (info -> lookup , segment_info -> lastReset ) < 0 )
607
613
{
614
+ DictInfo saveInfo = * info ;
615
+ MemoryContext ctx ;
616
+
608
617
/* relock in exclusive mode */
609
618
LWLockRelease (segment_info -> lock );
610
619
LWLockAcquire (segment_info -> lock , LW_EXCLUSIVE );
611
620
612
- init_shared_dict (info , info -> dictFile , info -> affixFile , info -> stopFile );
621
+ /*
622
+ * info is allocated in info->saveCntx, so that's why we use a copy of
623
+ * info here
624
+ */
625
+
626
+ MemoryContextResetAndDeleteChildren (saveInfo .saveCntx );
627
+ ctx = MemoryContextSwitchTo (saveInfo .saveCntx );
628
+
629
+ info = palloc0 (sizeof (* info ));
630
+
631
+ init_shared_dict (info , saveInfo .dictFile ,
632
+ saveInfo .affixFile , saveInfo .stopFile );
633
+
634
+ MemoryContextSwitchTo (ctx );
613
635
}
614
636
615
637
res = NINormalizeWord (& (info -> dict ), txt );
0 commit comments