Skip to content

Commit 5387690

Browse files
committed
Handle Allocator_Error correctly in core:math/big
1 parent 4963424 commit 5387690

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

core/math/big/common.odin

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package math_big
66
*/
77

88
import "base:intrinsics"
9+
import "base:runtime"
910

1011
/*
1112
TODO: Make the tunables runtime adjustable where practical.
@@ -138,11 +139,12 @@ Flags :: bit_set[Flag; u8]
138139
/*
139140
Errors are a strict superset of runtime.Allocation_Error.
140141
*/
141-
Error :: enum int {
142-
Okay = 0,
142+
Error :: enum byte {
143+
None = 0,
143144
Out_Of_Memory = 1,
144145
Invalid_Pointer = 2,
145146
Invalid_Argument = 3,
147+
Mode_Not_Implemented = 4, // Allocation
146148

147149
Assignment_To_Immutable = 10,
148150
Max_Iterations_Reached = 11,
@@ -160,11 +162,15 @@ Error :: enum int {
160162
Unimplemented = 127,
161163
}
162164

165+
#assert(intrinsics.type_is_superset_of(Error, runtime.Allocator_Error))
166+
167+
163168
Error_String :: #sparse[Error]string{
164-
.Okay = "Okay",
169+
.None = "None",
165170
.Out_Of_Memory = "Out of memory",
166171
.Invalid_Pointer = "Invalid pointer",
167172
.Invalid_Argument = "Invalid argument",
173+
.Mode_Not_Implemented = "Allocation mode not implemented",
168174

169175
.Assignment_To_Immutable = "Assignment to immutable",
170176
.Max_Iterations_Reached = "Max iterations reached",

core/math/big/internal.odin

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,11 @@ internal_int_grow :: proc(a: ^Int, digits: int, allow_shrink := false, allocator
21772177
If not yet initialized, initialize the `digit` backing with the allocator we were passed.
21782178
*/
21792179
if cap == 0 {
2180-
a.digit = make([dynamic]DIGIT, needed, allocator)
2180+
mem_err: mem.Allocator_Error
2181+
a.digit, mem_err = make([dynamic]DIGIT, needed, allocator)
2182+
if mem_err != nil {
2183+
return cast(Error)mem_err
2184+
}
21812185
} else if cap < needed {
21822186
/*
21832187
`[dynamic]DIGIT` already knows what allocator was used for it, so resize will do the right thing.

core/math/big/radix.odin

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ int_itoa_string :: proc(a: ^Int, radix := i8(10), zero_terminate := false, alloc
4444
/*
4545
Allocate the buffer we need.
4646
*/
47-
buffer := make([]u8, size)
47+
buffer, mem_err := make([]u8, size)
48+
if mem_err != nil {
49+
err = cast(Error)mem_err
50+
return
51+
}
4852

4953
/*
5054
Write the digits out into the buffer.

0 commit comments

Comments
 (0)