implemented arena allocator
This commit is contained in:
parent
1f594fbed7
commit
1dd1049bba
4 changed files with 92 additions and 12 deletions
|
|
@ -1,9 +0,0 @@
|
|||
vertex float4 basic_vertex( // 1
|
||||
const device packed_float3* vertex_array [[ buffer(0) ]], // 2
|
||||
unsigned int vid [[ vertex_id ]]) { // 3
|
||||
return float4(vertex_array[vid], 1.0); // 4
|
||||
}
|
||||
|
||||
fragment half4 basic_fragment() { // 1
|
||||
return half4(1.0); // 2
|
||||
}
|
||||
41
core/arena.c
Normal file
41
core/arena.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
#include "arena.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct arena_allocator *arena_init(usize size)
|
||||
{
|
||||
struct arena_allocator *allocator = (struct arena_allocator *) malloc(sizeof(struct arena_allocator));
|
||||
allocator->size = size;
|
||||
allocator->base = (usize) malloc(size);
|
||||
allocator->position = 0;
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
void arena_deinit(struct arena_allocator *allocator)
|
||||
{
|
||||
free((void *)allocator->base);
|
||||
free((void *)allocator);
|
||||
}
|
||||
|
||||
void *arena_alloc(struct arena_allocator *allocator, usize size)
|
||||
{
|
||||
void *ptr = (void *)(allocator->base + allocator->position);
|
||||
allocator->position += size;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *arena_zalloc(struct arena_allocator *allocator, usize size)
|
||||
{
|
||||
void *ptr = arena_alloc(allocator, size);
|
||||
for (usize i=0; i < size; i++) {
|
||||
((u8 *)ptr)[i] = 0x0;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void arena_bump(struct arena_allocator *allocator)
|
||||
{
|
||||
allocator->position = 0;
|
||||
}
|
||||
43
core/arena.h
Normal file
43
core/arena.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
#ifndef ARENA_H
|
||||
#define ARENA_H
|
||||
|
||||
#include "../types.h"
|
||||
|
||||
/*
|
||||
* An arena is a fast allocator that just
|
||||
* keeps everything in a contiguous chunk
|
||||
* of memory and moves a "pointer" when
|
||||
* allocating new memory. The allocated
|
||||
* memory is then free'd all at once.
|
||||
*/
|
||||
struct arena_allocator {
|
||||
usize size;
|
||||
usize base;
|
||||
usize position;
|
||||
};
|
||||
|
||||
|
||||
/* Create a new arena allocator of size `size` */
|
||||
struct arena_allocator *arena_init(usize size);
|
||||
/* Destroy the allocator and */
|
||||
void arena_deinit(struct arena_allocator *allocator);
|
||||
|
||||
/*
|
||||
* Allocate a chunk of memory of size `size` on the
|
||||
* arena.
|
||||
*/
|
||||
void *arena_alloc(struct arena_allocator *allocator, usize size);
|
||||
/*
|
||||
* Same as `arena_alloc()` but also set all the allocated
|
||||
* memory to zero.
|
||||
*/
|
||||
void *arena_zalloc(struct arena_allocator *allocator, usize size);
|
||||
/*
|
||||
* Free all the allocated memory at once.
|
||||
* This just sets the allocator cursor to its
|
||||
* starting position.
|
||||
*/
|
||||
void arena_bump(struct arena_allocator *allocator);
|
||||
|
||||
#endif
|
||||
11
makefile
11
makefile
|
|
@ -2,13 +2,18 @@
|
|||
|
||||
include config.mk
|
||||
|
||||
SRC:=topaz.c linear.c
|
||||
SRC:=\
|
||||
topaz.c\
|
||||
linear.c\
|
||||
core/arena.c
|
||||
|
||||
ifeq (${BACKEND},gl)
|
||||
SRC += gl/gl.c gl/platform.c
|
||||
SRC += gl/gl.c\
|
||||
gl/platform.c
|
||||
endif
|
||||
ifeq (${BACKEND},vk)
|
||||
SRC += vk/platform.c
|
||||
SRC += vk/platform.c\
|
||||
vk/renderer.c
|
||||
endif
|
||||
|
||||
OBJ:=${SRC:.c=.o}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue