高并发内存池:线程缓存测试

哈希函数头文件

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
static const size_t Maxbyte = 256 * 1024;
static const size_t Hashnum = 208;
class Sizeclass
{
public:
	inline static size_t _Roundup(size_t bytes, size_t hashsize)
	{
		size_t factsize = 0;
		if (bytes % hashsize == 0)
		{
			factsize = bytes;
		}
		else
		{
			factsize = ((bytes / hashsize) + 1) * hashsize;
		}
		return factsize;
	}
	inline static size_t Roundup(size_t bytes)
	{
        if (bytes <= 128) {
            return _Roundup(bytes, 8);
        }
        else if (bytes <= 1024) {
            return _Roundup(bytes, 16);
        }
        else if (bytes <= 8 * 1024) {
            return _Roundup(bytes, 128);
        }
        else if (bytes <= 64 * 1024) {
            return _Roundup(bytes, 1024);
        }
        else if (bytes <= 256 * 1024) {
            return _Roundup(bytes, 8 * 1024);
        }
        else {
            assert(false);
            return -1;
        }
	}
    inline static size_t _Index(size_t bytes, size_t hashsize)
    {
        size_t index = 0;
        if (bytes % hashsize != 0) {
            index = bytes / hashsize;
        }
        else {
            index = bytes / hashsize - 1;
        }
        return index;
    }
    inline static size_t Index(size_t bytes)
    {
        static size_t groupArray[4] = { 16, 56, 56, 56 };
        if (bytes <= 128)
        {
            return _Index(bytes, 3);
        }
        else if (bytes <= 1024)
        {
            return _Index(bytes - 128, 4) + groupArray[0];
        }
        else if (bytes <= 8 * 1024)
        {
            return _Index(bytes - 1024, 7) + groupArray[0] + groupArray[1];
        }
        else if (bytes <= 64 * 1024)
        {
            return _Index(bytes - 8 * 1024, 10) + groupArray[0] + groupArray[1] + groupArray[2];
        }
        else if (bytes <= 256 * 1024)
        {
            return _Index(bytes - 64 * 1024, 13) + groupArray[0] + groupArray[1] + groupArray[2] + groupArray[3];
        }
        else
        {
            assert(false);
            return -1;
        }

    }
    inline static size_t NumMoveSize(size_t size) {
        assert(size > 0);
        int num = Maxbyte / size;
        if (num < 2) {
            num = 2;
        }
        if (num > 512) {
            num = 512;
        }
        return num;
    }
};

自由链表头文件

#pragma once
#include<iostream>
#include<assert.h>
#include"哈希函数.hpp"
using namespace std;
void* NextObj(void* obj) {
    size_t alignSize = Sizeclass::Roundup(sizeof(obj));
    return reinterpret_cast<char*>(obj) + alignSize;
}
class FreeList
{
private:
	void* _freeList = nullptr;
	size_t _maxSize;
public:
    void Push(void* obj) {
        assert(obj);
        void* a = NextObj(obj);
        a = _freeList;
        _freeList = obj;
    }
    void* Pop() {
        assert(_freeList);
        void* obj = _freeList;
        _freeList = NextObj(_freeList);
        return obj;
    }
    bool Empty() {
        return _freeList == nullptr;
    }
    size_t MaxSize() {
        return _maxSize;
    }
};

线程缓存头文件

#pragma once
#include<iostream>
#include<assert.h>
#include"哈希函数.hpp"
#include"自由链表.hpp"
using namespace std;
class Threadcache
{
private:
	FreeList freelists[Hashnum];
public:
	void *Allocate(size_t size)
	{
		assert(size <= Maxbyte);
		size_t factsize =  Sizeclass::Roundup(size);
		size_t index = Sizeclass::Index(size);
		if (!freelists[index].Empty())
		{
			return freelists[index].Pop();
		}
		else
		{
			
			return nullptr;
		}
	}
	void addbyhead(size_t index, void* obj)
	{
		freelists[index].Push(obj);
	}
};
static _declspec(thread) Threadcache* pTLSThreadCache = nullptr;
Threadcache* getthread()
{
	if (pTLSThreadCache == nullptr)
	{
		pTLSThreadCache = new Threadcache;
	}
	return pTLSThreadCache;
}
void* reqirementapply(size_t size)
{
	Threadcache* newone = getthread();
	return newone->Allocate(size);
}

测试代码

#include"自由链表.hpp"
#include"哈希函数.hpp"
#include"线程缓存.hpp"
using namespace std;
int main()
{
    void* ptr = reqirementapply(50);
    if (ptr == nullptr) {
        std::cout << "第一次调用,无法分配内存." << std::endl;
    }

    Threadcache* cache = getthread();
    size_t index = Sizeclass::Index(50);
    void* newMemory = new char[Sizeclass::Roundup(50)];
    cache->addbyhead(index, newMemory);

    ptr = reqirementapply(50);
    if (ptr == nullptr) {
        std::cout << "第二次调用,无法分配内存." << std::endl;
    }
    else {
        std::cout << "第二次调用,分配内存成功." << std::endl;
    }

    return 0;
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值