2016-08-27 17:48:10 +00:00
|
|
|
/*
|
2017-03-18 21:07:25 +00:00
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 20.08.2016
|
|
|
|
*
|
|
|
|
* File tracking shared.
|
|
|
|
*
|
|
|
|
*/
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2017-07-18 22:34:57 +00:00
|
|
|
struct File_Index{
|
2017-03-18 21:07:25 +00:00
|
|
|
u32 id[4];
|
2017-07-18 22:34:57 +00:00
|
|
|
};
|
2016-08-27 17:48:10 +00:00
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
typedef u32 rptr32;
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
#define to_ptr(b,p) ((void*)((char*)b + p))
|
|
|
|
#define to_rptr32(b,p) ((rptr32)((char*)(p) - (char*)(b)))
|
|
|
|
|
2017-07-18 22:34:57 +00:00
|
|
|
struct File_Track_Entry{
|
2016-08-27 17:48:10 +00:00
|
|
|
File_Index hash;
|
2017-03-18 21:07:25 +00:00
|
|
|
u32 opaque[4];
|
2017-07-18 22:34:57 +00:00
|
|
|
};
|
2017-03-18 21:07:25 +00:00
|
|
|
global_const File_Track_Entry null_file_track_entry = {0};
|
2016-08-27 17:48:10 +00:00
|
|
|
|
2017-07-18 22:34:57 +00:00
|
|
|
struct File_Track_Tables{
|
2017-03-23 19:16:39 +00:00
|
|
|
i32 size;
|
2017-03-18 21:07:25 +00:00
|
|
|
u32 tracked_count;
|
|
|
|
u32 max;
|
2016-08-27 17:48:10 +00:00
|
|
|
rptr32 file_table;
|
2017-07-18 22:34:57 +00:00
|
|
|
};
|
2017-03-23 19:16:39 +00:00
|
|
|
|
2017-07-18 22:34:57 +00:00
|
|
|
struct DLL_Node {
|
|
|
|
DLL_Node *next;
|
|
|
|
DLL_Node *prev;
|
|
|
|
};
|
2017-03-23 19:16:39 +00:00
|
|
|
|
|
|
|
internal File_Index
|
|
|
|
zero_file_index(){
|
|
|
|
File_Index a = {0};
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal i32
|
2016-08-27 17:48:10 +00:00
|
|
|
file_hash_is_zero(File_Index a){
|
|
|
|
return ((a.id[0] == 0) &&
|
|
|
|
(a.id[1] == 0) &&
|
|
|
|
(a.id[2] == 0) &&
|
|
|
|
(a.id[3] == 0));
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal i32
|
2016-08-27 17:48:10 +00:00
|
|
|
file_hash_is_deleted(File_Index a){
|
|
|
|
return ((a.id[0] == 0xFFFFFFFF) &&
|
|
|
|
(a.id[1] == 0xFFFFFFFF) &&
|
|
|
|
(a.id[2] == 0xFFFFFFFF) &&
|
|
|
|
(a.id[3] == 0xFFFFFFFF));
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal i32
|
2016-08-27 17:48:10 +00:00
|
|
|
file_index_eq(File_Index a, File_Index b){
|
|
|
|
return ((a.id[0] == b.id[0]) &&
|
|
|
|
(a.id[1] == b.id[1]) &&
|
|
|
|
(a.id[2] == b.id[2]) &&
|
|
|
|
(a.id[3] == b.id[3]));
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal void
|
2016-08-27 17:48:10 +00:00
|
|
|
insert_node(DLL_Node *pos, DLL_Node *node){
|
|
|
|
node->prev = pos;
|
|
|
|
node->next = pos->next;
|
|
|
|
pos->next = node;
|
|
|
|
node->next->prev = node;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal void
|
2016-08-27 17:48:10 +00:00
|
|
|
remove_node(DLL_Node *node){
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
node->prev->next = node->next;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal void
|
2016-08-27 17:48:10 +00:00
|
|
|
init_sentinel_node(DLL_Node *node){
|
|
|
|
node->next = node;
|
|
|
|
node->prev = node;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal DLL_Node*
|
2016-08-27 17:48:10 +00:00
|
|
|
allocate_node(DLL_Node *sentinel){
|
|
|
|
DLL_Node *result = 0;
|
|
|
|
if (sentinel->next != sentinel){
|
|
|
|
result = sentinel->next;
|
|
|
|
remove_node(result);
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FILE_ENTRY_COST (sizeof(File_Track_Entry))
|
|
|
|
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal i32
|
|
|
|
tracking_system_has_space(File_Track_Tables *tables, i32 new_count){
|
|
|
|
u32 count = tables->tracked_count;
|
|
|
|
u32 max = tables->max;
|
|
|
|
i32 result = ((count + new_count)*8 < max*7);
|
2016-08-27 17:48:10 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal i32
|
2016-08-27 17:48:10 +00:00
|
|
|
entry_is_available(File_Track_Entry *entry){
|
2017-03-18 21:07:25 +00:00
|
|
|
i32 result = 0;
|
2016-08-27 17:48:10 +00:00
|
|
|
if (entry){
|
|
|
|
result =
|
|
|
|
file_hash_is_zero(entry->hash) ||
|
|
|
|
file_hash_is_deleted(entry->hash);
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal File_Track_Entry*
|
2016-08-27 17:48:10 +00:00
|
|
|
tracking_system_lookup_entry(File_Track_Tables *tables, File_Index key){
|
2017-03-18 21:07:25 +00:00
|
|
|
u32 hash = key.id[0];
|
|
|
|
u32 max = tables->max;
|
|
|
|
u32 index = (hash) % max;
|
|
|
|
u32 start = index;
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
File_Track_Entry *entries = (File_Track_Entry*)to_ptr(tables, tables->file_table);
|
|
|
|
|
|
|
|
File_Track_Entry* result = 0;
|
|
|
|
for (;;){
|
|
|
|
File_Track_Entry *entry = entries + index;
|
|
|
|
|
|
|
|
if (file_index_eq(entry->hash, key)){
|
|
|
|
result = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (file_hash_is_zero(entry->hash)){
|
|
|
|
if (result == 0){
|
|
|
|
result = entry;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (file_hash_is_deleted(entry->hash)){
|
|
|
|
if (result == 0){
|
|
|
|
result = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++index;
|
|
|
|
if (index == max) index = 0;
|
|
|
|
if (index == start) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal File_Track_Entry*
|
2016-08-27 17:48:10 +00:00
|
|
|
get_file_entry(File_Track_Tables *tables, File_Index index){
|
|
|
|
File_Track_Entry *entry = 0;
|
|
|
|
|
|
|
|
File_Track_Entry *result = tracking_system_lookup_entry(tables, index);
|
|
|
|
if (result && file_index_eq(index, result->hash)){
|
|
|
|
entry = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(entry);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal void
|
2016-08-27 17:48:10 +00:00
|
|
|
internal_free_slot(File_Track_Tables *tables, File_Track_Entry *entry){
|
|
|
|
Assert(!entry_is_available(entry));
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
*entry = null_file_track_entry;
|
2016-08-27 17:48:10 +00:00
|
|
|
entry->hash.id[0] = 0xFFFFFFFF;
|
|
|
|
entry->hash.id[1] = 0xFFFFFFFF;
|
|
|
|
entry->hash.id[2] = 0xFFFFFFFF;
|
|
|
|
entry->hash.id[3] = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
--tables->tracked_count;
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:16:39 +00:00
|
|
|
internal i32
|
|
|
|
enough_memory_to_init_table(i32 table_memory_size){
|
|
|
|
i32 result = (sizeof(File_Track_Tables) + FILE_ENTRY_COST*8 <= table_memory_size);
|
2016-08-27 17:48:10 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal void
|
2017-03-23 19:16:39 +00:00
|
|
|
init_table_memory(File_Track_Tables *tables, i32 table_memory_size){
|
2016-08-27 17:48:10 +00:00
|
|
|
tables->size = table_memory_size;
|
|
|
|
tables->tracked_count = 0;
|
|
|
|
|
2017-03-23 19:16:39 +00:00
|
|
|
i32 max_number_of_entries = (table_memory_size - sizeof(*tables)) / FILE_ENTRY_COST;
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
tables->file_table = sizeof(*tables);
|
|
|
|
tables->max = max_number_of_entries;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
internal File_Track_Result
|
2016-08-27 17:48:10 +00:00
|
|
|
move_table_memory(File_Track_Tables *original_tables,
|
2017-03-23 19:16:39 +00:00
|
|
|
void *mem, i32 size){
|
2016-08-27 17:48:10 +00:00
|
|
|
File_Track_Result result = FileTrack_Good;
|
|
|
|
|
|
|
|
if (original_tables->size < size){
|
|
|
|
File_Track_Tables *tables = (File_Track_Tables*)mem;
|
|
|
|
|
|
|
|
// NOTE(allen): Initialize main data tables
|
|
|
|
{
|
|
|
|
tables->size = size;
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
i32 likely_entry_size = FILE_ENTRY_COST;
|
2017-03-23 19:16:39 +00:00
|
|
|
i32 max_number_of_entries = (size - sizeof(*tables)) / likely_entry_size;
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
tables->file_table = sizeof(*tables);
|
|
|
|
tables->max = max_number_of_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tables->max > original_tables->max){
|
2017-03-18 21:07:25 +00:00
|
|
|
u32 original_max = original_tables->max;
|
2016-08-27 17:48:10 +00:00
|
|
|
|
|
|
|
// NOTE(allen): Rehash the tracking table
|
|
|
|
{
|
|
|
|
File_Track_Entry *entries = (File_Track_Entry*)
|
|
|
|
to_ptr(original_tables, original_tables->file_table);
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
for (u32 index = 0; index < original_max; ++index){
|
2016-08-27 17:48:10 +00:00
|
|
|
File_Track_Entry *entry = entries + index;
|
|
|
|
if (!entry_is_available(entry)){
|
|
|
|
File_Index hash = entry->hash;
|
|
|
|
File_Track_Entry *lookup =
|
|
|
|
tracking_system_lookup_entry(tables, hash);
|
|
|
|
|
|
|
|
Assert(entry_is_available(lookup));
|
|
|
|
*lookup = *entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tables->tracked_count = original_tables->tracked_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
result = FileTrack_MemoryTooSmall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
result = FileTrack_MemoryTooSmall;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
// BOTTOM
|
|
|
|
|