Skip to content

Commit 417e9d7

Browse files
committed
ibuf: copy ibuf.[ch] into module's code
Just copy and change includes. It is necessary to change function names in a next commit. The source is the small version, which is bundled into the module: 1.1-63-g3df5050. Part of #59
1 parent 5aaac83 commit 417e9d7

File tree

8 files changed

+318
-5
lines changed

8 files changed

+318
-5
lines changed

memcached/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_library(internalso SHARED
3939
"internal/expiration.c"
4040
"internal/memcached.c"
4141
"internal/mc_sasl.c"
42+
"${CMAKE_SOURCE_DIR}/third_party/memcached_ibuf.c"
4243
)
4344

4445
target_link_libraries(internalso msgpuck)

memcached/internal/memcached.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
#include <stdbool.h>
3535

3636
#include <tarantool/module.h>
37-
#include <small/ibuf.h>
3837
#include <small/obuf.h>
3938

4039
#include "memcached.h"
40+
#include "memcached_ibuf.h"
4141
#include "memcached_layer.h"
4242
#include "error.h"
4343
#include "network.h"

memcached/internal/network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
#include <tarantool/module.h>
1313
#include <small/mempool.h>
14-
#include <small/ibuf.h>
1514
#include <small/obuf.h>
1615

1716
#include "memcached.h"
17+
#include "memcached_ibuf.h"
1818
#include "constants.h"
1919
#include "network.h"
2020

memcached/internal/network.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#define TIMEOUT_INFINITY 365*86400*100.0
55

6-
#include <small/ibuf.h>
6+
#include "memcached_ibuf.h"
77

88
size_t
99
mnet_writev(int fd, struct iovec *iov, int iovcnt, size_t size_hint);

memcached/internal/proto_bin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "memcached_layer.h"
1616
#include "mc_sasl.h"
1717

18-
#include <small/ibuf.h>
18+
#include "memcached_ibuf.h"
1919
#include <small/obuf.h>
2020

2121
static inline int

memcached/internal/proto_txt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
#include <tarantool/module.h>
66
#include <msgpuck.h>
7-
#include <small/ibuf.h>
87
#include <small/obuf.h>
98

109
#include "memcached.h"
10+
#include "memcached_ibuf.h"
1111
#include "constants.h"
1212
#include "memcached_layer.h"
1313
#include "error.h"

third_party/memcached_ibuf.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
3+
*
4+
* Redistribution and use in source and binary forms, with or
5+
* without modification, are permitted provided that the following
6+
* conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above
9+
* copyright notice, this list of conditions and the
10+
* following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following
14+
* disclaimer in the documentation and/or other materials
15+
* provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21+
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29+
* SUCH DAMAGE.
30+
*/
31+
#include "memcached_ibuf.h"
32+
#include <string.h>
33+
#include <small/slab_cache.h>
34+
35+
/** Initialize an input buffer. */
36+
void
37+
ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t start_capacity)
38+
{
39+
ibuf->slabc = slabc;
40+
ibuf->buf = ibuf->rpos = ibuf->wpos = ibuf->end = NULL;
41+
ibuf->start_capacity = start_capacity;
42+
/* Don't allocate the buffer yet. */
43+
}
44+
45+
void
46+
ibuf_destroy(struct ibuf *ibuf)
47+
{
48+
if (ibuf->buf) {
49+
struct slab *slab = slab_from_data(ibuf->buf);
50+
slab_put(ibuf->slabc, slab);
51+
}
52+
}
53+
54+
/** Free memory allocated by this buffer */
55+
void
56+
ibuf_reinit(struct ibuf *ibuf)
57+
{
58+
struct slab_cache *slabc = ibuf->slabc;
59+
size_t start_capacity = ibuf->start_capacity;
60+
ibuf_destroy(ibuf);
61+
ibuf_create(ibuf, slabc, start_capacity);
62+
}
63+
64+
/**
65+
* Ensure the buffer has sufficient capacity
66+
* to store size bytes, and return pointer to
67+
* the beginning.
68+
*/
69+
void *
70+
ibuf_reserve_slow(struct ibuf *ibuf, size_t size)
71+
{
72+
assert(ibuf->wpos + size > ibuf->end);
73+
size_t used = ibuf_used(ibuf);
74+
size_t capacity = ibuf_capacity(ibuf);
75+
/*
76+
* Check if we have enough space in the
77+
* current buffer. In this case de-fragment it
78+
* by moving existing data to the beginning.
79+
* Otherwise, get a bigger buffer.
80+
*/
81+
if (size + used <= capacity) {
82+
memmove(ibuf->buf, ibuf->rpos, used);
83+
} else {
84+
/* Use iobuf_readahead as allocation factor. */
85+
size_t new_capacity = capacity * 2;
86+
if (new_capacity < ibuf->start_capacity)
87+
new_capacity = ibuf->start_capacity;
88+
89+
while (new_capacity < used + size)
90+
new_capacity *= 2;
91+
92+
struct slab *slab = slab_get(ibuf->slabc, new_capacity);
93+
if (slab == NULL)
94+
return NULL;
95+
char *ptr = (char *) slab_data(slab);
96+
memcpy(ptr, ibuf->rpos, used);
97+
if (ibuf->buf)
98+
slab_put(ibuf->slabc, slab_from_data(ibuf->buf));
99+
ibuf->buf = ptr;
100+
ibuf->end = ibuf->buf + slab_capacity(slab);
101+
}
102+
ibuf->rpos = ibuf->buf;
103+
ibuf->wpos = ibuf->rpos + used;
104+
return ibuf->wpos;
105+
}
106+

third_party/memcached_ibuf.h

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#ifndef TARANTOOL_SMALL_IBUF_H_INCLUDED
2+
#define TARANTOOL_SMALL_IBUF_H_INCLUDED
3+
/*
4+
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
5+
*
6+
* Redistribution and use in source and binary forms, with or
7+
* without modification, are permitted provided that the following
8+
* conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above
11+
* copyright notice, this list of conditions and the
12+
* following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above
15+
* copyright notice, this list of conditions and the following
16+
* disclaimer in the documentation and/or other materials
17+
* provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23+
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31+
* SUCH DAMAGE.
32+
*/
33+
#include <stddef.h>
34+
#include <assert.h>
35+
36+
#if defined(__cplusplus)
37+
extern "C" {
38+
#endif /* defined(__cplusplus) */
39+
40+
/** @module Input buffer. */
41+
42+
struct slab_cache;
43+
44+
/*
45+
* Continuous piece of memory to store input.
46+
* Allocated in factors of 'start_capacity'.
47+
* Maintains position of the data "to be processed".
48+
*
49+
* Typical use case:
50+
*
51+
* struct ibuf *in;
52+
* coio_bread(coio, in, request_len);
53+
* if (ibuf_size(in) >= request_len) {
54+
* process_request(in->rpos, request_len);
55+
* in->rpos += request_len;
56+
* }
57+
*/
58+
struct ibuf
59+
{
60+
struct slab_cache *slabc;
61+
char *buf;
62+
/** Start of input. */
63+
char *rpos;
64+
/** End of useful input */
65+
char *wpos;
66+
/** End of buffer. */
67+
char *end;
68+
size_t start_capacity;
69+
};
70+
71+
void
72+
ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t start_capacity);
73+
74+
void
75+
ibuf_destroy(struct ibuf *ibuf);
76+
77+
void
78+
ibuf_reinit(struct ibuf *ibuf);
79+
80+
/** How much data is read and is not parsed yet. */
81+
static inline size_t
82+
ibuf_used(struct ibuf *ibuf)
83+
{
84+
assert(ibuf->wpos >= ibuf->rpos);
85+
return ibuf->wpos - ibuf->rpos;
86+
}
87+
88+
/** How much data can we fit beyond buf->wpos */
89+
static inline size_t
90+
ibuf_unused(struct ibuf *ibuf)
91+
{
92+
assert(ibuf->wpos <= ibuf->end);
93+
return ibuf->end - ibuf->wpos;
94+
}
95+
96+
/** How much memory is allocated */
97+
static inline size_t
98+
ibuf_capacity(struct ibuf *ibuf)
99+
{
100+
return ibuf->end - ibuf->buf;
101+
}
102+
103+
/**
104+
* Integer value of the position in the buffer - stable
105+
* in case of realloc.
106+
*/
107+
static inline size_t
108+
ibuf_pos(struct ibuf *ibuf)
109+
{
110+
assert(ibuf->buf <= ibuf->rpos);
111+
return ibuf->rpos - ibuf->buf;
112+
}
113+
114+
/** Forget all cached input. */
115+
static inline void
116+
ibuf_reset(struct ibuf *ibuf)
117+
{
118+
ibuf->rpos = ibuf->wpos = ibuf->buf;
119+
}
120+
121+
void *
122+
ibuf_reserve_slow(struct ibuf *ibuf, size_t size);
123+
124+
static inline void *
125+
ibuf_reserve(struct ibuf *ibuf, size_t size)
126+
{
127+
if (ibuf->wpos + size <= ibuf->end)
128+
return ibuf->wpos;
129+
return ibuf_reserve_slow(ibuf, size);
130+
}
131+
132+
static inline void *
133+
ibuf_alloc(struct ibuf *ibuf, size_t size)
134+
{
135+
void *ptr;
136+
if (ibuf->wpos + size <= ibuf->end)
137+
ptr = ibuf->wpos;
138+
else {
139+
ptr = ibuf_reserve_slow(ibuf, size);
140+
if (ptr == NULL)
141+
return NULL;
142+
}
143+
ibuf->wpos += size;
144+
return ptr;
145+
}
146+
147+
static inline void *
148+
ibuf_reserve_cb(void *ctx, size_t *size)
149+
{
150+
struct ibuf *buf = (struct ibuf *) ctx;
151+
void *p = ibuf_reserve(buf, *size ? *size : buf->start_capacity);
152+
*size = ibuf_unused(buf);
153+
return p;
154+
}
155+
156+
static inline void *
157+
ibuf_alloc_cb(void *ctx, size_t size)
158+
{
159+
return ibuf_alloc((struct ibuf *) ctx, size);
160+
}
161+
162+
#if defined(__cplusplus)
163+
} /* extern "C" */
164+
165+
#include "exception.h"
166+
167+
/** Reserve space for sz bytes in the input buffer. */
168+
static inline void *
169+
ibuf_reserve_xc(struct ibuf *ibuf, size_t size)
170+
{
171+
void *ptr = ibuf_reserve(ibuf, size);
172+
if (ptr == NULL)
173+
tnt_raise(OutOfMemory, size, "ibuf", "reserve");
174+
return ptr;
175+
}
176+
177+
static inline void *
178+
ibuf_alloc_xc(struct ibuf *ibuf, size_t size)
179+
{
180+
void *ptr = ibuf_alloc(ibuf, size);
181+
if (ptr == NULL)
182+
tnt_raise(OutOfMemory, size, "ibuf", "alloc");
183+
return ptr;
184+
}
185+
186+
static inline void *
187+
ibuf_reserve_xc_cb(void *ctx, size_t *size)
188+
{
189+
void *ptr = ibuf_reserve_cb(ctx, size);
190+
if (ptr == NULL)
191+
tnt_raise(OutOfMemory, *size, "ibuf", "reserve");
192+
return ptr;
193+
}
194+
195+
static inline void *
196+
ibuf_alloc_xc_cb(void *ctx, size_t size)
197+
{
198+
void *ptr = ibuf_alloc_cb(ctx, size);
199+
if (ptr == NULL)
200+
tnt_raise(OutOfMemory, size, "ibuf", "alloc");
201+
return ptr;
202+
}
203+
204+
#endif /* defined(__cplusplus) */
205+
206+
#endif /* TARANTOOL_SMALL_IBUF_H_INCLUDED */

0 commit comments

Comments
 (0)