2
0
Эх сурвалжийг харах

Merge branch 'master' into windows-llvm-13.0.0

gingerBill 2 жил өмнө
parent
commit
1b2618055b

+ 5 - 3
core/net/dns.odin

@@ -373,18 +373,20 @@ load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocato
 	defer delete(res)
 	resolv_str := string(res)
 
+	id_str := "nameserver"
+	id_len := len(id_str)
+
 	_name_servers := make([dynamic]Endpoint, 0, allocator)
 	for line in strings.split_lines_iterator(&resolv_str) {
 		if len(line) == 0 || line[0] == '#' {
 			continue
 		}
 
-		id_str := "nameserver"
-		if strings.compare(line[:len(id_str)], id_str) != 0 {
+		if len(line) < id_len || strings.compare(line[:id_len], id_str) != 0 {
 			continue
 		}
 
-		server_ip_str := strings.trim_left_space(line[len(id_str):])
+		server_ip_str := strings.trim_left_space(line[id_len:])
 		if len(server_ip_str) == 0 {
 			continue
 		}

+ 26 - 11
src/common_memory.cpp

@@ -1,3 +1,4 @@
+#include <malloc.h>
 
 gb_internal gb_inline void zero_size(void *ptr, isize len) {
 	memset(ptr, 0, len);
@@ -121,7 +122,6 @@ struct PlatformMemoryBlock {
 	PlatformMemoryBlock *prev, *next;
 };
 
-
 gb_global std::atomic<isize> global_platform_memory_total_usage;
 gb_global PlatformMemoryBlock global_platform_memory_block_sentinel;
 
@@ -177,12 +177,12 @@ gb_internal void platform_virtual_memory_protect(void *memory, isize size);
 			gb_printf_err("Total Usage: %lld bytes\n", cast(long long)global_platform_memory_total_usage);
 			GB_ASSERT_MSG(pmblock != nullptr, "Out of Virtual Memory, oh no...");
 		}
-		global_platform_memory_total_usage += total_size;
+		global_platform_memory_total_usage.fetch_add(total_size);
 		return pmblock;
 	}
 	gb_internal void platform_virtual_memory_free(PlatformMemoryBlock *block) {
 		isize size = block->total_size;
-		global_platform_memory_total_usage -= size;
+		global_platform_memory_total_usage.fetch_sub(size);
 		munmap(block, size);
 	}
 	gb_internal void platform_virtual_memory_protect(void *memory, isize size) {
@@ -396,6 +396,8 @@ gb_internal gbAllocator heap_allocator(void) {
 	return a;
 }
 
+gb_internal std::atomic<isize> total_heap_memory_allocated;
+
 
 gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) {
 	void *ptr = nullptr;
@@ -403,7 +405,6 @@ gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) {
 	gb_unused(old_size);
 
 
-
 // TODO(bill): Throughly test!
 	switch (type) {
 #if defined(GB_COMPILER_MSVC)
@@ -436,28 +437,34 @@ gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) {
 #elif defined(GB_SYSTEM_LINUX)
 	// TODO(bill): *nix version that's decent
 	case gbAllocation_Alloc: {
-		ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
+		isize total_size = (size + alignment - 1) & ~(alignment - 1);
+		total_heap_memory_allocated.fetch_add(total_size);
+		ptr = aligned_alloc(alignment, total_size);
 		gb_zero_size(ptr, size);
 	} break;
 
 	case gbAllocation_Free:
 		if (old_memory != nullptr) {
+			total_heap_memory_allocated.fetch_sub(malloc_usable_size(old_memory));
 			free(old_memory);
 		}
 		break;
 
-	case gbAllocation_Resize:
+	case gbAllocation_Resize: {
 		if (size == 0) {
 			if (old_memory != nullptr) {
+				total_heap_memory_allocated.fetch_sub(malloc_usable_size(old_memory));
 				free(old_memory);
 			}
 			break;
 		}
-		
+
 		alignment = gb_max(alignment, gb_align_of(max_align_t));
-		
+
 		if (old_memory == nullptr) {
-			ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
+			isize total_size = (size + alignment - 1) & ~(alignment - 1);
+			total_heap_memory_allocated.fetch_add(total_size);
+			ptr = aligned_alloc(alignment, total_size);
 			gb_zero_size(ptr, size);
 			break;
 		}
@@ -466,11 +473,19 @@ gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) {
 			break;
 		}
 
-		ptr = aligned_alloc(alignment, (size + alignment - 1) & ~(alignment - 1));
+		size_t actual_old_size = malloc_usable_size(old_memory);
+		if (size <= actual_old_size) {
+			ptr = old_memory;
+			break;
+		}
+
+		isize total_size = (size + alignment - 1) & ~(alignment - 1);
+		total_heap_memory_allocated.fetch_add(total_size);
+		ptr = aligned_alloc(alignment, total_size);
 		gb_memmove(ptr, old_memory, old_size);
 		free(old_memory);
 		gb_zero_size(cast(u8 *)ptr + old_size, gb_max(size-old_size, 0));
-		break;
+	} break;
 #else
 	// TODO(bill): *nix version that's decent
 	case gbAllocation_Alloc: {

+ 3 - 1
src/ptr_map.cpp

@@ -114,7 +114,9 @@ gb_internal MapIndex map__add_entry(PtrMap<K, V> *h, K key) {
 	PtrMapEntry<K, V> e = {};
 	e.key = key;
 	e.next = MAP_SENTINEL;
-	map__reserve_entries(h, h->count+1);
+	if (h->count+1 >= h->entries_capacity) {
+		map__reserve_entries(h, gb_max(h->entries_capacity*2, 4));
+	}
 	h->entries[h->count++] = e;
 	return cast(MapIndex)(h->count-1);
 }

+ 3 - 1
src/string_map.cpp

@@ -96,7 +96,9 @@ gb_internal MapIndex string_map__add_entry(StringMap<T> *h, u32 hash, String con
 	e.key = key;
 	e.hash = hash;
 	e.next = MAP_SENTINEL;
-	string_map__reserve_entries(h, h->count+1);
+	if (h->count+1 >= h->entries_capacity) {
+		string_map__reserve_entries(h, gb_max(h->entries_capacity*2, 4));
+	}
 	h->entries[h->count++] = e;
 	return cast(MapIndex)(h->count-1);
 }