Nginx反向代理服务器高速缓存数据结构的部分实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_44065088/article/details/102585615
#ifndef _NGINX_H_
#define _NGINX_H_


#include <iostream>

#define NGX_QUEUE_T typedef
#define NGX_T        typedef
#define LIST_T        typedef
typedef int            NGX_ELEM_TYPE;

//双向循环链表
LIST_T struct _List_t {
    _List_t *prev;
    _List_t *next;
}List_t, *List_t_p;

//  在ngx中   是对头  出队   队尾入队
NGX_QUEUE_T struct _Ngx_queue_t {

    NGX_ELEM_TYPE lenth;
    
    _List_t   *rear;

    _List_t   *front;

}*Ngx_queue_p,Ngx_queue_node;

NGX_T struct _Ngx_t {
    //编号
    NGX_ELEM_TYPE fd;
    //调用次数  //用顺序表进行记录
    NGX_ELEM_TYPE num;
    //类似于linux 内核的共享双向链表
    _List_t queue_node;

}Ngx_node, *Ngx_node_p;
    
//宏函数定义区 对双向循环链表进行初始化等操作
//链表的初始化
#define Ngx_queue_init(L)                        \
    (L)->prev = L;                                \
    (L)->next = L    

#define Is_Ngx_queue_empty(L)                    \
    (L == (L)->prev)    //因为从队首入队

//link是原来的队列 , node是要插入的结点队列结点
#define Ngx_queue_insert_head(link, node)        \
    (node)->prev = (link)->prev;                \
    (node)->next = (link);                        \
    (link)->prev = node;                        \
    (link) = (link)->prev

//习惯用户的理解
#define Ngx_queue_insert_tail Ngx_queue_insert_head 

//_Ngx_t减去偏移量的  队列结点  地址
#define toNgx_queue_p(Type, queue_node, dance)    \
    (Type *)((size_t)queue_node - dance)

//1.队列的初始化
bool initQueue(_Ngx_queue_t  *LQ);

//2.队列的入队
bool EnQueue(_Ngx_queue_t* LQ, _List_t *node);

//3.队列的出队
bool PopQueue(_Ngx_queue_t* LQ, int& fd);

//4.队列的遍历
void travel_queue(_Ngx_queue_t* LQ);
    


#endif
#include "NGINX.h"

using namespace std;

bool initQueue(_Ngx_queue_t* LQ) {
    
    if (!LQ) {
        return false;
    }
    _List_t* L = new _List_t ;//初始化一个头结点

    if(!L)return false;

    LQ->lenth = 0;
    LQ->front = LQ->rear = L;
    
    /*
    LQ->lenth = 0;                //易错点   开出来的空间  地址是随机的不可用  没有生孩子 就开始起名字  
    LQ->front = LQ->rear  ;        //初始化还是可以的  
    */  
    
    Ngx_queue_init(LQ->rear);

    return true;
}

bool EnQueue(_Ngx_queue_t* LQ, _List_t* node) {
    
    if (!LQ || !node) {
        return false;
    }

    Ngx_queue_insert_tail(LQ->front, node);

    LQ->lenth++;
    return true;
}

bool PopQueue(_Ngx_queue_t* LQ, int& fd) {
    

    return true;
}

void travel_queue(_Ngx_queue_t* LQ) {
    
    auto tmp = LQ->rear->prev;
    int i = 1;
    auto dance = offsetof(Ngx_node, queue_node);
    while (tmp != LQ->rear) {
        printf_s("第%d个元素的fd: %d\n", i, (toNgx_queue_p(Ngx_node, tmp, dance))->fd );
        tmp = tmp->prev;
        i++;
    }

    return;
}

#include "NGINX.h"

#include <string>
#include <Windows.h>

using namespace std;


int main(void) {

    //初始化队列
    _Ngx_queue_t * LQ = new _Ngx_queue_t;
    if ( initQueue(LQ) ) {
        cout << "init  success!" << endl;
    }else {
        cout << "init  falure!" << endl;
        exit(-1);
    }

    //入队
    Ngx_node_p insert = NULL;
    for (int i = 0; i < 5;i++) {
        insert = new Ngx_node;
        insert->fd = i+1;
        insert->num = 1;
        if (EnQueue(LQ, &(insert->queue_node))) {
            printf("第 %d 个fd:%d\n", i+1, insert->fd);
            cout << "入队成功!" << endl;
        }else {
            cout << "入队失败!" << endl;
            delete insert;
        }
    }

    cout << LQ->lenth << endl;

    travel_queue(LQ);
    cout << endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44065088/article/details/102585615
今日推荐