formatted license comment
This commit is contained in:
parent
db5a728846
commit
0ffdb8c3ac
30 changed files with 16218 additions and 14914 deletions
10
core/arena.c
10
core/arena.c
|
|
@ -1,11 +1,11 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
/* 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));
|
||||
struct arena_allocator *allocator = (struct arena_allocator *)malloc(sizeof(struct arena_allocator));
|
||||
allocator->size = size;
|
||||
allocator->base = (usize) malloc(size);
|
||||
allocator->position = 0;
|
||||
|
|
@ -27,15 +27,15 @@ 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;
|
||||
for (usize i = 0; i < size; i++) {
|
||||
((u8 *) ptr)[i] = 0x0;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
|
|
|
|||
21
core/arena.h
21
core/arena.h
|
|
@ -1,15 +1,13 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
/* 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.
|
||||
* 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;
|
||||
|
|
@ -25,19 +23,16 @@ struct arena_allocator *arena_init(usize size);
|
|||
void arena_deinit(struct arena_allocator *allocator);
|
||||
|
||||
/*
|
||||
* Allocate a chunk of memory of size `size` on the
|
||||
* arena.
|
||||
* 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.
|
||||
* 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.
|
||||
* Free all the allocated memory at once. This just sets the allocator cursor
|
||||
* to its starting position.
|
||||
*/
|
||||
void arena_bump(struct arena_allocator *allocator);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#include "vector.h"
|
||||
#include "log.h"
|
||||
|
||||
struct vector *vector_init(usize size, usize element_size)
|
||||
{
|
||||
if (size == 0) size = 1;
|
||||
struct vector *vector = (struct vector *) malloc(sizeof(struct vector));
|
||||
if (size == 0)
|
||||
size = 1;
|
||||
struct vector *vector = (struct vector *)malloc(sizeof(struct vector));
|
||||
vector->length = 0;
|
||||
vector->size = size;
|
||||
vector->data = malloc(size * element_size);
|
||||
|
|
@ -14,12 +15,13 @@ struct vector *vector_init(usize size, usize element_size)
|
|||
|
||||
void *vector_shrink(struct vector *vector, usize element_size)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if (vector->length == 0) log_error("Popping from an empty vector.\n");
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
if (vector->length == 0)
|
||||
log_error("Popping from an empty vector.\n");
|
||||
#endif
|
||||
vector->length -= 1;
|
||||
if (vector->length <= vector->size/3) {
|
||||
vector->size = vector->length + vector->length/2;
|
||||
if (vector->length <= vector->size / 3) {
|
||||
vector->size = vector->length + vector->length / 2;
|
||||
vector->data = realloc(vector->data, vector->size * element_size + 1);
|
||||
}
|
||||
return vector->data;
|
||||
|
|
|
|||
|
|
@ -1,41 +1,39 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
#ifndef VECTOR_H
|
||||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "../types.h"
|
||||
|
||||
/* The vector is a dynamically growing array
|
||||
* that can be operated as a regular array
|
||||
* by accessing its `data` member and as a
|
||||
* stack using `vector_push()` and `vector_pop()`.
|
||||
/*
|
||||
* The vector is a dynamically growing array that can be operated as a regular
|
||||
* array by accessing its `data` member and as a stack using `vector_push()`
|
||||
* and `vector_pop()`.
|
||||
*/
|
||||
struct vector {
|
||||
usize length, size;
|
||||
void *data;
|
||||
};
|
||||
|
||||
/*
|
||||
* Create a new vector with starting capacity of `size`
|
||||
* where each element has size `element_size`.
|
||||
/*
|
||||
* Create a new vector with starting capacity of `size` where each element has
|
||||
* size `element_size`.
|
||||
*/
|
||||
struct vector *vector_init(usize size, usize element_size);
|
||||
void vector_deinit(struct vector *vector);
|
||||
|
||||
/*
|
||||
* This function reduces the lenth of the vector thus
|
||||
* removing the last element. If the used memory (length)
|
||||
* is less than 1/3 of the allocated memory (size) the
|
||||
* memory is reallocated to fit 1.5x the new length.
|
||||
* Consider using the `vector_pop()` macro instead.
|
||||
* This function reduces the lenth of the vector thus removing the last
|
||||
* element. If the used memory (length) is less than 1/3 of the allocated
|
||||
* memory (size) the memory is reallocated to fit 1.5x the new length. Consider
|
||||
* using the `vector_pop()` macro instead.
|
||||
*/
|
||||
void *vector_shrink(struct vector *vector, usize element_size);
|
||||
|
||||
/*
|
||||
* Push `value` on the vector. If there isn't enough
|
||||
* allocated memory, reallocate the internal array
|
||||
* to be 1.5x the current size and copy all the elements
|
||||
* to the new allocated memory.
|
||||
* Push `value` on the vector. If there isn't enough allocated memory,
|
||||
* reallocate the internal array to be 1.5x the current size and copy all the
|
||||
* elements to the new allocated memory.
|
||||
*/
|
||||
#define vector_push(vec, T, value) do {\
|
||||
if (vec->length + 1 >= vec->size) {\
|
||||
|
|
@ -47,8 +45,8 @@ void *vector_shrink(struct vector *vector, usize element_size);
|
|||
} while (0)
|
||||
|
||||
/*
|
||||
* Return the last element of the vector and calls
|
||||
* `vector_shrink()`. Check out that function description.
|
||||
* Return the last element of the vector and calls `vector_shrink()`. Check out
|
||||
* that function description.
|
||||
*/
|
||||
#define vector_pop(vector, T) (((T*)vector_shrink((vector), sizeof(T)))[vector->length])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue