~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to src/msg.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-06-04 21:21:09 UTC
  • Revision ID: package-import@ubuntu.com-20120604212109-b7b3m0rn21o8oo2q
Tags: upstream-3.1.0~beta+dfsg
ImportĀ upstreamĀ versionĀ 3.1.0~beta+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2009-2011 250bpm s.r.o.
 
3
    Copyright (c) 2007-2009 iMatix Corporation
 
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
8
    0MQ is free software; you can redistribute it and/or modify it under
 
9
    the terms of the GNU Lesser General Public License as published by
 
10
    the Free Software Foundation; either version 3 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    0MQ is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include "msg.hpp"
 
23
#include "../include/zmq.h"
 
24
 
 
25
#include <string.h>
 
26
#include <errno.h>
 
27
#include <stdlib.h>
 
28
#include <new>
 
29
 
 
30
#include "stdint.hpp"
 
31
#include "likely.hpp"
 
32
#include "err.hpp"
 
33
 
 
34
//  Check whether the sizes of public representation of the message (zmq_msg_t)
 
35
//  and private represenation of the message (zmq::msg_t) match.
 
36
typedef char zmq_msg_size_check
 
37
    [2 * ((sizeof (zmq::msg_t) == sizeof (zmq_msg_t)) != 0) - 1];
 
38
 
 
39
bool zmq::msg_t::check ()
 
40
{
 
41
     return u.base.type >= type_min && u.base.type <= type_max;
 
42
}
 
43
 
 
44
int zmq::msg_t::init ()
 
45
{
 
46
    u.vsm.type = type_vsm;
 
47
    u.vsm.flags = 0;
 
48
    u.vsm.size = 0;
 
49
    return 0;
 
50
}
 
51
 
 
52
int zmq::msg_t::init_size (size_t size_)
 
53
{
 
54
    if (size_ <= max_vsm_size) {
 
55
        u.vsm.type = type_vsm;
 
56
        u.vsm.flags = 0;
 
57
        u.vsm.size = (unsigned char) size_;
 
58
    }
 
59
    else {
 
60
        u.lmsg.type = type_lmsg;
 
61
        u.lmsg.flags = 0;
 
62
        u.lmsg.content =
 
63
            (content_t*) malloc (sizeof (content_t) + size_);
 
64
        if (!u.lmsg.content) {
 
65
            errno = ENOMEM;
 
66
            return -1;
 
67
        }
 
68
 
 
69
        u.lmsg.content->data = u.lmsg.content + 1;
 
70
        u.lmsg.content->size = size_;
 
71
        u.lmsg.content->ffn = NULL;
 
72
        u.lmsg.content->hint = NULL;
 
73
        new (&u.lmsg.content->refcnt) zmq::atomic_counter_t ();
 
74
    }
 
75
    return 0;
 
76
}
 
77
 
 
78
int zmq::msg_t::init_data (void *data_, size_t size_, msg_free_fn *ffn_,
 
79
    void *hint_)
 
80
{
 
81
    u.lmsg.type = type_lmsg;
 
82
    u.lmsg.flags = 0;
 
83
    u.lmsg.content = (content_t*) malloc (sizeof (content_t));
 
84
    if (!u.lmsg.content) {
 
85
        errno = ENOMEM;
 
86
        return -1;
 
87
    }
 
88
 
 
89
    u.lmsg.content->data = data_;
 
90
    u.lmsg.content->size = size_;
 
91
    u.lmsg.content->ffn = ffn_;
 
92
    u.lmsg.content->hint = hint_;
 
93
    new (&u.lmsg.content->refcnt) zmq::atomic_counter_t ();
 
94
    return 0;
 
95
 
 
96
}
 
97
 
 
98
int zmq::msg_t::init_delimiter ()
 
99
{
 
100
    u.delimiter.type = type_delimiter;
 
101
    u.delimiter.flags = 0;
 
102
    return 0;
 
103
}
 
104
 
 
105
int zmq::msg_t::close ()
 
106
{
 
107
    //  Check the validity of the message.
 
108
    if (unlikely (!check ())) {
 
109
        errno = EFAULT;
 
110
        return -1;
 
111
    }
 
112
 
 
113
    if (u.base.type == type_lmsg) {
 
114
 
 
115
        //  If the content is not shared, or if it is shared and the reference
 
116
        //  count has dropped to zero, deallocate it.
 
117
        if (!(u.lmsg.flags & msg_t::shared) ||
 
118
              !u.lmsg.content->refcnt.sub (1)) {
 
119
 
 
120
            //  We used "placement new" operator to initialize the reference
 
121
            //  counter so we call the destructor explicitly now.
 
122
            u.lmsg.content->refcnt.~atomic_counter_t ();
 
123
 
 
124
            if (u.lmsg.content->ffn)
 
125
                u.lmsg.content->ffn (u.lmsg.content->data,
 
126
                    u.lmsg.content->hint);
 
127
            free (u.lmsg.content);
 
128
        }
 
129
    }
 
130
 
 
131
    //  Make the message invalid.
 
132
    u.base.type = 0;
 
133
 
 
134
    return 0;
 
135
 
 
136
}
 
137
 
 
138
int zmq::msg_t::move (msg_t &src_)
 
139
{
 
140
    //  Check the validity of the source.
 
141
    if (unlikely (!src_.check ())) {
 
142
        errno = EFAULT;
 
143
        return -1;
 
144
    }
 
145
 
 
146
    int rc = close ();
 
147
    if (unlikely (rc < 0))
 
148
        return rc;
 
149
 
 
150
    *this = src_;
 
151
 
 
152
    rc = src_.init ();
 
153
    if (unlikely (rc < 0))
 
154
        return rc;
 
155
 
 
156
    return 0;
 
157
}
 
158
 
 
159
int zmq::msg_t::copy (msg_t &src_)
 
160
{
 
161
    //  Check the validity of the source.
 
162
    if (unlikely (!src_.check ())) {
 
163
        errno = EFAULT;
 
164
        return -1;
 
165
    }
 
166
 
 
167
    int rc = close ();
 
168
    if (unlikely (rc < 0))
 
169
        return rc;
 
170
 
 
171
    if (src_.u.base.type == type_lmsg) {
 
172
 
 
173
        //  One reference is added to shared messages. Non-shared messages
 
174
        //  are turned into shared messages and reference count is set to 2.
 
175
        if (src_.u.lmsg.flags & msg_t::shared)
 
176
            src_.u.lmsg.content->refcnt.add (1);
 
177
        else {
 
178
            src_.u.lmsg.flags |= msg_t::shared;
 
179
            src_.u.lmsg.content->refcnt.set (2);
 
180
        }
 
181
    }
 
182
 
 
183
    *this = src_;
 
184
 
 
185
    return 0;
 
186
 
 
187
}
 
188
 
 
189
void *zmq::msg_t::data ()
 
190
{
 
191
    //  Check the validity of the message.
 
192
    zmq_assert (check ());
 
193
 
 
194
    switch (u.base.type) {
 
195
    case type_vsm:
 
196
        return u.vsm.data;
 
197
    case type_lmsg:
 
198
        return u.lmsg.content->data;
 
199
    default:
 
200
        zmq_assert (false);
 
201
        return NULL;
 
202
    }
 
203
}
 
204
 
 
205
size_t zmq::msg_t::size ()
 
206
{
 
207
    //  Check the validity of the message.
 
208
    zmq_assert (check ());
 
209
 
 
210
    switch (u.base.type) {
 
211
    case type_vsm:
 
212
        return u.vsm.size;
 
213
    case type_lmsg:
 
214
        return u.lmsg.content->size;
 
215
    default:
 
216
        zmq_assert (false);
 
217
        return 0;
 
218
    }
 
219
}
 
220
 
 
221
unsigned char zmq::msg_t::flags ()
 
222
{
 
223
    return u.base.flags;
 
224
}
 
225
 
 
226
void zmq::msg_t::set_flags (unsigned char flags_)
 
227
{
 
228
    u.base.flags |= flags_;
 
229
}
 
230
 
 
231
void zmq::msg_t::reset_flags (unsigned char flags_)
 
232
{
 
233
    u.base.flags &= ~flags_;
 
234
}
 
235
 
 
236
bool zmq::msg_t::is_delimiter ()
 
237
{
 
238
    return u.base.type == type_delimiter;
 
239
}
 
240
 
 
241
bool zmq::msg_t::is_vsm ()
 
242
{
 
243
    return u.base.type == type_vsm;
 
244
}
 
245
 
 
246
void zmq::msg_t::add_refs (int refs_)
 
247
{
 
248
    zmq_assert (refs_ >= 0);
 
249
 
 
250
    //  No copies required.
 
251
    if (!refs_)
 
252
        return;
 
253
 
 
254
    //  VSMs and delimiters can be copied straight away. The only message type
 
255
    //  that needs special care are long messages.
 
256
    if (u.base.type == type_lmsg) {
 
257
        if (u.lmsg.flags & msg_t::shared)
 
258
            u.lmsg.content->refcnt.add (refs_);
 
259
        else {
 
260
            u.lmsg.content->refcnt.set (refs_ + 1);
 
261
            u.lmsg.flags |= msg_t::shared;
 
262
        }
 
263
    }
 
264
}
 
265
 
 
266
bool zmq::msg_t::rm_refs (int refs_)
 
267
{
 
268
    zmq_assert (refs_ >= 0);
 
269
 
 
270
    //  No copies required.
 
271
    if (!refs_)
 
272
        return true;
 
273
 
 
274
    //  If there's only one reference close the message.
 
275
    if (u.base.type != type_lmsg || !(u.lmsg.flags & msg_t::shared)) {
 
276
        close ();
 
277
        return false;
 
278
    }
 
279
 
 
280
    //  The only message type that needs special care are long messages.
 
281
    if (!u.lmsg.content->refcnt.sub (refs_)) {
 
282
        close ();
 
283
        return false;
 
284
    }
 
285
 
 
286
    return true;
 
287
}
 
288