Changeset 147 for trunk/lib/lru_cache.c


Ignore:
Timestamp:
02/22/09 14:31:52 (15 years ago)
Author:
tim
Message:

added talloc library

incorporated talloc into winsec and lru_cache modules

introduced talloc into SK caching system

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/lru_cache.c

    r146 r147  
    11/*
    2  * Copyright (C) 2008 Timothy D. Morgan
     2 * Copyright (C) 2008-2009 Timothy D. Morgan
    33 *
    44 * This program is free software; you can redistribute it and/or modify
     
    1818 */
    1919
    20 #include "../include/lru_cache.h"
     20#include "lru_cache.h"
    2121
    2222
     
    9797#endif
    9898
    99 lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret, bool free_data)
     99
     100lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret)
     101{
     102  return lru_cache_create_ctx(NULL, max_keys, secret, false);
     103}
     104
     105
     106lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys,
     107                                uint32_t secret, bool talloc_data)
    100108{
    101109  lru_cache* ret_val;
    102110
    103   ret_val = (lru_cache*)malloc(sizeof(lru_cache));
     111  ret_val = talloc(talloc_ctx, lru_cache);
    104112  if(ret_val == NULL)
    105113    return NULL;
     
    114122  }
    115123
    116   ret_val->table
    117     = (lru_cache_element**)malloc(sizeof(lru_cache_element*)
    118                                   * ret_val->num_buckets);
     124  ret_val->table = talloc_array(ret_val,
     125                                lru_cache_element*, ret_val->num_buckets);
    119126  if(ret_val->table == NULL)
    120127  {
    121     free(ret_val);
     128    talloc_free(ret_val);
    122129    return NULL;
    123130  }
     
    127134  ret_val->max_keys = max_keys;
    128135  ret_val->secret = secret;
    129   ret_val->free_data = free_data;
     136  ret_val->talloc_data = talloc_data;
    130137  ret_val->num_keys = 0;
    131138  memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*));
     
    137144void lru_cache_destroy(lru_cache* ht)
    138145{
    139   lru_cache_element* cur;
    140   lru_cache_element* last = NULL;
    141  
    142   for(cur=ht->oldest; cur != NULL; last=cur,cur=cur->newer)
    143   {
    144     if(last != NULL)
    145     {
    146       if(ht->free_data)
    147         free(last->data);
    148       free(last->index);
    149       free(last);
    150     }
    151   }
    152   free(ht->table);
    153146  ht->secret = 0;
    154   free(ht);
     147  talloc_free(ht);
    155148}
    156149
     
    179172     * so remove it from the list for now.
    180173     */
    181     if(ht->free_data)
    182       free(e->data);
     174    if(ht->talloc_data)
     175      talloc_free(e->data);
    183176
    184177    if(e->newer == NULL)
     
    226219      e->next = NULL;
    227220
    228       if(ht->free_data)
    229         free(e->data);
    230 
    231       tmp_index = realloc(e->index, index_len);
     221      if(ht->talloc_data)
     222        talloc_free(e->data);
     223
     224      tmp_index = talloc_realloc_size(e, e->index, index_len);
    232225      if(tmp_index == NULL)
    233226      {
    234         free(e->index);
    235         free(e);
     227        talloc_free(e);
    236228        return false;
    237229      }
     
    242234    { /* Brand new element because we have room to spare. */
    243235
    244       e = (lru_cache_element*)malloc(sizeof(lru_cache_element));
     236      e = talloc(ht->table, lru_cache_element);
    245237      if(e == NULL)
    246238        return false;
    247239     
    248       e->index = malloc(index_len);
     240      e->index = talloc_size(e, index_len);
    249241      if(e->index == NULL)
    250242      {
    251         free(e);
     243        talloc_free(e);
    252244        return false;
    253245      }
     
    264256  }
    265257  e->data = data;
     258  if(ht->talloc_data)
     259    talloc_steal(e, e->data);
    266260
    267261  /* Finally, let's insert the element to the newest position in the LRU list.*/
     
    336330    return false;
    337331
    338   if(ht->free_data)
    339     free(cur->data);
    340 
    341332  /* Detach from list */
    342333  if(cur->newer == NULL)
     
    356347    last->next = cur->next;
    357348
    358   free(cur->index);
    359   free(cur);
     349  talloc_free(cur);
    360350 
    361351  /* Removing entry, decrement counters. */
Note: See TracChangeset for help on using the changeset viewer.