mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-03 16:15:27 +00:00
Make the ISC_BUFFER_VALID() be exported from buffer.h, so bufferlist.h
can use internal buffer pointers without fear by first performing the same validity check used in the buffer.c file. This eliminates a function call inside a loop, and the bufferlist.c file is really a superset of buffers in many ways already.
This commit is contained in:
@@ -22,11 +22,6 @@
|
|||||||
#include <isc/assertions.h>
|
#include <isc/assertions.h>
|
||||||
#include <isc/buffer.h>
|
#include <isc/buffer.h>
|
||||||
|
|
||||||
#define BUFFER_MAGIC 0x42756621U /* Buf!. */
|
|
||||||
|
|
||||||
#define VALID_BUFFER(b) ((b) != NULL && \
|
|
||||||
(b)->magic == BUFFER_MAGIC)
|
|
||||||
|
|
||||||
void
|
void
|
||||||
isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
|
isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
|
||||||
unsigned int type)
|
unsigned int type)
|
||||||
@@ -37,7 +32,7 @@ isc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
|
|||||||
|
|
||||||
REQUIRE(b != NULL);
|
REQUIRE(b != NULL);
|
||||||
|
|
||||||
b->magic = BUFFER_MAGIC;
|
b->magic = ISC_BUFFER_MAGIC;
|
||||||
b->type = type;
|
b->type = type;
|
||||||
b->base = base;
|
b->base = base;
|
||||||
b->length = length;
|
b->length = length;
|
||||||
@@ -54,7 +49,7 @@ isc_buffer_invalidate(isc_buffer_t *b) {
|
|||||||
* Make 'b' an invalid buffer.
|
* Make 'b' an invalid buffer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(!ISC_LINK_LINKED(b, link));
|
REQUIRE(!ISC_LINK_LINKED(b, link));
|
||||||
REQUIRE(b->mctx == NULL);
|
REQUIRE(b->mctx == NULL);
|
||||||
|
|
||||||
@@ -73,7 +68,7 @@ isc_buffer_type(isc_buffer_t *b) {
|
|||||||
* The type of 'b'.
|
* The type of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
return (b->type);
|
return (b->type);
|
||||||
}
|
}
|
||||||
@@ -84,7 +79,7 @@ isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the region of 'b'.
|
* Make 'r' refer to the region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
r->base = b->base;
|
r->base = b->base;
|
||||||
@@ -97,7 +92,7 @@ isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the used region of 'b'.
|
* Make 'r' refer to the used region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
r->base = b->base;
|
r->base = b->base;
|
||||||
@@ -107,7 +102,7 @@ isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
unsigned int
|
unsigned int
|
||||||
isc_buffer_usedcount(isc_buffer_t *b)
|
isc_buffer_usedcount(isc_buffer_t *b)
|
||||||
{
|
{
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
return (b->used);
|
return (b->used);
|
||||||
}
|
}
|
||||||
@@ -118,7 +113,7 @@ isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the available region of 'b'.
|
* Make 'r' refer to the available region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
r->base = (unsigned char *)b->base + b->used;
|
r->base = (unsigned char *)b->base + b->used;
|
||||||
@@ -128,7 +123,7 @@ isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
unsigned int
|
unsigned int
|
||||||
isc_buffer_availablecount(isc_buffer_t *b)
|
isc_buffer_availablecount(isc_buffer_t *b)
|
||||||
{
|
{
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
return (b->length - b->used);
|
return (b->length - b->used);
|
||||||
}
|
}
|
||||||
@@ -139,7 +134,7 @@ isc_buffer_add(isc_buffer_t *b, unsigned int n) {
|
|||||||
* Increase the 'used' region of 'b' by 'n' bytes.
|
* Increase the 'used' region of 'b' by 'n' bytes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used + n <= b->length);
|
REQUIRE(b->used + n <= b->length);
|
||||||
|
|
||||||
b->used += n;
|
b->used += n;
|
||||||
@@ -151,7 +146,7 @@ isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
|
|||||||
* Decrease the 'used' region of 'b' by 'n' bytes.
|
* Decrease the 'used' region of 'b' by 'n' bytes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used >= n);
|
REQUIRE(b->used >= n);
|
||||||
|
|
||||||
b->used -= n;
|
b->used -= n;
|
||||||
@@ -167,7 +162,7 @@ isc_buffer_clear(isc_buffer_t *b) {
|
|||||||
* Make the used region empty.
|
* Make the used region empty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
b->used = 0;
|
b->used = 0;
|
||||||
b->current = 0;
|
b->current = 0;
|
||||||
@@ -180,7 +175,7 @@ isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the consumed region of 'b'.
|
* Make 'r' refer to the consumed region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
r->base = b->base;
|
r->base = b->base;
|
||||||
@@ -193,7 +188,7 @@ isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the remaining region of 'b'.
|
* Make 'r' refer to the remaining region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
r->base = (unsigned char *)b->base + b->current;
|
r->base = (unsigned char *)b->base + b->current;
|
||||||
@@ -206,7 +201,7 @@ isc_buffer_active(isc_buffer_t *b, isc_region_t *r) {
|
|||||||
* Make 'r' refer to the active region of 'b'.
|
* Make 'r' refer to the active region of 'b'.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(r != NULL);
|
REQUIRE(r != NULL);
|
||||||
|
|
||||||
if (b->current < b->active) {
|
if (b->current < b->active) {
|
||||||
@@ -226,7 +221,7 @@ isc_buffer_setactive(isc_buffer_t *b, unsigned int n) {
|
|||||||
* Sets the end of the active region 'n' bytes after current.
|
* Sets the end of the active region 'n' bytes after current.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
active = b->current + n;
|
active = b->current + n;
|
||||||
REQUIRE(active <= b->used);
|
REQUIRE(active <= b->used);
|
||||||
|
|
||||||
@@ -239,7 +234,7 @@ isc_buffer_first(isc_buffer_t *b) {
|
|||||||
* Make the consumed region empty.
|
* Make the consumed region empty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
b->current = 0;
|
b->current = 0;
|
||||||
}
|
}
|
||||||
@@ -250,7 +245,7 @@ isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
|
|||||||
* Increase the 'consumed' region of 'b' by 'n' bytes.
|
* Increase the 'consumed' region of 'b' by 'n' bytes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->current + n <= b->used);
|
REQUIRE(b->current + n <= b->used);
|
||||||
|
|
||||||
b->current += n;
|
b->current += n;
|
||||||
@@ -262,7 +257,7 @@ isc_buffer_back(isc_buffer_t *b, unsigned int n) {
|
|||||||
* Decrease the 'consumed' region of 'b' by 'n' bytes.
|
* Decrease the 'consumed' region of 'b' by 'n' bytes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(n <= b->current);
|
REQUIRE(n <= b->current);
|
||||||
|
|
||||||
b->current -= n;
|
b->current -= n;
|
||||||
@@ -279,7 +274,7 @@ isc_buffer_compact(isc_buffer_t *b) {
|
|||||||
* of the consumed region, and the consumed region is then made empty.
|
* of the consumed region, and the consumed region is then made empty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
|
|
||||||
src = (unsigned char *)b->base + b->current;
|
src = (unsigned char *)b->base + b->current;
|
||||||
length = b->used - b->current;
|
length = b->used - b->current;
|
||||||
@@ -302,7 +297,7 @@ isc_buffer_getuint8(isc_buffer_t *b) {
|
|||||||
* Read an unsigned 8-bit integer from 'b' and return it.
|
* Read an unsigned 8-bit integer from 'b' and return it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used - b->current >= 1);
|
REQUIRE(b->used - b->current >= 1);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -318,7 +313,7 @@ isc_buffer_putuint8(isc_buffer_t *b, isc_uint8_t val)
|
|||||||
{
|
{
|
||||||
unsigned char *cp;
|
unsigned char *cp;
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used + 1 <= b->length);
|
REQUIRE(b->used + 1 <= b->length);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -337,7 +332,7 @@ isc_buffer_getuint16(isc_buffer_t *b) {
|
|||||||
* convert it to host byte order, and return it.
|
* convert it to host byte order, and return it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used - b->current >= 2);
|
REQUIRE(b->used - b->current >= 2);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -354,7 +349,7 @@ isc_buffer_putuint16(isc_buffer_t *b, isc_uint16_t val)
|
|||||||
{
|
{
|
||||||
unsigned char *cp;
|
unsigned char *cp;
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used + 2 <= b->length);
|
REQUIRE(b->used + 2 <= b->length);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -374,7 +369,7 @@ isc_buffer_getuint32(isc_buffer_t *b) {
|
|||||||
* convert it to host byte order, and return it.
|
* convert it to host byte order, and return it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used - b->current >= 4);
|
REQUIRE(b->used - b->current >= 4);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -393,7 +388,7 @@ isc_buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
|
|||||||
{
|
{
|
||||||
unsigned char *cp;
|
unsigned char *cp;
|
||||||
|
|
||||||
REQUIRE(VALID_BUFFER(b));
|
REQUIRE(ISC_BUFFER_VALID(b));
|
||||||
REQUIRE(b->used + 4 <= b->length);
|
REQUIRE(b->used + 4 <= b->length);
|
||||||
|
|
||||||
cp = b->base;
|
cp = b->base;
|
||||||
@@ -435,7 +430,7 @@ isc_buffer_free(isc_buffer_t **dynbuffer)
|
|||||||
isc_mem_t *mctx;
|
isc_mem_t *mctx;
|
||||||
|
|
||||||
REQUIRE(dynbuffer != NULL);
|
REQUIRE(dynbuffer != NULL);
|
||||||
REQUIRE(VALID_BUFFER(*dynbuffer));
|
REQUIRE(ISC_BUFFER_VALID(*dynbuffer));
|
||||||
REQUIRE((*dynbuffer)->mctx != NULL);
|
REQUIRE((*dynbuffer)->mctx != NULL);
|
||||||
|
|
||||||
dbuf = *dynbuffer;
|
dbuf = *dynbuffer;
|
||||||
|
@@ -33,7 +33,8 @@ isc_bufferlist_usedcount(isc_bufferlist_t *bl)
|
|||||||
length = 0;
|
length = 0;
|
||||||
buffer = ISC_LIST_HEAD(*bl);
|
buffer = ISC_LIST_HEAD(*bl);
|
||||||
while (buffer != NULL) {
|
while (buffer != NULL) {
|
||||||
length += isc_buffer_usedcount(buffer);
|
REQUIRE(ISC_BUFFER_VALID(buffer));
|
||||||
|
length += buffer->used;
|
||||||
buffer = ISC_LIST_NEXT(buffer, link);
|
buffer = ISC_LIST_NEXT(buffer, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +52,8 @@ isc_bufferlist_availablecount(isc_bufferlist_t *bl)
|
|||||||
length = 0;
|
length = 0;
|
||||||
buffer = ISC_LIST_HEAD(*bl);
|
buffer = ISC_LIST_HEAD(*bl);
|
||||||
while (buffer != NULL) {
|
while (buffer != NULL) {
|
||||||
length += isc_buffer_availablecount(buffer);
|
REQUIRE(ISC_BUFFER_VALID(buffer));
|
||||||
|
length += (buffer->length - buffer->used);
|
||||||
buffer = ISC_LIST_NEXT(buffer, link);
|
buffer = ISC_LIST_NEXT(buffer, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -109,6 +109,14 @@
|
|||||||
|
|
||||||
ISC_LANG_BEGINDECLS
|
ISC_LANG_BEGINDECLS
|
||||||
|
|
||||||
|
/***
|
||||||
|
*** Magic numbers
|
||||||
|
***/
|
||||||
|
#define ISC_BUFFER_MAGIC 0x42756621U /* Buf!. */
|
||||||
|
|
||||||
|
#define ISC_BUFFER_VALID(b) ((b) != NULL && \
|
||||||
|
(b)->magic == ISC_BUFFER_MAGIC)
|
||||||
|
|
||||||
/***
|
/***
|
||||||
*** Types
|
*** Types
|
||||||
***/
|
***/
|
||||||
|
Reference in New Issue
Block a user