修复部分bug,添加内存池,为后续内存池化分配打基础
This commit is contained in:
@ -17,7 +17,8 @@ add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstal
|
||||
add_library(Log SHARED tools/log/log.c)
|
||||
add_library(Toml SHARED tools/toml/toml.c)
|
||||
add_library(Quit SHARED tools/quit/quit.c)
|
||||
add_library(Memctl SHARED memctl/memctl.c)
|
||||
|
||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit)
|
||||
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit Memctl)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR})
|
||||
20
c/config.h
20
c/config.h
@ -1,18 +1,36 @@
|
||||
#ifndef SEVERCONFG
|
||||
#define SEVERCONFG
|
||||
|
||||
/*------日志管理---------*/
|
||||
#define MAX_LOG 64
|
||||
#define MAX_LOG_LENGTH 4096
|
||||
#define MAX_LOG_LENGTH 4080
|
||||
#define INFO_LENGTH 8
|
||||
/*------日志管理---------*/
|
||||
|
||||
/*-------终端管理-------*/
|
||||
#define TEM_MAX_BUF 256
|
||||
#define TEM_HISTORY_BUF 210
|
||||
#define TEM_PROMPT "chatbot$$ "
|
||||
/*----终端管理--------*/
|
||||
|
||||
/*-------网路池管理-----*/
|
||||
#define NET_MAX_POOL 1
|
||||
#define NET_MAX_MESSAGE_BUF 1024
|
||||
#define HTTP_BLOCK_SIZE 512
|
||||
#define MAX_HTTP_LENGTH 20
|
||||
/*-------网路池管理-----*/
|
||||
|
||||
/*------解释器管理-------*/
|
||||
#define INTER_MAX_BUF 256
|
||||
/*------解释器管理-------*/
|
||||
|
||||
/*------内存池管理------*/
|
||||
#define MAX_MEM_SIZE 256
|
||||
#define COMINE_MEM_SIZE 192
|
||||
#define MEM_BLOCK_SIZE 4096
|
||||
#define POOL_EXPEND_ID 3
|
||||
#define POOL_EXPEND_SIZE 4
|
||||
#define CYCLE_NUM 7
|
||||
/*------内存池管理------*/
|
||||
|
||||
#endif
|
||||
144
c/memctl/memctl.c
Normal file
144
c/memctl/memctl.c
Normal file
@ -0,0 +1,144 @@
|
||||
#include "memctl.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int extend_pool(mem_ctl* self,int size)
|
||||
{
|
||||
if(self == NULL)//入参检查
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
int target = poolsize +size;//计算目标大小
|
||||
if(target >MAX_MEM_SIZE)
|
||||
target = MAX_MEM_SIZE;//计算限制
|
||||
|
||||
for(int i = poolsize;i<target;i++)//分配内存
|
||||
{
|
||||
if(self->blocks[i].location !=NULL)//若存在未释放内存
|
||||
free(self->blocks[i].location);
|
||||
self->blocks[i].location = malloc(MEM_BLOCK_SIZE);
|
||||
if(self->blocks[i].location == NULL)
|
||||
return target-i;//堆内存不足
|
||||
atomic_store(&self->blocks[i].condition,FREE);
|
||||
}
|
||||
atomic_fetch_add(&self->poolsize,size);
|
||||
int log = atomic_load(&self->logbuf)+size/2;
|
||||
atomic_store(&self->logbuf,log);//重新划定日志区
|
||||
return 0;
|
||||
}
|
||||
|
||||
mem_block *GetBlock(mem_ctl* self,int type)
|
||||
{
|
||||
int status = 0;
|
||||
int sp=0,start = 0;
|
||||
atomic_int *id;
|
||||
//模式判断
|
||||
if(type == LOGMOD)
|
||||
{
|
||||
id = &self->Loglast_loc;
|
||||
sp = self->poolsize;
|
||||
start = self->logbuf;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = &self->Commenlast_loc;
|
||||
sp = self->logbuf;
|
||||
}
|
||||
int i = atomic_load(id);
|
||||
for(status;status<CYCLE_NUM;status++)//最多扫描轮次
|
||||
{
|
||||
//获取空闲块
|
||||
for(i;i<sp;i++)
|
||||
{
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == FREE)
|
||||
{
|
||||
atomic_fetch_sub(&self->blocks[i].condition,1);
|
||||
return &self->blocks[i];
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->blocks[i].condition,1);//回滚路径
|
||||
}
|
||||
}
|
||||
i = start;
|
||||
atomic_fetch_add(&self->mem_e_indicator,1);
|
||||
//检查是否需要扩池
|
||||
if(atomic_fetch_sub(&self->mem_e_indicator,POOL_EXPEND_ID)>POOL_EXPEND_ID)
|
||||
{
|
||||
extend_pool(self,POOL_EXPEND_SIZE);
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->mem_e_indicator,POOL_EXPEND_ID);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int FreeBlock(mem_ctl* self,mem_block *block)
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
if(block == NULL)
|
||||
return -1;
|
||||
if(block < &self->blocks[COMINE_MEM_SIZE])//标准池部分归还
|
||||
{
|
||||
atomic_fetch_add(&block->condition,2);
|
||||
block = NULL;
|
||||
return 0;
|
||||
}
|
||||
else//扩容池尝试回收
|
||||
{
|
||||
atomic_fetch_add(&block->condition,1);
|
||||
free(block->location);
|
||||
block->location = NULL;
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
if(&self->blocks[poolsize] == block)
|
||||
{
|
||||
for(int i = poolsize -1;i>COMINE_MEM_SIZE;i--)
|
||||
{
|
||||
if(atomic_load(&self->blocks[i].condition) == PROCESSING)
|
||||
{
|
||||
poolsize--;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
if(poolsize-COMINE_MEM_SIZE%2 == 0)
|
||||
{
|
||||
atomic_fetch_sub(&self->logbuf,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
atomic_store(&self->poolsize,poolsize);
|
||||
}
|
||||
}
|
||||
|
||||
int init_memctl(mem_ctl *self)
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
self->poolsize = 0;
|
||||
for(int i =0;i<MAX_MEM_SIZE;i++)
|
||||
{
|
||||
self->blocks[i].location = NULL;
|
||||
atomic_init(&self->blocks[i].condition,PROCESSING);//初始化池
|
||||
}
|
||||
extend_pool(self,COMINE_MEM_SIZE);//预分配内存
|
||||
self->Commenlast_loc = 0;
|
||||
self->logbuf = COMINE_MEM_SIZE/2;
|
||||
self->mem_e_indicator = 0;
|
||||
self->FreeBlock = FreeBlock;
|
||||
self->GetBlock = GetBlock;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int free_memctl(mem_ctl *self)//清除内存池
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
for(int i = 0;i++;i<MAX_MEM_SIZE)
|
||||
{
|
||||
if(self->blocks[i].location!=NULL)
|
||||
free(self->blocks[i].location);
|
||||
}
|
||||
}
|
||||
36
c/memctl/memctl.h
Normal file
36
c/memctl/memctl.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef MEMCTL
|
||||
#define MEMCTL
|
||||
#include "config.h"
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define FREE 1
|
||||
#define INUSE -1
|
||||
#define PROCESSING 0
|
||||
|
||||
#define LOGMOD 0
|
||||
#define COMMENMOD 1
|
||||
|
||||
typedef struct mem_block
|
||||
{
|
||||
void *location;//块地址
|
||||
atomic_int condition;//块状态
|
||||
}mem_block;
|
||||
|
||||
typedef struct mem_ctl
|
||||
{
|
||||
mem_block blocks[MAX_MEM_SIZE];
|
||||
atomic_int logbuf;
|
||||
atomic_int poolsize;
|
||||
atomic_int Commenlast_loc;//分配起始
|
||||
atomic_int Loglast_loc;
|
||||
atomic_int mem_e_indicator;//内存不足指示器
|
||||
//获取一个内存块
|
||||
mem_block* (*GetBlock)(struct mem_ctl*,int);
|
||||
//释放一个内存块
|
||||
int (*FreeBlock)(struct memctl*,mem_block*);
|
||||
}mem_ctl;
|
||||
|
||||
int init_memctl(mem_ctl *self);
|
||||
int free_memctl(mem_ctl *self);
|
||||
|
||||
#endif
|
||||
@ -5,11 +5,12 @@
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "errno.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "http_rel.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
int write_in_bk(char input,httpbuf* blk){
|
||||
if(blk->size%HTTP_BLOCK_SIZE == 0)//块满分配新块
|
||||
|
||||
@ -33,6 +33,8 @@ static void safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
/* 主解析 */
|
||||
int rbt_parse_json(const char *json_text, rbt_msg *out)
|
||||
{
|
||||
if(json_text == NULL)
|
||||
return -1;
|
||||
memset(out, 0, sizeof(*out)); // 统一清 0,gid 天然 '\0'
|
||||
|
||||
cJSON *root = cJSON_Parse(json_text);
|
||||
@ -108,7 +110,7 @@ int process_message(char *req, log_manager *logger,rbt_msg *swap) {
|
||||
make_swap(swap);
|
||||
logger->in_log(log, logger);
|
||||
}
|
||||
|
||||
//通知前端已收到消息
|
||||
const char *response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
|
||||
17
c/network/protocal.h
Normal file
17
c/network/protocal.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef PROTOCAL
|
||||
#define PROTOCAL
|
||||
|
||||
typedef struct network_pakage
|
||||
{
|
||||
char *data;
|
||||
int buf_block;
|
||||
}network_package;
|
||||
|
||||
typedef struct net_protocal
|
||||
{
|
||||
void *protocal;
|
||||
int (*init)(void *self);
|
||||
int (*rev_message)(void *self,network_package *data);
|
||||
}net_protocal;
|
||||
|
||||
#endif
|
||||
@ -10,6 +10,7 @@ typedef struct logs
|
||||
{
|
||||
char log[MAX_LOG_LENGTH];
|
||||
struct logs *next;
|
||||
char info[INFO_LENGTH];
|
||||
}logs;
|
||||
|
||||
typedef struct log_manager
|
||||
|
||||
Reference in New Issue
Block a user