Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 570e569

Browse files
committed
More fixes to shared ispell
1 parent 20bb213 commit 570e569

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

contrib/shared_ispell/src/shared_ispell.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ _PG_fini(void)
166166
static void
167167
ispell_shmem_startup()
168168
{
169-
bool found = FALSE;
169+
bool found = false;
170170
char *segment;
171171

172172
if (prev_shmem_startup_hook)
@@ -177,27 +177,19 @@ ispell_shmem_startup()
177177
*/
178178
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
179179

180-
segment = ShmemInitStruct(SEGMENT_NAME,
181-
max_ispell_mem_size(),
182-
&found);
180+
segment = ShmemInitStruct(SEGMENT_NAME, max_ispell_mem_size(), &found);
183181
segment_info = (SegmentInfo *) segment;
184182

185183
/* Was the shared memory segment already initialized? */
186184
if (!found)
187185
{
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-
}
194186
memset(segment, 0, max_ispell_mem_size());
195187

196-
#if PG_VERSION_NUM >= 90600
188+
#if PG_VERSION_NUM >= 90600
197189
segment_info->lock = &(GetNamedLWLockTranche("shared_ispell"))->lock;
198-
#else
190+
#else
199191
segment_info->lock = LWLockAssign();
200-
#endif
192+
#endif
201193
segment_info->firstfree = segment + MAXALIGN(sizeof(SegmentInfo));
202194
segment_info->available = max_ispell_mem_size()
203195
- (int)(segment_info->firstfree - segment);
@@ -291,11 +283,15 @@ clean_dict_affix(IspellDict *dict)
291283
* of the shared memory (using SegmentInfo->lock).
292284
*/
293285
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)
295288
{
296-
int size;
289+
int size;
297290
SharedIspellDict *shdict = NULL;
298-
SharedStopList *shstop = NULL;
291+
SharedStopList *shstop = NULL;
292+
MemoryContext oldctx;
293+
294+
oldctx = MemoryContextSwitchTo(infoCntx);
299295

300296
/* DICTIONARY + AFFIXES */
301297

@@ -315,7 +311,7 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
315311
/* load the dictionary (word list) if not yet defined */
316312
if (shdict == NULL)
317313
{
318-
IspellDict *dict;
314+
IspellDict *dict;
319315

320316
dict = (IspellDict *) palloc0(sizeof(IspellDict));
321317

@@ -337,11 +333,13 @@ init_shared_dict(DictInfo *info, char *dictFile, char *affFile, char *stopFile)
337333
*/
338334
if (info->dict.useFlagAliases)
339335
{
340-
int i;
336+
int i;
337+
341338
dict->useFlagAliases = true;
342339
dict->lenAffixData = info->dict.lenAffixData;
343340
dict->nAffixData = info->dict.nAffixData;
344341
dict->AffixData = (char **) palloc0(dict->nAffixData * sizeof(char *));
342+
345343
for (i = 0; i < dict->nAffixData; i++)
346344
{
347345
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)
419417
else
420418
memset(info->stopFile, 0, sizeof(info->stopFile));
421419

420+
MemoryContextSwitchTo(oldctx);
422421
/* save current context as long-lived */
423-
info->saveCntx = CurrentMemoryContext;
422+
info->infoCntx = infoCntx;
424423
}
425424

426425
Datum dispell_init(PG_FUNCTION_ARGS);
@@ -582,7 +581,15 @@ dispell_init(PG_FUNCTION_ARGS)
582581
/* search if the dictionary is already initialized */
583582
LWLockAcquire(segment_info->lock, LW_EXCLUSIVE);
584583

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);
586593

587594
LWLockRelease(segment_info->lock);
588595

@@ -611,8 +618,7 @@ dispell_lexize(PG_FUNCTION_ARGS)
611618
/* do we need to reinit the dictionary? was the dict reset since the lookup */
612619
if (timestamp_cmp_internal(info->lookup, segment_info->lastReset) < 0)
613620
{
614-
DictInfo saveInfo = *info;
615-
MemoryContext ctx;
621+
DictInfo saveInfo = *info;
616622

617623
/* relock in exclusive mode */
618624
LWLockRelease(segment_info->lock);
@@ -623,15 +629,11 @@ dispell_lexize(PG_FUNCTION_ARGS)
623629
* info here
624630
*/
625631

626-
MemoryContextResetAndDeleteChildren(saveInfo.saveCntx);
627-
ctx = MemoryContextSwitchTo(saveInfo.saveCntx);
628-
632+
MemoryContextResetAndDeleteChildren(saveInfo.infoCntx);
629633
MemSet(info, 0, sizeof(*info));
630634

631-
init_shared_dict(info, saveInfo.dictFile,
635+
init_shared_dict(info, saveInfo.infoCntx, saveInfo.dictFile,
632636
saveInfo.affixFile, saveInfo.stopFile);
633-
634-
MemoryContextSwitchTo(ctx);
635637
}
636638

637639
res = NINormalizeWord(&(info->dict), txt);
@@ -721,8 +723,8 @@ shstrcpy(char *str)
721723
static SPNode *
722724
copySPNode(SPNode *node)
723725
{
724-
int i;
725-
SPNode *copy = NULL;
726+
int i;
727+
SPNode *copy = NULL;
726728

727729
if (node == NULL)
728730
return NULL;
@@ -739,8 +741,8 @@ copySPNode(SPNode *node)
739741
static int
740742
sizeSPNode(SPNode *node)
741743
{
742-
int i;
743-
int size = 0;
744+
int i;
745+
int size = 0;
744746

745747
if (node == NULL)
746748
return 0;
@@ -852,9 +854,9 @@ sizeIspellDict(IspellDict *dict, char *dictFile, char *affixFile)
852854
Datum
853855
dispell_list_dicts(PG_FUNCTION_ARGS)
854856
{
855-
FuncCallContext *funcctx;
856-
TupleDesc tupdesc;
857-
SharedIspellDict *dict;
857+
FuncCallContext *funcctx;
858+
TupleDesc tupdesc;
859+
SharedIspellDict *dict;
858860

859861
/* init on the first call */
860862
if (SRF_IS_FIRSTCALL())

contrib/shared_ispell/src/shared_ispell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ typedef struct DictInfo
6969
SharedStopList *shstop;
7070

7171
/* MemoryContext of dict local content */
72-
MemoryContext saveCntx;
72+
MemoryContext infoCntx;
7373
} DictInfo;
7474

7575
#endif

0 commit comments

Comments
 (0)