1 /*
2     Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include "msgqueue.h"
24 
25 #include "../../utils/alloc.h"
26 #include "../../utils/fast.h"
27 #include "../../utils/err.h"
28 
29 #include <string.h>
30 
nn_msgqueue_init(struct nn_msgqueue * self,size_t maxmem)31 void nn_msgqueue_init (struct nn_msgqueue *self, size_t maxmem)
32 {
33     struct nn_msgqueue_chunk *chunk;
34 
35     self->count = 0;
36     self->mem = 0;
37     self->maxmem = maxmem;
38 
39     chunk = nn_alloc (sizeof (struct nn_msgqueue_chunk), "msgqueue chunk");
40     alloc_assert (chunk);
41     chunk->next = NULL;
42 
43     self->out.chunk = chunk;
44     self->out.pos = 0;
45     self->in.chunk = chunk;
46     self->in.pos = 0;
47 
48     self->cache = NULL;
49 }
50 
nn_msgqueue_term(struct nn_msgqueue * self)51 void nn_msgqueue_term (struct nn_msgqueue *self)
52 {
53     int rc;
54     struct nn_msg msg;
55 
56     /*  Deallocate messages in the pipe. */
57     while (1) {
58         rc = nn_msgqueue_recv (self, &msg);
59         if (rc == -EAGAIN)
60             break;
61         errnum_assert (rc >= 0, -rc);
62         nn_msg_term (&msg);
63     }
64 
65     /*  There are no more messages in the pipe so there's at most one chunk
66         in the queue. Deallocate it. */
67     nn_assert (self->in.chunk == self->out.chunk);
68     nn_free (self->in.chunk);
69 
70     /*  Deallocate the cached chunk, if any. */
71     if (self->cache)
72         nn_free (self->cache);
73 }
74 
nn_msgqueue_empty(struct nn_msgqueue * self)75 int nn_msgqueue_empty (struct nn_msgqueue *self)
76 {
77     return self->count == 0 ? 1 : 0;
78 }
79 
nn_msgqueue_send(struct nn_msgqueue * self,struct nn_msg * msg)80 int nn_msgqueue_send (struct nn_msgqueue *self, struct nn_msg *msg)
81 {
82     size_t msgsz;
83 
84     /*  By allowing one message of arbitrary size to be written to the queue,
85         we allow even messages that exceed max buffer size to pass through.
86         Beyond that we'll apply the buffer limit as specified by the user. */
87     msgsz = nn_chunkref_size (&msg->sphdr) + nn_chunkref_size (&msg->body);
88     if (nn_slow (self->count > 0 && self->mem + msgsz >= self->maxmem))
89         return -EAGAIN;
90 
91     /*  Adjust the statistics. */
92     ++self->count;
93     self->mem += msgsz;
94 
95     /*  Move the content of the message to the pipe. */
96     nn_msg_mv (&self->out.chunk->msgs [self->out.pos], msg);
97     ++self->out.pos;
98 
99     /*  If there's no space for a new message in the pipe, either re-use
100         the cache chunk or allocate a new chunk if it does not exist. */
101     if (nn_slow (self->out.pos == NN_MSGQUEUE_GRANULARITY)) {
102         if (nn_slow (!self->cache)) {
103             self->cache = nn_alloc (sizeof (struct nn_msgqueue_chunk),
104                 "msgqueue chunk");
105             alloc_assert (self->cache);
106             self->cache->next = NULL;
107         }
108         self->out.chunk->next = self->cache;
109         self->out.chunk = self->cache;
110         self->cache = NULL;
111         self->out.pos = 0;
112     }
113 
114     return 0;
115 }
116 
nn_msgqueue_recv(struct nn_msgqueue * self,struct nn_msg * msg)117 int nn_msgqueue_recv (struct nn_msgqueue *self, struct nn_msg *msg)
118 {
119     struct nn_msgqueue_chunk *o;
120 
121     /*  If there is no message in the queue. */
122     if (nn_slow (!self->count))
123         return -EAGAIN;
124 
125     /*  Move the message from the pipe to the user. */
126     nn_msg_mv (msg, &self->in.chunk->msgs [self->in.pos]);
127 
128     /*  Move to the next position. */
129     ++self->in.pos;
130     if (nn_slow (self->in.pos == NN_MSGQUEUE_GRANULARITY)) {
131         o = self->in.chunk;
132         self->in.chunk = self->in.chunk->next;
133         self->in.pos = 0;
134         if (nn_fast (!self->cache))
135             self->cache = o;
136         else
137             nn_free (o);
138     }
139 
140     /*  Adjust the statistics. */
141     --self->count;
142     self->mem -= (nn_chunkref_size (&msg->sphdr) +
143         nn_chunkref_size (&msg->body));
144 
145     return 0;
146 }
147 
148