summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/string.c
blob: f5cde949e4effda06c5776b6ffdda4b145157a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "avro_private.h"
#include "avro/data.h"
#include "avro/allocation.h"
#include "avro/errors.h"

#ifndef AVRO_STRING_DEBUG
#define AVRO_STRING_DEBUG 0
#endif

#if AVRO_STRING_DEBUG
#include <stdio.h>
#define DEBUG(...) \
	do { \
		fprintf(stderr, __VA_ARGS__); \
		fprintf(stderr, "\n"); \
	} while (0)
#else
#define DEBUG(...)  /* don't print messages */
#endif


/*
 * A resizable wrapped buffer implementation.  This implementation makes
 * actual copies in its copy method; if we wanted a zero-copy solution
 * here, then we'd have to keep track of all copies of the buffer, so
 * that we can update pointers whenever the buffer is resized (since
 * this might change the location of the memory region).
 */

struct avro_wrapped_resizable {
	size_t  buf_size;
};

#define avro_wrapped_resizable_size(sz) \
	(sizeof(struct avro_wrapped_resizable) + (sz))

static void
avro_wrapped_resizable_free(avro_wrapped_buffer_t *self)
{
	DEBUG("--- Freeing resizable <%p:%" PRIsz "> (%p)", self->buf, self->size, self->user_data);
	struct avro_wrapped_resizable  *resizable = (struct avro_wrapped_resizable *) self->user_data;
	avro_free(resizable, avro_wrapped_resizable_size(resizable->buf_size));
}

static int
avro_wrapped_resizable_resize(avro_wrapped_buffer_t *self, size_t desired)
{
	struct avro_wrapped_resizable  *resizable = (struct avro_wrapped_resizable *) self->user_data;

	/*
	 * If we've already allocated enough memory for the desired
	 * size, there's nothing to do.
	 */

	if (resizable->buf_size >= desired) {
		return 0;
	}

	size_t  new_buf_size = resizable->buf_size * 2;
	if (desired > new_buf_size) {
		new_buf_size = desired;
	}

	DEBUG("--- Resizing <%p:%" PRIsz "> (%p) -> %" PRIsz,
	      self->buf, self->buf_size, self->user_data, new_buf_size);

	struct avro_wrapped_resizable  *new_resizable =
	    (struct avro_wrapped_resizable *) avro_realloc(resizable,
		         avro_wrapped_resizable_size(resizable->buf_size),
			 avro_wrapped_resizable_size(new_buf_size));
	if (new_resizable == NULL) {
		return ENOMEM;
	}
	DEBUG("--- New buffer <%p:%" PRIsz ">", new_buf, new_buf_size);

	new_resizable->buf_size = new_buf_size;

	char  *old_buf = (char *) resizable;
	char  *new_buf = (char *) new_resizable;

	ptrdiff_t  offset = (char *) self->buf - old_buf;
	DEBUG("--- Old data pointer is %p", self->buf);
	self->buf = new_buf + offset;
	self->user_data = new_resizable;
	DEBUG("--- New data pointer is %p", self->buf);
	return 0;
}

static int
avro_wrapped_resizable_new(avro_wrapped_buffer_t *dest, size_t buf_size)
{
	size_t  allocated_size = avro_wrapped_resizable_size(buf_size);
	struct avro_wrapped_resizable  *resizable =
	    (struct avro_wrapped_resizable *) avro_malloc(allocated_size);
	if (resizable == NULL) {
		return ENOMEM;
	}

	resizable->buf_size = buf_size;

	dest->buf = ((char *) resizable) + sizeof(struct avro_wrapped_resizable);
	DEBUG("--- Creating resizable <%p:%" PRIsz "> (%p)", dest->buf, buf_size, resizable);
	dest->size = buf_size;
	dest->user_data = resizable;
	dest->free = avro_wrapped_resizable_free;
	dest->copy = NULL;
	dest->slice = NULL;
	return 0;
}

#define is_resizable(buf) \
	((buf).free == avro_wrapped_resizable_free)



void
avro_raw_string_init(avro_raw_string_t *str)
{
	memset(str, 0, sizeof(avro_raw_string_t));
}


void
avro_raw_string_clear(avro_raw_string_t *str)
{
	/*
	 * If the string's buffer is one that we control, then we don't
	 * free it; that lets us reuse the storage on the next call to
	 * avro_raw_string_set[_length].
	 */

	if (is_resizable(str->wrapped)) {
		DEBUG("--- Clearing resizable buffer");
		str->wrapped.size = 0;
	} else {
		DEBUG("--- Freeing wrapped buffer");
		avro_wrapped_buffer_free(&str->wrapped);
		avro_raw_string_init(str);
	}
}


void
avro_raw_string_done(avro_raw_string_t *str)
{
	avro_wrapped_buffer_free(&str->wrapped);
	avro_raw_string_init(str);
}


/**
 * Makes sure that the string's buffer is one that we allocated
 * ourselves, and that the buffer is big enough to hold a string of the
 * given length.
 */

static int
avro_raw_string_ensure_buf(avro_raw_string_t *str, size_t length)
{
	int  rval;

	DEBUG("--- Ensuring resizable buffer of size %" PRIsz, length);
	if (is_resizable(str->wrapped)) {
		/*
		 * If we've already got a resizable buffer, just have it
		 * resize itself.
		 */

		return avro_wrapped_resizable_resize(&str->wrapped, length);
	} else {
		/*
		 * Stash a copy of the old wrapped buffer, and then
		 * create a new resizable buffer to store our content
		 * in.
		 */

		avro_wrapped_buffer_t  orig = str->wrapped;
		check(rval, avro_wrapped_resizable_new(&str->wrapped, length));

		/*
		 * If there was any content in the old wrapped buffer,
		 * copy it into the new resizable one.
		 */

		if (orig.size > 0) {
			size_t  to_copy =
			    (orig.size < length)? orig.size: length;
			memcpy((void *) str->wrapped.buf, orig.buf, to_copy);
		}
		avro_wrapped_buffer_free(&orig);

		return 0;
	}
}


void
avro_raw_string_set_length(avro_raw_string_t *str,
			   const void *src, size_t length)
{
	avro_raw_string_ensure_buf(str, length+1);
	memcpy((void *) str->wrapped.buf, src, length);
	((char *) str->wrapped.buf)[length] = '\0';
	str->wrapped.size = length;
}


void avro_raw_string_append_length(avro_raw_string_t *str,
				   const void *src,
				   size_t length)
{
	if (avro_raw_string_length(str) == 0) {
		return avro_raw_string_set_length(str, src, length);
	}

	avro_raw_string_ensure_buf(str, str->wrapped.size + length);
	memcpy((char *) str->wrapped.buf + str->wrapped.size, src, length);
	str->wrapped.size += length;
}


void
avro_raw_string_set(avro_raw_string_t *str, const char *src)
{
	size_t  length = strlen(src);
	avro_raw_string_ensure_buf(str, length+1);
	memcpy((void *) str->wrapped.buf, src, length+1);
	str->wrapped.size = length+1;
}


void
avro_raw_string_append(avro_raw_string_t *str, const char *src)
{
	if (avro_raw_string_length(str) == 0) {
		return avro_raw_string_set(str, src);
	}

	/* Assume that str->wrapped.size includes a NUL terminator */
	size_t  length = strlen(src);
	avro_raw_string_ensure_buf(str, str->wrapped.size + length);
	memcpy((char *) str->wrapped.buf + str->wrapped.size - 1, src, length+1);
	str->wrapped.size += length;
}


void
avro_raw_string_give(avro_raw_string_t *str,
		     avro_wrapped_buffer_t *src)
{
	DEBUG("--- Giving control of <%p:%" PRIsz "> (%p) to string",
	      src->buf, src->size, src);
	avro_wrapped_buffer_free(&str->wrapped);
	avro_wrapped_buffer_move(&str->wrapped, src);
}

int
avro_raw_string_grab(const avro_raw_string_t *str,
		     avro_wrapped_buffer_t *dest)
{
	return avro_wrapped_buffer_copy(dest, &str->wrapped, 0, str->wrapped.size);
}


int
avro_raw_string_equals(const avro_raw_string_t *str1,
		       const avro_raw_string_t *str2)
{
	if (str1 == str2) {
		return 1;
	}

	if (!str1 || !str2) {
		return 0;
	}

	if (str1->wrapped.size != str2->wrapped.size) {
		return 0;
	}

	return (memcmp(str1->wrapped.buf, str2->wrapped.buf,
		       str1->wrapped.size) == 0);
}