From 5b96945bd308339c2225106ff4d0aa6f3d96ddfc Mon Sep 17 00:00:00 2001 From: Nicolas Pennequin Date: Tue, 7 Aug 2007 22:51:24 +0200 Subject: [PATCH] Add the bufalloc API call to load data from a source other than a file. --- testplugin.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/testplugin.c b/testplugin.c index cf9e44f..7168f38 100644 --- a/testplugin.c +++ b/testplugin.c @@ -492,6 +492,7 @@ BUFFERING API FUNCTIONS ======================= bufopen : Request the opening of a new handle for a file +bufalloc : Open a new handle for data other than a file. bufclose : Close an open handle bufseek : Set the read pointer in a handle bufadvance : Move the read pointer in a handle @@ -555,6 +556,47 @@ int bufopen(char *file, size_t offset, enum data_type type) return h->id; } +/* Open a new handle from data that isn't in a file. + src is the source buffer from which to copy data. It can be NULL to simply + reserve buffer space. + size is the requested size. The call will only be successful if the + requested amount of data can entirely fit in the buffer without wrapping. + Return value is the handle id for success or <0 for failure. +*/ +int bufalloc(void *src, size_t size, enum data_type type) +{ + if (!can_add_handle()) + return -2; + + if (size + sizeof(struct memory_handle) > buffer_len - buf_widx) + /* The data would need to wrap. */ + return -2; + + size_t allocsize = size; + struct memory_handle *h = add_handle(&allocsize); + + if (!h || allocsize != size) + return -2; + + if (src) + rb->memcpy(&buffer[buf_widx], src, size); + + h->fd = -1; + *h->path = 0; + h->filesize = size; + h->filerem = 0; + h->offset = 0; + h->ridx = buf_widx; + h->widx = buf_widx; + h->data = buf_widx; + h->available = size; + h->type = type; + + buf_widx = RINGBUF_ADD(buf_widx, size); + + return h->id; +} + /* Close the handle. Return 0 for success and < 0 for failure */ int bufclose(int handle_id) { -- 2.11.4.GIT