-
Notifications
You must be signed in to change notification settings - Fork 55
/
mc_util.c
665 lines (599 loc) · 18.3 KB
/
mc_util.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2018 JaM2in Co., Ltd.
*
* 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 <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mc_util.h"
/*
* memory block : internal functions
*/
static void do_mblck_pool_init(mblck_pool_t *pool, uint32_t blck_len)
{
pool->tail = NULL;
pool->head = NULL;
pool->blck_len = blck_len;
pool->body_len = blck_len - sizeof(void *);
pool->used_cnt = 0;
pool->free_cnt = 0;
}
static uint32_t do_mblck_pool_grow(mblck_pool_t *pool, uint32_t blck_cnt)
{
mblck_node_t *blck_ptr;
int count;
for (count = 0; count < blck_cnt; count++) {
blck_ptr = (mblck_node_t *)malloc(pool->blck_len);
if (blck_ptr == NULL) {
break;
}
blck_ptr->next = NULL;
if (pool->tail) pool->tail->next = blck_ptr;
else pool->head = blck_ptr;
pool->tail = blck_ptr;
}
pool->free_cnt += count;
return count;
}
static void do_mblck_pool_final(mblck_pool_t *pool)
{
mblck_node_t *blck_ptr;
while (pool->head != NULL) {
blck_ptr = pool->head;
pool->head = pool->head->next;
free(blck_ptr);
pool->free_cnt -= 1;
}
assert(pool->free_cnt == 0);
pool->tail = NULL;
pool->blck_len = 0;
pool->body_len = 0;
}
/*
* memory block : external functions
*/
int mblck_pool_create(mblck_pool_t *pool, uint32_t blck_len, uint32_t blck_cnt)
{
do_mblck_pool_init(pool, blck_len);
if (do_mblck_pool_grow(pool, blck_cnt) < blck_cnt) {
/* incomplete memory block pool */
do_mblck_pool_final(pool);
return -1;
}
return 0;
}
void mblck_pool_destroy(mblck_pool_t *pool)
{
assert(pool->used_cnt == 0);
do_mblck_pool_final(pool);
}
int mblck_list_alloc(mblck_pool_t *pool, uint32_t item_len, uint32_t item_cnt,
mblck_list_t *list)
{
uint32_t nitems_per_blck = pool->body_len / item_len;
uint32_t blck_cnt = ((item_cnt-1) / nitems_per_blck) + 1;
if (pool->free_cnt < blck_cnt) {
uint32_t need_cnt = blck_cnt - pool->free_cnt;
if (do_mblck_pool_grow(pool, need_cnt) < need_cnt) {
return -1; /* out of memory */
}
}
assert(pool->free_cnt >= blck_cnt);
list->pool = (void*)pool;
list->head = pool->head;
list->tail = list->head;
list->blck_cnt = 1;
while (list->blck_cnt < blck_cnt) {
list->tail = list->tail->next;
list->blck_cnt += 1;
}
pool->head = list->tail->next;
list->tail->next = NULL;
if (pool->head == NULL) {
pool->tail = NULL;
}
pool->used_cnt += list->blck_cnt;
pool->free_cnt -= list->blck_cnt;
list->item_cnt = item_cnt;
list->item_len = item_len;
list->body_len = nitems_per_blck * item_len;
return 0;
}
void mblck_list_merge(mblck_list_t *pri_list, mblck_list_t *add_list)
{
assert(pri_list->pool == add_list->pool);
//assert(pri_list->item_len == add_list->item_len);
pri_list->tail->next = add_list->head;
pri_list->tail = add_list->tail;
pri_list->blck_cnt += add_list->blck_cnt;
/* clear the add_list */
add_list->head = NULL;
add_list->tail = NULL;
add_list->blck_cnt = 0;
/* FIXME: item_cnt and item_len: how to merge them ? */
}
void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
{
assert(list->pool != NULL);
if (list->blck_cnt > 0) {
list->tail->next = pool->head;
pool->head = list->head;
if (pool->tail == NULL) {
pool->tail = list->tail;
}
pool->used_cnt -= list->blck_cnt;
pool->free_cnt += list->blck_cnt;
/* clear the list */
list->head = NULL;
list->tail = NULL;
list->blck_cnt = 0;
}
list->pool = NULL;
}
/*
* token buffer functions
*/
int token_buff_create(token_buff_t *buff, uint32_t count)
{
buff->array = malloc(sizeof(token_t) * count);
if (buff->array == NULL) {
return -1;
}
buff->count = count;
buff->nused = 0;
return 0;
}
void token_buff_destroy(token_buff_t *buff)
{
assert(buff->nused == 0);
free(buff->array);
buff->array = NULL;
buff->count = 0;
}
void *token_buff_get(token_buff_t *buff, uint32_t count)
{
assert(buff->nused == 0);
if (count > buff->count) {
token_t *new_array;
uint32_t new_count = buff->count;
while (new_count < count) {
new_count += 5000;
}
new_array = realloc(buff->array, sizeof(token_t) * new_count);
if (new_array == NULL) {
return NULL;
}
buff->count = new_count;
buff->array = new_array;
}
buff->nused += 1;
return (void*)buff->array;
}
void token_buff_release(token_buff_t *buff, void *tokens)
{
assert(tokens == (void*)buff->array);
assert(buff->nused == 1);
buff->nused -= 1;
}
/*
* tokernize functions
*/
/*
* Tokenize the command string by replacing whitespace with '\0' and update
* the token array tokens with pointer to start of each token and length.
* Returns total number of tokens. The last valid token is the terminal
* token (value points to the first unprocessed character of the string and
* length zero).
*
* Usage example:
*
* while(tokenize_command(command, ncommand, tokens, max_tokens) > 0) {
* for(int ix = 0; tokens[ix].length != 0; ix++) {
* ...
* }
* ncommand = tokens[ix].value - command;
* command = tokens[ix].value;
* }
*/
size_t tokenize_command(char *command, int cmdlen, token_t *tokens, const size_t max_tokens)
{
assert(command != NULL && tokens != NULL && max_tokens > 1);
char *s, *e = NULL;
size_t ntokens = 0;
size_t checked = 0;
s = command;
while (ntokens < max_tokens - 1) {
e = memchr(s, ' ', cmdlen - checked);
if (e) {
if (s != e) {
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
*e = '\0';
}
s = (++e);
checked = s - command;
} else {
e = command + cmdlen;
if (s != e) {
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
}
break; /* string end */
}
}
/*
* If we scanned the whole string, the terminal value pointer is null,
* otherwise it is the first unprocessed character.
*/
if (*e == '\0') {
tokens[ntokens].value = NULL;
} else {
assert(ntokens == (max_tokens-1));
tokens[ntokens].value = e;
/* The next reserved token keeps the length of untokenized command. */
tokens[ntokens+1].length = cmdlen - (e - command);
}
tokens[ntokens].length = 0;
ntokens++;
return ntokens;
}
int detokenize(token_t *tokens, int ntokens, char *buffer, int length)
{
int i, nb;
char *p;
nb = ntokens; // account for spaces, which is ntokens-1, plus the null
for (i = 0; i < ntokens; ++i) {
nb += tokens[i].length;
}
if (nb > length) {
return -1; /* buffer overflow */
}
p = buffer;
for (i = 0; i < ntokens; ++i) {
memcpy(p, tokens[i].value, tokens[i].length);
p += tokens[i].length;
*p = ' ';
p++;
}
buffer[nb - 1] = '\0';
return nb;
}
int tokenize_keys(char *keystr, int keylen, int keycnt, char delimiter, token_t *tokens)
{
assert(keystr != NULL && keylen > 0 && keycnt > 0 && tokens != NULL);
char *s, *e;
int checked = 0;
int ntokens = 0;
bool finish = false;
s = keystr;
for (e = s; ntokens < keycnt; ++e, ++checked) {
if (checked >= keylen) {
if (s == e) break;
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
if (ntokens == keycnt)
finish = true;
break; /* string end */
}
if (*e == delimiter) {
if (s == e) break;
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
s = e + 1;
} else if (*e == ' ') {
break; /* invalid character in key string */
}
}
if (finish == true) {
return ntokens;
} else {
return -1; /* some errors */
}
}
/*
* string memory block
*/
static int check_sblock_tail_string(mblck_list_t *sblcks, int length)
{
mblck_node_t *blckptr;
char *dataptr;
uint32_t bodylen = MBLCK_GET_BODYLEN(sblcks);
uint32_t lastlen;
uint32_t numblks;
/* check the last "\r\n" string */
blckptr = MBLCK_GET_TAILBLK(sblcks);
dataptr = MBLCK_GET_BODYPTR(blckptr);
lastlen = (length % bodylen) > 0
? (length % bodylen) : bodylen;
if (*(dataptr + lastlen - 1) != '\n') {
return -1; /* invalid strings */
}
if ((--lastlen) == 0) {
numblks = MBLCK_GET_NUMBLKS(sblcks);
blckptr = MBLCK_GET_HEADBLK(sblcks);
for (int i = 1; i < numblks-1; i++) {
blckptr = MBLCK_GET_NEXTBLK(blckptr);
}
dataptr = MBLCK_GET_BODYPTR(blckptr);
lastlen = bodylen;
}
if (*(dataptr + lastlen - 1) != '\r') {
return -1; /* invalid strings */
}
return 0;
}
static int tokenize_mblck(char *keystr, int keylen, int keycnt,
char delimiter, token_t *tokens)
{
assert(keylen > 0 && keycnt > 0);
char *s, *e;
int checked = 0;
int ntokens = 0;
bool finish = false;
s = keystr;
for (e = s; ntokens < keycnt; ++e, ++checked) {
if (checked >= keylen) {
if (s == e) break;
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
finish = true;
break; /* string end */
}
if (*e == delimiter) {
if (s == e) break;
tokens[ntokens].value = s;
tokens[ntokens].length = e - s;
ntokens++;
s = e + 1;
} else if (*e == ' ') {
break; /* invalid character in key string */
}
}
if (finish == true) {
return ntokens;
} else {
return -1; /* some errors */
}
}
/* segmented token structure */
typedef struct {
char *value;
uint32_t length;
uint32_t tokidx;
} segtok_t;
static ENGINE_ERROR_CODE concat_segmented_tokens(mblck_list_t *blist, token_t *tokens,
segtok_t *segtoks, int nsegtok, int maxklen)
{
assert(blist->pool != NULL);
mblck_list_t newblcks;
mblck_node_t *blckptr;
char *dataptr;
char *saveptr;
token_t *tok_ptr;
uint32_t bodylen = MBLCK_GET_BODYLEN(blist);
uint32_t numblks = 1;
uint32_t datalen = 0;
uint32_t complen, i;
/* calculate the # of blocks needed */
for (i = 0; i < nsegtok; i++) {
tok_ptr = &tokens[segtoks[i].tokidx];
complen = segtoks[i].length + tok_ptr->length;
if (complen > maxklen) break;
if ((datalen + complen) > bodylen) {
numblks += 1;
datalen = complen;
} else {
datalen += complen;
}
}
if (i < nsegtok) {
return ENGINE_EBADVALUE;
}
/* allocate new mblock list */
if (mblck_list_alloc((mblck_pool_t*)blist->pool, bodylen, numblks, &newblcks) < 0) {
return ENGINE_ENOMEM; /* out of memory */
}
/* build the complete strings with new mblock */
blckptr = MBLCK_GET_HEADBLK(&newblcks);
dataptr = MBLCK_GET_BODYPTR(blckptr);
datalen = 0;
for (i = 0; i < nsegtok; i++) {
tok_ptr = &tokens[segtoks[i].tokidx];
complen = segtoks[i].length + tok_ptr->length;
if ((datalen + complen) > bodylen) {
blckptr = MBLCK_GET_NEXTBLK(blckptr);
dataptr = MBLCK_GET_BODYPTR(blckptr);
datalen = 0;
}
saveptr = &dataptr[datalen];
memcpy(dataptr + datalen, segtoks[i].value, segtoks[i].length);
datalen += segtoks[i].length;
memcpy(dataptr + datalen, tok_ptr->value, tok_ptr->length);
datalen += tok_ptr->length;
tok_ptr->value = saveptr;
tok_ptr->length = complen;
}
/* merge to main mblock list */
mblck_list_merge(blist, &newblcks);
return ENGINE_SUCCESS;
}
#define SEGTOK_ARRAY_SIZE 128
/*
* Assume key string blocks without the trailing "\r\n" characters.
*/
ENGINE_ERROR_CODE tokenize_mblocks(mblck_list_t *blist, int keylen, int keycnt,
int maxklen, bool must_backward_compatible,
token_t *tokens)
{
assert(keylen > 0 && keycnt > 0 && tokens != NULL);
mblck_node_t *blckptr;
char *dataptr;
uint32_t bodylen = MBLCK_GET_BODYLEN(blist);
uint32_t numblks = ((keylen - 1) / bodylen) + 1;
uint32_t lastlen = (keylen % bodylen) > 0
? (keylen % bodylen) : bodylen;
uint32_t ntokens;
uint32_t chkblks;
uint32_t datalen;
segtok_t segtoks[SEGTOK_ARRAY_SIZE];
uint32_t nsegtok;
char delimiter = ' ';
ENGINE_ERROR_CODE ret;
/* More than 2 keys must be found in each block except the last block */
assert(maxklen < (bodylen-2)); /* 2 delimiters */
do_again:
/* reset return value */
ret = ENGINE_SUCCESS;
/* reset ntokens */
ntokens = 0;
nsegtok = 0;
/* get the first block */
chkblks = 1;
blckptr = MBLCK_GET_HEADBLK(blist);
dataptr = MBLCK_GET_BODYPTR(blckptr);
datalen = (chkblks < numblks) ? bodylen : lastlen;
while (ntokens < keycnt) {
/* check the last character */
bool segmented_blck = false;
if (chkblks < numblks) {
if (dataptr[datalen-1] == delimiter) {
datalen -= 1;
} else {
segmented_blck = true;
}
}
/* tokenize string in the block */
int tokcnt = tokenize_mblck(dataptr, datalen, keycnt-ntokens,
delimiter, &tokens[ntokens]);
if (tokcnt <= 0) {
ret = ENGINE_EBADVALUE; break;
} else {
int i;
for (i = 0; i < tokcnt; i++) {
if (tokens[ntokens+i].length > maxklen)
break;
}
if (i < tokcnt) {
ret = ENGINE_EBADVALUE; break;
}
}
ntokens += tokcnt;
/* check the end of strings */
if (chkblks >= numblks) {
ret = (ntokens == keycnt) ? ENGINE_SUCCESS
: ENGINE_EBADVALUE;
break; /* string end */
}
/* get the next block */
chkblks += 1;
blckptr = MBLCK_GET_NEXTBLK(blckptr);
dataptr = MBLCK_GET_BODYPTR(blckptr);
datalen = (chkblks < numblks) ? bodylen : lastlen;
if (segmented_blck == false)
continue;
/* The segmented block is found. */
if (dataptr[0] == delimiter) {
/* NOT segmented string */
dataptr += 1;
datalen -= 1;
} else {
/* real segmented string: save it */
if (nsegtok >= SEGTOK_ARRAY_SIZE) {
ret = concat_segmented_tokens(blist, tokens, segtoks, nsegtok, maxklen);
if (ret != ENGINE_SUCCESS) {
break; /* ENGINE_EBADVALUE or ENGINE_ENOMEM */
}
nsegtok = 0;
}
ntokens -= 1;
segtoks[nsegtok].value = tokens[ntokens].value;
segtoks[nsegtok].length = (uint32_t)tokens[ntokens].length;
segtoks[nsegtok].tokidx = ntokens;
nsegtok += 1;
}
}
if (ret == ENGINE_EBADVALUE) {
if (delimiter == ' ' && must_backward_compatible) {
delimiter = ','; /* do again to ensure backward compatibility */
goto do_again;
}
return ret;
}
if (ret == ENGINE_SUCCESS && nsegtok > 0) {
ret = concat_segmented_tokens(blist, tokens, segtoks, nsegtok, maxklen);
/* ret: ENGINE_SUCCESS or ENGINE_EBADVALUE or ENGINE_ENOMEM */
}
return ret;
}
/*
* Assume key string blocks in ascii protocol.
*/
ENGINE_ERROR_CODE tokenize_sblocks(mblck_list_t *blist, int keylen, int keycnt,
int maxklen, bool must_backward_compatible,
token_t *tokens)
{
assert(keylen > 2);
/* check the last "\r\n" string */
if (check_sblock_tail_string(blist, keylen) != 0) {
return ENGINE_EBADVALUE;
}
return tokenize_mblocks(blist, keylen-2, keycnt, maxklen,
must_backward_compatible, tokens);
}
/*
* event callback functions
*/
/* event handlers structure */
struct engine_event_handler {
EVENT_CALLBACK cb;
const void *cb_data;
struct engine_event_handler *next;
};
static struct engine_event_handler *engine_event_handlers[MAX_ENGINE_EVENT_TYPE + 1];
/*
* Register a callback.
*/
void register_callback(ENGINE_HANDLE *eh,
ENGINE_EVENT_TYPE type,
EVENT_CALLBACK cb, const void *cb_data)
{
struct engine_event_handler *h =
calloc(sizeof(struct engine_event_handler), 1);
assert(h);
h->cb = cb;
h->cb_data = cb_data;
h->next = engine_event_handlers[type];
engine_event_handlers[type] = h;
}
/*
* Perform all callbacks of a given type for the given connection.
*/
void perform_callbacks(ENGINE_EVENT_TYPE type,
const void *data, const void *c)
{
for (struct engine_event_handler *h = engine_event_handlers[type];
h; h = h->next) {
h->cb(c, type, data, h->cb_data);
}
}