Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ibuf: don't clash with tarantool's ibuf_*() symbols #111

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Hacking

Here we collect arcane knowledge, which may be useful for developers of the
module.

## memcached_ibuf

`third_party/memcached_ibuf.[ch]` is the copy of ibuf from the small library
with renaming of the functions. The motivation is to avoid possible name clash
between tarantool's symbols and the module's symbols. See [1] and [2] for
details.

We can remove it, when we'll dedice to drop support of tarantool versions
affected by [1] or will find another way to overcome the name clash (see [3]).

[1]: https://github.com/tarantool/tarantool/issues/6873
[2]: https://github.com/tarantool/memcached/issues/59#issuecomment-1081106140
[3]: https://github.com/tarantool/memcached/issues/92#issuecomment-1081128938
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,8 @@ For custom configuration file path, please, use `SASL_CONF_PATH` environment var
## Caution

This rock is in early beta.

## Hacking

We're collecting information for the module developers in the
[HACKING.md](HACKING.md) file.
1 change: 1 addition & 0 deletions memcached/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_library(internalso SHARED
"internal/expiration.c"
"internal/memcached.c"
"internal/mc_sasl.c"
"${CMAKE_SOURCE_DIR}/third_party/memcached_ibuf.c"
)

target_link_libraries(internalso msgpuck)
Expand Down
20 changes: 10 additions & 10 deletions memcached/internal/memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#include <stdbool.h>

#include <tarantool/module.h>
#include <small/ibuf.h>
#include <small/obuf.h>

#include "alloc.h"
#include "memcached.h"
#include "memcached_ibuf.h"
#include "memcached_layer.h"
#include "error.h"
#include "network.h"
Expand All @@ -56,9 +56,9 @@ destroy_allocations() {
static inline int
memcached_skip_request(struct memcached_connection *con) {
struct ibuf *in = con->in;
while (ibuf_used(in) < con->len && con->noprocess) {
con->len -= ibuf_used(in);
ibuf_reset(in);
while (memcached_ibuf_used(in) < con->len && con->noprocess) {
con->len -= memcached_ibuf_used(in);
memcached_ibuf_reset(in);
ssize_t read = mnet_read_ibuf(con->fd, in, 1);
if (read == -1)
memcached_error_ENOMEM(1, "ibuf");
Expand All @@ -77,10 +77,10 @@ memcached_flush(struct memcached_connection *con) {
obuf_iovcnt(con->out),
obuf_size(con->out));
con->cfg->stat.bytes_written += total;
if (ibuf_used(con->in) == 0)
ibuf_reset(con->in);
if (memcached_ibuf_used(con->in) == 0)
memcached_ibuf_reset(con->in);
obuf_reset(con->out);
if (ibuf_reserve(con->in, con->cfg->readahead) == NULL)
if (memcached_ibuf_reserve(con->in, con->cfg->readahead) == NULL)
return -1;
return total;
}
Expand All @@ -99,7 +99,7 @@ memcached_connection_gc(struct memcached_connection *con)
static inline int
memcached_loop_read(struct memcached_connection *con, size_t to_read)
{
if (ibuf_reserve(con->in, to_read) == NULL) {
if (memcached_ibuf_reserve(con->in, to_read) == NULL) {
/* memcached_error_ENOMEM(to_read, "ibuf");*/
return -1;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ memcached_loop(struct memcached_connection *con)
if (con->close_connection) {
say_debug("Requesting exit. Exiting.");
break;
} else if (rc == 0 && ibuf_used(con->in) > 0 &&
} else if (rc == 0 && memcached_ibuf_used(con->in) > 0 &&
batch_count < con->cfg->batch_count) {
batch_count++;
goto next;
Expand All @@ -215,7 +215,7 @@ memcached_loop(struct memcached_connection *con)
memcached_flush(con);
fiber_reschedule();
batch_count = 0;
if (ibuf_used(con->in) > 0) {
if (memcached_ibuf_used(con->in) > 0) {
goto next;
}
continue;
Expand Down
12 changes: 7 additions & 5 deletions memcached/internal/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

#include <tarantool/module.h>
#include <small/mempool.h>
#include <small/ibuf.h>
#include <small/obuf.h>

#include "alloc.h"
#include "memcached.h"
#include "memcached_ibuf.h"
#include "constants.h"
#include "network.h"

Expand Down Expand Up @@ -49,7 +49,8 @@ ibuf_new()
{
void *ibuf = mempool_alloc(&ibuf_pool);
if (ibuf == NULL) return NULL;
ibuf_create((struct ibuf *)ibuf, memcached_slab_cache(), iobuf_readahead);
memcached_ibuf_create((struct ibuf *)ibuf, memcached_slab_cache(),
iobuf_readahead);
return ibuf;
}

Expand All @@ -65,7 +66,7 @@ obuf_new()
void
iobuf_delete(struct ibuf *ibuf, struct obuf *obuf)
{
ibuf_destroy(ibuf);
memcached_ibuf_destroy(ibuf);
obuf_destroy(obuf);
mempool_free(&ibuf_pool, ibuf);
mempool_free(&obuf_pool, obuf);
Expand Down Expand Up @@ -148,10 +149,11 @@ mnet_read_ahead(int fd, void *buf, size_t bufsz, size_t sz)
size_t
mnet_read_ibuf(int fd, struct ibuf *buf, size_t sz)
{
if (ibuf_reserve(buf, sz) == NULL) {
if (memcached_ibuf_reserve(buf, sz) == NULL) {
return -1;
}
ssize_t n = mnet_read_ahead(fd, buf->wpos, ibuf_unused(buf), sz);
ssize_t n = mnet_read_ahead(fd, buf->wpos, memcached_ibuf_unused(buf),
sz);
buf->wpos += n;
return n;
}
Expand Down
2 changes: 1 addition & 1 deletion memcached/internal/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define TIMEOUT_INFINITY 365*86400*100.0

#include <small/ibuf.h>
#include "memcached_ibuf.h"

size_t
mnet_writev(int fd, struct iovec *iov, int iovcnt, size_t size_hint);
Expand Down
2 changes: 1 addition & 1 deletion memcached/internal/proto_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "memcached_layer.h"
#include "mc_sasl.h"

#include <small/ibuf.h>
#include "memcached_ibuf.h"
#include <small/obuf.h>

static inline int
Expand Down
2 changes: 1 addition & 1 deletion memcached/internal/proto_txt.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include <tarantool/module.h>
#include <msgpuck.h>
#include <small/ibuf.h>
#include <small/obuf.h>

#include "memcached.h"
#include "memcached_ibuf.h"
#include "constants.h"
#include "memcached_layer.h"
#include "error.h"
Expand Down
107 changes: 107 additions & 0 deletions third_party/memcached_ibuf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "memcached_ibuf.h"
#include <string.h>
#include <small/slab_cache.h>

/** Initialize an input buffer. */
void
memcached_ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc,
size_t start_capacity)
{
ibuf->slabc = slabc;
ibuf->buf = ibuf->rpos = ibuf->wpos = ibuf->end = NULL;
ibuf->start_capacity = start_capacity;
/* Don't allocate the buffer yet. */
}

void
memcached_ibuf_destroy(struct ibuf *ibuf)
{
if (ibuf->buf) {
struct slab *slab = slab_from_data(ibuf->buf);
slab_put(ibuf->slabc, slab);
}
}

/** Free memory allocated by this buffer */
void
memcached_ibuf_reinit(struct ibuf *ibuf)
{
struct slab_cache *slabc = ibuf->slabc;
size_t start_capacity = ibuf->start_capacity;
memcached_ibuf_destroy(ibuf);
memcached_ibuf_create(ibuf, slabc, start_capacity);
}

/**
* Ensure the buffer has sufficient capacity
* to store size bytes, and return pointer to
* the beginning.
*/
void *
memcached_ibuf_reserve_slow(struct ibuf *ibuf, size_t size)
{
assert(ibuf->wpos + size > ibuf->end);
size_t used = memcached_ibuf_used(ibuf);
size_t capacity = memcached_ibuf_capacity(ibuf);
/*
* Check if we have enough space in the
* current buffer. In this case de-fragment it
* by moving existing data to the beginning.
* Otherwise, get a bigger buffer.
*/
if (size + used <= capacity) {
memmove(ibuf->buf, ibuf->rpos, used);
} else {
/* Use iobuf_readahead as allocation factor. */
size_t new_capacity = capacity * 2;
if (new_capacity < ibuf->start_capacity)
new_capacity = ibuf->start_capacity;

while (new_capacity < used + size)
new_capacity *= 2;

struct slab *slab = slab_get(ibuf->slabc, new_capacity);
if (slab == NULL)
return NULL;
char *ptr = (char *) slab_data(slab);
memcpy(ptr, ibuf->rpos, used);
if (ibuf->buf)
slab_put(ibuf->slabc, slab_from_data(ibuf->buf));
ibuf->buf = ptr;
ibuf->end = ibuf->buf + slab_capacity(slab);
}
ibuf->rpos = ibuf->buf;
ibuf->wpos = ibuf->rpos + used;
return ibuf->wpos;
}

Loading