From 624c176ef3f9886e113e7eab69107af0b11a3c53 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 27 May 2025 04:28:56 +0200 Subject: [PATCH 1/3] ptr_sub prose clarification --- core/mem/mem.odin | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/core/mem/mem.odin b/core/mem/mem.odin index 5789309f7..2b4d39341 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -315,18 +315,38 @@ check_zero_ptr :: proc(ptr: rawptr, len: int) -> bool { Offset a given pointer by a given amount. This procedure offsets the pointer `ptr` to an object of type `T`, by the amount -of bytes specified by `offset*size_of(T)`, and returns the pointer `ptr`. +of bytes specified by `offset * size_of(T)`, and returns the pointer `ptr`. **Note**: Prefer to use multipointer types, if possible. */ ptr_offset :: intrinsics.ptr_offset /* -Offset a given pointer by a given amount backwards. +Subtract two pointers of the same type, and return the number of `T` between them. -This procedure offsets the pointer `ptr` to an object of type `T`, by the amount -of bytes specified by `offset*size_of(T)` in the negative direction, and -returns the pointer `ptr`. +This procedure subtracts pointer `b` from pointer `a`, both of type `^T`, +and returns an integer count of the `T` between them. + +**Inputs** +- `a`: A pointer to a type T +- `b`: A pointer to a type T + +**Returns** +- `b` - `a` in items of T + +Example: + + import "core:mem" + import "core:fmt" + + ptr_sub_example :: proc() { + arr: [2]int + print(mem.ptr_sub(&arr[1], &arr[0])) + } + +Output: + + 1 */ ptr_sub :: intrinsics.ptr_sub From c513f035adb1d2c5f07c427129f804a55d6d405b Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 27 May 2025 04:33:22 +0200 Subject: [PATCH 2/3] Fix example --- core/mem/mem.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mem/mem.odin b/core/mem/mem.odin index 2b4d39341..bddce02ba 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -341,7 +341,7 @@ Example: ptr_sub_example :: proc() { arr: [2]int - print(mem.ptr_sub(&arr[1], &arr[0])) + fmt.println(mem.ptr_sub(&arr[1], &arr[0])) } Output: From 0d55764aa785cc78010c2cef3c3d3a7eb793499a Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 27 May 2025 04:42:12 +0200 Subject: [PATCH 3/3] int --- core/mem/mem.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mem/mem.odin b/core/mem/mem.odin index bddce02ba..96ec1990a 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -332,7 +332,7 @@ and returns an integer count of the `T` between them. - `b`: A pointer to a type T **Returns** -- `b` - `a` in items of T +- `b` - `a` in items of T as an `int`. Example: