diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-04-26 05:57:37 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-04-26 05:57:37 -0700 |
commit | be83bc717db0375e19431fe38c0490ff1a9e40ec (patch) | |
tree | 354d2107e8a5d4b518b5081a036894b87fc82c68 /buf.c | |
parent | d081a595dfa053c78f7e3f580f82ca7823f02f74 (diff) | |
download | txr-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.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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); |