summaryrefslogtreecommitdiff
path: root/c/vrefbuffer.c
blob: bbaf61d75d21d5adbdcf069b7aad13d1e7868c5a (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
/*
 * MessagePack for C zero-copy buffer implementation
 *
 * Copyright (C) 2008-2009 FURUHASHI Sadayuki
 *
 *    Licensed 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
 *
 *        http://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 "msgpack/vrefbuffer.h"
#include <stdlib.h>
#include <string.h>

bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
		size_t ref_size, size_t chunk_size)
{
	if(chunk_size < sizeof(msgpack_vrefbuffer_chunk)+72) {
		chunk_size = 72;
	} else {
		chunk_size -= sizeof(msgpack_vrefbuffer_chunk);
	}

	vbuf->chunk_size = chunk_size;
	vbuf->ref_size = ref_size;

	// glibcは72バイト以下のmallocが高速
	size_t nfirst = (sizeof(struct iovec) < 72/2) ?
			72 / sizeof(struct iovec) : 8;

	struct iovec* array = (struct iovec*)malloc(
			sizeof(struct iovec) * nfirst);
	if(array == NULL) {
		return false;
	}

	vbuf->tail  = array;
	vbuf->end   = array + nfirst;
	vbuf->array = array;

	vbuf->chunk = (msgpack_vrefbuffer_chunk*)malloc(
			chunk_size + sizeof(msgpack_vrefbuffer_chunk));
	if(vbuf->chunk == NULL) {
		free(array);
		return false;
	}

	vbuf->chunk->next = NULL;
	vbuf->chunk->free = chunk_size;

	return true;
}

void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
{
	msgpack_vrefbuffer_chunk* c = vbuf->chunk;
	while(true) {
		msgpack_vrefbuffer_chunk* n = c->next;
		free(c);
		if(n) {
			c = n;
		} else {
			break;
		}
	}
	free(vbuf->array);
}

int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
		const char* buf, unsigned int len)
{
	if(vbuf->tail == vbuf->end) {
		const size_t nused = vbuf->end - vbuf->array;
		const size_t nnext = nused * 2;

		struct iovec* nvec = (struct iovec*)realloc(
				vbuf->array, sizeof(struct iovec)*nnext);
		if(nvec == NULL) {
			return -1;
		}

		vbuf->array = nvec;
		vbuf->end   = nvec + nnext;
		vbuf->tail  = nvec + nused;
	}

	vbuf->tail->iov_base = (char*)buf;
	vbuf->tail->iov_len  = len;
	++vbuf->tail;

	return 0;
}

int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
		const char* buf, unsigned int len)
{
	msgpack_vrefbuffer_chunk* chunk = vbuf->chunk;
	size_t cur_size = vbuf->chunk_size;

	if(chunk->free < len) {
		cur_size = (cur_size > len) ? cur_size : len;

		chunk = (msgpack_vrefbuffer_chunk*)malloc(
				cur_size + sizeof(msgpack_vrefbuffer_chunk));
		if(chunk == NULL) {
			return -1;
		}

		chunk->free = cur_size;
		chunk->next = vbuf->chunk;
		vbuf->chunk = chunk;
	}

	char* m = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk)
		+ (cur_size - chunk->free);

	memcpy(m, buf, len);
	chunk->free -= len;

	if(vbuf->tail != vbuf->array && m ==
			(const char*)((vbuf->tail-1)->iov_base) + (vbuf->tail-1)->iov_len) {
		(vbuf->tail-1)->iov_len += len;
		return 0;
	} else {
		return msgpack_vrefbuffer_append_ref(vbuf, m, len);
	}
}