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
|
// @(#)cdr.cpp 1.2 95/11/04
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// CDR: Encode/Decode basic machine data types
//
// Implementation of OMG "Common Data Representation" (CDR) ... there
// are one routine each for byte/halfword/word/doubleword put/get,
// which adjust to establish "natural" alignment (the bulk of the
// code) and then put or get with byteswapping as needed.
//
// The implementation knows that native data formats are conformant
// with OMG-IDL's (and hence CDR's) size requirements, and relies on
// the fact that (for example) CORBA_Long is always four bytes long
// even if the environment's "int" is a different size.
//
// char, octet 8 bits (1 byte)
// short, unsigned short 16 bits (2 bytes)
// long, unsigned long, float 32 bits (4 bytes)
// double, (unsigned) long long 64 bits (8 bytes)
// long double 128 bits (16 bytes)
//
// Moreover, this "knows" that the native 'char' represents ISO
// Latin/1 characters (an ASCII superset addressing Western European
// characters) and that "double" and "float" comply with the IEEE
// standards. (The "long double" may not be a native data type,
// though.)
//
// THREADING NOTE: "CDR" is a data structure which must be protected
// by external critical sections. Like simpler numeric types, "CDR"
// instances are accessed and modified atomically. This
// implementation is reentrant, so that independent "CDR" values may
// be manipulated concurrently when the underlying programming
// environment is itself reentrant.
#include <assert.h>
#include <limits.h>
#include <string.h>
#include "orb.h"
#include "cdr.h"
#if !defined(ACE_INLINE)
inline
#endif
void CDR::swap_long(unsigned char *orig, CORBA_Long &target)
{
register unsigned char *lp = (unsigned char *) ⌖
lp [ 3] = *orig++;
lp [ 2] = *orig++;
lp [ 1] = *orig++;
lp [ 0] = *orig++;
}
#if !defined(ACE_INLINE)
inline
#endif
void CDR::swap_longlong(unsigned char *orig, CORBA_LongLong &target)
{
register unsigned char *llp = (unsigned char *) ⌖
llp [ 7] = *orig++;
llp [ 6] = *orig++;
llp [ 5] = *orig++;
llp [ 4] = *orig++;
llp [ 3] = *orig++;
llp [ 2] = *orig++;
llp [ 1] = *orig++;
llp [ 0] = *orig++;
}
#if !defined(ACE_INLINE)
inline
#endif
void CDR::swap_longdouble(unsigned char *orig, CORBA_LongDouble &target)
{
register unsigned char *ldp = (unsigned char *) ⌖
ldp [15] = *orig++;
ldp [14] = *orig++;
ldp [13] = *orig++;
ldp [12] = *orig++;
ldp [11] = *orig++;
ldp [10] = *orig++;
ldp [ 9] = *orig++;
ldp [ 8] = *orig++;
ldp [ 7] = *orig++;
ldp [ 6] = *orig++;
ldp [ 5] = *orig++;
ldp [ 4] = *orig++;
ldp [ 3] = *orig++;
ldp [ 2] = *orig++;
ldp [ 1] = *orig++;
ldp [ 0] = *orig++;
}
// Grow the CDR buffer, either to a known size (incoming message) or
// by a standard increment (creating outgoing message).
//
// We can't use realloc() because of a constraint that the part of the
// buffer into which we marshal be aligned according to MAX_ALIGNMENT,
// which can be a stronger requirement than malloc/realloc places on
// buffer. This makes growing a buffer on the encode side costly,
// since it can need to be done repetitively and copies more data each
// time.
//
// NOTE: this code knows about what's involved in the constructor and
// destructor, as it needs to invoke the constructor and do what the
// destructor would do (and not in the normal order). It also knows
// all other state that's significant. Change with care!
//
// NOTE: arguably this is a good place to ensure that the memory's
// zeroed out to comply with Orange Book C2 "object reuse" (meaning
// data, like I/O buffers) policy. IIOP doesn't mandate such policies
// though.
CORBA_Boolean
CDR::grow (size_t newsize)
{
u_char *old_realbuf;
u_char *oldbuf;
size_t offset;
int old_do_swap = do_byteswap;
// Iff old buffer was heap allocated, it gets freed soon. In any
// case, we need to know which bytes that have been marshaled or
// read thus far, so they'll also be in the newly grown buffer.
if (do_free)
old_realbuf = real_buffer;
else
old_realbuf = 0;
oldbuf = buffer;
assert ((next - buffer) < UINT_MAX);
offset = (u_int) (next - buffer);
// Calculate the new buffer's length; if growing for encode, we
// don't grow in "small" chunks because of the cost.
size_t new_len;
if (newsize == 0)
{
// @@ Can we replace the magic number 4096 with a const or enum?
// I defer to Andy on this since he has much more experience... --cjc
if (length < 4096)
new_len = length + 4096;
else
new_len = length * 2;
}
else if (newsize <= length)
return CORBA_B_TRUE;
else
new_len = newsize;
// Get a new buffer that's adequately aligned, and use it to
// reinitialize ourselves with the "free this buffer later" flag.
u_char *new_buffer;
new_len += MAX_ALIGNMENT - 1;
// Is there a reason we use malloc here and not new?
new_buffer = (u_char *) ACE_OS::malloc (new_len);
if (new_buffer == 0)
return CORBA_B_FALSE;
(void) new (this) CDR (new_buffer, new_len, MY_BYTE_SEX, 1);
// Now restore all the relevant old state that we saved earlier, and
// free the original buffer if needed. (The first buffer is
// normally stack-allocated and so mustn't be freed this way.)
do_byteswap = old_do_swap;
ACE_OS::memcpy (buffer, oldbuf, offset);
skip_bytes (offset);
if (old_realbuf)
ACE_OS::free ((char *) old_realbuf);
return CORBA_B_TRUE;
}
|