NOT QUITE WORKING YET

This commit is contained in:
secXsQuared 2016-05-31 22:00:29 -07:00
parent 4410fb50fa
commit eb88af7d33
33 changed files with 251 additions and 229 deletions

View File

@ -20,4 +20,4 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
file(GLOB_RECURSE SOURCE_FILES ./x64/*.h ./x64/*.c)
add_executable(Workspace ${SOURCE_FILES} x64/src/c/common/lib/salloc/inc/salloc.h)
add_executable(Workspace ${SOURCE_FILES})

View File

@ -10,7 +10,7 @@
#include <stdarg.h>
#include "k_type.h"
#define SAPI __attribute__((sysv_abi))
#define KAPI __attribute__((sysv_abi))
#define UAPI __attribute__((sysv_abi))
#endif

View File

@ -7,4 +7,5 @@
#define _K_TYPE_H_
#include <stdint.h>
#include <stdbool.h>
#endif

View File

@ -6,19 +6,19 @@
#include <stdbool.h>
#include "avl_tree.h"
static inline int32_t SAPI _avl_tree_node_get_height(avl_tree_node_t *node)
static inline int32_t KAPI _avl_tree_node_get_height(avl_tree_node_t *node)
{
return node == NULL ? -1 : node->height;
}
static inline int32_t SAPI _avl_tree_node_get_balance_factor(avl_tree_node_t *node)
static inline int32_t KAPI _avl_tree_node_get_balance_factor(avl_tree_node_t *node)
{
if (node == NULL)
return 0;
return _avl_tree_node_get_height(node->left) - _avl_tree_node_get_height(node->right);
}
static avl_tree_node_t *SAPI _avl_tree_node_right_rotate(avl_tree_node_t *root)
static avl_tree_node_t *KAPI _avl_tree_node_right_rotate(avl_tree_node_t *root)
{
avl_tree_node_t *left_children = root->left;
//adjust parents first
@ -35,7 +35,7 @@ static avl_tree_node_t *SAPI _avl_tree_node_right_rotate(avl_tree_node_t *root)
return left_children;
}
static avl_tree_node_t *SAPI _avl_tree_node_left_rotate(avl_tree_node_t *root)
static avl_tree_node_t *KAPI _avl_tree_node_left_rotate(avl_tree_node_t *root)
{
avl_tree_node_t *right_children = root->right;
//adjust parents
@ -52,7 +52,7 @@ static avl_tree_node_t *SAPI _avl_tree_node_left_rotate(avl_tree_node_t *root)
return right_children;
}
static avl_tree_node_t *SAPI _avl_tree_node_balance(avl_tree_node_t *node)
static avl_tree_node_t *KAPI _avl_tree_node_balance(avl_tree_node_t *node)
{
const int32_t bf = _avl_tree_node_get_balance_factor(node);
@ -89,7 +89,7 @@ static avl_tree_node_t *SAPI _avl_tree_node_balance(avl_tree_node_t *node)
}
static avl_tree_node_t *SAPI _avl_tree_node_insert(avl_tree_node_t *root, avl_tree_node_t *node, int32_t(*compare)(avl_tree_node_t *, avl_tree_node_t *), avl_tree_node_t *parent)
static avl_tree_node_t *KAPI _avl_tree_node_insert(avl_tree_node_t *root, avl_tree_node_t *node, int32_t(*compare)(avl_tree_node_t *, avl_tree_node_t *), avl_tree_node_t *parent)
{
if (node == NULL || compare == NULL)
return root;
@ -234,7 +234,7 @@ static void _avl_tree_swap_nodes(avl_tree_node_t *node1, avl_tree_node_t *node2)
return;
}
static avl_tree_node_t *SAPI _avl_tree_node_delete(avl_tree_node_t *root, avl_tree_node_t *node,
static avl_tree_node_t *KAPI _avl_tree_node_delete(avl_tree_node_t *root, avl_tree_node_t *node,
int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
if (root == NULL || node == NULL || compare == NULL)
@ -282,7 +282,7 @@ static avl_tree_node_t *SAPI _avl_tree_node_delete(avl_tree_node_t *root, avl_tr
return root;
}
static avl_tree_node_t *SAPI _avl_tree_node_search(avl_tree_node_t *root, avl_tree_node_t *node,
static avl_tree_node_t *KAPI _avl_tree_node_search(avl_tree_node_t *root, avl_tree_node_t *node,
int32_t(*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
if(root == NULL || compare == NULL)
@ -296,7 +296,7 @@ static avl_tree_node_t *SAPI _avl_tree_node_search(avl_tree_node_t *root, avl_tr
return _avl_tree_node_search(root->left, node, compare);
}
static void SAPI _avl_tree_node_init(avl_tree_node_t * it)
static void KAPI _avl_tree_node_init(avl_tree_node_t * it)
{
if(it != NULL)
{
@ -309,7 +309,7 @@ static void SAPI _avl_tree_node_init(avl_tree_node_t * it)
}
avl_tree_node_t *SAPI avl_tree_smallest(avl_tree_t *tree)
avl_tree_node_t *KAPI avl_tree_smallest(avl_tree_t *tree)
{
if (tree == NULL)
return NULL;
@ -321,7 +321,7 @@ avl_tree_node_t *SAPI avl_tree_smallest(avl_tree_t *tree)
return entry;
}
avl_tree_node_t *SAPI avl_tree_largest(avl_tree_t *tree)
avl_tree_node_t *KAPI avl_tree_largest(avl_tree_t *tree)
{
if (tree == NULL)
return NULL;
@ -334,7 +334,7 @@ avl_tree_node_t *SAPI avl_tree_largest(avl_tree_t *tree)
}
avl_tree_node_t *SAPI avl_tree_larger(avl_tree_node_t *it)
avl_tree_node_t *KAPI avl_tree_larger(avl_tree_node_t *it)
{
if (it == NULL)
return NULL;
@ -358,7 +358,7 @@ avl_tree_node_t *SAPI avl_tree_larger(avl_tree_node_t *it)
}
}
avl_tree_node_t *SAPI avl_tree_smaller(avl_tree_node_t *it)
avl_tree_node_t *KAPI avl_tree_smaller(avl_tree_node_t *it)
{
if (it == NULL)
return NULL;
@ -382,13 +382,13 @@ avl_tree_node_t *SAPI avl_tree_smaller(avl_tree_node_t *it)
}
}
avl_tree_node_t * SAPI avl_tree_search(avl_tree_t *tree, avl_tree_node_t * node, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
avl_tree_node_t * KAPI avl_tree_search(avl_tree_t *tree, avl_tree_node_t * node, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
return _avl_tree_node_search(tree->root, node, compare);
}
void SAPI avl_tree_insert(avl_tree_t *tree, avl_tree_node_t* data, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
void KAPI avl_tree_insert(avl_tree_t *tree, avl_tree_node_t* data, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
if(tree != NULL && data != NULL)
{
@ -398,7 +398,7 @@ void SAPI avl_tree_insert(avl_tree_t *tree, avl_tree_node_t* data, int32_t (*com
return;
}
void SAPI avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *data, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
void KAPI avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *data, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
if(tree != NULL && data != NULL)
{
@ -407,7 +407,7 @@ void SAPI avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *data, int32_t (*com
return;
}
int32_t SAPI avl_tree_size(avl_tree_t *tree)
int32_t KAPI avl_tree_size(avl_tree_t *tree)
{
if(tree == NULL)
return -1;
@ -423,7 +423,7 @@ int32_t SAPI avl_tree_size(avl_tree_t *tree)
return size;
}
void SAPI avl_tree_init(avl_tree_t * tree)
void KAPI avl_tree_init(avl_tree_t * tree)
{
if(tree != NULL)
{
@ -436,14 +436,14 @@ void SAPI avl_tree_init(avl_tree_t * tree)
// TESTING STUFF
static int32_t SAPI _avl_tree_node_calculate_height(avl_tree_node_t *tree)
static int32_t KAPI _avl_tree_node_calculate_height(avl_tree_node_t *tree)
{
if (tree == NULL)
return -1;
return max_32(_avl_tree_node_calculate_height(tree->left), _avl_tree_node_calculate_height(tree->right)) + 1;
}
static bool SAPI _avl_tree_node_test(avl_tree_node_t *tree, int32_t (*compare)(avl_tree_node_t*, avl_tree_node_t*))
static bool KAPI _avl_tree_node_test(avl_tree_node_t *tree, int32_t (*compare)(avl_tree_node_t*, avl_tree_node_t*))
{
if (tree == NULL)
return true;
@ -467,7 +467,7 @@ static bool SAPI _avl_tree_node_test(avl_tree_node_t *tree, int32_t (*compare)(a
return _avl_tree_node_test(tree->left,compare) && _avl_tree_node_test(tree->right,compare);
}
bool SAPI avl_tree_validate(avl_tree_t *tree, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
bool KAPI avl_tree_validate(avl_tree_t *tree, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *))
{
if(tree == NULL)
return true;

View File

@ -22,27 +22,27 @@ typedef struct
} avl_tree_t;
avl_tree_node_t *SAPI avl_tree_search(avl_tree_t *tree, avl_tree_node_t *entry,
avl_tree_node_t *KAPI avl_tree_search(avl_tree_t *tree, avl_tree_node_t *entry,
int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *));
void SAPI avl_tree_insert(avl_tree_t *tree, avl_tree_node_t *entry,
void KAPI avl_tree_insert(avl_tree_t *tree, avl_tree_node_t *entry,
int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *));
void SAPI avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *entry,
void KAPI avl_tree_delete(avl_tree_t *tree, avl_tree_node_t *entry,
int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *));
void SAPI avl_tree_init(avl_tree_t *tree);
void KAPI avl_tree_init(avl_tree_t *tree);
avl_tree_node_t *SAPI avl_tree_largest(avl_tree_t *tree);
avl_tree_node_t *KAPI avl_tree_largest(avl_tree_t *tree);
avl_tree_node_t *SAPI avl_tree_smallest(avl_tree_t *tree);
avl_tree_node_t *KAPI avl_tree_smallest(avl_tree_t *tree);
avl_tree_node_t *SAPI avl_tree_larger(avl_tree_node_t *entry);
avl_tree_node_t *KAPI avl_tree_larger(avl_tree_node_t *entry);
avl_tree_node_t *SAPI avl_tree_smaller(avl_tree_node_t *entry);
avl_tree_node_t *KAPI avl_tree_smaller(avl_tree_node_t *entry);
bool SAPI avl_tree_validate(avl_tree_t *tree, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *));
bool KAPI avl_tree_validate(avl_tree_t *tree, int32_t (*compare)(avl_tree_node_t *, avl_tree_node_t *));
int32_t SAPI avl_tree_size(avl_tree_t *tree);
int32_t KAPI avl_tree_size(avl_tree_t *tree);
#endif

View File

@ -20,36 +20,36 @@ typedef struct _linked_list_t
linked_list_node_t *tail;
} linked_list_t;
void SAPI linked_list_init(linked_list_t *list);
void KAPI linked_list_init(linked_list_t *list);
int SAPI linked_list_size(linked_list_t *list);
int KAPI linked_list_size(linked_list_t *list);
void SAPI linked_list_push_front(linked_list_t *list, linked_list_node_t *node);
void KAPI linked_list_push_front(linked_list_t *list, linked_list_node_t *node);
void SAPI linked_list_push_back(linked_list_t *list, linked_list_node_t *node);
void KAPI linked_list_push_back(linked_list_t *list, linked_list_node_t *node);
linked_list_node_t *SAPI linked_list_pop_front(linked_list_t *list);
linked_list_node_t *KAPI linked_list_pop_front(linked_list_t *list);
linked_list_node_t *SAPI linked_list_pop_back(linked_list_t *list);
linked_list_node_t *KAPI linked_list_pop_back(linked_list_t *list);
void SAPI linked_list_insert_idx(linked_list_t *list, int32_t index, linked_list_node_t *node);
void KAPI linked_list_insert_idx(linked_list_t *list, int32_t index, linked_list_node_t *node);
void SAPI linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node);
void KAPI linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node);
linked_list_node_t *SAPI linked_list_remove_idx(linked_list_t *list, int32_t index);
linked_list_node_t *KAPI linked_list_remove_idx(linked_list_t *list, int32_t index);
linked_list_node_t *SAPI linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node);
linked_list_node_t *KAPI linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node);
linked_list_node_t *SAPI linked_list_get(linked_list_t *list, int32_t index);
linked_list_node_t *KAPI linked_list_get(linked_list_t *list, int32_t index);
linked_list_node_t *SAPI linked_list_next(linked_list_node_t *node);
linked_list_node_t *KAPI linked_list_next(linked_list_node_t *node);
linked_list_node_t *SAPI linked_list_prev(linked_list_node_t *node);
linked_list_node_t *KAPI linked_list_prev(linked_list_node_t *node);
linked_list_node_t *SAPI linked_list_first(linked_list_t *list);
linked_list_node_t *KAPI linked_list_first(linked_list_t *list);
linked_list_node_t *SAPI linked_list_last(linked_list_t *list);
linked_list_node_t *KAPI linked_list_last(linked_list_t *list);
int32_t SAPI linked_list_search(linked_list_t *list, linked_list_node_t* target, bool (*equals)(linked_list_node_t*, linked_list_node_t*));
int32_t KAPI linked_list_search(linked_list_t *list, linked_list_node_t* target, bool (*equals)(linked_list_node_t*, linked_list_node_t*));
#endif

View File

@ -5,7 +5,7 @@
#include "inc/linked_list.h"
static void SAPI _init_linked_list_node(linked_list_node_t *node)
static void KAPI _init_linked_list_node(linked_list_node_t *node)
{
if (node != NULL)
{
@ -15,7 +15,7 @@ static void SAPI _init_linked_list_node(linked_list_node_t *node)
return;
}
static void SAPI _append_node(linked_list_node_t *target, linked_list_node_t *node)
static void KAPI _append_node(linked_list_node_t *target, linked_list_node_t *node)
{
if(target == NULL || node == NULL)
return;
@ -38,7 +38,7 @@ static void SAPI _append_node(linked_list_node_t *target, linked_list_node_t *no
}
// link target with node, suppose target is in the current list
static void SAPI _prepend_node(linked_list_node_t *target, linked_list_node_t *node)
static void KAPI _prepend_node(linked_list_node_t *target, linked_list_node_t *node)
{
if(target == NULL || node == NULL)
return;
@ -60,7 +60,7 @@ static void SAPI _prepend_node(linked_list_node_t *target, linked_list_node_t *n
return;
}
static void SAPI _unlink_node(linked_list_node_t* node)
static void KAPI _unlink_node(linked_list_node_t* node)
{
if(node == NULL)
return;
@ -78,7 +78,7 @@ static void SAPI _unlink_node(linked_list_node_t* node)
return;
}
void SAPI linked_list_init(linked_list_t *list)
void KAPI linked_list_init(linked_list_t *list)
{
if (list != NULL)
{
@ -88,7 +88,7 @@ void SAPI linked_list_init(linked_list_t *list)
return;
}
int32_t SAPI linked_list_size(linked_list_t *list)
int32_t KAPI linked_list_size(linked_list_t *list)
{
if (list == NULL)
return -1;
@ -105,7 +105,7 @@ int32_t SAPI linked_list_size(linked_list_t *list)
return size;
}
void SAPI linked_list_push_front(linked_list_t *list, linked_list_node_t *node)
void KAPI linked_list_push_front(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
@ -117,7 +117,7 @@ void SAPI linked_list_push_front(linked_list_t *list, linked_list_node_t *node)
return;
}
void SAPI linked_list_push_back(linked_list_t *list, linked_list_node_t *node)
void KAPI linked_list_push_back(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
@ -129,14 +129,14 @@ void SAPI linked_list_push_back(linked_list_t *list, linked_list_node_t *node)
return;
}
linked_list_node_t *SAPI linked_list_pop_front(linked_list_t *list)
linked_list_node_t *KAPI linked_list_pop_front(linked_list_t *list)
{
if (list == NULL)
return NULL;
return linked_list_remove_ref(list, list->head);
}
linked_list_node_t *SAPI linked_list_pop_back(linked_list_t *list)
linked_list_node_t *KAPI linked_list_pop_back(linked_list_t *list)
{
if (list == NULL)
return NULL;
@ -145,7 +145,7 @@ linked_list_node_t *SAPI linked_list_pop_back(linked_list_t *list)
}
void SAPI linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node)
void KAPI linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_node, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return;
@ -180,7 +180,7 @@ void SAPI linked_list_insert_ref(linked_list_t *list, linked_list_node_t *prev_n
}
}
void SAPI linked_list_insert_idx(linked_list_t *list, int32_t index, linked_list_node_t *node)
void KAPI linked_list_insert_idx(linked_list_t *list, int32_t index, linked_list_node_t *node)
{
if (list == NULL || index < 0 || node == NULL)
return;
@ -202,7 +202,7 @@ void SAPI linked_list_insert_idx(linked_list_t *list, int32_t index, linked_list
return;
}
linked_list_node_t *SAPI linked_list_remove_idx(linked_list_t *list, int32_t index)
linked_list_node_t *KAPI linked_list_remove_idx(linked_list_t *list, int32_t index)
{
if (list == NULL || index < 0)
return NULL;
@ -214,7 +214,7 @@ linked_list_node_t *SAPI linked_list_remove_idx(linked_list_t *list, int32_t ind
return linked_list_remove_ref(list, cur_node);
}
linked_list_node_t *SAPI linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node)
linked_list_node_t *KAPI linked_list_remove_ref(linked_list_t *list, linked_list_node_t *node)
{
if (list == NULL || node == NULL)
return NULL;
@ -236,7 +236,7 @@ linked_list_node_t *SAPI linked_list_remove_ref(linked_list_t *list, linked_list
return node;
}
linked_list_node_t *SAPI linked_list_get(linked_list_t *list, int32_t index)
linked_list_node_t *KAPI linked_list_get(linked_list_t *list, int32_t index)
{
if (list == NULL || index < 0 || list->head == NULL)
return NULL;
@ -245,7 +245,7 @@ linked_list_node_t *SAPI linked_list_get(linked_list_t *list, int32_t index)
return cur_node;
}
linked_list_node_t *SAPI linked_list_next(linked_list_node_t *node)
linked_list_node_t *KAPI linked_list_next(linked_list_node_t *node)
{
if (node != NULL)
{
@ -254,7 +254,7 @@ linked_list_node_t *SAPI linked_list_next(linked_list_node_t *node)
return node;
}
linked_list_node_t *SAPI linked_list_prev(linked_list_node_t *node)
linked_list_node_t *KAPI linked_list_prev(linked_list_node_t *node)
{
if (node != NULL)
{
@ -263,7 +263,7 @@ linked_list_node_t *SAPI linked_list_prev(linked_list_node_t *node)
return node;
}
linked_list_node_t *SAPI linked_list_first(linked_list_t *list)
linked_list_node_t *KAPI linked_list_first(linked_list_t *list)
{
linked_list_node_t *result = NULL;
if (list != NULL)
@ -273,7 +273,7 @@ linked_list_node_t *SAPI linked_list_first(linked_list_t *list)
return result;
}
linked_list_node_t *SAPI linked_list_last(linked_list_t *list)
linked_list_node_t *KAPI linked_list_last(linked_list_t *list)
{
linked_list_node_t *result = NULL;
if (list != NULL)
@ -283,7 +283,7 @@ linked_list_node_t *SAPI linked_list_last(linked_list_t *list)
return result;
}
int32_t SAPI linked_list_search(linked_list_t *list, linked_list_node_t *target, bool (*equals)(linked_list_node_t *, linked_list_node_t *))
int32_t KAPI linked_list_search(linked_list_t *list, linked_list_node_t *target, bool (*equals)(linked_list_node_t *, linked_list_node_t *))
{
if(list == NULL || target == NULL)
return -1;

View File

@ -8,13 +8,13 @@
#include "k_def.h"
void SAPI salloc_init(void *base, uint32_t size);
void KAPI salloc_init(void *base, uint32_t size);
void* SAPI salloc(void *base, uint32_t size);
void* KAPI salloc(void *base, uint32_t size);
void SAPI sfree(void *base, void *ptr);
void KAPI sfree(void *base, void *ptr);
bool SAPI salloc_assert(void *base, uint32_t blk_size[], bool blk_free[], uint32_t size);
bool KAPI salloc_assert(void *base, uint32_t blk_size[], bool blk_free[], uint32_t size);
#endif

View File

@ -91,7 +91,7 @@ static void _salloc_join(void *base)
}
}
bool SAPI salloc_assert(void *base, uint32_t blk_size[], bool blk_free[], uint32_t size)
bool KAPI salloc_assert(void *base, uint32_t blk_size[], bool blk_free[], uint32_t size)
{
if (base == NULL || blk_free == NULL || blk_size == NULL)
return NULL;
@ -118,7 +118,7 @@ bool SAPI salloc_assert(void *base, uint32_t blk_size[], bool blk_free[], uint32
}
}
void SAPI salloc_init(void *base, uint32_t size)
void KAPI salloc_init(void *base, uint32_t size)
{
if (base != NULL && size >= sizeof(_salloc_header))
{
@ -130,7 +130,7 @@ void SAPI salloc_init(void *base, uint32_t size)
return;
}
void *SAPI salloc(void *base, uint32_t size)
void *KAPI salloc(void *base, uint32_t size)
{
void *result = NULL;
if (base != NULL && size != 0)
@ -185,7 +185,7 @@ void *SAPI salloc(void *base, uint32_t size)
return result;
}
void SAPI sfree(void *base, void *ptr)
void KAPI sfree(void *base, void *ptr)
{
if (base != NULL && ptr != NULL)
{

View File

@ -8,22 +8,22 @@
#include "k_type.h"
static inline uint64_t SAPI bit_mask_64(uint32_t bit)
static inline uint64_t KAPI bit_mask_64(uint32_t bit)
{
return (uint64_t)1 << bit;
}
static inline uint32_t SAPI bit_mask_32(uint32_t bit)
static inline uint32_t KAPI bit_mask_32(uint32_t bit)
{
return (uint32_t)1 << bit;
}
static inline uint64_t SAPI bit_field_mask_64(uint32_t low, uint32_t high)
static inline uint64_t KAPI bit_field_mask_64(uint32_t low, uint32_t high)
{
return ~(~(uint64_t)0 << high << 1) << low;
}
static inline uint32_t SAPI bit_field_mask_32(uint32_t low, uint32_t high)
static inline uint32_t KAPI bit_field_mask_32(uint32_t low, uint32_t high)
{
return ~(~(uint32_t)0 << high << 1) << low;
}

View File

@ -9,53 +9,53 @@
#include "k_def.h"
#include "k_type.h"
uint32_t SAPI rand( void );
uint32_t KAPI rand( void );
void SAPI srand(uint32_t _seed );
void KAPI srand(uint32_t _seed );
void SAPI mrand(uint32_t max);
void KAPI mrand(uint32_t max);
uint64_t SAPI str_len(char const *str);
uint64_t KAPI str_len(char const *str);
uint64_t SAPI str_cmp(char const *str1, char const *str2);
uint64_t KAPI str_cmp(char const *str1, char const *str2);
void SAPI mem_cpy(void *src, void *dst, uint64_t size);
void KAPI mem_cpy(void *src, void *dst, uint64_t size);
void SAPI mem_move(void *src, void *dst, uint64_t size);
void KAPI mem_move(void *src, void *dst, uint64_t size);
void SAPI mem_set(void *src, int8_t const val, uint64_t size);
void KAPI mem_set(void *src, int8_t const val, uint64_t size);
static inline uint64_t SAPI align_down(uint64_t val, uint64_t alignment)
static inline uint64_t KAPI align_down(uint64_t val, uint64_t alignment)
{
return (val / alignment) * alignment;
}
static inline uint64_t SAPI align_up(uint64_t val, uint64_t alignment)
static inline uint64_t KAPI align_up(uint64_t val, uint64_t alignment)
{
return ((((val) % (alignment)) == 0) ? (((val) / (alignment)) * (alignment)) : ((((val) / (alignment)) * (alignment)) + 1));
}
static inline uint64_t SAPI is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
static inline uint64_t KAPI is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
{
return ((x1 <= y2) && (y1 <= x2)) ? 1 : 0;
}
static inline int64_t SAPI max_64(int64_t a, int64_t b)
static inline int64_t KAPI max_64(int64_t a, int64_t b)
{
return (a) > (b) ? a : b;
}
static inline int64_t SAPI min_64(int64_t a, int64_t b)
static inline int64_t KAPI min_64(int64_t a, int64_t b)
{
return (a) < (b) ? a : b;
}
static inline int32_t SAPI max_32(int32_t a, int32_t b)
static inline int32_t KAPI max_32(int32_t a, int32_t b)
{
return (a) > (b) ? a : b;
}
static inline int32_t SAPI min_32(int32_t a, int32_t b)
static inline int32_t KAPI min_32(int32_t a, int32_t b)
{
return (a) < (b) ? a : b;
}

View File

@ -7,7 +7,7 @@
#include "k_def.h"
#include "std_lib.h"
void SAPI mem_cpy(void *src, void *dst, uint64_t size)
void KAPI mem_cpy(void *src, void *dst, uint64_t size)
{
if (src == NULL || dst == NULL)
return;
@ -18,7 +18,7 @@ void SAPI mem_cpy(void *src, void *dst, uint64_t size)
return;
}
void SAPI mem_set(void *src, int8_t const val, uint64_t size)
void KAPI mem_set(void *src, int8_t const val, uint64_t size)
{
if (src == NULL)
return;
@ -27,7 +27,7 @@ void SAPI mem_set(void *src, int8_t const val, uint64_t size)
return;
}
void SAPI mem_move(void *src, void *dst, uint64_t size)
void KAPI mem_move(void *src, void *dst, uint64_t size)
{
if (src == NULL || dst == NULL)
return;

View File

@ -8,18 +8,18 @@
static uint32_t seed = 1;
static uint32_t max = 16777215;
uint32_t SAPI rand( void )
uint32_t KAPI rand( void )
{
seed = seed * 1103512986 + 29865;
return (unsigned int)(seed / 65536) % (max+1);
}
void SAPI srand( uint32_t _seed )
void KAPI srand( uint32_t _seed )
{
seed = _seed;
}
void SAPI mrand(uint32_t _max)
void KAPI mrand(uint32_t _max)
{
max = _max;
}

View File

@ -5,7 +5,7 @@
#include "std_lib.h"
uint64_t SAPI str_len(char const *str)
uint64_t KAPI str_len(char const *str)
{
uint64_t length = 0;
if(str == NULL)
@ -18,7 +18,7 @@ uint64_t SAPI str_len(char const *str)
return length;
}
uint64_t SAPI str_cmp(char const *str1, char const *str2)
uint64_t KAPI str_cmp(char const *str1, char const *str2)
{
if(str1 == NULL || str2 == NULL)
return 0;

View File

@ -0,0 +1,19 @@
#include "hal_atomic.h"
void KAPI hal_spin_lock(uint64_t *lock)
{
if (lock != NULL)
{
while (hal_interlocked_exchange(lock, 1) == 1);
}
return;
}
void KAPI hal_spin_unlock(uint64_t *lock)
{
if (lock != NULL)
{
*lock = 0;
}
return;
}

View File

@ -3,15 +3,17 @@
* See COPYING under root for details
*/
#include "hal.h"
#include "hal_multiboot.h"
#include "hal_print.h"
#include "hal_mem.h"
#include "hal_io.h"
#include "hal_intr.h"
#include "hal_var.h"
#include "k_sys_info.h"
#include "std_lib.h"
boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
boot_info_t *KAPI hal_init(void *_m_info)
{
multiboot_info_t* m_info = (multiboot_info_t*) _m_info;
if (m_info == NULL)
return NULL;
@ -181,22 +183,3 @@ boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
return boot_info;
}
void SAPI hal_spin_lock(uint64_t *lock)
{
if (lock != NULL)
{
while (hal_interlocked_exchange(lock, 1) == 1)
{ };
}
return;
}
void SAPI hal_spin_unlock(uint64_t *lock)
{
if (lock != NULL)
{
*lock = 0;
}
return;
}

View File

@ -3,18 +3,11 @@
* See COPYING under root for details
*/
#include "hal_io.h"
#include "hal_intr.h"
#include "hal_print.h"
#include "hal_mem.h"
void hal_interrupt_handler_dummy(void)
{
hal_printf("Yep... That just happened, an interrupt...\n");
return;
}
void SAPI hal_write_gate(void *const gate,
void KAPI hal_write_gate(void *const gate,
uint64_t const offset,
uint32_t const selector,
uint32_t const attr)
@ -38,7 +31,7 @@ void SAPI hal_write_gate(void *const gate,
return;
}
void SAPI hal_set_interrupt_handler(uint64_t index,
void KAPI hal_set_interrupt_handler(uint64_t index,
void (*handler)(void))
{
hal_write_gate(g_idt + 16 * index, (uint64_t) handler, seg_selector(1, 0),
@ -46,7 +39,7 @@ void SAPI hal_set_interrupt_handler(uint64_t index,
return;
}
void SAPI hal_assert(int64_t expression,
void KAPI hal_assert(int64_t expression,
char *message)
{
if (!expression)

View File

@ -12,7 +12,7 @@
char kernel_heap[KERNEL_HEAP_SIZE];
void SAPI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t const attr)
void KAPI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t const attr)
{
if (base == NULL)
return;
@ -28,7 +28,7 @@ void SAPI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t c
return;
}
void SAPI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t const attr)
void KAPI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t const attr)
{
if (base == NULL)
return;
@ -44,7 +44,7 @@ void SAPI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t
return;
}
void SAPI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_t const attr)
void KAPI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_t const attr)
{
if (base == NULL)
return;
@ -60,7 +60,7 @@ void SAPI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_
return;
}
void SAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint64_t const attr)
void KAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint64_t const attr)
{
if (base == NULL)
return;
@ -76,7 +76,7 @@ void SAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint6
return;
}
void SAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
void KAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
uint64_t const attr)
{
if (gdt == NULL)
@ -95,18 +95,18 @@ void SAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uin
return;
}
void *SAPI halloc(uint32_t size)
void *KAPI halloc(uint32_t size)
{
return salloc(kernel_heap, size);
}
void SAPI hfree(void *ptr)
void KAPI hfree(void *ptr)
{
sfree(kernel_heap, ptr);
return;
}
void SAPI hal_alloc_init()
void KAPI hal_alloc_init()
{
salloc_init(kernel_heap, KERNEL_HEAP_SIZE);
return;

View File

@ -8,13 +8,13 @@
#include "hal_print.h"
#include "hal_var.h"
void SAPI _hal_print_scroll()
void KAPI _hal_print_scroll()
{
mem_move((void *) (0xb8000 + get_pos(1, 0) * 2), (void *) (0xb8000 + get_pos(0, 0) * 2), (80 * 24) * 2);
return;
}
void SAPI _hal_print_str(char const *str)
void KAPI _hal_print_str(char const *str)
{
if(str == NULL)
return;
@ -49,7 +49,7 @@ void SAPI _hal_print_str(char const *str)
return;
}
void SAPI _hal_print_uint(uint64_t number)
void KAPI _hal_print_uint(uint64_t number)
{
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
@ -68,7 +68,7 @@ void SAPI _hal_print_uint(uint64_t number)
return;
}
void SAPI _hal_print_int(int64_t number)
void KAPI _hal_print_int(int64_t number)
{
char arr[21]; // do not need to initialize
arr[20] = 0; //zero-terminated
@ -97,7 +97,7 @@ void SAPI _hal_print_int(int64_t number)
return;
}
void SAPI _hal_print_hex(uint64_t number, uint64_t capital)
void KAPI _hal_print_hex(uint64_t number, uint64_t capital)
{
char const lookup_table_cap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char const lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@ -119,14 +119,14 @@ void SAPI _hal_print_hex(uint64_t number, uint64_t capital)
return;
}
void SAPI hal_clear_screen(void)
void KAPI hal_clear_screen(void)
{
text_pos = 0; // reset text_pos
mem_set((void *) 0xb8000, 0, 25 * 80 * 2);
return;
}
void SAPI hal_printf(char const *format, ...)
void KAPI hal_printf(char const *format, ...)
{
va_list args;
va_start(args, format);

View File

@ -1,31 +0,0 @@
/* Copyright 2016 secXsQuared
* Distributed under GPL license
* See COPYING under root for details
*/
#ifndef _HAL_H_
#define _HAL_H_
#include "k_def.h"
#include "k_type.h"
#include "hal_multiboot.h"
#include "k_sys_info.h"
// concurrency
void SAPI hal_spin_lock(uint64_t * lock);
void SAPI hal_spin_unlock(uint64_t * lock);
// Atomically set *dst = val
// return: the previous value of *dst
extern uint64_t SAPI hal_interlocked_exchange(uint64_t* dst,
uint64_t val);
// loaded kernel addr
extern char kernel_start[];
extern char kernel_end[];
//hal
boot_info_t*SAPI hal_init(multiboot_info_t* m_info);
//debug
extern void SAPI BOCHS_MAGIC_BREAKPOINT();
#endif

View File

@ -0,0 +1,7 @@
#ifndef _HAL_ATOMIC_H_
#define _HAL_ATOMIC_H_
#include "k_def.h"
extern uint64_t KAPI hal_interlocked_exchange(uint64_t* dst,
uint64_t val);
#endif

View File

@ -17,20 +17,20 @@
#define GATE_TYPE_INTERRUPT (14ull << 8)
#define GATE_TYPE_TRAP (15ull << 8)
extern void SAPI hal_write_port(uint64_t port, int64_t data);
extern int64_t SAPI hal_read_port(uint64_t port);
void SAPI hal_interrupt_handler_dummy();
extern void KAPI hal_write_port(uint64_t port, int64_t data);
extern int64_t KAPI hal_read_port(uint64_t port);
void KAPI hal_interrupt_handler_dummy();
void SAPI hal_set_interrupt_handler(uint64_t index, void (*handler)());
extern void SAPI hal_enable_interrupt();
extern void SAPI hal_disable_interrupt();
extern void SAPI hal_interrupt_handler_wrapper();
extern void SAPI hal_halt_cpu();
void KAPI hal_set_interrupt_handler(uint64_t index, void (*handler)(void));
extern void KAPI hal_enable_interrupt();
extern void KAPI hal_disable_interrupt();
extern void KAPI hal_interrupt_handler_wrapper();
extern void KAPI hal_halt_cpu();
void SAPI hal_write_gate(void *const gate, uint64_t const offset, uint32_t const selector, uint32_t const attr);
void KAPI hal_write_gate(void *const gate, uint64_t const offset, uint32_t const selector, uint32_t const attr);
//assert
void SAPI hal_assert(int64_t exp, char* message);
void KAPI hal_assert(int64_t exp, char* message);
extern uint8_t g_idt[];
#endif

View File

@ -11,7 +11,7 @@
#include "linked_list.h"
static inline uint32_t SAPI seg_selector(uint32_t index, uint32_t rpl)
static inline uint32_t KAPI seg_selector(uint32_t index, uint32_t rpl)
{
return (index << 3) + rpl;
}
@ -98,32 +98,32 @@ typedef struct __attribute__((packed))
uint64_t edx;
} cpuid_t;
void* SAPI halloc(uint32_t size);
void* KAPI halloc(uint32_t size);
void SAPI hfree(void *ptr);
void KAPI hfree(void *ptr);
void SAPI hal_alloc_init();
void KAPI hal_alloc_init();
extern void SAPI hal_flush_gdt(gdt_ptr_t *gdt_ptr, uint64_t code_slct, uint64_t data_slct);
extern void KAPI hal_flush_gdt(gdt_ptr_t *gdt_ptr, uint64_t code_slct, uint64_t data_slct);
extern void SAPI hal_flush_tlb();
extern void KAPI hal_flush_tlb();
extern void SAPI hal_cpuid(uint64_t * eax, uint64_t * ebx, uint64_t* ecx, uint64_t* edx);
extern void KAPI hal_cpuid(uint64_t * eax, uint64_t * ebx, uint64_t* ecx, uint64_t* edx);
extern void SAPI hal_flush_idt(idt_ptr_t* idt_ptr);
extern void KAPI hal_flush_idt(idt_ptr_t* idt_ptr);
extern void SAPI hal_write_page_base(void* base);
extern void KAPI hal_write_page_base(void* base);
extern void*SAPI hal_read_page_base();
extern void*KAPI hal_read_page_base();
void SAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr);
void KAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit, uint64_t const attr);
void SAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint64_t const attr);
void KAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint64_t const attr);
void SAPI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_t const attr);
void KAPI hal_write_pdpt_entry(void *const base, uint64_t const pd_addr, uint64_t const attr);
void SAPI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t const attr);
void KAPI hal_write_pd_entry(void *const base, uint64_t const pt_addr, uint64_t const attr);
void SAPI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t const attr);
void KAPI hal_write_pt_entry(void *const base, uint64_t const p_addr, uint64_t const attr);
#endif

View File

@ -12,7 +12,7 @@
#define get_row(pos) (pos / 80)
#define get_pos(row,col) ((row) * 80 + (col))
void SAPI hal_clear_screen(void);
void SAPI hal_printf(char const *format, ...);
void KAPI hal_clear_screen(void);
void KAPI hal_printf(char const *format, ...);
#endif

View File

@ -0,0 +1,42 @@
/* Copyright 2016 secXsQuared
* Distributed under GPL license
* See COPYING under root for details
*/
#ifndef _K_HAL_H_
#define _K_HAL_H_
#include "k_def.h"
#include "k_type.h"
#include "k_sys_info.h"
// hal interrupt
extern void KAPI hal_enable_interrupt();
extern void KAPI hal_disable_interrupt();
extern void KAPI hal_mask_interrupt();
extern void KAPI hal_unmask_interrupt();
extern void KAPI hal_set_interrupt_handler(uint64_t index, void (*handler)(void));
// concurrency
extern void KAPI hal_spin_lock(uint64_t * lock);
extern void KAPI hal_spin_unlock(uint64_t * lock);
// loaded kernel addr
extern char kernel_start[];
extern char kernel_end[];
// hal init
extern boot_info_t* KAPI hal_init(void* m_info);
// hal output
extern void KAPI hal_printf(char const *format, ...);
extern void KAPI hal_clear_screen(void);
// hal CPU control
extern void KAPI hal_halt_cpu(void);
// hal memory
//debug
extern void KAPI BOCHS_MAGIC_BREAKPOINT();
#endif

View File

@ -0,0 +1,11 @@
#ifndef _K_INTR_H_
#define _K_INTR_H_
#include "k_type.h"
typedef uint64_t irql_t;
#define IRQL_DPC 1
#define IRQL_APC 2
#define IRQL_KERNEL 3
#define IRQL_USER 4
#endif

View File

@ -4,14 +4,11 @@
*/
#include "k_def.h"
#include "k_hal.h"
#include "std_lib.h"
#include "k_lib_test.h"
#include "hal.h"
#include "hal_print.h"
#include "hal_io.h"
void SAPI kmain(multiboot_info_t *multiboot_info)
void KAPI kmain(void *multiboot_info)
{
boot_info_t* boot_info = hal_init(multiboot_info);

View File

@ -5,7 +5,7 @@
#include "k_pmm.h"
#include "k_sys_info.h"
#include "hal_io.h"
#include "hal_intr.h"
#include "std_lib.h"
linked_list_t* occupied_mem;

View File

@ -1,10 +1,10 @@
#ifndef _LIB_TEST_H_
#define _LIB_TEST_H_
void SAPI linked_list_test(void);
void KAPI linked_list_test(void);
void SAPI avl_tree_test(void);
void KAPI avl_tree_test(void);
void SAPI salloc_test(void);
void KAPI salloc_test(void);
#endif

View File

@ -4,12 +4,12 @@
#include "k_type.h"
#include "k_def.h"
void SAPI test_begin(char *name);
void KAPI test_begin(char *name);
void SAPI test_end();
void KAPI test_end();
void *SAPI talloc(uint32_t size);
void *KAPI talloc(uint32_t size);
void SAPI run_case(char *name, bool result);
void KAPI run_case(char *name, bool result);
#endif

View File

@ -435,7 +435,7 @@ static bool search_test()
}
void SAPI linked_list_test(void)
void KAPI linked_list_test(void)
{
test_begin("Linked list test");
run_case("insert_test_beginning", insert_test_beginning());

View File

@ -256,7 +256,7 @@ static bool salloc_free_all()
}
void SAPI salloc_test()
void KAPI salloc_test()
{
test_begin("salloc test");

View File

@ -85,7 +85,7 @@ static void ginfo_push(char *case_name, bool success)
hal_printf("GINFO full, [%s] result not recorded.\n", r_case_name);
}
void SAPI test_begin(char *name)
void KAPI test_begin(char *name)
{
test_name = (name == NULL ? "Anonymous Test" : name);
for (int i = 0; i < GAT_SIZE; i++)
@ -98,7 +98,7 @@ void SAPI test_begin(char *name)
}
}
void SAPI test_end()
void KAPI test_end()
{
gat_free();
int32_t total = 0, failed = 0, success = 0;
@ -136,7 +136,7 @@ void SAPI test_end()
}
}
void SAPI *talloc(uint32_t size)
void KAPI *talloc(uint32_t size)
{
if (!gat_full())
{
@ -152,7 +152,7 @@ void SAPI *talloc(uint32_t size)
return NULL;
}
void SAPI run_case(char *name, bool result)
void KAPI run_case(char *name, bool result)
{
ginfo_push(name, result);
}