ldns  1.7.0
buffer.h
Go to the documentation of this file.
1 /*
2  * buffer.h -- generic memory buffer.
3  *
4  * Copyright (c) 2005-2008, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  *
9  * The buffer module implements a generic buffer. The API is based on
10  * the java.nio.Buffer interface.
11  */
12 
13 #ifndef LDNS_BUFFER_H
14 #define LDNS_BUFFER_H
15 
16 #include <assert.h>
17 #include <stdarg.h>
18 #include <string.h>
19 
20 #include <ldns/error.h>
21 #include <ldns/common.h>
22 
23 #include "ldns/util.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
33 #define LDNS_MIN_BUFLEN 512
34 
51 {
53  size_t _position;
54 
56  size_t _limit;
57 
59  size_t _capacity;
60 
62  uint8_t *_data;
63 
65  unsigned _fixed : 1;
66 
71 };
72 typedef struct ldns_struct_buffer ldns_buffer;
73 
74 
75 #ifdef NDEBUG
76 INLINE void
77 ldns_buffer_invariant(const ldns_buffer *ATTR_UNUSED(buffer))
78 {
79 }
80 #else
81 INLINE void
82 ldns_buffer_invariant(const ldns_buffer *buffer)
83 {
84  assert(buffer != NULL);
85  assert(buffer->_position <= buffer->_limit);
86  assert(buffer->_limit <= buffer->_capacity);
87  assert(buffer->_data != NULL);
88 }
89 #endif
90 
97 ldns_buffer *ldns_buffer_new(size_t capacity);
98 
108 void ldns_buffer_new_frm_data(ldns_buffer *buffer, const void *data, size_t size);
109 
115 INLINE void ldns_buffer_clear(ldns_buffer *buffer)
116 {
117  ldns_buffer_invariant(buffer);
118 
119  /* reset status here? */
120 
121  buffer->_position = 0;
122  buffer->_limit = buffer->_capacity;
123 }
124 
133 INLINE void ldns_buffer_flip(ldns_buffer