當前位置:首頁 » 礦機知識 » mempool礦池怎麼用

mempool礦池怎麼用

發布時間: 2021-06-03 09:56:00

1. 用C編一個內存託管函數庫...

使用方法:
__________________________________________
#include "mempool.h"
#include <stdio.h>
int main() {
MemPool* pool = open_pool();//這個鏈表儲存了所有指針信息。
char* str =NEW_STR(pool, 1024);//申請內存空間,並在pool中記錄。
scanf("%s", str);printf("%s\n",str);
close_pool(pool);//釋放所有申請的空間
return 0;}
__________________________________________
#include <stdlib.h>
struct _MEM_TABLE_ {
void * pointer;
struct _MEM_TABLE_* next;
};
typedef struct _MEM_TABLE_ MemPool;
typedef void (*FUNC_POOL_EACH)(MemPool*);
MemPool* open_pool(void);
void close_pool(MemPool*);
MemPool* __pool_bottom(const MemPool*);
void __pool_for_each(MemPool*, FUNC_POOL_EACH);
void __pool_alloc_memset(void*,size_t);
void* alloc_pool(MemPool*, size_t);
void delloc_pool(MemPool*, void*);
#define NEW_STR(pPool,len) ((char*)alloc_pool((pPool),(len)))
#define NEW_INT(pPool) ((int*)alloc_pool((pPool),sizeof(int)))
#define NEW_DOUBLE(pPool) ((double*)alloc_pool((pPool),sizeof(double)))
#define NEW_FLOAT(pPool) ((float*)alloc_pool((pPool),sizeof(float)))
#define NEW_TYPE(pPool, tType) ((tType*)alloc_pool((pPool),sizeof(tType)))
#define DELETE(pPool,p) (delloc_pool((pPool),(p)))
MemPool* open_pool(void) {
MemPool* p = malloc(sizeof(MemPool));
if (p != NULL) {
p->pointer = NULL;
p->next = NULL;
}
return p;
}
void close_pool(MemPool* p) {
MemPool* now = p, *tmp;
while (now != NULL) {
tmp = now;
now = now->next;
if (tmp->pointer != NULL) {
free(tmp->pointer);
}
free(tmp);
}
}
void __pool_alloc_memset(void* p, size_t len) {
size_t i;
for (i = 0; i < len; i++) {
*((char*)(p+i)) = '\0';
}
}
MemPool* __pool_bottom(const MemPool* p) {
MemPool* tmp = (MemPool*)p;
if (p == NULL) return NULL;
while (tmp != NULL) {
if (tmp->next == NULL) {
break;
} else {
tmp = tmp->next;
}
}
return tmp;
}
void __pool_for_each(MemPool* pool, FUNC_POOL_EACH func) {
MemPool *now = pool, *tmp;
while(now != NULL) {
tmp = now;
now = now->next;
(*func)(tmp);
}
}
void* alloc_pool(MemPool* p, size_t l) {
MemPool *tmp = __pool_bottom(p), *pnew;
if (tmp == NULL) return NULL;
pnew = malloc(sizeof(MemPool));
if (pnew == NULL) return NULL;
pnew->next = NULL;
pnew->pointer = malloc(l);
__pool_alloc_memset(pnew->pointer, l);
tmp->next = pnew;
return pnew->pointer;
}
void delloc_pool(MemPool* pool, void * p) {
MemPool *now = pool, *parent;
if (p == NULL) {
return;
}
for(parent = now; now != NULL; parent=now, now = now->next) {
if (p == now->pointer) {
parent->next = now->next;
free(now->pointer);
free(now);
}
}
}

熱點內容
比特幣bay 發布:2025-08-23 13:47:26 瀏覽:141
以太坊轉賬幾次確認 發布:2025-08-23 13:38:06 瀏覽:794
比特幣價格是美元還是 發布:2025-08-23 13:21:08 瀏覽:877
以太坊YOH智能合約能關閉嗎 發布:2025-08-23 13:19:24 瀏覽:538
文章轉成區塊鏈保存 發布:2025-08-23 13:17:50 瀏覽:815
414xrp瑞波幣最新消息 發布:2025-08-23 13:07:49 瀏覽:931
數字貨幣錢包能否套利 發布:2025-08-23 13:02:32 瀏覽:211
雲南省區塊鏈解決了什麼問題 發布:2025-08-23 13:01:40 瀏覽:864
如何將usdt兌換成usd 發布:2025-08-23 12:57:36 瀏覽:863
數字貨幣usdt一般提現要多久 發布:2025-08-23 12:56:21 瀏覽:965