xref: /openbsd/usr.bin/dig/lib/isc/bufferlist.c (revision d89ec533)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: bufferlist.c,v 1.3 2020/02/25 05:00:43 jsg Exp $ */
18 
19 /*! \file */
20 
21 #include <stddef.h>
22 
23 #include <isc/buffer.h>
24 #include <isc/bufferlist.h>
25 #include <isc/util.h>
26 
27 unsigned int
28 isc_bufferlist_usedcount(isc_bufferlist_t *bl) {
29 	isc_buffer_t *buffer;
30 	unsigned int length;
31 
32 	REQUIRE(bl != NULL);
33 
34 	length = 0;
35 	buffer = ISC_LIST_HEAD(*bl);
36 	while (buffer != NULL) {
37 		length += isc_buffer_usedlength(buffer);
38 		buffer = ISC_LIST_NEXT(buffer, link);
39 	}
40 
41 	return (length);
42 }
43 
44 unsigned int
45 isc_bufferlist_availablecount(isc_bufferlist_t *bl) {
46 	isc_buffer_t *buffer;
47 	unsigned int length;
48 
49 	REQUIRE(bl != NULL);
50 
51 	length = 0;
52 	buffer = ISC_LIST_HEAD(*bl);
53 	while (buffer != NULL) {
54 		length += isc_buffer_availablelength(buffer);
55 		buffer = ISC_LIST_NEXT(buffer, link);
56 	}
57 
58 	return (length);
59 }
60