Net/memory.h
Saturneic 3138d0f68a 调整结构
调整了CPart 的结构与计算模块获取参数的方式,使之更加合理。为以后的对更加复杂的传参要求的支持奠定了基础。
2019-01-18 22:57:54 +08:00

103 lines
2.4 KiB
C++

//
// memory.h
// Net
//
// Created by 胡一兵 on 2019/1/18.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#ifndef memory_h
#define memory_h
#include "type.h"
struct block_info{
uint32_t size = 0;
int lock = 0;
bool pted = false;
void *pvle = nullptr;
block_info(uint32_t size,void *pvle){
this->size = size;
this->pvle = pvle;
}
block_info(){
}
};
class BlocksPool{
// 内存块表
map<void *, block_info> blocks_list;
public:
// 声明某内存块
void *b_malloc(uint32_t size){
void *ptr = malloc(size);
if(ptr == nullptr) return nullptr;
block_info bifo;
bifo.size = size;
bifo.lock = 1;
bifo.pted = false;
bifo.pvle = ptr;
blocks_list.insert(pair<void *, block_info>(ptr,bifo));
return ptr;
}
template<class T>
void *bv_malloc(T value){
T *pvalue = (T *)b_malloc(sizeof(T));
*pvalue = T(value);
return pvalue;
}
void *bp_malloc(uint32_t size, void *ptvle){
void *pvalue = b_malloc(size);
memcpy(pvalue, ptvle, size);
return pvalue;
}
uint32_t size(void *ptr){
return blocks_list.find(ptr)->second.size;
}
// 标记使用某内存块
void *b_get(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.lock++;
return ptr;
}
else return nullptr;
}
// 标记保护某内存块
void b_protect(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.pted = true;
}
else throw "protect nil value";
}
// 标记不再保护某内存块
void b_noprotect(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.pted = false;
}
else throw "noprotect nil value";
}
// 标记不再使用某内存块
void b_free(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end() && blk->second.pted == false){
if(blk->second.lock - 1 == 0){
free(blk->first);
blocks_list.erase(blk);
}
else blk->second.lock--;
}
}
};
extern BlocksPool main_pool;
#endif /* memory_h */