File: membuf.c

package info (click to toggle)
libopenobex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 972 kB
  • ctags: 1,552
  • sloc: ansic: 9,988; xml: 407; makefile: 96
file content (214 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (2)
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
/**
	\file membuf.c
	Memory buffer handling routines.
	OpenOBEX library - Free implementation of the Object Exchange protocol.

	Copyright (c) 2012 Hendrik Sattler, All Rights Reserved.

	OpenOBEX is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation; either version 2.1 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with OpenOBEX. If not, see <http://www.gnu.org/>.
 */

#include "membuf.h"
#include "databuffer.h"
#include "debug.h"

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

/* If an object has no expected length we have to reallocate every
 * ALLOCATION_TRESHOLD bytes */
#if defined _SC_PAGESIZE
#define ALLOCATION_TRESHOLD (size_t)sysconf(_SC_PAGESIZE)
#else
#define ALLOCATION_TRESHOLD (size_t)4096
#endif

struct membuf_data {
	uint8_t *buffer;
	size_t buffer_size;

	size_t offset;
	size_t data_len;
};

static int membuf_set_size(void *self, size_t new_size) {
	struct membuf_data *p = self;
	uint8_t *tmp;

	new_size += p->offset;
	if (new_size == 0 && p->buffer == NULL)
		return 0;

	if (new_size == 0) {
		free(p->buffer);
		p->buffer = NULL;
		p->data_len = 0;
		p->buffer_size = 0;
		return 0;
	}

	tmp = realloc(p->buffer, new_size);
	if (!tmp)
		return -errno;

	p->buffer = tmp;
	p->buffer_size = new_size;

	if (p->buffer_size < (p->offset + p->data_len)) {
		p->data_len = p->buffer_size - p->offset;

	} else if (p->buffer_size > (p->offset + p->data_len)) {		
		memset(p->buffer + (p->offset + p->data_len), 0,
		       p->buffer_size - (p->offset + p->data_len));
	}
	return 0;
}

static void *membuf_new(size_t default_size) {
	struct membuf_data *p;

	p = malloc(sizeof(*p));
	if (!p)
		return NULL;

	p->buffer = NULL;
	p->buffer_size = 0;
	p->offset = 0;
	p->data_len = 0;

	if (membuf_set_size(p, default_size) < 0) {
		free(p);
		p = NULL;
	}

	return (void*)p;
}

static void membuf_delete(void *self) {
	struct membuf_data *p = self;	

	if (!p)
		return;
	if (p->buffer) {
		free(p->buffer);
	}
	free(p);
}

static size_t membuf_get_offset(void *self) {
	struct membuf_data *p = self;

	if (!p)
		return 0;
	else
		return p->offset;
}

static void membuf_set_offset(void *self, size_t offset) {
	struct membuf_data *p = self;

	if (!p)
		return;
	p->data_len = (p->offset + p->data_len) - offset;
	p->offset = offset;
}

static size_t membuf_get_size(void *self) {
	struct membuf_data *p = self;

	if (!p)
		return 0;
	else
		return p->buffer_size - p->offset;
}

static size_t membuf_get_length(const void *self) {
	const struct membuf_data *p = self;

	if (!p)
		return 0;
	else
		return p->data_len;
}

static void* membuf_get(const void *self) {
	const struct membuf_data *p = self;

	if (!p)
		return NULL;
	else
		return p->buffer + p->offset;
}

static void membuf_clear(void *self, size_t len) {
	struct membuf_data *p = self;

	if (!p || !p->data_len)
		return;

	if (len < p->data_len)
		memmove(membuf_get(self), (uint8_t *)membuf_get(self)+len,
			p->data_len - len);

	p->data_len -= len;
	memset((uint8_t *)membuf_get(self) + p->data_len, 0, len);
}

static int membuf_append(void *self, const void *data, size_t len) {
	struct membuf_data *p = self;

	if (!p)
		return -EINVAL;

	DEBUG(4, "Request to append %lu bytes\n", (unsigned long)len);
	if (len > (p->buffer_size - (p->offset + p->data_len))) {
		size_t alloc_size = ALLOCATION_TRESHOLD;
		int ret;
		if (len > alloc_size)
			alloc_size = len;
		DEBUG(4, "Resizing buffer to %lu\n",
		      (unsigned long)p->buffer_size + alloc_size);
		ret = membuf_set_size(self, p->buffer_size + alloc_size);
		if (ret < 0) {
			DEBUG(4, "Resizing failed\n");
			return ret;
		}
	}

	if (data) {
		void *dest = ((uint8_t *)p->buffer) + (p->offset + p->data_len);
		memcpy(dest, data, len);
	}
	p->data_len += len;
	return 0;
}

static struct databuffer_ops membuf_ops = {
	&membuf_new,
	&membuf_delete,
	&membuf_get_offset,
	&membuf_set_offset,
	&membuf_get_size,
	&membuf_set_size,
	&membuf_get_length,
	&membuf_get,
	&membuf_clear,
	&membuf_append,
};

struct databuffer *membuf_create(size_t default_size) {
	return buf_create(default_size, &membuf_ops);
}