summaryrefslogtreecommitdiffstats
path: root/buf.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-04-26 05:57:37 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-04-26 05:57:37 -0700
commitbe83bc717db0375e19431fe38c0490ff1a9e40ec (patch)
tree354d2107e8a5d4b518b5081a036894b87fc82c68 /buf.c
parentd081a595dfa053c78f7e3f580f82ca7823f02f74 (diff)
downloadtxr-be83bc717db0375e19431fe38c0490ff1a9e40ec.tar.gz
txr-be83bc717db0375e19431fe38c0490ff1a9e40ec.tar.bz2
txr-be83bc717db0375e19431fe38c0490ff1a9e40ec.zip
ffi: support buf objects.
* buf.c (make_duplicate_buf, buf_get, buf_fill): New functions. * buf.h (make_duplicate_buf, buf_get, buf_fill): Declared. * ffi.c (struct txr_ffi_type): New member, nelem. Keeps track of number of elements, for types that are FFI pointers. This lets us support the get method so that a buf can be a C function return value, if its size is declared in our FFI type system. (ffi_buf_put, ffi_buf_get, ffi_buf_fill): New functions. (ffi_type_compile): Handle two new cases of syntax for buffers: (buf <size>) and buf.
Diffstat (limited to 'buf.c')
-rw-r--r--buf.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/buf.c b/buf.c
index 0c1ecf18..58cd4177 100644
--- a/buf.c
+++ b/buf.c
@@ -107,6 +107,18 @@ val make_borrowed_buf(val len, mem_t *data)
return obj;
}
+val make_duplicate_buf(val len, mem_t *data)
+{
+ val obj = make_obj();
+
+ obj->b.type = BUF;
+ obj->b.data = chk_copy_obj(data, c_num(len));
+ obj->b.len = len;
+ obj->b.size = nil;
+
+ return obj;
+}
+
static struct buf *buf_handle(val buf, val ctx)
{
if (type(buf) == BUF)
@@ -186,6 +198,18 @@ val length_buf(val buf)
return b->len;
}
+mem_t *buf_get(val buf, val self)
+{
+ struct buf *b = buf_handle(buf, self);
+ return b->data;
+}
+
+void buf_fill(val buf, mem_t *src, val self)
+{
+ struct buf *b = buf_handle(buf, self);
+ memcpy(b->data, src, c_num(b->len));
+}
+
static void buf_put_bytes(val buf, val pos, mem_t *ptr, cnum size, val self)
{
struct buf *b = buf_handle(buf, self);