Ticket #168: jabberd2_limit_nadcache.diff

File jabberd2_limit_nadcache.diff, 2.9 KB (added by markdoliner, 15 months ago)
  • util/nad.c

     
    117117nad_cache_t nad_cache_new(void) 
    118118{ 
    119119    nad_cache_t cache; 
    120     cache = malloc(sizeof(nad_cache_t)); 
    121     *cache = NULL; 
     120    cache = malloc(sizeof(struct nad_cache_st)); 
     121    cache->len = 0; 
     122    cache->nads = malloc(sizeof(struct nad_st **)); 
     123    *cache->nads = NULL; 
    122124 
    123125#ifdef NAD_DEBUG 
    124126    if(_nad_alloc_tracked == NULL) _nad_alloc_tracked = xhash_new(501); 
     
    133135void nad_cache_free(nad_cache_t cache) 
    134136{ 
    135137    nad_t cur; 
    136     while((cur = *cache) != NULL) 
     138    while((cur = *cache->nads) != NULL) 
    137139    { 
    138         *cache = cur->next; 
     140        *cache->nads = cur->next; 
    139141        free(cur->elems); 
    140142        free(cur->attrs); 
    141143        free(cur->nss); 
     
    143145        free(cur->depths); 
    144146        free(cur); 
    145147    } 
     148    free(cache->nads); 
    146149    free(cache); 
    147150} 
    148151 
     
    154157#ifndef NAD_DEBUG 
    155158    /* If cache==NULL, then this NAD is not in a cache */ 
    156159 
    157     if ((cache!=NULL) && (*cache != NULL)) 
     160    if ((cache!=NULL) && (*cache->nads != NULL)) 
    158161    { 
    159         nad = *cache; 
    160         *cache = nad->next; 
     162        nad = *cache->nads; 
     163        *cache->nads = nad->next; 
     164        cache->len--; 
    161165        nad->ccur = nad->ecur = nad->acur = nad->ncur = 0; 
    162166        nad->scope = -1; 
    163167        nad->cache = cache; 
     
    232236    xhash_put(_nad_free_tracked, pstrdup(xhash_pool(_nad_free_tracked), loc), (void *) nad); 
    233237    } 
    234238#else 
    235     /* If nad->cache != NULL, then put back into cache, otherwise this nad is not in a cache */ 
    236  
    237     if (nad->cache != NULL) { 
    238        nad->next = *(nad->cache); 
    239        *(nad->cache) = nad; 
     239    /* If nad->cache != NULL, there are less than 100 nads in the 
     240     * cache and this nad isn't gigantic then put back into cache, 
     241     * otherwise we should just free this nad */ 
     242    if (nad->cache != NULL && nad->cache->len < 100 && nad->elen < 100000 && nad->alen < 100000 && nad->clen < 100000 && nad->dlen < 100000) { 
     243       nad->next = *(nad->cache->nads); 
     244       *(nad->cache->nads) = nad; 
     245       nad->cache->len++; 
    240246       return; 
    241247    }  
    242248#endif 
  • util/nad.h

     
    4949# include <config.h> 
    5050#endif 
    5151 
     52#ifdef HAVE_SYS_TYPES_H 
     53# include <sys/types.h> 
     54#endif 
     55 
    5256/* jabberd2 Windows DLL */ 
    5357#ifndef JABBERD2_API 
    5458# ifdef _WIN32 
     
    6266# endif /* _WIN32 */ 
    6367#endif /* JABBERD2_API */ 
    6468 
    65 typedef struct nad_st **nad_cache_t; 
     69typedef struct nad_cache_st *nad_cache_t; 
    6670 
    6771struct nad_elem_st { 
    6872    int parent; 
     
    102106    struct nad_st *next; /* for keeping a list of nads */ 
    103107} *nad_t; 
    104108 
     109struct nad_cache_st 
     110{ 
     111    struct nad_st **nads; 
     112    size_t len; 
     113}; 
     114 
    105115/** create a new cache, simple pointer to a list of nads */ 
    106116JABBERD2_API nad_cache_t nad_cache_new(void); 
    107117