哈希函数头文件
#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;
}