[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

memory allocation in deb9 vs deb10



Hi,
In the below c++ program I am trying to divide a bigger memory block into smaller subblock by moving pointers by 1 page. While creating a subblock I am
initializing the variable of a structure which is of 64 bits. I tried to run this code on debian10 and debian9. What I observed is in deb9 only 64 bits memory is consumed but  deb10 consumes the entire page which is off 4K.
Can someone help me understand what causes this difference? Can this be related to the way the kernel handles the memory? Thanks for your help in advance.  

#include <vector>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;

struct e{
   u8* f;
   u64* s;
};
struct se{
   u64 cid;
   void* np;
};
const u64 a = -1;
u32 subchunk = 2878;
u32 subchunkperchunk = 140;
u32 chunksize = 1028096;   //in bytes
u32 TSize = 4096;
u32 eleOfChunk = 50;
u32 eSize = 4096;
u64 x;
vector<e> v;
int it=0;

u64& add(const void* ep)
{
    void* subChunkPtr = (void*) (((u64)ep) / chunksize * chunksize);
    u64 firstElementLocation = ((u64)subChunkPtr) + TSize;
    u32 indexInsideSubChunk = (((u64)ep)-firstElementLocation) / eSize;
    se* elePtr = ((se*)subChunkPtr) + indexInsideSubChunk;
    return elePtr->cid;
}

int main()
{
    int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU);
    dup2(file, 1);
    for (u32 i=0; i<= subchunk/subchunkperchunk; i++)
    {
      u32 eleInCurrChunk;
      if (i != subchunk/subchunkperchunk)
      {
         eleInCurrChunk = subchunkperchunk;
      }
      else
      {
         eleInCurrChunk = subchunk % subchunkperchunk;
         if (eleInCurrChunk == 0)
         {
            break;
         }
      }
      u32 CSize = (eleInCurrChunk+1) * chunksize;
      u8* sp;
      sp = new u8[CSize];
      u8* ssp = sp + (chunksize - ((u64)sp % chunksize));
      for (u32 j = 0; j < eleInCurrChunk; ++j)    //creat block
      {
         u8* scp = ssp + j * chunksize;
         u8* ep = scp + TSize;
         for (u32 k=0; k < eleOfChunk ; ++k)   //creat subblock
         {
             v.push_back(e());
             v[it].f = ep;
             v[it].s = &add(ep);
             add(ep)= a;               //set the 64 bits to high
             ep += eSize;
             ++it;
        }
        system("free -m");
      }
    }
  return 0;
}


Reply to: