forked from cheekyguy/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslabs_items_support.h
115 lines (86 loc) · 2.81 KB
/
slabs_items_support.h
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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/**
* the contents of this file must be separate from slabs_items.h because it
* depends on the full contents of memcached.h. the last part of memcached.h
* does not get processed until after slabs_items.h gets processed, so we have
* to do it separately.
*/
#if defined(USE_SLAB_ALLOCATOR)
#if !defined(_slabs_items_support_h_)
#define _slabs_items_support_h_
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "generic.h"
#include "slabs_items.h"
#include "memcached.h"
#define ITEM_data(item) ((char*) &((item)->end) + (item)->nkey)
static inline int add_item_key_to_iov(conn *c, const item* it) {
return add_iov(c, ITEM_key_const(it), ITEM_nkey(it), false);
}
static inline int add_item_value_to_iov(conn *c, const item* it, bool send_cr_lf) {
if (send_cr_lf) {
return (add_iov(c, ITEM_data(it), it->nbytes, false) ||
add_iov(c, "\r\n", 2, false));
} else {
return add_iov(c, ITEM_data(it), it->nbytes, false);
}
}
static inline bool item_setup_receive(item* it, conn* c) {
size_t iov_len_required;
if (c->binary == false) {
iov_len_required = 2;
assert(c->riov == NULL);
assert(c->riov_size == 0);
c->riov = (struct iovec*) alloc_conn_buffer(c->cbg, sizeof(struct iovec) * iov_len_required);
if (c->riov == NULL) {
return false;
}
} else {
iov_len_required = 1;
}
report_max_rusage(c->cbg, c->riov, sizeof(struct iovec) * iov_len_required);
c->riov_size = iov_len_required;
c->riov_left = iov_len_required;
c->riov_curr = 0;
c->riov[0].iov_base = ITEM_data(it);
c->riov[0].iov_len = it->nbytes;
if (c->binary) {
return true;
} else {
c->riov[1].iov_base = c->crlf;
c->riov[1].iov_len = 2;
return true;
}
}
static inline int item_strtoul(const item* it, int base) {
uint32_t value = 0;
char* src;
int i;
for (i = 0, src = ITEM_data(it);
i < ITEM_nbytes(it);
i ++, src ++) {
if (! isdigit(*src)) {
return 0;
} else {
uint32_t prev_value = value;
value = (value * 10) + (*src - '0');
if (prev_value > value) {
/* overflowed. return 0. */
return 0;
}
}
}
return value;
}
static inline void item_memset(item* it, size_t offset, int c, size_t nbytes) {
memset(ITEM_data(it) + offset, c, nbytes);
}
// undefine the macro to resist catch inappropriate use of the macro.
#if defined(__need_ITEM_data)
#undef __need_ITEM_data
#else
#undef ITEM_data
#endif /* #if defined(__need_ITEM_data) */
#endif /* #if !defined(_slabs_items_support_h_) */
#endif /* #if defined(USE_SLAB_ALLOCATOR) */