Changeset 147 for trunk/lib/lru_cache.c
- Timestamp:
- 02/22/09 14:31:52 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/lru_cache.c
r146 r147 1 1 /* 2 * Copyright (C) 2008 Timothy D. Morgan2 * Copyright (C) 2008-2009 Timothy D. Morgan 3 3 * 4 4 * This program is free software; you can redistribute it and/or modify … … 18 18 */ 19 19 20 #include " ../include/lru_cache.h"20 #include "lru_cache.h" 21 21 22 22 … … 97 97 #endif 98 98 99 lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret, bool free_data) 99 100 lru_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 106 lru_cache* lru_cache_create_ctx(void* talloc_ctx, uint32_t max_keys, 107 uint32_t secret, bool talloc_data) 100 108 { 101 109 lru_cache* ret_val; 102 110 103 ret_val = (lru_cache*)malloc(sizeof(lru_cache));111 ret_val = talloc(talloc_ctx, lru_cache); 104 112 if(ret_val == NULL) 105 113 return NULL; … … 114 122 } 115 123 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); 119 126 if(ret_val->table == NULL) 120 127 { 121 free(ret_val);128 talloc_free(ret_val); 122 129 return NULL; 123 130 } … … 127 134 ret_val->max_keys = max_keys; 128 135 ret_val->secret = secret; 129 ret_val-> free_data = free_data;136 ret_val->talloc_data = talloc_data; 130 137 ret_val->num_keys = 0; 131 138 memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*)); … … 137 144 void lru_cache_destroy(lru_cache* ht) 138 145 { 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);153 146 ht->secret = 0; 154 free(ht);147 talloc_free(ht); 155 148 } 156 149 … … 179 172 * so remove it from the list for now. 180 173 */ 181 if(ht-> free_data)182 free(e->data);174 if(ht->talloc_data) 175 talloc_free(e->data); 183 176 184 177 if(e->newer == NULL) … … 226 219 e->next = NULL; 227 220 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); 232 225 if(tmp_index == NULL) 233 226 { 234 free(e->index); 235 free(e); 227 talloc_free(e); 236 228 return false; 237 229 } … … 242 234 { /* Brand new element because we have room to spare. */ 243 235 244 e = (lru_cache_element*)malloc(sizeof(lru_cache_element));236 e = talloc(ht->table, lru_cache_element); 245 237 if(e == NULL) 246 238 return false; 247 239 248 e->index = malloc(index_len);240 e->index = talloc_size(e, index_len); 249 241 if(e->index == NULL) 250 242 { 251 free(e);243 talloc_free(e); 252 244 return false; 253 245 } … … 264 256 } 265 257 e->data = data; 258 if(ht->talloc_data) 259 talloc_steal(e, e->data); 266 260 267 261 /* Finally, let's insert the element to the newest position in the LRU list.*/ … … 336 330 return false; 337 331 338 if(ht->free_data)339 free(cur->data);340 341 332 /* Detach from list */ 342 333 if(cur->newer == NULL) … … 356 347 last->next = cur->next; 357 348 358 free(cur->index); 359 free(cur); 349 talloc_free(cur); 360 350 361 351 /* Removing entry, decrement counters. */
Note: See TracChangeset
for help on using the changeset viewer.