MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / _addReplyProtoToList

Function _addReplyProtoToList

src/networking.cpp:400–436  ·  view source on GitHub ↗

Adds the reply to the reply linked list. * Note: some edits to this function need to be relayed to AddReplyFromClient. */

Source from the content-addressed store, hash-verified

398/* Adds the reply to the reply linked list.
399 * Note: some edits to this function need to be relayed to AddReplyFromClient. */
400void _addReplyProtoToList(client *c, const char *s, size_t len) {
401 if (c->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_AFTER_REPLY) return;
402 AssertCorrectThread(c);
403
404 listNode *ln = listLast(c->reply);
405 clientReplyBlock *tail = (clientReplyBlock*) (ln? listNodeValue(ln): NULL);
406
407 /* Note that 'tail' may be NULL even if we have a tail node, because when
408 * addReplyDeferredLen() is used, it sets a dummy node to NULL just
409 * fo fill it later, when the size of the bulk length is set. */
410
411 /* Append to tail string when possible. */
412 if (tail) {
413 /* Copy the part we can fit into the tail, and leave the rest for a
414 * new node */
415 size_t avail = tail->size - tail->used;
416 size_t copy = avail >= len? len: avail;
417 memcpy(tail->buf() + tail->used, s, copy);
418 tail->used += copy;
419 s += copy;
420 len -= copy;
421 }
422 if (len) {
423 /* Create a new node, make sure it is allocated to at
424 * least PROTO_REPLY_CHUNK_BYTES */
425 size_t size = len < PROTO_REPLY_CHUNK_BYTES? PROTO_REPLY_CHUNK_BYTES: len;
426 tail = (clientReplyBlock*)zmalloc(size + sizeof(clientReplyBlock), MALLOC_LOCAL);
427 /* take over the allocation's internal fragmentation */
428 tail->size = zmalloc_usable_size(tail) - sizeof(clientReplyBlock);
429 tail->used = len;
430 memcpy(tail->buf(), s, len);
431 listAddNodeTail(c->reply, tail);
432 c->reply_bytes += tail->size;
433
434 closeClientOnOutputBufferLimitReached(c, 1);
435 }
436}
437
438/* -----------------------------------------------------------------------------
439 * Higher level functions to queue data on the client output buffer.

Callers 3

addReplyFunction · 0.85
addReplySdsFunction · 0.85
addReplyProtoFunction · 0.85

Calls 5

zmallocFunction · 0.85
zmalloc_usable_sizeFunction · 0.85
listAddNodeTailFunction · 0.85
bufMethod · 0.45

Tested by

no test coverage detected