summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-09-22 21:23:29 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-09-22 21:23:29 +0200
commited9f20507cb3199827feb203159656e233372e44 (patch)
tree81408dbc7a493f71650669b7138a3352fe571a27
parent242b87209b778d033d1da828ae9ec47170c17f38 (diff)
downloadkit-ed9f20507cb3199827feb203159656e233372e44.zip
GL registry parsing
-rw-r--r--.gitignore1
-rw-r--r--gen_gl.c57
-rw-r--r--include/kit.inl.h7
-rw-r--r--source/kit/allocator.c4
-rw-r--r--source/kit/xml.c3
5 files changed, 70 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 3a26fa3..838f752 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
/*build*/
+/.gl_registry
*.swp
gen_inl
gen_gl
diff --git a/gen_gl.c b/gen_gl.c
index 117d9bc..aabd13f 100644
--- a/gen_gl.c
+++ b/gen_gl.c
@@ -6,6 +6,63 @@ exit
#define KIT_IMPLEMENTATION
#include "include/kit.inl.h"
+#define MB *1024 * 1024
+#define GL_FOLDER ".gl_registry"
+#define GL_REPO "https://github.com/KhronosGroup/OpenGL-Registry"
+
int main(int argc, char **argv) {
+ if (path_type(SZ("." PATH_DELIM GL_FOLDER)) == PATH_NONE) {
+ int s = system("git clone --quiet --depth 1 " GL_REPO
+ " " GL_FOLDER);
+ int code = s & 0xff;
+
+ if (code != 0) {
+ printf("`git clone` failed.\n");
+ return code;
+ }
+ }
+
+ if (path_type(SZ("." PATH_DELIM GL_FOLDER)) != PATH_FOLDER) {
+ printf("OpenGL registry folder not found.\n");
+ return 1;
+ }
+
+ FILE *f = fopen("." PATH_DELIM GL_FOLDER PATH_DELIM "xml" PATH_DELIM
+ "gl.xml",
+ "rb");
+
+ if (f == NULL) {
+ printf("Failed to read OpenGL registry.\n");
+ return 1;
+ }
+
+ // FIXME
+ // Optimize memory use
+ i64 arena_size = 100 MB;
+ u8 *arena = kit_alloc_dispatch(NULL, KIT_ALLOCATE, arena_size, 0,
+ NULL);
+ kit_allocator_t alloc = kit_alloc_buffer(arena_size, arena);
+
+ is_handle_t is = is_wrap_file(f, &alloc);
+ struct timespec t0, t1;
+ timespec_get(&t0, TIME_UTC);
+ xml_parse_result_t res = xml_parse(is, &alloc);
+ timespec_get(&t1, TIME_UTC);
+ is_destroy(is);
+ fclose(f);
+
+ if (res.status != KIT_OK) {
+ printf("XML parse failed.\n");
+ kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, arena);
+ return 1;
+ }
+
+ i64 diff = 1000 * (t1.tv_sec - t0.tv_sec) +
+ (t1.tv_nsec - t0.tv_nsec + 500000) / 1000000;
+ printf("XML parsed in %lld milliseconds.\n", diff);
+
+ xml_destroy(&res.xml);
+
+ kit_alloc_dispatch(NULL, KIT_DEALLOCATE, 0, 0, arena);
return 0;
}
diff --git a/include/kit.inl.h b/include/kit.inl.h
index 9752919..7b7407d 100644
--- a/include/kit.inl.h
+++ b/include/kit.inl.h
@@ -2088,7 +2088,9 @@ void *kit_alloc_dispatch(kit_allocator_t *alloc, i32 request,
return kit_allocate_default_(request, size, previous_size,
pointer);
case KIT_ALLOC_TYPE_BUFFER:
- return kit_allocate_from_buffer_(alloc, request, size,
+ return kit_allocate_from_buffer_(alloc, request,
+ // alignment
+ ((size + 7) / 8) * 8,
previous_size, pointer);
default:;
}
@@ -4341,6 +4343,7 @@ static ib_t kit_xml_parse_text_(ib_t begin) {
if (next_text.status == KIT_OK && next_text.data.size > 0) {
i64 n = text.data.size;
DA_RESIZE(text.data, n + next_text.data.size);
+ assert(text.data.size == n + next_text.data.size);
if (text.data.size != n + next_text.data.size)
next_text.status = KIT_ERROR_BAD_ALLOC;
else
@@ -4438,6 +4441,7 @@ static kit_xml_intermediate_t kit_xml_parse_buf_(
if (last.status == KIT_OK) {
i64 n = tag.properties.size;
DA_RESIZE(tag.properties, n + 1);
+ assert(tag.properties.size == n + 1);
if (tag.properties.size != n + 1) {
last.status = KIT_ERROR_BAD_ALLOC;
DA_DESTROY(tag.properties);
@@ -4499,6 +4503,7 @@ static kit_xml_intermediate_t kit_xml_parse_buf_(
if (last.status == KIT_OK) {
i64 n = res.tags.size;
DA_RESIZE(res.tags, n + 1);
+ assert(res.tags.size == n + 1);
if (res.tags.size != n + 1) {
last.status = KIT_ERROR_BAD_ALLOC;
xml_destroy(&tag);
diff --git a/source/kit/allocator.c b/source/kit/allocator.c
index 920da5a..e1446ac 100644
--- a/source/kit/allocator.c
+++ b/source/kit/allocator.c
@@ -173,7 +173,9 @@ void *kit_alloc_dispatch(kit_allocator_t *alloc, i32 request,
pointer);
case KIT_ALLOC_TYPE_BUFFER:
- return kit_allocate_from_buffer_(alloc, request, size,
+ return kit_allocate_from_buffer_(alloc, request,
+ // alignment
+ ((size + 7) / 8) * 8,
previous_size, pointer);
default:;
diff --git a/source/kit/xml.c b/source/kit/xml.c
index 1c8e9f6..a9d18e3 100644
--- a/source/kit/xml.c
+++ b/source/kit/xml.c
@@ -69,6 +69,7 @@ static ib_t kit_xml_parse_text_(ib_t begin) {
i64 n = text.data.size;
DA_RESIZE(text.data, n + next_text.data.size);
+ assert(text.data.size == n + next_text.data.size);
if (text.data.size != n + next_text.data.size)
next_text.status = KIT_ERROR_BAD_ALLOC;
else
@@ -200,6 +201,7 @@ static kit_xml_intermediate_t kit_xml_parse_buf_(
i64 n = tag.properties.size;
DA_RESIZE(tag.properties, n + 1);
+ assert(tag.properties.size == n + 1);
if (tag.properties.size != n + 1) {
last.status = KIT_ERROR_BAD_ALLOC;
DA_DESTROY(tag.properties);
@@ -277,6 +279,7 @@ static kit_xml_intermediate_t kit_xml_parse_buf_(
i64 n = res.tags.size;
DA_RESIZE(res.tags, n + 1);
+ assert(res.tags.size == n + 1);
if (res.tags.size != n + 1) {
last.status = KIT_ERROR_BAD_ALLOC;
xml_destroy(&tag);