-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4995927
Showing
35 changed files
with
2,114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.PHONY: clean | ||
|
||
all: | ||
i686-w64-mingw32-gcc -Os -Wconversion -Wsign-conversion -Wextra -Wall -Werror -pedantic -mwindows -municode -lm ./src/*.c ./deps/**/*.c -o ./bin/output -lgdi32 -lgdiplus | ||
|
||
run: all | ||
wine64 ./bin/output.exe | ||
|
||
clean: | ||
# if [ -d "./bin" ]; then rm ./bin/*; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-xc | ||
-mwindows | ||
--target=i686-w64-mingw32 | ||
-I/usr/i686-w64-mingw32/include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Useful macros | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#pragma once | ||
|
||
#define OF(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Type aliases | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <inttypes.h> | ||
#include <unistd.h> | ||
|
||
typedef uint64_t u64; | ||
typedef int64_t i64; | ||
|
||
typedef uint32_t u32; | ||
typedef int32_t i32; | ||
|
||
typedef uint16_t u16; | ||
typedef int16_t i16; | ||
|
||
typedef uint8_t u8; | ||
typedef int8_t i8; | ||
|
||
typedef size_t usize; | ||
typedef ssize_t isize; | ||
|
||
typedef float f32; | ||
typedef double f64; | ||
|
||
typedef float f32; | ||
typedef double f64; | ||
|
||
#define nullable /* @nullable */ | ||
#define pub /* @public */ | ||
#define FORRANGE(T, handle, from, to) for (T handle = from; handle < to; handle += 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include "enty.h" | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
struct Enty enty_new(usize type_size) { | ||
return (struct Enty) { | ||
.slab = slab_new(type_size), | ||
.last_index = -1, | ||
.free_capacity = 1, | ||
.frees = calloc(1, sizeof(u32)) | ||
}; | ||
} | ||
|
||
// Returns rightmost set bit | ||
u32 rmsb(u32 num) { | ||
return (num << 16 ? (num << 24 ? (num << 28 ? (num << 30 ? (num << 31 ? 0 : 1) : (num << 29 ? 2 : 3)) : (num << 26 ? (num << 27 ? 4 : 5) : (num << 25 ? 6 : 7))) : (num << 20 ? (num << 22 ? (num << 23 ? 8 : 9) : (num << 21 ? 10 : 11)) : (num << 18 ? (num << 19 ? 12 : 13) : (num << 17 ? 14 : 15)))) : (num << 8 ? (num << 12 ? (num << 14 ? (num << 15 ? 16 : 17) : (num << 13 ? 18 : 19)) : (num << 10 ? (num << 11 ? 20 : 21) : (num << 9 ? 22 : 23))) : (num << 4 ? (num << 6 ? (num << 7 ? 24 : 25) : (num << 5 ? 26 : 27)) : (num << 2 ? (num << 3 ? 28 : 29) : (num << 1 ? 30 : (num << 0 ? 31 : 32)))))); | ||
} | ||
|
||
usize enty_first_index(struct Enty *self) { | ||
usize index = 0; | ||
while (enty_is_free(self, index) && (isize)index < self->last_index) index += 1; | ||
return index; | ||
} | ||
|
||
usize enty_advance_index(struct Enty *self, usize index) { | ||
index += 1; | ||
while (enty_is_free(self, index) && (isize)index < self->last_index) index += 1; | ||
return index; | ||
} | ||
|
||
usize enty_retreat_index(struct Enty *self, usize index) { | ||
if (index == 0) return 0; | ||
index -= 1; | ||
while (enty_is_free(self, index) && index > 0) index -= 1; | ||
return index; | ||
} | ||
|
||
usize enty_insert(struct Enty *self, void *value) { | ||
for (usize i = 0; i < self->free_capacity; i += 1) { | ||
u32 bitmap = self->frees[i]; | ||
if (bitmap != 0xFFFFFFFF) { | ||
usize unset_pos = (usize) rmsb(~bitmap); | ||
usize offset = (i * FREED_BITS) + unset_pos; | ||
if (self->slab.capacity <= offset) { | ||
if (!slab_resize(&self->slab, self->slab.capacity * 2)) { | ||
fprintf(stderr, "sus\n"); | ||
exit(-1); | ||
} | ||
} | ||
if ((isize)offset > self->last_index) { | ||
self->last_index = (isize) offset; | ||
} | ||
slab_write(&self->slab, offset, value); | ||
self->frees[i] |= (1 << unset_pos); | ||
return offset; | ||
} | ||
} | ||
|
||
// Resize and repeat | ||
void *old_frees = self->frees; | ||
void *new_frees = calloc(self->free_capacity*2, sizeof(u32)); | ||
memcpy(new_frees, self->frees, self->free_capacity*sizeof(u32)); | ||
free(old_frees); | ||
self->frees = new_frees; | ||
self->free_capacity *= 2; | ||
|
||
|
||
return enty_insert(self, value); | ||
} | ||
|
||
void* enty_get(struct Enty *self, usize index) { | ||
usize bitmap_offset = index / FREED_BITS; | ||
u32 flag = 1 << (index-bitmap_offset*FREED_BITS); | ||
|
||
if (bitmap_offset > self->free_capacity) { | ||
fprintf(stderr, "Enty container invalid bitmap offset, maximum %d got %d, requested via index %d\n", self->free_capacity, bitmap_offset, index); | ||
exit(-1); | ||
}; | ||
if ((self->frees[bitmap_offset] & flag) == 0) { | ||
fprintf(stderr, "Enty container, attempt to access freed area\n"); | ||
exit(-1); | ||
} | ||
|
||
return slab_read(&self->slab, index); | ||
} | ||
|
||
bool enty_is_free(struct Enty *self, usize index) { | ||
usize bitmap_offset = index / FREED_BITS; | ||
u32 flag = 1 << (index-bitmap_offset*FREED_BITS); | ||
if (bitmap_offset >= self->free_capacity) return true; | ||
if ((self->frees[bitmap_offset] & flag) == 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void enty_free_index(struct Enty *self, usize index) { | ||
usize bitmap_offset = index / FREED_BITS; | ||
u32 flag = 1 << (index-bitmap_offset*FREED_BITS); | ||
|
||
if (bitmap_offset > self->free_capacity) { | ||
fprintf(stderr, "Enty container invalid bitmap offset, maximum %d got %d, requested via index %d", self->free_capacity, bitmap_offset, index); | ||
exit(-1); | ||
}; | ||
if ((self->frees[bitmap_offset] & flag) == 0) { | ||
fprintf(stderr, "Enty container, attempt to free already freed area"); | ||
exit(-1); | ||
} | ||
|
||
// Mark free | ||
self->frees[bitmap_offset] &= ~flag; | ||
} | ||
|
||
void enty_drop(struct Enty *self) { | ||
slab_drop(&self->slab); | ||
free(self->frees); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Homogenous data allocator | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#pragma once | ||
#include "../slab/slab.h" | ||
#include "../common/types.h" | ||
|
||
#define FREED_BITS 32L | ||
|
||
struct Enty { | ||
struct Slab slab; | ||
// Each element in the list will represent if some element is free in the slab | ||
// We'll hide the implementation detail that the size is limited, | ||
// It will **panic** when the slab size exceeds 32 bits * 32 cells, | ||
// However, that will change as the demands will be growing | ||
// 1 = occupied | ||
// 0 = freed | ||
u32 *frees; | ||
usize free_capacity; | ||
isize last_index; | ||
}; | ||
|
||
struct Enty enty_new(usize type_size); | ||
usize enty_first_index(struct Enty *self); | ||
usize enty_advance_index(struct Enty *self, usize index); | ||
usize enty_retreat_index(struct Enty *self, usize index); | ||
void enty_free_index(struct Enty *self, usize index); | ||
usize enty_insert(struct Enty *self, void *value); | ||
bool enty_is_free(struct Enty *self, usize index); | ||
void* enty_get(struct Enty *self, usize index); | ||
void enty_drop(struct Enty *self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Convenience | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#include "slab/slab.h" | ||
#include "enty/enty.h" | ||
#include "vec/vec.h" | ||
#include "common/types.h" | ||
#include "common/macros.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "slab.h" | ||
#include <stdbool.h> | ||
#include <memory.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <inttypes.h> | ||
#include <stdlib.h> | ||
|
||
#define OFTYPE(t) sizeof(t) | ||
|
||
struct Slab slab_new(size_t etype) { | ||
return (struct Slab) { | ||
.el_size = etype, | ||
.capacity = 32, | ||
.elements = malloc(etype*32) | ||
}; | ||
} | ||
|
||
bool slab_write( | ||
struct Slab *cont, | ||
size_t at, | ||
void *val | ||
) { | ||
if (at > cont->capacity) return false; | ||
memcpy(cont->elements+(cont->el_size*at), val, cont->el_size); | ||
return true; | ||
} | ||
|
||
void* slab_read( | ||
struct Slab *cont, | ||
size_t at | ||
) { | ||
if (at >= cont->capacity) return NULL; | ||
return cont->elements+(cont->el_size*at); | ||
} | ||
|
||
|
||
|
||
bool slab_resize( | ||
struct Slab *cont, | ||
size_t capacity) | ||
{ | ||
if (capacity < cont->capacity) | ||
return false; | ||
printf("Resizing for size %d, previously %d\n", capacity*cont->el_size, cont->capacity*cont->el_size); | ||
cont->elements = realloc(cont->elements, capacity*cont->el_size); | ||
cont->capacity = capacity; | ||
return true; | ||
} | ||
|
||
void slab_drop(struct Slab *cont) { | ||
free(cont->elements); | ||
cont->elements = NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Homogenous data container | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <memory.h> | ||
#include <unistd.h> | ||
#include <inttypes.h> | ||
#include <stdlib.h> | ||
|
||
#define OFTYPE(t) sizeof(t) | ||
|
||
struct Slab { | ||
size_t el_size; | ||
size_t capacity; | ||
uint8_t *elements; | ||
}; | ||
|
||
struct Slab slab_new(size_t etype); | ||
|
||
bool slab_write( | ||
struct Slab *cont, | ||
size_t at, | ||
void *val | ||
); | ||
|
||
void* slab_read( | ||
struct Slab *cont, | ||
size_t at | ||
); | ||
|
||
bool slab_resize( | ||
struct Slab *cont, | ||
size_t capacity | ||
); | ||
|
||
void slab_drop(struct Slab *cont); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "vec.h" | ||
|
||
struct Vec vec_new(size_t type_size) { | ||
return (struct Vec) { .size = 0, .cont = slab_new(type_size)}; | ||
} | ||
|
||
void vec_push(struct Vec *vec, void *val) { | ||
if (!slab_write(&vec->cont, vec->size, val)) { | ||
slab_resize(&vec->cont, vec->cont.capacity*2); | ||
slab_write(&vec->cont, vec->size, val); | ||
} | ||
vec->size += 1; | ||
} | ||
|
||
void* vec_get(struct Vec *vec, size_t at) { | ||
if (at >= vec->size) return NULL; | ||
return slab_read(&vec->cont, at); | ||
} | ||
|
||
void* vec_pop(struct Vec *vec) { | ||
if (vec->size <= 0) return NULL; | ||
void* val = slab_read(&vec->cont, vec->size-1); | ||
vec->size -= 1; | ||
return val; | ||
} | ||
|
||
void vec_drop(struct Vec *vec) { | ||
slab_drop(&vec->cont); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Size annotated vector type | ||
* ishidex2 2021, MIT license | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <memory.h> | ||
#include <unistd.h> | ||
#include <inttypes.h> | ||
#include <stdlib.h> | ||
#include "../slab/slab.h" | ||
|
||
struct Vec { | ||
size_t size; | ||
struct Slab cont; | ||
}; | ||
|
||
struct Vec vec_new(size_t etype); | ||
void vec_push(struct Vec *vec, void *val); | ||
void* vec_get(struct Vec *vec, size_t at); | ||
void* vec_pop(struct Vec *vec); | ||
void vec_drop(struct Vec *vec); |
Binary file not shown.
Oops, something went wrong.