diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-11-27 09:10:03 +0000 |
|---|---|---|
| committer | gingerBill <gingerBill@users.noreply.github.com> | 2025-11-27 09:10:03 +0000 |
| commit | 53876907c61faaadf68ae8b8140ed55df3f691d3 (patch) | |
| tree | 5b68b4e4a1ce2d8da9ad82985a9c795f5a05d911 | |
| parent | 49634246c5853dceb770161bea3742b9171b7a0f (diff) | |
Handle `Allocator_Error` correctly in `core:math/big`
| -rw-r--r-- | core/math/big/common.odin | 12 | ||||
| -rw-r--r-- | core/math/big/internal.odin | 6 | ||||
| -rw-r--r-- | core/math/big/radix.odin | 6 |
3 files changed, 19 insertions, 5 deletions
diff --git a/core/math/big/common.odin b/core/math/big/common.odin index a1eb4a7fd..cebe25119 100644 --- a/core/math/big/common.odin +++ b/core/math/big/common.odin @@ -6,6 +6,7 @@ package math_big */ import "base:intrinsics" +import "base:runtime" /* TODO: Make the tunables runtime adjustable where practical. @@ -138,11 +139,12 @@ Flags :: bit_set[Flag; u8] /* Errors are a strict superset of runtime.Allocation_Error. */ -Error :: enum int { - Okay = 0, +Error :: enum byte { + None = 0, Out_Of_Memory = 1, Invalid_Pointer = 2, Invalid_Argument = 3, + Mode_Not_Implemented = 4, // Allocation Assignment_To_Immutable = 10, Max_Iterations_Reached = 11, @@ -160,11 +162,15 @@ Error :: enum int { Unimplemented = 127, } +#assert(intrinsics.type_is_superset_of(Error, runtime.Allocator_Error)) + + Error_String :: #sparse[Error]string{ - .Okay = "Okay", + .None = "None", .Out_Of_Memory = "Out of memory", .Invalid_Pointer = "Invalid pointer", .Invalid_Argument = "Invalid argument", + .Mode_Not_Implemented = "Allocation mode not implemented", .Assignment_To_Immutable = "Assignment to immutable", .Max_Iterations_Reached = "Max iterations reached", diff --git a/core/math/big/internal.odin b/core/math/big/internal.odin index b3e23da8c..723bb0b8d 100644 --- a/core/math/big/internal.odin +++ b/core/math/big/internal.odin @@ -2177,7 +2177,11 @@ internal_int_grow :: proc(a: ^Int, digits: int, allow_shrink := false, allocator If not yet initialized, initialize the `digit` backing with the allocator we were passed. */ if cap == 0 { - a.digit = make([dynamic]DIGIT, needed, allocator) + mem_err: mem.Allocator_Error + a.digit, mem_err = make([dynamic]DIGIT, needed, allocator) + if mem_err != nil { + return cast(Error)mem_err + } } else if cap < needed { /* `[dynamic]DIGIT` already knows what allocator was used for it, so resize will do the right thing. diff --git a/core/math/big/radix.odin b/core/math/big/radix.odin index 2bbe593a2..4583ab0f7 100644 --- a/core/math/big/radix.odin +++ b/core/math/big/radix.odin @@ -44,7 +44,11 @@ int_itoa_string :: proc(a: ^Int, radix := i8(10), zero_terminate := false, alloc /* Allocate the buffer we need. */ - buffer := make([]u8, size) + buffer, mem_err := make([]u8, size) + if mem_err != nil { + err = cast(Error)mem_err + return + } /* Write the digits out into the buffer. |