Handle Allocator_Error correctly in core:math/big

This commit is contained in:
gingerBill
2025-11-27 09:10:03 +00:00
parent 49634246c5
commit 53876907c6
3 changed files with 19 additions and 5 deletions

View File

@@ -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",

View File

@@ -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.

View File

@@ -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.