1
0
mirror of https://github.com/Yours3lf/rpi-vk-driver.git synced 2025-02-19 16:54:18 +01:00

fixed consecutive pool allocator infinite loop

This commit is contained in:
Unknown 2020-03-01 15:26:12 +00:00
parent 35c3a4327a
commit a918e4216f
7 changed files with 36 additions and 40 deletions

View File

@ -3,6 +3,7 @@
#include "CustomAssert.h"
#include <stdint.h>
#include <string.h>
ConsecutivePoolAllocator createConsecutivePoolAllocator(char* b, unsigned bs, unsigned s)
{
@ -96,7 +97,8 @@ void* consecutivePoolAllocate(ConsecutivePoolAllocator* pa, uint32_t numBlocks)
}
}
return ret;
//return a pointer pointing past the linked list ptr
return ret > 0 ? (char*)ret + 4 : ret;
}
//free numBlocks consecutive memory
@ -105,6 +107,8 @@ void consecutivePoolFree(ConsecutivePoolAllocator* pa, void* p, uint32_t numBloc
assert(pa->buf);
assert(p);
p = (char*)p - 4;
if((void*)pa->nextFreeBlock > p)
{
for(uint32_t c = 0; c < numBlocks - 1; ++c)
@ -119,20 +123,9 @@ void consecutivePoolFree(ConsecutivePoolAllocator* pa, void* p, uint32_t numBloc
return;
}
int counter = 0;
//somewhere the linked list may point after the free block (or null), we need to correct this
for(uint32_t* nextFreeBlockCandidate = pa->nextFreeBlock; nextFreeBlockCandidate; nextFreeBlockCandidate = (uint32_t*)*nextFreeBlockCandidate)
{
//TODO infinite loop
counter++;
if(counter > 100)
{
fprintf(stderr, "--------------detected infinite loop nextFreeCandidate: %p, *nextfreecandidate: %p, p: %p\n", nextFreeBlockCandidate, *nextFreeBlockCandidate, p);
break;
}
if((void*)*nextFreeBlockCandidate > p || !*nextFreeBlockCandidate)
{
for(uint32_t c = 0; c < numBlocks - 1; ++c)
@ -153,6 +146,8 @@ void consecutivePoolFree(ConsecutivePoolAllocator* pa, void* p, uint32_t numBloc
//else it frees current block and allocates a new one
void* consecutivePoolReAllocate(ConsecutivePoolAllocator* pa, void* currentMem, uint32_t currNumBlocks)
{
currentMem = (char*)currentMem - 4;
if(pa->nextFreeBlock == (uint32_t*)((char*)currentMem + currNumBlocks * pa->blockSize))
{
//we have one more block after current one, so just expand current
@ -162,7 +157,12 @@ void* consecutivePoolReAllocate(ConsecutivePoolAllocator* pa, void* currentMem,
else
{
void* ret = consecutivePoolAllocate(pa, currNumBlocks + 1);
char* newContents = ret;
char* oldContents = currentMem;
newContents += 4;
oldContents += 4;
memcpy(newContents, oldContents, currNumBlocks * pa->blockSize - 4);
consecutivePoolFree(pa, currentMem, currNumBlocks);
return ret;
return newContents;
}
}

View File

@ -21,6 +21,7 @@ void destroyConsecutivePoolAllocator(ConsecutivePoolAllocator* pa);
void* consecutivePoolAllocate(ConsecutivePoolAllocator* pa, uint32_t numBlocks);
void consecutivePoolFree(ConsecutivePoolAllocator* pa, void* p, uint32_t numBlocks);
void* consecutivePoolReAllocate(ConsecutivePoolAllocator* pa, void* currentMem, uint32_t currNumBlocks);
void debugPrint(ConsecutivePoolAllocator*, const char* text);
#if defined (__cplusplus)
}

View File

@ -19,7 +19,7 @@ uint32_t clHasEnoughSpace(ControlList* cl, uint32_t size)
assert(cl->buffer);
assert(cl->nextFreeByte);
uint32_t currSize = cl->nextFreeByte - cl->buffer;
if(currSize + size < CONTROL_LIST_SIZE)
if(currSize + size < cl->numBlocks * cl->blockSize - 4)
{
return 1; //fits!
}
@ -29,12 +29,13 @@ uint32_t clHasEnoughSpace(ControlList* cl, uint32_t size)
}
}
void clInit(ControlList* cl, void* buffer)
void clInit(ControlList* cl, void* buffer, uint32_t blockSize)
{
assert(cl);
assert(buffer);
cl->buffer = buffer;
cl->numBlocks = 1;
cl->blockSize = blockSize;
cl->nextFreeByte = &cl->buffer[0];
cl->currMarker = 0;
}

View File

@ -42,12 +42,11 @@ typedef struct CLMarker
uint32_t uniformsSize;
} CLMarker;
#define CONTROL_LIST_SIZE 4096
typedef struct ControlList
{
uint8_t* buffer;
uint32_t numBlocks;
uint32_t blockSize;
uint8_t* nextFreeByte; //pointer to the next available free byte
CLMarker* currMarker;
} ControlList;
@ -67,7 +66,7 @@ void clDummyRelocation(ControlList* relocCl, const ControlListAddress* address);
uint32_t divRoundUp(uint32_t n, uint32_t d);
uint32_t moveBits(uint32_t d, uint32_t bits, uint32_t offset);
uint32_t clHasEnoughSpace(ControlList* cl, uint32_t size);
void clInit(ControlList* cl, void* buffer);
void clInit(ControlList* cl, void* buffer, uint32_t blockSize);
void clInsertNewCLMarker(ControlList* cl,
ControlList* handlesCL,
ControlList* shaderRecCL,

View File

@ -50,8 +50,9 @@ VKAPI_ATTR VkResult VKAPI_CALL rpi_vkCreateCommandPool(
//initial number of command buffers to hold
int numCommandBufs = 128;
int consecutivePoolSize = ARM_PAGE_SIZE*128;
int consecutiveBlockSize = ARM_PAGE_SIZE>>2;
int consecutiveBlockNumber = 64;
int consecutivePoolSize = consecutiveBlockNumber * consecutiveBlockSize;
static int counter = 0;
@ -118,10 +119,10 @@ VKAPI_ATTR VkResult VKAPI_CALL rpi_vkAllocateCommandBuffers(
pCommandBuffers[c]->usageFlags = 0;
pCommandBuffers[c]->state = CMDBUF_STATE_INITIAL;
pCommandBuffers[c]->cp = cp;
clInit(&pCommandBuffers[c]->binCl, consecutivePoolAllocate(&cp->cpa, 1));
clInit(&pCommandBuffers[c]->handlesCl, consecutivePoolAllocate(&cp->cpa, 1));
clInit(&pCommandBuffers[c]->shaderRecCl, consecutivePoolAllocate(&cp->cpa, 1));
clInit(&pCommandBuffers[c]->uniformsCl, consecutivePoolAllocate(&cp->cpa, 1));
clInit(&pCommandBuffers[c]->binCl, consecutivePoolAllocate(&cp->cpa, 1), cp->cpa.blockSize);
clInit(&pCommandBuffers[c]->handlesCl, consecutivePoolAllocate(&cp->cpa, 1), cp->cpa.blockSize);
clInit(&pCommandBuffers[c]->shaderRecCl, consecutivePoolAllocate(&cp->cpa, 1), cp->cpa.blockSize);
clInit(&pCommandBuffers[c]->uniformsCl, consecutivePoolAllocate(&cp->cpa, 1), cp->cpa.blockSize);
pCommandBuffers[c]->graphicsPipeline = 0;
pCommandBuffers[c]->computePipeline = 0;
@ -179,10 +180,10 @@ VKAPI_ATTR VkResult VKAPI_CALL rpi_vkAllocateCommandBuffers(
{
for(int c = 0; c < pAllocateInfo->commandBufferCount; ++c)
{
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->binCl, pCommandBuffers[c]->binCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->handlesCl, pCommandBuffers[c]->handlesCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->shaderRecCl, pCommandBuffers[c]->shaderRecCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->uniformsCl, pCommandBuffers[c]->uniformsCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->binCl.buffer, pCommandBuffers[c]->binCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->handlesCl.buffer, pCommandBuffers[c]->handlesCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->shaderRecCl.buffer, pCommandBuffers[c]->shaderRecCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->uniformsCl.buffer, pCommandBuffers[c]->uniformsCl.numBlocks);
poolFree(&cp->pa, pCommandBuffers[c]);
pCommandBuffers[c] = 0;
}
@ -666,10 +667,10 @@ VKAPI_ATTR void VKAPI_CALL rpi_vkFreeCommandBuffers(
{
if(pCommandBuffers[c])
{
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->binCl, pCommandBuffers[c]->binCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->handlesCl, pCommandBuffers[c]->handlesCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->shaderRecCl, pCommandBuffers[c]->shaderRecCl.numBlocks);
consecutivePoolFree(&cp->cpa, &pCommandBuffers[c]->uniformsCl, pCommandBuffers[c]->uniformsCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->binCl.buffer, pCommandBuffers[c]->binCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->handlesCl.buffer, pCommandBuffers[c]->handlesCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->shaderRecCl.buffer, pCommandBuffers[c]->shaderRecCl.numBlocks);
consecutivePoolFree(&cp->cpa, pCommandBuffers[c]->uniformsCl.buffer, pCommandBuffers[c]->uniformsCl.numBlocks);
poolFree(&cp->pa, pCommandBuffers[c]);
}
}

View File

@ -539,8 +539,11 @@ void clFit(VkCommandBuffer cb, ControlList* cl, uint32_t commandSize)
if(!clHasEnoughSpace(cl, commandSize))
{
uint32_t currSize = cl->nextFreeByte - cl->buffer;
uint32_t currMarkerOffset = (uint8_t*)cl->currMarker - cl->buffer;
cl->buffer = consecutivePoolReAllocate(&cb->cp->cpa, cl->buffer, cl->numBlocks); assert(cl->buffer);
cl->nextFreeByte = cl->buffer + currSize;
cl->numBlocks++;
cl->currMarker = cl->buffer + currMarkerOffset;
}
}

View File

@ -121,14 +121,6 @@ static uint32_t drawCommon(VkCommandBuffer commandBuffer)
clFit(commandBuffer, &commandBuffer->binCl, V3D21_FLAT_SHADE_FLAGS_length);
clInsertFlatShadeFlags(&commandBuffer->binCl, 0);
//GL Shader State
clFit(commandBuffer, &commandBuffer->binCl, V3D21_GL_SHADER_STATE_length);
clInsertShaderState(&commandBuffer->binCl,
@ -406,7 +398,6 @@ static uint32_t drawCommon(VkCommandBuffer commandBuffer)
}
}
return maxIndex;
}