2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

Add support for dyamic buffers. These are allocated using

isc_dynbuffer_allocate() and freed with isc_dynbuffer_free(), and are
a linkable buffer type with a built-in data area.
This commit is contained in:
Michael Graff
1999-04-19 23:56:36 +00:00
parent 292c7d395e
commit 441a57f959
2 changed files with 117 additions and 2 deletions

View File

@@ -22,9 +22,14 @@
#include <isc/assertions.h>
#include <isc/buffer.h>
#define BUFFER_MAGIC 0x42756621U /* Buf!. */
#define DYNBUFFER_MAGIC 0x64427566U /* dBuf. */
#define VALID_BUFFER(b) ((b) != NULL && \
(b)->magic == BUFFER_MAGIC)
#define BUFFER_MAGIC 0x42756621U /* Buf!. */
#define VALID_DYNBUFFER(b) ((b) != NULL && \
(b)->magic == DYNBUFFER_MAGIC)
void
isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
@@ -349,3 +354,46 @@ isc_buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
cp[2] = (val & 0x0000ff00) >> 8;
cp[3] = (val & 0x000000ff);
}
isc_result_t
isc_dynbuffer_allocate(isc_mem_t *mctx, isc_dynbuffer_t **dynbuffer,
unsigned int length, unsigned int type)
{
isc_dynbuffer_t *dbuf;
REQUIRE(dynbuffer != NULL);
REQUIRE(*dynbuffer == NULL);
dbuf = isc_mem_get(mctx, length + sizeof(isc_dynbuffer_t));
if (dbuf == NULL)
return (ISC_R_NOMEMORY);
dbuf->magic = DYNBUFFER_MAGIC;
ISC_LINK_INIT(dbuf, link);
isc_buffer_init(&dbuf->buffer,
((unsigned char *)dbuf) + sizeof(isc_dynbuffer_t),
length, type);
*dynbuffer = dbuf;
return (ISC_R_SUCCESS);
}
void
isc_dynbuffer_free(isc_mem_t *mctx, isc_dynbuffer_t **dynbuffer)
{
unsigned int real_length;
isc_dynbuffer_t *dbuf;
REQUIRE(dynbuffer != NULL);
REQUIRE(VALID_DYNBUFFER(*dynbuffer));
dbuf = *dynbuffer;
*dynbuffer = NULL; /* destroy external reference */
real_length = dbuf->buffer.length + sizeof(isc_dynbuffer_t);
isc_buffer_invalidate(&dbuf->buffer);
dbuf->magic = 0;
isc_mem_put(mctx, dbuf, real_length);
}