summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/wrapped-buffer.c
blob: e7496b4242231c48946af83fe15ce99ea1364ee5 (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
/*
 * 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 <errno.h>
#include <stdlib.h>
#include <string.h>

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

struct avro_wrapped_copy {
	volatile int  refcount;
	size_t  allocated_size;
};

static void
avro_wrapped_copy_free(avro_wrapped_buffer_t *self)
{
	struct avro_wrapped_copy  *copy = (struct avro_wrapped_copy *) self->user_data;
	if (avro_refcount_dec(&copy->refcount)) {
		avro_free(copy, copy->allocated_size);
	}
}

static int
avro_wrapped_copy_copy(avro_wrapped_buffer_t *dest,
		       const avro_wrapped_buffer_t *src,
		       size_t offset, size_t length)
{
	struct avro_wrapped_copy  *copy = (struct avro_wrapped_copy *) src->user_data;
	avro_refcount_inc(&copy->refcount);
	dest->buf = (char *) src->buf + offset;
	dest->size = length;
	dest->user_data = copy;
	dest->free = avro_wrapped_copy_free;
	dest->copy = avro_wrapped_copy_copy;
	dest->slice = NULL;
	return 0;
}

int
avro_wrapped_buffer_new_copy(avro_wrapped_buffer_t *dest,
			     const void *buf, size_t length)
{
	size_t  allocated_size = sizeof(struct avro_wrapped_copy) + length;
	struct avro_wrapped_copy  *copy = (struct avro_wrapped_copy *) avro_malloc(allocated_size);
	if (copy == NULL) {
		return ENOMEM;
	}

	dest->buf = ((char *) copy) + sizeof(struct avro_wrapped_copy);
	dest->size = length;
	dest->user_data = copy;
	dest->free = avro_wrapped_copy_free;
	dest->copy = avro_wrapped_copy_copy;
	dest->slice = NULL;

	avro_refcount_set(&copy->refcount, 1);
	copy->allocated_size = allocated_size;
	memcpy((void *) dest->buf, buf, length);
	return 0;
}

int
avro_wrapped_buffer_new(avro_wrapped_buffer_t *dest,
			const void *buf, size_t length)
{
	dest->buf = buf;
	dest->size = length;
	dest->user_data = NULL;
	dest->free = NULL;
	dest->copy = NULL;
	dest->slice = NULL;
	return 0;
}


void
avro_wrapped_buffer_move(avro_wrapped_buffer_t *dest,
			 avro_wrapped_buffer_t *src)
{
	memcpy(dest, src, sizeof(avro_wrapped_buffer_t));
	memset(src, 0, sizeof(avro_wrapped_buffer_t));
}

int
avro_wrapped_buffer_copy(avro_wrapped_buffer_t *dest,
			 const avro_wrapped_buffer_t *src,
			 size_t offset, size_t length)
{
	if (offset > src->size) {
		avro_set_error("Invalid offset when slicing buffer");
		return EINVAL;
	}

	if ((offset+length) > src->size) {
		avro_set_error("Invalid length when slicing buffer");
		return EINVAL;
	}

	if (src->copy == NULL) {
		return avro_wrapped_buffer_new_copy(dest, (char *) src->buf + offset, length);
	} else {
		return src->copy(dest, src, offset, length);
	}
}

int
avro_wrapped_buffer_slice(avro_wrapped_buffer_t *self,
			  size_t offset, size_t length)
{
	if (offset > self->size) {
		avro_set_error("Invalid offset when slicing buffer");
		return EINVAL;
	}

	if ((offset+length) > self->size) {
		avro_set_error("Invalid length when slicing buffer");
		return EINVAL;
	}

	if (self->slice == NULL) {
		self->buf  = (char *) self->buf + offset;
		self->size = length;
		return 0;
	} else {
		return self->slice(self, offset, length);
	}
}