diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2020-05-14 16:03:10 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-05-19 02:41:47 +0200 |
commit | f4771fcc1c3fda21a46d0cb85d8b29e012254696 (patch) | |
tree | 012df74ed2cf44cb20605b239e1b88dab6edc645 /src/util/buffer.c | |
parent | 6a52ce5f9bc35a852b215e7073f3a0d2665ca8de (diff) | |
download | gnunet-f4771fcc1c3fda21a46d0cb85d8b29e012254696.tar.gz gnunet-f4771fcc1c3fda21a46d0cb85d8b29e012254696.zip |
Improved BIO API
BIO now supports reading from and writing to in-memory buffers.
For reading, an allocated buffer (array) and a size is passed as arguments to
the function opening the handle.
For writing, a GNUNET_Buffer is created and used internally. The buffer
contents can be extracted using the relevant function.
There is a new API in addition to the existing read/write: this new API is
more "declarative" in nature and is meant to mimic APIs like GNUNET_SQ.
The read/write operations are defined in an array of specs which are then
"commited" in a single (non-atomic) operation, rather than explicitly
executing multiple function calls and checking their return value.
Also there are small changes to GNUNET_Buffer to account for BIO's new
features.
Signed-off-by: Christian Grothoff <christian@grothoff.org>
Diffstat (limited to 'src/util/buffer.c')
-rw-r--r-- | src/util/buffer.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/util/buffer.c b/src/util/buffer.c index dabf630c7..c865f6307 100644 --- a/src/util/buffer.c +++ b/src/util/buffer.c | |||
@@ -130,7 +130,26 @@ GNUNET_buffer_reap_str (struct GNUNET_Buffer *buf) | |||
130 | buf->mem[buf->position++] = '\0'; | 130 | buf->mem[buf->position++] = '\0'; |
131 | } | 131 | } |
132 | res = buf->mem; | 132 | res = buf->mem; |
133 | *buf = (struct GNUNET_Buffer) { 0 }; | 133 | memset (buf, 0, sizeof (struct GNUNET_Buffer)); |
134 | return res; | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Clear the buffer and return its contents. | ||
140 | * The caller is responsible to eventually #GNUNET_free | ||
141 | * the returned data. | ||
142 | * | ||
143 | * @param buf the buffer to reap the contents from | ||
144 | * @param size where to store the size of the returned data | ||
145 | * @returns the data contained in the string | ||
146 | */ | ||
147 | void * | ||
148 | GNUNET_buffer_reap (struct GNUNET_Buffer *buf, size_t *size) | ||
149 | { | ||
150 | *size = buf->position; | ||
151 | void *res = buf->mem; | ||
152 | memset (buf, 0, sizeof (struct GNUNET_Buffer)); | ||
134 | return res; | 153 | return res; |
135 | } | 154 | } |
136 | 155 | ||
@@ -144,7 +163,7 @@ void | |||
144 | GNUNET_buffer_clear (struct GNUNET_Buffer *buf) | 163 | GNUNET_buffer_clear (struct GNUNET_Buffer *buf) |
145 | { | 164 | { |
146 | GNUNET_free_non_null (buf->mem); | 165 | GNUNET_free_non_null (buf->mem); |
147 | *buf = (struct GNUNET_Buffer) { 0 }; | 166 | memset (buf, 0, sizeof (struct GNUNET_Buffer)); |
148 | } | 167 | } |
149 | 168 | ||
150 | 169 | ||