123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "cache.h"
- cache_page_t* cache_page_new(cache_t* parent, db_data_t* data)
- {
- cache_page_t* cp = NULL;
- cp = (cache_page_t*) malloc(sizeof(cache_page_t));
- if(!cp)
- {
- fprintf(stderr, "Unable to allocate %ld bytes for cache page (cache_page_new())",
- sizeof(cache_page_t));
- perror(" ");
- return NULL;
- }
-
- memset(cp, 0, sizeof(cache_page_t));
- cp->ptr=data;
- cp->page_size=parent->page_size;
- return cp;
- }
- void cache_page_set_value(cache_page_t* cp, db_interval_t inter, db_data_t* data)
- {
- memcpy(cp->ptr, data, sizeof(db_data_t)*cp->page_size);
- cp->inter=inter;
- }
- void cache_page_insert(cache_page_t* parent, cache_page_t* new)
- {
- cache_page_t *tmp = parent->next;
- parent->next=new;
- new->next=tmp;
- new->prev=parent;
- if(tmp) tmp->prev=new;
- }
- cache_page_t* cache_page_remove(cache_page_t* tr)
- {
- if(tr->next)
- tr->next->prev=tr->prev;
- if(tr->prev)
- tr->prev->next=tr->next;
-
-
- tr->next=NULL;
- tr->prev=NULL;
- return tr;
- }
- void cache_page_free(cache_page_t* tr)
- {
- free(tr);
- }
- int cache_init(cache_t *cache, int page_size_byte, int page_count)
- {
- cache_page_t *curr=NULL;
- int i;
- if(!cache) return -1;
- cache->page_count=page_count;
- cache->page_size=(1+page_size_byte)/sizeof(db_data_t*);
- cache->cache=(db_data_t*) malloc((page_count)*page_size_byte);
- if(!cache->cache)
- {
- fprintf(stderr, "Unable to allocate %d bytes for cache (cache_init())",
- (page_count)*page_size_byte);
- perror(" ");
- return -1;
- }
-
- cache->head=cache_page_new(cache, NULL);
- for(i=0, curr=cache->head; i<page_count; i++, curr=curr->next)
- {
- cache_page_t *cp = cache_page_new(cache, &cache->cache[i]);
- cache_page_insert(curr, cp);
- }
-
- return 0;
- }
- void cache_free(cache_t *cache)
- {
- cache_page_t* curr = cache->head;
- free(cache->cache);
- while(curr)
- {
- cache_page_t * c = curr;
- curr=curr->next;
- cache_page_free(c);
- }
- }
- void cache_get(cache_t* cache, db_time_t start, db_time_t end)
- {
-
- }
- void printcache(cache_page_t* cache)
- {
- cache_page_t *cp = cache->next;
- while(cp)
- {
- printf("%d ", cp->data);
- cp=cp->next;
- }
- printf("\n");
- }
|