diff --git a/core/net/dns.odin b/core/net/dns.odin index f5bf912bc..a0223e6ee 100644 --- a/core/net/dns.odin +++ b/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 } diff --git a/src/common_memory.cpp b/src/common_memory.cpp index f33fb0dff..d6a87529f 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -1,3 +1,4 @@ +#include 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 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 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: { diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index e353b2f97..fbde98693 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -114,7 +114,9 @@ gb_internal MapIndex map__add_entry(PtrMap *h, K key) { PtrMapEntry 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); } diff --git a/src/string_map.cpp b/src/string_map.cpp index bf1bbf6ca..f8b86a950 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -96,7 +96,9 @@ gb_internal MapIndex string_map__add_entry(StringMap *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); }