작성
·
562
0
MemoryPool* pool = new MemoryPool(size)
부분에서 할당 메모리 크기인 size가 각 for문 마다 고정되어야 한다고 생각했습니다.
그런데 강의 코드에서는 아래와 같이 size가 for loop를 한번 돌 때 마다 늘어나더라고요.
Memory::Memory()
{
int32 size = 0;
int32 tableIndex = 0;
for (size = 32; size <= 1024; size += 32)
{
MemoryPool* pool = new MemoryPool(size);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
for (; size <= 2048; size += 128)
{
MemoryPool* pool = new MemoryPool(size);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
for (; size <= 4096; size += 256)
{
MemoryPool* pool = new MemoryPool(size);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
}
제가 강의를 듣고 생각한건
0~1024 byte까지는 32 byte단위,
~2048 byte까지 128 btye단위,
~4096 byte까지 256 byte단위로 할당 한다고 생각했거든요.
근데 위 코드에서 첫번째 for문을 예를 들면 처음에는
Memory Pool을 32 byte 사이즈로 할당하는데
그다음에는 64 byte, 그다음은 96 byte ... 로 할당 사이즈가 점진적으로 증가하더라고요.
저는 1024 byte까지는 32byte로 쭉 하다가,
1024 byte부터 128 byte 단위로 할당할것이라고 생각했습니다.
그래서 저는 아래와 같은 식으로 되어야 하지 않을까 생각했습니다.
Memory::Memory()
{
int32 size = 0;
int32 tableIndex = 0;
for (size = 32; size <= 1024; size += 32)
{
MemoryPool* pool = new MemoryPool(32);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
for (; size <= 2048; size += 128)
{
MemoryPool* pool = new MemoryPool(128);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
for (; size <= 4096; size += 256)
{
MemoryPool* pool = new MemoryPool(256);
_pools.push_back(pool);
while (tableIndex <= size)
{
_poolTable[tableIndex] = pool;
tableIndex++;
}
}
}
제가 이해하지 못한 부분이 어떤것인지 알고 싶습니다.
답변 1
2
https://www.inflearn.com/questions/357795
Memory Pool을 32 byte 사이즈로 할당하는데
그다음에는 64 byte, 그다음은 96 byte ... 로 할당 사이즈가 점진적으로 증가하더라고요.
네 그게 의도한게 맞습니다.
방으로 비유하면 32인실 방을 여러개를 준비하는게 아니라,
32인실, 64인실, 96인실... 이렇게 점차 크기를 늘려가는겁니다.
15명이 방문한다면 32인실로 넣어주고,
70명이 방문한다면 96인실로 배치시켜주면 되겠죠.
추가로 MemoryPool은 내부적으로 할당받은 크기의 메모리를 여러개를 할당합니다.
32byte 크기의 MemoryPool을 만들면,
딱 1개의 32byte 크기만 받을 수 있는게 아니라
내부적으로 32byte 크기의 chunk를 여러개 할당하는 것이죠.