Remove enum for favour of Go-style enumerations

This commit is contained in:
Ginger Bill
2016-12-19 14:03:59 +00:00
parent ac1566762b
commit 478d63424f
15 changed files with 266 additions and 899 deletions
+19 -37
View File
@@ -70,11 +70,6 @@ type Type_Info union {
Struct Type_Info_Record;
Union Type_Info_Record;
Raw_Union Type_Info_Record;
Enum struct #ordered {
base ^Type_Info;
values []i64;
names []string;
};
};
proc type_info_base(info ^Type_Info) -> ^Type_Info {
@@ -113,12 +108,13 @@ proc fmuladd64(a, b, c f64) -> f64 #foreign "llvm.fmuladd.f64"
type Allocator_Mode enum {
ALLOC,
FREE,
FREE_ALL,
RESIZE,
}
type Allocator_Mode int;
const (
ALLOCATOR_ALLOC Allocator_Mode = iota;
ALLOCATOR_FREE;
ALLOCATOR_FREE_ALL;
ALLOCATOR_RESIZE;
);
type Allocator_Proc proc(allocator_data rawptr, mode Allocator_Mode,
size, alignment int,
old_memory rawptr, old_size int, flags u64) -> rawptr;
@@ -162,20 +158,20 @@ proc alloc(size int) -> rawptr #inline { return alloc_align(size, DEFAULT_ALIGNM
proc alloc_align(size, alignment int) -> rawptr #inline {
__check_context();
var a = context.allocator;
return a.procedure(a.data, Allocator_Mode.ALLOC, size, alignment, nil, 0, 0);
return a.procedure(a.data, ALLOCATOR_ALLOC, size, alignment, nil, 0, 0);
}
proc free(ptr rawptr) #inline {
__check_context();
var a = context.allocator;
if ptr != nil {
a.procedure(a.data, Allocator_Mode.FREE, 0, 0, ptr, 0, 0);
a.procedure(a.data, ALLOCATOR_FREE, 0, 0, ptr, 0, 0);
}
}
proc free_all() #inline {
__check_context();
var a = context.allocator;
a.procedure(a.data, Allocator_Mode.FREE_ALL, 0, 0, nil, 0, 0);
a.procedure(a.data, ALLOCATOR_FREE_ALL, 0, 0, nil, 0, 0);
}
@@ -183,7 +179,7 @@ proc resize (ptr rawptr, old_size, new_size int) -> rawptr #inline { return
proc resize_align(ptr rawptr, old_size, new_size, alignment int) -> rawptr #inline {
__check_context();
var a = context.allocator;
return a.procedure(a.data, Allocator_Mode.RESIZE, new_size, alignment, ptr, old_size, 0);
return a.procedure(a.data, ALLOCATOR_RESIZE, new_size, alignment, ptr, old_size, 0);
}
@@ -216,10 +212,9 @@ proc default_resize_align(old_memory rawptr, old_size, new_size, alignment int)
proc default_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
size, alignment int,
old_memory rawptr, old_size int, flags u64) -> rawptr {
using Allocator_Mode;
when false {
match mode {
case ALLOC:
case ALLOCATOR_ALLOC:
var total_size = size + alignment + size_of(mem.AllocationHeader);
var ptr = os.heap_alloc(total_size);
var header = ptr as ^mem.AllocationHeader;
@@ -227,14 +222,14 @@ proc default_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
mem.allocation_header_fill(header, ptr, size);
return mem.zero(ptr, size);
case FREE:
case ALLOCATOR_FREE:
os.heap_free(mem.allocation_header(old_memory));
return nil;
case FREE_ALL:
case ALLOCATOR_FREE_ALL:
// NOTE(bill): Does nothing
case RESIZE:
case ALLOCATOR_RESIZE:
var total_size = size + alignment + size_of(mem.AllocationHeader);
var ptr = os.heap_resize(mem.allocation_header(old_memory), total_size);
var header = ptr as ^mem.AllocationHeader;
@@ -244,17 +239,17 @@ proc default_allocator_proc(allocator_data rawptr, mode Allocator_Mode,
}
} else {
match mode {
case ALLOC:
case ALLOCATOR_ALLOC:
return os.heap_alloc(size);
case FREE:
case ALLOCATOR_FREE:
os.heap_free(old_memory);
return nil;
case FREE_ALL:
case ALLOCATOR_FREE_ALL:
// NOTE(bill): Does nothing
case RESIZE:
case ALLOCATOR_RESIZE:
return os.heap_resize(old_memory, size);
}
}
@@ -332,17 +327,4 @@ proc __substring_expr_error(file string, line, column int, low, high int) {
__debug_trap();
}
proc __enum_to_string(info ^Type_Info, value i64) -> string {
match type ti : type_info_base(info) {
case Type_Info.Enum:
// TODO(bill): Search faster than linearly
for var i = 0; i < ti.values.count; i++ {
if ti.values[i] == value {
return ti.names[i];
}
}
}
return "";
}