about summary refs log tree commit diff
path: root/src/rt/rust_builtin.cpp
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-06-16 17:06:24 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-06-16 17:07:02 -0700
commit40746fa447b78a27b649db41a6bfe63f6645d5eb (patch)
treec01cd7e7efdf0f29b30bbd75b48008c73fbd74cf /src/rt/rust_builtin.cpp
parente50c918e6b35c853813480b5c65c35813ceb6aa1 (diff)
downloadrust-40746fa447b78a27b649db41a6bfe63f6645d5eb.tar.gz
rust-40746fa447b78a27b649db41a6bfe63f6645d5eb.zip
rustc: Implement conversions from interior vector data to unsafe pointers and vice-versa
Diffstat (limited to 'src/rt/rust_builtin.cpp')
-rw-r--r--src/rt/rust_builtin.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 892cdc0cb0b..cc9b48cc014 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -616,6 +616,43 @@ ivec_on_heap(rust_task *task, type_desc *ty, rust_ivec *v)
     return !v->fill && v->payload.ptr;
 }
 
+/**
+ * Returns an unsafe pointer to the data part of an interior vector.
+ */
+extern "C" void *
+ivec_to_ptr(rust_task *task, type_desc *ty, rust_ivec *v)
+{
+    return v->fill ? v->payload.data : v->payload.ptr->data;
+}
+
+/**
+ * Copies elements in an unsafe buffer to the given interior vector. The
+ * vector must have size zero.
+ */
+extern "C" void
+ivec_copy_from_buf(rust_task *task, type_desc *ty, rust_ivec *v, void *ptr,
+                   size_t count)
+{
+    if (v->fill || (v->payload.ptr && v->payload.ptr->fill)) {
+        task->fail(1);
+        return;
+    }
+
+    ivec_reserve(task, ty, v, count);
+
+    size_t new_size = count * ty->size;
+    if (v->fill) {
+        // On stack.
+        memmove(v->payload.data, ptr, new_size);
+        v->fill = new_size;
+        return;
+    }
+
+    // On heap.
+    memmove(v->payload.ptr->data, ptr, new_size);
+    v->payload.ptr->fill = new_size;
+}
+
 
 //
 // Local Variables: