[postgis-devel] Memory context question
Stephen Woodbridge
woodbri at swoodbridge.com
Tue Apr 23 07:32:58 PDT 2013
Hi Guys,
I have created the function below. At the end of the function I add a
new standardizer to my cache. As part of doing this I need to copy the
table names and attach them to the cache entry and do this with pstrdup().
Because pstrdup() does a palloc(), I think this needs to be done in the
memory context of the cache entry. Am I correct in assuming that I
should do a MemoryContextSwitchTo(STDMemoryContext) before the pstrdup()
and then switch back to the old_context afterwards?
static void
AddToStdPortalCache(StdPortalCache *STDCache, char *lextab, char
*gaztab, char *rultab)
{
MemoryContext STDMemoryContext;
STANDARDIZER *std = NULL;
std = CreateStd(lextab, gaztab, rultab);
if (!std)
elog(ERROR,
"AddToStdPortalCache: could not create address standardizer
for '%s', '%s', '%s'", lextab, gaztab, rultab);
/* if the NextSlot in the cache is used, then delete it */
if (STDCache->StdCache[STDCache->NextSlot].std != NULL) {
StdCacheItem *ci = &STDCache->StdCache[STDCache->NextSlot];
DBG("Removing item from STD cache ('%s', '%s', '%s') index %d",
ci->lextab, ci->gaztab, ci->rultab, STDCache->NextSlot);
DeleteNextSlotFromStdCache(STDCache);
}
DBG("Adding item to STD cache ('%s', '%s', '%s') index %d", lextab,
gaztab, rultab, STDCache->NextSlot);
STDMemoryContext = MemoryContextCreate(T_AllocSetContext, 8192,
&StdCacheContextMethods,
STDCache->StdCacheContext,
"PAGC STD Memory Context");
/* Create the backend hash if it doesn't already exist */
if (!StdHash)
StdHash = CreateStdHash();
/*
* Add the MemoryContext to the backend hash so we can
* clean up upon portal shutdown
*/
DBG("Adding standardizer obj (%p) to hash table with MemoryContext
key (%p)", std, STDMemoryContext);
AddStdHashEntry(STDMemoryContext, std);
// TODO: do we need to change memory context before the pstrdup ????
STDCache->StdCache[STDCache->NextSlot].lextab = pstrdup(lextab);
STDCache->StdCache[STDCache->NextSlot].gaztab = pstrdup(gaztab);
STDCache->StdCache[STDCache->NextSlot].rultab = pstrdup(rultab);
STDCache->StdCache[STDCache->NextSlot].std = std;
STDCache->StdCache[STDCache->NextSlot].std_mcxt = STDMemoryContext;
STDCache->NextSlot = (STDCache->NextSlot + 1) % STD_CACHE_ITEMS;
}
More information about the postgis-devel
mailing list