This commit is contained in:
secXsQuared 2018-10-28 22:11:43 -04:00
parent 807ac4de22
commit 6a32f03f20
12 changed files with 321 additions and 347 deletions

View File

@ -19,7 +19,7 @@ struct atree_node
*/
typedef int32 (*atree_cmp_fp)(struct atree_node *tree_node, struct atree_node *self);
struct atree
struct a_tree
{
atree_cmp_fp cmpf;
struct atree_node *root;
@ -27,39 +27,39 @@ struct atree
struct atree_node *
lb_atree_search(struct atree *tree, struct atree_node *entry);
lb_atree_search(struct a_tree *tree, struct atree_node *entry);
struct atree_node *
lb_atree_insert(struct atree *tree, struct atree_node *entry);
lb_atree_insert(struct a_tree *tree, struct atree_node *entry);
struct atree_node *
lb_atree_delete(struct atree *tree, struct atree_node *entry);
lb_atree_delete(struct a_tree *tree, struct atree_node *entry);
void
lb_atree_init(struct atree *tree, atree_cmp_fp compare);
lb_atree_init(struct a_tree *tree, atree_cmp_fp compare);
struct atree_node *
lb_atree_max(struct atree *tree);
lb_atree_max(struct a_tree *tree);
struct atree_node *
lb_atree_min(struct atree *tree);
lb_atree_min(struct a_tree *tree);
struct atree_node *
lb_atree_next(struct atree *tree, struct atree_node *entry);
lb_atree_next(struct a_tree *tree, struct atree_node *entry);
struct atree_node *
lb_atree_prev(struct atree *tree, struct atree_node *entry);
lb_atree_prev(struct a_tree *tree, struct atree_node *entry);
bool
lb_atree_validate(struct atree *tree);
lb_atree_validate(struct a_tree *tree);
uint32
lb_atree_size(struct atree *tree);
lb_atree_size(struct a_tree *tree);

45
kernel/inc/lb/dlist.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "cdef.h"
struct dlist_node
{
struct dlist_node *prev;
struct dlist_node *next;
};
struct dlist
{
struct dlist_node *head;
struct dlist_node *tail;
uint32 size;
};
void
lb_llist_init(struct dlist *list);
uint32
lb_llist_size(struct dlist *list);
void
lb_llist_insert(struct dlist *list, struct dlist_node *cur_node, struct dlist_node *new_node);
struct dlist_node *
lb_llist_remove(struct dlist *list, struct dlist_node *node);
struct dlist_node *
lb_llist_next(struct dlist_node *node);
struct dlist_node *
lb_llist_prev(struct dlist_node *node);
struct dlist_node *
lb_llist_first(struct dlist *list);
struct dlist_node *
lb_llist_last(struct dlist *list);

43
kernel/inc/lb/htable.h Normal file
View File

@ -0,0 +1,43 @@
#include "cdef.h"
#include "dlist.h"
struct htable_node;
typedef uint32 (*htable_cmp_fp)(struct htable_node* table_node, struct htable_node* self);
typedef uint32 (*htable_hash_fp)(struct htable_node* self);
struct htable_node
{
struct dlist_node list_node;
};
struct htable
{
uint32 bkts;
struct dlist *buf;
htable_cmp_fp cmp_fp;
htable_hash_fp hash_fp;
};
void
htable_init(struct htable* table, htable_cmp_fp cmp_fp, htable_hash_fp hash_fp, struct dlist *buf, uint32 bkts);
/**
* returns the overwritten object
* returns NULL if no duplicates are overwritten
*/
struct htable_node*
htable_insert(struct htable* table, struct htable_node* entry);
/**
* returns the removed node
* NULL if doesn't exist
*/
struct htable_node*
htable_remove(struct htable* table, struct htable_node* entry);
struct htable_node*
htable_contains(struct htable* table, struct htable_node* entry);

View File

@ -1,69 +0,0 @@
#pragma once
#include "cdef.h"
struct llist_node
{
struct llist_node *prev;
struct llist_node *next;
};
struct llist
{
struct llist_node *head;
struct llist_node *tail;
uint32 size;
};
void
lb_llist_init(struct llist *list);
uint32
lb_llist_size(struct llist *list);
void
lb_llist_push_front(struct llist *list, struct llist_node *node);
void
lb_llist_push_back(struct llist *list, struct llist_node *node);
struct llist_node *
lb_llist_pop_front(struct llist *list);
struct llist_node *
lb_llist_pop_back(struct llist *list);
void
lb_llist_insert_by_idx(struct llist *list, uint32 index, struct llist_node *node);
struct llist_node *
lb_llist_remove_by_idx(struct llist *list, uint32 index);
struct llist_node *
lb_llist_get(struct llist *list, uint32 index);
void
lb_llist_insert_by_ref(struct llist *list, struct llist_node *cur_node, struct llist_node *new_node);
struct llist_node *
lb_llist_remove_by_ref(struct llist *list, struct llist_node *node);
struct llist_node *
lb_llist_next(struct llist_node *node);
struct llist_node *
lb_llist_prev(struct llist_node *node);
struct llist_node *
lb_llist_first(struct llist *list);
struct llist_node *
lb_llist_last(struct llist *list);

0
kernel/inc/lb/sl_list.c Normal file
View File

View File

@ -336,21 +336,21 @@ atree_node_delete(struct atree_node *node, struct atree_node *entry, atree_cmp_f
struct atree_node *
lb_atree_min(struct atree *tree)
lb_atree_min(struct a_tree *tree)
{
return atree_node_min(tree->root);
}
struct atree_node *
lb_atree_max(struct atree *tree)
lb_atree_max(struct a_tree *tree)
{
return atree_node_max(tree->root);
}
struct atree_node *
lb_atree_next(struct atree *tree, struct atree_node *entry)
lb_atree_next(struct a_tree *tree, struct atree_node *entry)
{
struct atree_node *succ;
struct atree_node *node;
@ -390,7 +390,7 @@ lb_atree_next(struct atree *tree, struct atree_node *entry)
struct atree_node *
lb_atree_prev(struct atree *tree, struct atree_node *entry)
lb_atree_prev(struct a_tree *tree, struct atree_node *entry)
{
struct atree_node *prev;
struct atree_node *node;
@ -430,14 +430,14 @@ lb_atree_prev(struct atree *tree, struct atree_node *entry)
struct atree_node *
lb_atree_search(struct atree *tree, struct atree_node *entry)
lb_atree_search(struct a_tree *tree, struct atree_node *entry)
{
return atree_node_search(tree->root, entry, tree->cmpf, NULL);
}
struct atree_node *
lb_atree_insert(struct atree *tree, struct atree_node *entry)
lb_atree_insert(struct a_tree *tree, struct atree_node *entry)
{
struct atree_node *old;
@ -448,7 +448,7 @@ lb_atree_insert(struct atree *tree, struct atree_node *entry)
struct atree_node *
lb_atree_delete(struct atree *tree, struct atree_node *entry)
lb_atree_delete(struct a_tree *tree, struct atree_node *entry)
{
struct atree_node *node;
@ -459,7 +459,7 @@ lb_atree_delete(struct atree *tree, struct atree_node *entry)
uint32
lb_atree_size(struct atree *tree)
lb_atree_size(struct a_tree *tree)
{
uint32 size;
struct atree_node *node;
@ -479,7 +479,7 @@ lb_atree_size(struct atree *tree)
void
lb_atree_init(struct atree *tree, atree_cmp_fp compare)
lb_atree_init(struct a_tree *tree, atree_cmp_fp compare)
{
tree->cmpf = compare;
tree->root = NULL;
@ -538,7 +538,7 @@ atree_node_test(struct atree_node *tree, atree_cmp_fp compare)
bool
lb_atree_validate(struct atree *tree)
lb_atree_validate(struct a_tree *tree)
{
if (tree == NULL)
{

156
kernel/lb/dlist.c Normal file
View File

@ -0,0 +1,156 @@
#include "lb/dlist.h"
static void
llist_node_init(struct dlist_node *node)
{
node->next = NULL;
node->prev = NULL;
}
void
lb_llist_init(struct dlist *list)
{
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
uint32
lb_llist_size(struct dlist *list)
{
return list->size;
}
void
lb_llist_insert(struct dlist *list, struct dlist_node *cur_node, struct dlist_node *new_node)
{
struct dlist_node *left_node;
struct dlist_node *right_node;
if (list == NULL || new_node == NULL)
{
return;
}
llist_node_init(new_node);
/*
* adjust the current node
*/
if (cur_node == NULL)
{
new_node->next = list->head;
new_node->prev = NULL;
}
else
{
new_node->prev = cur_node;
new_node->next = cur_node->next;
}
/*
* assign left and treenode node
*/
if (cur_node == NULL)
{
left_node = NULL;
right_node = list->head == NULL ? NULL : list->head;
}
else
{
left_node = cur_node;
right_node = cur_node->next;
}
/*
* adjust left and treenode node accordingly
*/
if (left_node != NULL)
{
left_node->next = new_node;
}
else
{
list->head = new_node;
}
if (right_node != NULL)
{
right_node->prev = new_node;
}
else
{
list->tail = new_node;
}
list->size++;
}
/**
* returns the next node
*/
struct dlist_node *
lb_llist_remove(struct dlist *list, struct dlist_node *node)
{
struct dlist_node *ret;
/*
* Adjust the left and treenode node
*/
if (node->prev != NULL)
{
node->prev->next = node->next;
}
else
{
list->head = node->next;
}
if (node->next != NULL)
{
node->next->prev = node->prev;
}
else
{
list->tail = node->prev;
}
ret = node->next;
llist_node_init(node);
list->size--;
return ret;
}
struct dlist_node *
lb_llist_next(struct dlist_node *node)
{
return node->next;
}
struct dlist_node *
lb_llist_prev(struct dlist_node *node)
{
return node->prev;
}
struct dlist_node *
lb_llist_first(struct dlist *list)
{
return list->head;
}
struct dlist_node *
lb_llist_last(struct dlist *list)
{
return list->tail;
}

49
kernel/lb/htable.c Normal file
View File

@ -0,0 +1,49 @@
#include "clib.h"
#include "cdef.h"
#include "lb/htable.h"
#include "lb/dlist.h"
void
htable_init(struct htable *table, htable_cmp_fp cmp_fp, htable_hash_fp hash_fp, struct dlist *buf, uint32 bkts)
{
table->hash_fp = hash_fp;
table->cmp_fp = cmp_fp;
table->buf = buf;
table->bkts = bkts;
for (uint32 i = 0; i < bkts; i++)
{
lb_llist_init(&buf[i]);
}
}
struct htable_node *
htable_insert(struct htable *table, struct htable_node *entry)
{
uint32 hash = table->hash_fp(entry) % table->bkts;
struct dlist *hlist = &table->buf[hash];
struct htable_node *ret = NULL;
struct dlist_node *lnode = lb_llist_first(hlist);
while(lnode != NULL)
{
struct htable_node *each = OBTAIN_STRUCT_ADDR(lnode, struct htable_node, list_node);
if (table->cmp_fp(each, entry) == 0)
{
ret = each;
break;
}
lnode = lb_llist_next(lnode);
}
if(ret != NULL)
{
lb_llist_remove(hlist, &ret->list_node);
}
/*
* insert to the head
*/
lb_llist_insert(hlist, NULL, &entry->list_node);
return ret;
}

View File

@ -1,250 +0,0 @@
#include "lb/llist.h"
static void
llist_node_init(struct llist_node *node)
{
node->next = NULL;
node->prev = NULL;
}
void
lb_llist_init(struct llist *list)
{
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
uint32
lb_llist_size(struct llist *list)
{
return list->size;
}
void
lb_llist_push_front(struct llist *list, struct llist_node *node)
{
llist_node_init(node);
lb_llist_insert_by_ref(list, NULL, node);
}
void
lb_llist_push_back(struct llist *list, struct llist_node *node)
{
llist_node_init(node);
lb_llist_insert_by_ref(list, list->tail, node);
}
struct llist_node *
lb_llist_pop_front(struct llist *list)
{
struct llist_node *ret;
ret = list->head;
lb_llist_remove_by_ref(list, list->head);
return ret;
}
struct llist_node *
lb_llist_pop_back(struct llist *list)
{
struct llist_node *ret;
ret = list->tail;
lb_llist_remove_by_ref(list, list->tail);
return ret;
}
void
lb_llist_insert_by_ref(struct llist *list, struct llist_node *cur_node, struct llist_node *new_node)
{
struct llist_node *left_node;
struct llist_node *right_node;
if (list == NULL || new_node == NULL)
{
return;
}
llist_node_init(new_node);
/*
* adjust the current node
*/
if (cur_node == NULL)
{
new_node->next = list->head;
new_node->prev = NULL;
}
else
{
new_node->prev = cur_node;
new_node->next = cur_node->next;
}
/*
* assign left and treenode node
*/
if (cur_node == NULL)
{
left_node = NULL;
right_node = list->head == NULL ? NULL : list->head;
}
else
{
left_node = cur_node;
right_node = cur_node->next;
}
/*
* adjust left and treenode node accordingly
*/
if (left_node != NULL)
{
left_node->next = new_node;
}
else
{
list->head = new_node;
}
if (right_node != NULL)
{
right_node->prev = new_node;
}
else
{
list->tail = new_node;
}
list->size++;
}
void
lb_llist_insert_by_idx(struct llist *list, uint32 index, struct llist_node *node)
{
struct llist_node *prev_node;
prev_node = lb_llist_get(list, index - 1);
llist_node_init(node);
if (prev_node == NULL)
{
if (index == 0)
{
lb_llist_insert_by_ref(list, NULL, node);
}
}
else
{
lb_llist_insert_by_ref(list, prev_node, node);
}
}
struct llist_node *
lb_llist_remove_by_idx(struct llist *list, uint32 index)
{
struct llist_node *cur_node;
cur_node = lb_llist_get(list, index);
if (cur_node == NULL)
{
return NULL;
}
return lb_llist_remove_by_ref(list, cur_node);
}
/**
* returns the next node
*/
struct llist_node *
lb_llist_remove_by_ref(struct llist *list, struct llist_node *node)
{
struct llist_node *ret;
/*
* Adjust the left and treenode node
*/
if (node->prev != NULL)
{
node->prev->next = node->next;
}
else
{
list->head = node->next;
}
if (node->next != NULL)
{
node->next->prev = node->prev;
}
else
{
list->tail = node->prev;
}
ret = node->next;
llist_node_init(node);
list->size--;
return ret;
}
struct llist_node *
lb_llist_get(struct llist *list, uint32 index)
{
if (list->head == NULL)
{
return NULL;
}
struct llist_node *cur_node = list->head;
while (index-- && (cur_node = cur_node->next) != NULL)
{}
return cur_node;
}
struct llist_node *
lb_llist_next(struct llist_node *node)
{
return node->next;
}
struct llist_node *
lb_llist_prev(struct llist_node *node)
{
return node->prev;
}
struct llist_node *
lb_llist_first(struct llist *list)
{
return list->head;
}
struct llist_node *
lb_llist_last(struct llist *list)
{
return list->tail;
}

View File

@ -1,21 +1,21 @@
#include "mm/pmm.h"
#include "lb/atree.h"
#include "lb/llist.h"
#include "lb/dlist.h"
#include "ke/rww_lock.h"
#include "clib.h"
#include "ke/intr.h"
struct phys_page_desc
{
struct llist_node free_list_node;
struct dlist_node free_list_node;
struct atree_node tree_node;
uintptr base;
int32 attr;
};
static struct atree active_tree;
static struct llist free_list;
static struct a_tree active_tree;
static struct dlist free_list;
static struct rww_lock lock;
/*
@ -109,7 +109,7 @@ k_status mm_alloc_page(uintptr *out)
irql = ke_raise_irql(IRQL_HIGH);
ke_rww_w_lock(&lock);
struct llist_node *node = NULL;
struct dlist_node *node = NULL;
struct phys_page_desc *page_info = NULL;
node = lb_llist_pop_front(&free_list);
if (node != NULL)

View File

@ -7,7 +7,7 @@
#define K_IDENT_BASE (0x80000000ul)
static struct atree ident_tree;
static struct a_tree ident_tree;
static struct spin_lock ident_tree_lock;
static uint32 ident_base;

View File

@ -1,6 +1,6 @@
#include "test_main.h"
#include "test_case.h"
#include "lb/llist.h"
#include "lb/dlist.h"
#include "clib.h"
#include <stdio.h>