[vty] Allow to create a buffer in a given context.

Stop using the global vty context for all allocations
and allow to embed the buffer into a given context, and
allocate sub buffers with the context of its parent.
This commit is contained in:
Holger Hans Peter Freyther 2010-04-30 12:26:52 +08:00
parent 9e282de7c8
commit 1700c933f2
4 changed files with 6 additions and 6 deletions

View File

@ -28,7 +28,7 @@
/* Create a new buffer. Memory will be allocated in chunks of the given /* Create a new buffer. Memory will be allocated in chunks of the given
size. If the argument is 0, the library will supply a reasonable size. If the argument is 0, the library will supply a reasonable
default size suitable for buffering socket I/O. */ default size suitable for buffering socket I/O. */
struct buffer *buffer_new(size_t); struct buffer *buffer_new(void *ctx, size_t);
/* Free all data in the buffer. */ /* Free all data in the buffer. */
void buffer_reset(struct buffer *); void buffer_reset(struct buffer *);

View File

@ -65,11 +65,11 @@ struct buffer_data {
#define BUFFER_DATA_FREE(D) talloc_free((D)) #define BUFFER_DATA_FREE(D) talloc_free((D))
/* Make new buffer. */ /* Make new buffer. */
struct buffer *buffer_new(size_t size) struct buffer *buffer_new(void *ctx, size_t size)
{ {
struct buffer *b; struct buffer *b;
b = talloc_zero(tall_vty_ctx, struct buffer); b = talloc_zero(ctx, struct buffer);
if (size) if (size)
b->size = size; b->size = size;
@ -138,7 +138,7 @@ static struct buffer_data *buffer_add(struct buffer *b)
{ {
struct buffer_data *d; struct buffer_data *d;
d = _talloc_zero(tall_vty_ctx, d = _talloc_zero(b,
offsetof(struct buffer_data, data[b->size]), offsetof(struct buffer_data, data[b->size]),
"buffer_add"); "buffer_add");
if (!d) if (!d)

View File

@ -51,7 +51,7 @@ struct vty *vty_new()
if (!new) if (!new)
goto out; goto out;
new->obuf = buffer_new(0); /* Use default buffer size. */ new->obuf = buffer_new(new, 0); /* Use default buffer size. */
if (!new->obuf) if (!new->obuf)
goto out_new; goto out_new;
new->buf = _talloc_zero(new, VTY_BUFSIZ, "vty_new->buf"); new->buf = _talloc_zero(new, VTY_BUFSIZ, "vty_new->buf");

View File

@ -58,7 +58,7 @@ static int dummy_config_write(struct vty *v)
static struct buffer *argv_to_buffer(int argc, const char *argv[], int base) static struct buffer *argv_to_buffer(int argc, const char *argv[], int base)
{ {
struct buffer *b = buffer_new(1024); struct buffer *b = buffer_new(NULL, 1024);
int i; int i;
if (!b) if (!b)