1
0
Fork 0

Compare commits

...

5 Commits

9 changed files with 257 additions and 1 deletions

View File

@ -6,6 +6,6 @@ if not exist ".\build" mkdir ".\build"
pushd .\build
cl /Zi ..\src\main.c /Feapp.exe /Od /nologo /I..\src
cl /Zi ..\src\main.c /Feapp.exe /Od /nologo
popd .\build

40
src/arena.c Normal file
View File

@ -0,0 +1,40 @@
internal Arena *
arena_initialize(u64 size)
{
Arena *result = 0;
void *backing_buffer = malloc(size);
if (backing_buffer != NULL)
{
result = (Arena*) backing_buffer;
result->buffer = backing_buffer;
result->offset = sizeof(Arena);
result->size = sizeof(Arena) + size;
}
return result;
}
internal void *
arena_allocate(Arena *arena, u64 size)
{
void *result = 0;
if (arena->offset + size <= arena->size)
{
result = (void*)((u64) arena->buffer + (u64) arena->offset);
arena->offset += size;
memset(result, 0, size);
}
return result;
}
internal void
arena_release(Arena *arena)
{
arena->offset = 0;
}

31
src/arena.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef ARENA_H
#define ARENA_H
typedef struct Arena Arena;
struct Arena {
u64 *buffer;
u64 offset;
u64 size;
};
internal Arena * arena_initialize(u64 size);
internal void * arena_allocate(Arena *arena, u64 size);
internal void arena_release(Arena *arena);
//= rhjr: arena helpers
#define arena_push_array(arena, type, count) \
(type*) arena_allocate((arena), sizeof(type) * (count))
#define arena_push(arena, type) \
(type*) arena_allocate((arena), sizeof(type))
//= rhjr: memory helpers
#define memory_zero(s,z) memset((s), 0, (z))
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))
#define memory_copy(dst, src, length) memmove((dst), (src), (length))
#endif // ARENA_H

29
src/base.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef BASE_H
#define BASE_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define internal static
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
#define KB(b) ((b) << 10)
#define MB(b) ((b) << 20)
#define STATEMENT(S) do{ S } while(0)
#define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } )
#define MIN(A,B) (((A)<(B))?(A):(B))
#endif // BASE_H

59
src/hash-store.c Normal file
View File

@ -0,0 +1,59 @@
internal Table
hash_store_initialize(void)
{
Table result = {0};
result.ptr_arena = arena_initialize(KB(1));
result.str8_arena = arena_initialize(KB(1));
result.str8_ptr = arena_push(result.ptr_arena, String8);
result.str8_count = 1;
result.str8_ptr[0] = str8_lit("");
return result;
}
internal Key
hash_store_string8_to_key(Table *table, String8 value)
{
Key result = 0;
u32 count = table->str8_count;
result = count;
String8 *str8_ptr = table->str8_ptr;
for (u32 index = 0; index < count; index += 1, str8_ptr += 1)
{
if (string8_match(*str8_ptr, value))
{
result = index;
break;
}
}
if (result == count)
{
String8 *new_str8 = arena_push(table->ptr_arena, String8);
new_str8->content =
(u8*) arena_allocate(table->str8_arena, sizeof(u8) * value.length);
new_str8->length = value.length;
memory_copy(new_str8->content, value.content, value.length);
table->str8_count += 1;
}
return result;
}
internal String8
hash_store_string8_from_key(Table *table, Key index)
{
String8 result = {0};
if (index < table->str8_count)
{
result = table->str8_ptr[index];
}
return result;
}

21
src/hash-store.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
typedef u32 Key;
typedef struct Table Table;
struct Table
{
Arena *ptr_arena;
Arena *str8_arena;
String8 *str8_ptr;
u32 str8_count;
};
internal Table hash_store_initialize(void);
internal String8 hash_store_string8_from_key(Table *table, Key index);
internal Key hash_store_string8_to_key(Table *table, String8 value);
#endif

View File

@ -1,5 +1,32 @@
#include "base.h"
#include "arena.h"
#include "string.h"
#include "hash-store.h"
#include "arena.c"
#include "string.c"
#include "hash-store.c"
int
main(void)
{
Table store = hash_store_initialize();
Key a = hash_store_string8_to_key(&store, str8_lit("Hello"));
Key b = hash_store_string8_to_key(&store, str8_lit("Beautiful"));
Key c = hash_store_string8_to_key(&store, str8_lit("World"));
String8 str_a = hash_store_string8_from_key(&store, a);
String8 str_b = hash_store_string8_from_key(&store, b);
String8 str_c = hash_store_string8_from_key(&store, c);
ASSERT(hash_store_string8_to_key(&store, str8_lit("Hello")) == a);
ASSERT(hash_store_string8_to_key(&store, str8_lit("Beautiful")) == b);
ASSERT(hash_store_string8_to_key(&store, str8_lit("World")) == c);
ASSERT(string8_match(hash_store_string8_from_key(&store, a), str8_lit("Hello")));
ASSERT(string8_match(hash_store_string8_from_key(&store, b), str8_lit("Beautiful")));
ASSERT(string8_match(hash_store_string8_from_key(&store, c), str8_lit("World")));
return 0;
};

32
src/string.c Normal file
View File

@ -0,0 +1,32 @@
internal String8
str8_init(u8 *cstring, u64 length)
{
String8 result = { cstring, length };
return result;
}
internal u8
string8_match(String8 a, String8 b)
{
u8 result = 0;
if (a.length == b.length)
{
u64 size = MIN(a.length, b.length);
result = 1;
for (u64 i = 0; i < size; i++ )
{
u8 at = a.content[i];
i8 bt = b.content[i];
if (at != bt)
{
result = 0;
break;
}
}
}
return result;
}

17
src/string.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef STRING_H
#define STRING_H
typedef struct String8 String8;
struct String8
{
u8 *content;
u64 length;
};
internal String8 str8_init(u8 *cstring, u64 length);
#define str8_lit(cstring) \
str8_init((u8*)(cstring), sizeof(cstring) - 1)
internal u8 string8_match(String8 a, String8 b);
#endif // STRING_H