1 
2 /*
3  * Redistribution and use in source and binary forms, with or
4  * without modification, are permitted provided that the following
5  * conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above
8  *    copyright notice, this list of conditions and the
9  *    following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20  * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <string.h>
34 
35 #include <sys/types.h>
36 
37 #include <tarantool/tnt_mem.h>
38 #include <tarantool/tnt_proto.h>
39 #include <tarantool/tnt_reply.h>
40 #include <tarantool/tnt_stream.h>
41 
42 uint32_t
tnt_stream_reqid(struct tnt_stream * s,uint32_t reqid)43 tnt_stream_reqid(struct tnt_stream *s, uint32_t reqid)
44 {
45 	uint32_t old = s->reqid;
46 	s->reqid = reqid;
47 	return old;
48 }
49 
50 struct tnt_stream*
tnt_stream_init(struct tnt_stream * s)51 tnt_stream_init(struct tnt_stream *s)
52 {
53 	int allocated = s == NULL;
54 	if (!s) {
55 		s = tnt_mem_alloc(sizeof(struct tnt_stream));
56 		if (s == NULL)
57 			return NULL;
58 	}
59 	memset(s, 0, sizeof(struct tnt_stream));
60 	s->alloc = allocated;
61 	return s;
62 }
63 
tnt_stream_free(struct tnt_stream * s)64 void tnt_stream_free(struct tnt_stream *s) {
65 	if (s->free)
66 		s->free(s);
67 	if (s->alloc)
68 		tnt_mem_free(s);
69 }
70