#pragma once #include "Common.h" #include "ThreadCache.h" #include "PageCache.h" #include "ObjectPool.h" static void* ConcurrentAlloc(size_t size) { if (size > MAX_BYTES) { size_t alignSize = SizeClass::RoundUp(size); size_t kpage = alignSize >> PAGE_SHIFT; PageCache::GetInstance()->_pageMtx.lock(); Span* span = PageCache::GetInstance()->NewSpan(kpage); span->_objSize = size; PageCache::GetInstance()->_pageMtx.unlock(); void* ptr = (void*)(span->_pageId << PAGE_SHIFT); return ptr; } else { // 通过TLS 每个线程无锁的获取自己的专属的ThreadCache对象 if (pTLSThreadCache == nullptr) { static ObjectPool<ThreadCache> tcPool; //pTLSThreadCache = new ThreadCache; pTLSThreadCache = tcPool.New(); } //cout << std::this_thread::get_id() << ":" << pTLSThreadCache << endl; return pTLSThreadCache->Allocate(size); } } static void ConcurrentFree(void* ptr) { Span* span = PageCache::GetInstance()->MapObjectToSpan(ptr); size_t size = span->_objSize; if (size > MAX_BYTES) { PageCache::GetInstance()->_pageMtx.lock(); PageCache::GetInstance()->ReleaseSpanToPageCache(span); PageCache::GetInstance()->_pageMtx.unlock(); } else { assert(pTLSThreadCache); pTLSThreadCache->Deallocate(ptr, size); } }
时间: 2024-04-12 10:34:23 浏览: 211
这段代码实现了一个名为`ConcurrentAlloc`的静态函数,用于在多线程环境下分配内存。
函数首先检查请求的大小`size`是否超过了最大字节数`MAX_BYTES`。如果超过了最大字节数,它会按照一定的对齐方式计算出相应的页数,并从`PageCache`获取一个`Span`对象来分配内存。然后,它将计算得到的页地址转换为指针,并返回该指针作为分配的内存。
如果请求的大小没有超过最大字节数,它将通过线程本地存储(TLS)获取每个线程专属的`ThreadCache`对象。如果此对象为空,则使用对象池`tcPool`来分配一个新的`ThreadCache`对象,并将其赋值给`pTLSThreadCache`。然后,它调用`pTLSThreadCache->Allocate(size)`来从线程专属的缓存中分配内存,并返回分配的内存指针。
另外,代码还提供了一个名为`ConcurrentFree`的静态函数,用于在多线程环境下释放通过`ConcurrentAlloc`分配的内存。该函数会根据分配内存的大小,将内存释放回相应的内存池或页缓存中。
需要注意的是,代码中使用了一些其他头文件("Common.h"、"ThreadCache.h"、"PageCache.h"、"ObjectPool.h"),以及一些全局变量和对象(`pTLSThreadCache`、`PageCache::GetInstance()`)。这些部分的实现并未在你提供的代码中包含,所以我无法对其进行详细解释。但根据代码的上下文,可以推测它们可能是用于内存管理的相关组件和数据结构。
相关问题
我的头文件是这样写的#include <stdio.h> #include <math.h> #include <fstream> #include <iostream> #include <string>//getline #include <sstream> #include "stb_image.h" #pragma comment(lib, "openGL32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glaux.lib") #include <gl/glaux.h> #include "glew.h" #include "glut.h" #include <vector>,请问如何避免报错gl.h included before glew.h呢
在你的头文件中,应该将 `glew.h` 的引入放在 `gl.h` 的引入之前。因此,你应该将 `glew.h` 的引入语句移到 `gl.h` 的引入语句之前,就像这样:
```
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <string>//getline
#include <sstream>
#include "stb_image.h"
#include <gl/glaux.h>
#include <vector>
#include "glew.h"
#include <gl/glut.h>
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
```
这样就能够避免 `gl.h included before glew.h` 的错误了。
画出该程序的流程图#include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" #include"print.h" using namespace std; Kid* pfirst = 0; Kid* pcurrent = 0; Kid* pivot = 0; void main() { int numberofboys, m; cout << "请输入参与约瑟夫的小孩人数"; cin >> numberofboys; cout << endl; cout << "请输出约瑟夫问题的间隔数:"; cin >> m; cout << endl; pfirst = new Kid; pfirst->code = 1; pfirst->pnext = pfirst; pcurrent = pfirst; for (int i = 1; i < numberofboys; i++) { pivot = pcurrent; pcurrent = new Kid; pcurrent->code = i + 1; cout << "请输入第" << i << "个小孩名字"; cin >> pcurrent->name; pivot->pnext = pcurrent; } pcurrent->pnext = pfirst; cout << setw(4) << pfirst->code;//遍历输出 pcurrent = pfirst->pnext; while (pcurrent != pfirst) { cout << setw(4) << pcurrent->code; pcurrent = pcurrent->pnext; } cout << endl; countchildren(pfirst, m); printWinner(pcurrent); } //countchildren.h void countchildren(Kid* pfirst, int m); //countchildren.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"leave.h" #include"print.h" using namespace std; void countchildren(Kid* pfirst, int m) { Kid* pcurrent = 0; Kid* pivot = 0; pcurrent = pfirst; int j; while (pcurrent->pnext != pcurrent) { j = m; do { pivot = pcurrent; pcurrent = pcurrent->pnext; j--; } while (j > 1); printLoser(pcurrent); leave(pivot, pcurrent); } } //print.h #pragma once #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void printWinner(Kid* winner); void printLoser(Kid* loser); //print.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; #include "print.h" void printWinner(Kid* winner) { cout << "最后胜利的人:" << endl; cout << "其编号:" << setw(4) << winner->code << endl; cout << "名字:" << winner->name << endl; } void printLoser(Kid* loser) { cout << "这轮失败的人:" << endl; cout << "其编号:" << setw(4) << loser->code << endl; cout << "名字:" << loser->name << endl; } //Leave.h #pragma once #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void leave(Kid* pivot, Kid* pcurrent ); //leave.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void leave(Kid* pivot, Kid* pcurrent) { pivot->pnext = pcurrent->pnext; delete pcurrent; pcurrent = pivot->pnext; }
抱歉,由于我是一名语言模型,无法绘制流程图。不过,我可以为您解释程序的大致流程:
1. 定义 Kid 类型的指针变量 pfirst、pcurrent、pivot 和 winner,分别表示第一个孩子、当前孩子、前一个孩子和胜利者。
2. 通过输入获取参与游戏的小孩人数和问题的间隔数。
3. 创建 Kid 类型的对象,存储每个孩子的编号和名字,将它们连接成一个环形链表,并输出链表中每个孩子的编号。
4. 调用 countchildren 函数,对孩子进行计数,每次计数到第 m 个孩子就将其删除,直到只剩下一个孩子为止。
5. 输出最后胜利的孩子的编号和名字。
其中,countchildren 函数调用了 leave 函数和 printLoser 函数,leave 函数用于删除孩子,printLoser 函数用于输出每轮被删除的孩子的编号和名字。程序还包括 printWinner 函数,用于输出最后胜利的孩子的编号和名字。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c++; charset=iso-8859-1](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)