[postgis-devel] Memory context question

Paul Ramsey pramsey at cleverelephant.ca
Tue Apr 23 09:46:59 PDT 2013


Yes, you have to be in the memory context you want the allocated memory to live in. I like to do all my allocations in once place so there's only one context switch, but there's you reason you can't do it as often as you like. 

-- 
Paul Ramsey
http://cleverelephant.ca/
http://postgis.net/


On Tuesday, 23 April, 2013 at 7:32 AM, Stephen Woodbridge wrote:

> 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;
> }
> 
> 
> _______________________________________________
> postgis-devel mailing list
> postgis-devel at lists.osgeo.org (mailto:postgis-devel at lists.osgeo.org)
> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-devel






More information about the postgis-devel mailing list