log管理器内存分配池化,修复部分log写入部分与内存池部分存在的恶性bug
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
#include "memctl.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
((type *)((char *)(ptr) - offsetof(type, member)))
|
||||
|
||||
int extend_pool(mem_ctl* self,int size)
|
||||
{
|
||||
@ -20,7 +24,7 @@ int extend_pool(mem_ctl* self,int size)
|
||||
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_store(&self->blocks[i].condition,MEM_FREE);
|
||||
}
|
||||
atomic_fetch_add(&self->poolsize,size);
|
||||
int log = atomic_load(&self->logbuf)+size/2;
|
||||
@ -28,33 +32,41 @@ int extend_pool(mem_ctl* self,int size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
mem_block *GetBlock(mem_ctl* self,int type)
|
||||
void **GetBlock(mem_ctl* self,int type)
|
||||
{
|
||||
int status = 0;
|
||||
int sp=0,start = 0;
|
||||
atomic_int *id;
|
||||
int id;
|
||||
//模式判断
|
||||
if(type == LOGMOD)
|
||||
{
|
||||
id = &self->Loglast_loc;
|
||||
sp = self->poolsize;
|
||||
start = self->logbuf;
|
||||
id = atomic_load(&self->Loglast_loc);
|
||||
sp = atomic_load(&self->poolsize);
|
||||
start = atomic_load(&self->logbuf);
|
||||
if(id<start)
|
||||
id = start;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = &self->Commenlast_loc;
|
||||
sp = self->logbuf;
|
||||
id = atomic_load(&self->Commenlast_loc);
|
||||
sp = atomic_load(&self->logbuf)+1;
|
||||
if(id>sp)
|
||||
id = start;
|
||||
}
|
||||
int i = atomic_load(id);
|
||||
int i = id;
|
||||
for(status;status<CYCLE_NUM;status++)//最多扫描轮次
|
||||
{
|
||||
//获取空闲块
|
||||
for(i;i<sp;i++)
|
||||
{
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == FREE)
|
||||
if(atomic_fetch_sub(&self->blocks[i].condition,1) == MEM_FREE)
|
||||
{
|
||||
atomic_fetch_sub(&self->blocks[i].condition,1);
|
||||
return &self->blocks[i];
|
||||
if(i>self->logbuf)
|
||||
atomic_store(&self->Loglast_loc,i);
|
||||
else
|
||||
atomic_store(&self->Commenlast_loc,i);
|
||||
return &self->blocks[i].location;
|
||||
}
|
||||
else{
|
||||
atomic_fetch_add(&self->blocks[i].condition,1);//回滚路径
|
||||
@ -74,8 +86,10 @@ mem_block *GetBlock(mem_ctl* self,int type)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int FreeBlock(mem_ctl* self,mem_block *block)
|
||||
int FreeBlock(mem_ctl* self,void** block_p)
|
||||
{
|
||||
mem_block *block;
|
||||
block = container_of(block_p,mem_block,location);
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
if(block == NULL)
|
||||
@ -92,7 +106,7 @@ int FreeBlock(mem_ctl* self,mem_block *block)
|
||||
free(block->location);
|
||||
block->location = NULL;
|
||||
int poolsize = atomic_load(&self->poolsize);
|
||||
if(&self->blocks[poolsize] == block)
|
||||
if(&self->blocks[poolsize-1] == block)
|
||||
{
|
||||
for(int i = poolsize -1;i>COMINE_MEM_SIZE;i--)
|
||||
{
|
||||
@ -136,9 +150,10 @@ int free_memctl(mem_ctl *self)//清除内存池
|
||||
{
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
for(int i = 0;i++;i<MAX_MEM_SIZE)
|
||||
for(int i = 0;i<MAX_MEM_SIZE;i++)
|
||||
{
|
||||
if(self->blocks[i].location!=NULL)
|
||||
free(self->blocks[i].location);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
#include "config.h"
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define FREE 1
|
||||
#define MEM_FREE 1
|
||||
#define INUSE -1
|
||||
#define PROCESSING 0
|
||||
|
||||
@ -25,9 +25,9 @@ typedef struct mem_ctl
|
||||
atomic_int Loglast_loc;
|
||||
atomic_int mem_e_indicator;//内存不足指示器
|
||||
//获取一个内存块
|
||||
mem_block* (*GetBlock)(struct mem_ctl*,int);
|
||||
void** (*GetBlock)(struct mem_ctl*,int);
|
||||
//释放一个内存块
|
||||
int (*FreeBlock)(struct memctl*,mem_block*);
|
||||
int (*FreeBlock)(struct mem_ctl*,void**);
|
||||
}mem_ctl;
|
||||
|
||||
int init_memctl(mem_ctl *self);
|
||||
|
||||
Reference in New Issue
Block a user