From be83bc717db0375e19431fe38c0490ff1a9e40ec Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 26 Apr 2017 05:57:37 -0700 Subject: 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 ) and buf. --- buf.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'buf.c') 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); -- cgit v1.2.3