Commit bc63438c authored by Niels Möller's avatar Niels Möller
Browse files

* buffer.c (nettle_buffer_grow): New function, replacing

grow_realloc.
(nettle_buffer_clear): Rewritten to use buffer->realloc.

* buffer.h (struct nettle_buffer): REplaced the GROW function
pointer with a nettle_realloc_func pointer and a void
(NETTLE_BUFFER_GROW): Deleted macro, use function instead.

Rev: src/nettle/buffer.c:1.2
Rev: src/nettle/buffer.h:1.3
parent e82d0a95
...@@ -28,40 +28,29 @@ ...@@ -28,40 +28,29 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static int int
grow_realloc(struct nettle_buffer *buffer, nettle_buffer_grow(struct nettle_buffer *buffer,
unsigned length) unsigned length)
{ {
if (!length) assert(buffer->size <= buffer->realloc);
{
realloc(buffer->contents, 0); if (buffer->size + length > buffer->alloc)
buffer->contents = NULL;
buffer->alloc = 0;
buffer->size = 0;
return 1;
}
else
{ {
unsigned alloc = buffer->alloc * 2 + length + 100; unsigned alloc;
uint8_t *p = realloc(buffer->contents, alloc); uint8_t *p;
if (!buffer->realloc)
return 0;
alloc = buffer->alloc * 2 + length + 100;
p = buffer->realloc(buffer->realloc_ctx, buffer->contents, alloc);
if (!p) if (!p)
return 0; return 0;
buffer->contents = p; buffer->contents = p;
buffer->alloc = alloc; buffer->alloc = alloc;
return 1;
} }
} return 1;
void
nettle_buffer_init(struct nettle_buffer *buffer)
{
buffer->contents = NULL;
buffer->alloc = 0;
buffer->grow = grow_realloc;
buffer->size = 0;
} }
void void
...@@ -70,14 +59,20 @@ nettle_buffer_init_size(struct nettle_buffer *buffer, ...@@ -70,14 +59,20 @@ nettle_buffer_init_size(struct nettle_buffer *buffer,
{ {
buffer->contents = space; buffer->contents = space;
buffer->alloc = length; buffer->alloc = length;
buffer->grow = NULL; buffer->realloc = NULL;
buffer->realloc_ctx = NULL;
buffer->size = 0; buffer->size = 0;
} }
void void
nettle_buffer_clear(struct nettle_buffer *buffer) nettle_buffer_clear(struct nettle_buffer *buffer)
{ {
NETTLE_BUFFER_GROW(buffer, 0); if (buffer->realloc)
buffer->realloc(buffer->realloc_ctx, buffer->contents, 0);
buffer->contents = NULL;
buffer->alloc = 0;
buffer->size = 0;
} }
uint8_t * uint8_t *
...@@ -85,9 +80,9 @@ nettle_buffer_space(struct nettle_buffer *buffer, ...@@ -85,9 +80,9 @@ nettle_buffer_space(struct nettle_buffer *buffer,
unsigned length) unsigned length)
{ {
uint8_t *p; uint8_t *p;
if (buffer->size + length > buffer->alloc)
if (!NETTLE_BUFFER_GROW(buffer, length)) if (!nettle_buffer_grow(buffer, length))
return NULL; return NULL;
p = buffer->contents + buffer->size; p = buffer->contents + buffer->size;
buffer->size += length; buffer->size += length;
......
...@@ -26,20 +26,16 @@ ...@@ -26,20 +26,16 @@
#ifndef NETTLE_BUFFER_H_INCLUDED #ifndef NETTLE_BUFFER_H_INCLUDED
#define NETTLE_BUFFER_H_INCLUDED #define NETTLE_BUFFER_H_INCLUDED
#include <inttypes.h> #include "realloc.h"
struct nettle_buffer struct nettle_buffer
{ {
uint8_t *contents; uint8_t *contents;
/* Allocated size */ /* Allocated size */
unsigned alloc; unsigned alloc;
/* If GROW is NULL, no reallocation is done. Otherwise, it should be nettle_realloc_func *realloc;
* a function that reallocates the contents to at least the size SIZE void *realloc_ctx;
* + LENGTH. It can return 0 if allocation fails. Furthermore, if
* GROW is called with LENGTH = 0, it should deallocate the buffer
* space. */
int (*grow)(struct nettle_buffer *buffer, unsigned length);
/* Current size */ /* Current size */
unsigned size; unsigned size;
...@@ -57,12 +53,12 @@ nettle_buffer_init_size(struct nettle_buffer *buffer, ...@@ -57,12 +53,12 @@ nettle_buffer_init_size(struct nettle_buffer *buffer,
void void
nettle_buffer_clear(struct nettle_buffer *buffer); nettle_buffer_clear(struct nettle_buffer *buffer);
/* FIXME: Put the comparison buffer->size + length > buffer->alloc int
* inside this macro. */ nettle_buffer_grow(struct nettle_buffer *buffer,
#define NETTLE_BUFFER_GROW(o, l) ((o)->grow && (o)->grow((o), l)) unsigned length);
#define NETTLE_BUFFER_PUTC(buffer, c) \ #define NETTLE_BUFFER_PUTC(buffer, c) \
( (((buffer)->size < (buffer)->alloc) || NETTLE_BUFFER_GROW((buffer), 1)) \ ( (((buffer)->size < (buffer)->alloc) || nettle_buffer_grow((buffer), 1)) \
&& ((buffer)->contents[(buffer)->size++] = (c), 1) ) && ((buffer)->contents[(buffer)->size++] = (c), 1) )
int int
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment