aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/kpl.h5
-rw-r--r--include/kpl/allocate.h5
-rw-r--r--include/kpl/ast.h6
-rw-r--r--include/kpl/env.h7
-rw-r--r--include/kpl/identifier.h41
-rw-r--r--include/kpl/io.h4
-rw-r--r--include/kpl/object.h16
-rw-r--r--include/kpl/storage.h44
-rw-r--r--include/kpl/tree.h1
-rw-r--r--include/kpl/type.h67
10 files changed, 175 insertions, 21 deletions
diff --git a/include/kpl.h b/include/kpl.h
index 838478b..8623488 100644
--- a/include/kpl.h
+++ b/include/kpl.h
@@ -21,6 +21,7 @@
#include <ctype.h>
#include <errno.h>
#include <limits.h>
+#include <sched.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
@@ -51,6 +52,10 @@ typedef struct _kpl_env kpl_env;
#include "kpl/class.h"
#include "kpl/object.h"
#include "kpl/tree.h"
+#include "kpl/identifier.h"
+#include "kpl/type.h"
+#include "kpl/storage.h"
+#include "kpl/ast.h"
#include "kpl/memory.h"
#include "kpl/io.h"
#include "kpl/env.h"
diff --git a/include/kpl/allocate.h b/include/kpl/allocate.h
index ee24d72..3da4a10 100644
--- a/include/kpl/allocate.h
+++ b/include/kpl/allocate.h
@@ -19,6 +19,7 @@ inline void *kpl_object_allocate(kpl_thread *thread, int32_t size) {
if (thread->gc_head)
thread->gc_head->gc_prev = obj;
thread->gc_head = obj;
+ thread->gc_cycle_change++;
return obj;
}
@@ -31,5 +32,7 @@ inline void kpl_object_return(kpl_env *env, void *object) {
obj->gc_prev->gc_next = obj->gc_next;
if (obj->gc_next)
obj->gc_next->gc_prev = obj->gc_prev;
- kpl_memory_allocator_return(&env->threads[obj->thread_id].memory_allocator, object);
+ kpl_thread *thread = &env->threads[obj->thread_id];
+ kpl_memory_allocator_return(&thread->memory_allocator, object);
+ thread->gc_cycle_change--;
}
diff --git a/include/kpl/ast.h b/include/kpl/ast.h
new file mode 100644
index 0000000..b9ba591
--- /dev/null
+++ b/include/kpl/ast.h
@@ -0,0 +1,6 @@
+/*
+ * KPL AST
+ * An AST is the definition of code and data
+ */
+
+#pragma once
diff --git a/include/kpl/env.h b/include/kpl/env.h
index 1e3d3d8..5ea3b33 100644
--- a/include/kpl/env.h
+++ b/include/kpl/env.h
@@ -14,6 +14,7 @@ typedef enum : int32_t {
typedef struct _kpl_thread {
uint16_t thread_id; // self id
kpl_memory_allocator memory_allocator;
+ _Atomic ssize_t gc_cycle_change; // number of objects added(+)/removed(-) to gc_head after last sweep
kpl_object *gc_head, *gc_mark_head; // sweep if not marked or not in io pending
kpl_io_uring io_uring;
// TODO jit allocator
@@ -40,7 +41,6 @@ typedef enum : uint32_t {
* kpl_env_config &env, argc, argv
* kpl_env_init &env
* add to the namespace tree
- * add a file load task, string evaluate task or REPL task to main thread task queue
* kpl_env_run &env
* kpl_env_free &env
*/
@@ -51,8 +51,9 @@ typedef struct _kpl_env {
const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace
_Atomic ssize_t total_priority; // the sum of the threads priority fields, application stops on zero
kpl_thread *threads; // 0 is main thread, >= 1 are spawned pthreads
- // TODO namespace tree
- // TODO module tree
+ kpl_tree namespace_tree, module_tree; // Trees of kpl_storage
+ pthread_mutex_t module_tree_mutex;
+ // TODO jit
} kpl_env; // kpl state, no globals, must be zero initialized
/*
diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h
new file mode 100644
index 0000000..092b0c4
--- /dev/null
+++ b/include/kpl/identifier.h
@@ -0,0 +1,41 @@
+/*
+ * Thread safe identifiers, at the cost of space remove the need for hash/tree lookups
+ * Keep 50 [A-Za-z0-9_] Characters in 40 bytes
+ */
+
+#pragma once
+
+#define KPL_IDENTIFIER_PARTS 5
+
+#define KPL_IDENTIFIER_BITS_PER_PART 64
+
+#define KPL_IDENTIFIER_BITS_PER_VALUE 6
+
+#define KPL_IDENTIFIER_VALUES_PER_PART (KPL_IDENTIFIER_BITS_PER_PART / KPL_IDENTIFIER_BITS_PER_VALUE)
+
+#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART)
+
+#define KPL_IDENTIFIER_MAX_LENGTH (KPL_IDENTIFIER_VALUES_PER_PART * KPL_IDENTIFIER_PARTS)
+
+#define KPL_IDENTIFIER_C_STRING_SIZE (KPL_IDENTIFIER_MAX_LENGTH + sizeof(char))
+
+/*
+ * Each character takes 6 bits from bit 0 to bit 59
+ * The upper 4 bits represent the number of characters in the part
+ */
+typedef struct {
+ uint64_t parts[KPL_IDENTIFIER_PARTS];
+} kpl_identifier;
+
+/*
+ * Converts a c string into an identifier
+ * Checks if the c_string_length <= KPL_IDENTIFIER_MAX_LENGTH
+ */
+bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length);
+
+/*
+ * Converts an identifier into a c string
+ * Does not clear the c_string buffer
+ * Does not check the c_string buffer assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE
+ */
+void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]);
diff --git a/include/kpl/io.h b/include/kpl/io.h
index f5904f4..3838a55 100644
--- a/include/kpl/io.h
+++ b/include/kpl/io.h
@@ -6,8 +6,8 @@
typedef enum : int32_t {
KPL_IO_URING_QUEUE_SIZE_POW2_MIN = 12,
- KPL_IO_URING_QUEUE_SIZE_POW2_MAX = 20,
- KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT = 15
+ KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT = 15,
+ KPL_IO_URING_QUEUE_SIZE_POW2_MAX = 20
} kpl_io_uring_enum;
/*
diff --git a/include/kpl/object.h b/include/kpl/object.h
index 1053348..b7b1a33 100644
--- a/include/kpl/object.h
+++ b/include/kpl/object.h
@@ -6,7 +6,8 @@
typedef enum : uint8_t {
KPL_OBJECT_FLAGS_EMPTY = 0,
- KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0 // When io pending tasks will not be reachable by GC
+ KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0, // When io pending tasks will not be reachable by GC
+ KPL_OBJECT_FLAGS_GC_PERSIST = 1 << 1 // Object is never swept by GC until program end
} kpl_object_flags;
// for atomic linked list queue
@@ -84,16 +85,3 @@ typedef struct _kpl_object_task {
kpl_result result;
kpl_class variables[]; // a task has no distinction between an argument or a local, both live here
} kpl_object_task;
-
-/*
- * KPL Object Mutex is a queue of tasks on an object
- * Adding: Increment counter, then add task to task queue
- * Removing: Remove from task queue, run task, then decrement counter
- * Requires spin check when removing task due to space between counter adding and task adding
- * If the counter value before was zero take control of the mutex
- * The thread that controls the mutex runs each task until counter is zero
- */
-typedef struct {
- _Atomic int32_t lock_counter;
- kpl_object_queue task_queue;
-} kpl_object_mutex;
diff --git a/include/kpl/storage.h b/include/kpl/storage.h
new file mode 100644
index 0000000..d78f80b
--- /dev/null
+++ b/include/kpl/storage.h
@@ -0,0 +1,44 @@
+/*
+ * KPL Object storage
+ */
+
+#pragma once
+
+/*
+ * Inner tree storage
+ * Keys will always be kpl_identifier
+ */
+typedef struct _kpl_storgae_object {
+ KPL_TREE_HEADER(_kpl_storgae_object);
+ kpl_type_base *type;
+ kpl_class class;
+ kpl_identifier identifier;
+} kpl_stoage_object;
+
+// c_string_ptr used for tree search comparisons
+#define KPL_STORAGE_HEADER(STRUCT_NAME) \
+ KPL_TREE_HEADER(STRUCT_NAME); \
+ kpl_tree object_tree; \
+ const char *c_string_ptr;
+
+/*
+ * Uses const c string keys
+ * The namespace is c_string_ptr
+ */
+typedef struct _kpl_storage_namespace {
+ KPL_TREE_HEADER(_kpl_storage_namespace);
+} kpl_stoage_namespace;
+
+/*
+ * Alias used for string comparison functions
+ */
+typedef kpl_stoage_namespace kpl_stoage_search;
+
+/*
+ * Modules make a copy of absolute file path
+ * The c_string_ptr is pointed to this file path copy string
+ */
+typedef struct _kpl_storage_module {
+ KPL_TREE_HEADER(_kpl_storage_module);
+ char c_string_array[]; // null term, c_string_ptr points to this
+} kpl_storage_module;
diff --git a/include/kpl/tree.h b/include/kpl/tree.h
index de9492d..f3503d2 100644
--- a/include/kpl/tree.h
+++ b/include/kpl/tree.h
@@ -6,7 +6,6 @@
// tree_duplicates is a linked stack
#define KPL_TREE_HEADER(STRUCT_NAME) \
- KPL_OBJECT_HEADER(STRUCT_NAME); \
struct STRUCT_NAME *tree_left, *tree_right, *tree_duplicates; \
int32_t tree_height
diff --git a/include/kpl/type.h b/include/kpl/type.h
new file mode 100644
index 0000000..546a1ed
--- /dev/null
+++ b/include/kpl/type.h
@@ -0,0 +1,67 @@
+/*
+ * KPL Types
+ * A Type is a declaration of code and data
+ */
+
+#pragma once
+
+typedef enum : uint8_t {
+ KPL_TYPE_NAME_UNKOWN,
+ // VALUES
+ KPL_TYPE_NAME_BOOLEAN,
+ KPL_TYPE_NAME_INTEGER,
+ KPL_TYPE_NAME_U8,
+ KPL_TYPE_NAME_U16,
+ KPL_TYPE_NAME_U32,
+ KPL_TYPE_NAME_U64,
+ KPL_TYPE_NAME_I8,
+ KPL_TYPE_NAME_I16,
+ KPL_TYPE_NAME_I32,
+ KPL_TYPE_NAME_I64,
+ KPL_TYPE_NAME_FLOAT,
+ KPL_TYPE_NAME_F32,
+ KPL_TYPE_NAME_F64,
+ KPL_TYPE_NAME_CHAR,
+ // CONTAINERS
+ KPL_TYPE_NAME_STRING,
+ KPL_TYPE_NAME_ARRAY,
+ KPL_TYPE_NAME_TYPLE,
+ KPL_TYPE_NAME_TABLE,
+ KPL_TYPE_NAME_MAP,
+ KPL_TYPE_NAME_SET,
+ KPL_TYPE_NAME_UNION,
+ KPL_TYPE_NAME_ANY,
+ KPL_TYPE_NAME_NATIVE,
+ KPL_TYPE_NAME_ERROR,
+ // COROUTINES
+ KPL_TYPE_NAME_LAMBDA,
+ KPL_TYPE_NAME_GENERATOR,
+ KPL_TYPE_NAME_REGEXP,
+ // STORAGE
+ KPL_TYPE_NAME_MODULE,
+ KPL_TYPE_NAME_NAMESPACE
+} kpl_type_name;
+
+typedef enum : uint8_t {
+ KPL_TYPE_KIND_BASE
+} kpl_type_kind;
+
+typedef enum : uint32_t {
+ KPL_TYPE_FLAG_EMPTY = 0
+} kpl_type_flags;
+
+// type_kind is for struct polymorphism
+// type_specific meaning changes depending on type_name
+#define KPL_TYPE_HEADER(STRUCT_NAME) \
+ KPL_OBJECT_HEADER(STRUCT_NAME); \
+ kpl_type_name type_name; \
+ kpl_type_kind type_kind; \
+ uint16_t type_specific; \
+ uint32_t type_flags
+
+/*
+ * Type has no additional information
+ */
+typedef struct _kpl_type_base {
+ KPL_TYPE_HEADER(_kpl_type_base);
+} kpl_type_base;