mirror of
https://github.com/Yours3lf/rpi-vk-driver.git
synced 2024-12-01 13:24:20 +01:00
added custom assert and pool allocator
This commit is contained in:
parent
ba5fb7e761
commit
0f8f62e4da
@ -12,6 +12,11 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
|
||||
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
|
||||
if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
|
||||
message("Debug build")
|
||||
add_definitions(-DDEBUG_BUILD)
|
||||
endif()
|
||||
|
||||
set (EXTERNAL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/external/)
|
||||
link_directories(${EXTERNAL_PATH}/lib)
|
||||
include_directories(${EXTERNAL_PATH}/include)
|
||||
|
31
driver/AlignedAllocator.h
Normal file
31
driver/AlignedAllocator.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
void* alignedAlloc( unsigned bytes, unsigned alignment = 16 )
|
||||
{
|
||||
if( !bytes )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const unsigned maxBytes = 1024 * 1024 * 1024; //1GB is max on RPi
|
||||
|
||||
if( bytes > maxBytes )
|
||||
{
|
||||
return 0; //bad alloc
|
||||
}
|
||||
|
||||
void* pv = 0;
|
||||
|
||||
if( posix_memalign( &pv, alignment, bytes ) )
|
||||
{
|
||||
pv = 0; //allocation failed
|
||||
}
|
||||
|
||||
return pv;
|
||||
}
|
||||
|
||||
void alignedFree( void* p )
|
||||
{
|
||||
free( p );
|
||||
}
|
24
driver/CustomAssert.h
Normal file
24
driver/CustomAssert.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
enum { HAVE_TRAP_INSTRUCTION = 1, };
|
||||
__attribute__((gnu_inline, always_inline))
|
||||
__inline__ static void DEBUG_BREAK(void)
|
||||
{
|
||||
__asm__ volatile(".inst 0xe7f001f0");
|
||||
}
|
||||
|
||||
#ifdef DEBUG_BUILD
|
||||
#define assert(expr) \
|
||||
if( expr ){} \
|
||||
else \
|
||||
{ \
|
||||
printf("Assert failed: %s\n" \
|
||||
"File: %s\n" \
|
||||
"Line: %i\n", #expr, __FILE__, __LINE__); \
|
||||
DEBUG_BREAK(); \
|
||||
}
|
||||
#else
|
||||
#define assert(expr) //do nothing
|
||||
#endif
|
77
driver/PoolAllocator.h
Normal file
77
driver/PoolAllocator.h
Normal file
@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include "CustomAssert.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct PoolAllocator
|
||||
{
|
||||
char* buf; //preallocated buffer
|
||||
char* nextFreeBlock;
|
||||
unsigned blockSize;
|
||||
unsigned size; //size is exact multiple of block size
|
||||
} PoolAllocator;
|
||||
|
||||
PoolAllocator createPoolAllocator(char* b, unsigned bs, unsigned s)
|
||||
{
|
||||
assert(b); //only allocated memory
|
||||
assert(bs >= sizeof(void*)); //we need to be able to store
|
||||
assert(s%bs==0); //we want a size that is the exact multiple of block size
|
||||
|
||||
PoolAllocator pa =
|
||||
{
|
||||
.buf = b,
|
||||
.nextFreeBlock = b,
|
||||
.blockSize = bs,
|
||||
.size = s
|
||||
};
|
||||
|
||||
//initialize linked list of free pointers
|
||||
char* ptr = pa.nextFreeBlock;
|
||||
for(unsigned c = 0; c < s/bs - 1; ++c)
|
||||
{
|
||||
*(uint32_t*)ptr = ptr + bs;
|
||||
ptr += bs;
|
||||
}
|
||||
|
||||
*(uint32_t*)ptr = 0; //last element
|
||||
}
|
||||
|
||||
void destroyPoolAllocator(PoolAllocator* pa)
|
||||
{
|
||||
//actual memory freeing is done by caller
|
||||
pa->buf = 0;
|
||||
pa->nextFreeBlock = 0;
|
||||
pa->blockSize = 0;
|
||||
pa->size = 0;
|
||||
}
|
||||
|
||||
void* poolAllocte(PoolAllocator* pa)
|
||||
{
|
||||
assert(pa->buf);
|
||||
|
||||
if(!pa->nextFreeBlock)
|
||||
{
|
||||
return 0; //no free blocks
|
||||
}
|
||||
|
||||
//next free block will be allocated
|
||||
void* ret = pa->nextFreeBlock;
|
||||
|
||||
//set next free block to the one the current next points to
|
||||
pa->nextFreeBlock = *(uint32_t*)pa->nextFreeBlock;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void poolFree(PoolAllocator* pa, void* p)
|
||||
{
|
||||
assert(pa->buf);
|
||||
assert(p);
|
||||
|
||||
//set block to be freed to point to the current next free block
|
||||
*(uint32_t*)p = pa->nextFreeBlock;
|
||||
|
||||
//set next free block to the freshly freed block
|
||||
pa->nextFreeBlock = p;
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "CustomAssert.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
@ -19,6 +18,8 @@
|
||||
|
||||
#include "modeset.h"
|
||||
|
||||
#include "PoolAllocator.h"
|
||||
|
||||
#ifndef min
|
||||
#define min(a, b) (a < b ? a : b)
|
||||
#endif
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include "driver/CustomAssert.h"
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
@ -670,15 +670,11 @@ int main() {
|
||||
|
||||
//window = glfwCreateWindow(640, 480, "The 630 line cornflower blue window", nullptr, nullptr);
|
||||
|
||||
//TODO create surface
|
||||
//swapbuffer
|
||||
|
||||
// Use Vulkan
|
||||
setupVulkan();
|
||||
|
||||
mainLoop();
|
||||
|
||||
//TODO destroy surface
|
||||
cleanup();
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user