cache_double_list

来源:互联网 发布:类似知北游走小说 编辑:程序博客网 时间:2024/04/27 22:10
#ifndef _DOUBLE_LIST_CACHE_H_#define _DOUBLE_LIST_CACHE_H_#define NODE_SIZE 128typedef struct _cnode {int m_data[NODE_SIZE];int m_start;int m_finish;int m_index;struct _cnode* m_prev;struct _cnode* m_next;} cnode_t;void cache_creater (cnode_t** head, int cachesize);void cache_acquire (cnode_t** head, cnode_t** node);void cache_release (cnode_t** head, cnode_t* node);void cache_destroy (cnode_t** head);#endif


#include "doublelistcache.h"#include <malloc.h>#include <string.h>#include <stdio.h>void cache_creater (cnode_t** head, int cachesize) {int nodeNum, cusor;cnode_t *node, **cusornode;cusornode = &node;nodeNum = cachesize / NODE_SIZE;for (cusor = 0; cusor < nodeNum; ++cusor) {*cusornode = (cnode_t*)malloc(sizeof(cnode_t));memset ((*cusornode)->m_data, 0, NODE_SIZE);(*cusornode)->m_start = 0;(*cusornode)->m_finish = 0;(*cusornode)->m_next = 0;(*cusornode)->m_prev = 0;(*cusornode)->m_index = cusor;cusornode = &(*cusornode)->m_next;}*head = node;}void cache_acquire (cnode_t** head, cnode_t** node) {if (!(*head)) {*node = 0;}*node = *head;*head = (*head)->m_next;}void cache_release (cnode_t** head, cnode_t* node) {if (node) {node->m_next = *head;(*head)->m_prev = node;*head = node;}}void cache_destroy (cnode_t** head) {cnode_t *cusor, *tmp;cusor = *head;while (cusor) {tmp = cusor;cusor = cusor->m_next;free (tmp);}*head = 0;}void cache_display (cnode_t* head) {while (head) {printf ("index = %d\n",head->m_index);head = head->m_next;}}int main () {cnode_t* head;cnode_t* result;cache_creater (&head, 1024);cache_display (head);cache_acquire (&head, &result);printf ("-------------------\n");cache_display (head);printf ("%d\n", result->m_index);printf ("-------------------\n");cache_release (&head, result);cache_display (head);return 0;}


原创粉丝点击