使用c++模拟增量垃圾收集

incremental garbage collection simulation using c++

本文关键字:c++ 模拟 使用      更新时间:2023-10-16

我有一个项目,需要我模拟增量垃圾收集。该算法采用分代算法结合标记和扫描方法。到目前为止,我已经设计了一个结构,如所示的代码。问题在于为代码分配内存。我现在用的是向量。我还需要使用指针指向内存的开始和内存的结束。我不知道该怎么做。请一定要帮我设计这个。下面是我到目前为止的代码。

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <limits>
#include <stdio.h>
#include <sstream>
#include <vector>

using namespace std;
using std::stringstream;

string pMem, comment, sGen, val,input,id,size,inits,incs;
double pmemSize =0;
char t[10], m[256],init[10],inc[10];

struct rootset {
  double totSize;
  double *rStrtPtr;
  double *rEndPtr;
  vector<double> physicalM; /* This is the size of physical memory i need to assign*/
  struct generations {
    double totSize;
    const char *genStrtPtr;
    const char *genEndPtr; 
    int numOfGen;
    string genName;
    struct object {
      double objSize;
      const char *objStrtPtr;
      const char *objEndPtr;
      string id;
      char markBit;
      char objPtr;
    };
    struct freeList {
      double freeSpace;
      int flNumb; 
    };
  };
};
int main()
{
  int pmemSize;
  cout<<" ENter the size "<<endl;
  cin >> pmemSize;
  vector<rootset> pRootSet;
  pRootSet.push_back(rootset());
  pRootSet[0].totSize = pmemSize;
  pRootSet[0].physicalM.reserve(pmemSize);
   for (int s=0; s<pmemSize; ++s)
      pRootSet[0].physicalM.push_back(s);
  vector<double>::iterator it;
  for(it = pRootSet[0].physicalM.begin(); it!= pRootSet[0].physicalM.end(); ++it) 
      cout <<"Printing it: " <<(*it)<<endl;
}

我现在的问题是如何指向指针*rStrtPtr和*rEndPtr;到物理内存的第一个位置…?详细信息:用户输入要为模拟保留的物理内存量。它的单位是字节。为了简单起见,我只使用int。稍后我将把它更改为双倍,因为分配可以增加到1gb。我创建了一个名为physicalM的向量。这是实际的物理内存块。稍后将划分为几代(由子结构代表示)。当用户指定命令(abc = alloc(50B));时,我必须在较低的代中创建一个名为abc的对象,并为其分配50MB的大小。(这部分我会在后面讨论)。任何帮助都很感激……

编辑:我试着在代码中使用这行,但我得到一个错误:
pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM);
error: cannot convert ‘std::vector<double>*’ to ‘double*’ in assignment

Edit:修复必须将rStrtPtr初始化为vector

pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM);

应该读:

pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM[0]);

pRootSet[0].rStrtPtr = pRootSet[0].physicalM.data();

pRootSet[0].rStrtPtr = &*(pRootSet[0].physicalM.begin());
pRootSet[0].rEndPtr = &*(pRootSet[0].physicalM.end());     // this will point to the first byte AFTER the end of the buffer.

我不知道为什么你在double中存储数据大小。一个unsigned long可以存储2^32 - 1(4294967295),即4GB