Skip to content

Commit

Permalink
Fix memset, memcpy, memmove calls on Pointer type.
Browse files Browse the repository at this point in the history
Just note that it's short fix for #4589 only.
  • Loading branch information
akzhan committed Jun 23, 2017
1 parent a431a57 commit 73c3c4b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/pointer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ struct Pointer(T)
(self + offset).value = value
end

{% if flag?(:x86_64) %}
private def to_size_t(size : Int) : UInt64
size.to_u64
end
{% else %}
private def to_size_t(size : Int) : UInt32
size.to_u32
end
{% end %}

# Copies *count* elements from *source* into `self`.
# If *source* and `self` overlap, behaviour is undefined.
# Use `#move_from` if they overlap (slower but always works).
Expand Down Expand Up @@ -242,7 +252,7 @@ struct Pointer(T)
raise ArgumentError.new("Negative count") if count < 0

if self.class == source.class
Intrinsics.memcpy(self.as(Void*), source.as(Void*), (count * sizeof(T)).to_u32, 0_u32, false)
Intrinsics.memcpy(self.as(Void*), source.as(Void*), to_size_t(count * sizeof(T)), 0_u32, false)
else
while (count -= 1) >= 0
self[count] = source[count]
Expand All @@ -255,7 +265,7 @@ struct Pointer(T)
raise ArgumentError.new("Negative count") if count < 0

if self.class == source.class
Intrinsics.memmove(self.as(Void*), source.as(Void*), (count * sizeof(T)).to_u32, 0_u32, false)
Intrinsics.memmove(self.as(Void*), source.as(Void*), to_size_t(count * sizeof(T)), 0_u32, false)
else
if source.address < address
copy_from source, count
Expand Down Expand Up @@ -496,7 +506,7 @@ struct Pointer(T)
# ```
def clear(count = 1)
ptr = self.as(Pointer(Void))
Intrinsics.memset(self.as(Void*), 0_u8, (count * sizeof(T)).to_u32, 0_u32, false)
Intrinsics.memset(self.as(Void*), 0_u8, to_size_t(count * sizeof(T)), 0_u32, false)
end

def clone
Expand Down

0 comments on commit 73c3c4b

Please sign in to comment.