1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "ArgumentEncoder.h"
28 
29 #include <algorithm>
30 #include <stdio.h>
31 
32 namespace CoreIPC {
33 
create(uint64_t destinationID)34 PassOwnPtr<ArgumentEncoder> ArgumentEncoder::create(uint64_t destinationID)
35 {
36     return adoptPtr(new ArgumentEncoder(destinationID));
37 }
38 
ArgumentEncoder(uint64_t destinationID)39 ArgumentEncoder::ArgumentEncoder(uint64_t destinationID)
40     : m_buffer(0)
41     , m_bufferPointer(0)
42     , m_bufferSize(0)
43     , m_bufferCapacity(0)
44 {
45     // Encode the destination ID.
46     encodeUInt64(destinationID);
47 }
48 
~ArgumentEncoder()49 ArgumentEncoder::~ArgumentEncoder()
50 {
51     if (m_buffer)
52         fastFree(m_buffer);
53 #if !PLATFORM(QT) && !PLATFORM(GTK)
54     // FIXME: We need to dispose of the attachments in cases of failure.
55 #else
56     for (int i = 0; i < m_attachments.size(); ++i)
57         m_attachments[i].dispose();
58 #endif
59 }
60 
roundUpToAlignment(size_t value,unsigned alignment)61 static inline size_t roundUpToAlignment(size_t value, unsigned alignment)
62 {
63     return ((value + alignment - 1) / alignment) * alignment;
64 }
65 
grow(unsigned alignment,size_t size)66 uint8_t* ArgumentEncoder::grow(unsigned alignment, size_t size)
67 {
68     size_t alignedSize = roundUpToAlignment(m_bufferSize, alignment);
69 
70     if (alignedSize + size > m_bufferCapacity) {
71         size_t newCapacity = std::max(alignedSize + size, std::max(static_cast<size_t>(32), m_bufferCapacity + m_bufferCapacity / 4 + 1));
72         if (!m_buffer)
73             m_buffer = static_cast<uint8_t*>(fastMalloc(newCapacity));
74         else
75             m_buffer = static_cast<uint8_t*>(fastRealloc(m_buffer, newCapacity));
76 
77         // FIXME: What should we do if allocating memory fails?
78 
79         m_bufferCapacity = newCapacity;
80     }
81 
82     m_bufferSize = alignedSize + size;
83     m_bufferPointer = m_buffer + alignedSize + size;
84 
85     return m_buffer + alignedSize;
86 }
87 
encodeBytes(const uint8_t * bytes,size_t size)88 void ArgumentEncoder::encodeBytes(const uint8_t* bytes, size_t size)
89 {
90     // Encode the size.
91     encodeUInt64(static_cast<uint64_t>(size));
92 
93     uint8_t* buffer = grow(1, size);
94 
95     memcpy(buffer, bytes, size);
96 }
97 
encodeBool(bool n)98 void ArgumentEncoder::encodeBool(bool n)
99 {
100     uint8_t* buffer = grow(sizeof(n), sizeof(n));
101 
102     *reinterpret_cast<bool*>(buffer) = n;
103 }
104 
encodeUInt32(uint32_t n)105 void ArgumentEncoder::encodeUInt32(uint32_t n)
106 {
107     uint8_t* buffer = grow(sizeof(n), sizeof(n));
108 
109     *reinterpret_cast<uint32_t*>(buffer) = n;
110 }
111 
encodeUInt64(uint64_t n)112 void ArgumentEncoder::encodeUInt64(uint64_t n)
113 {
114     uint8_t* buffer = grow(sizeof(n), sizeof(n));
115 
116     *reinterpret_cast<uint64_t*>(buffer) = n;
117 }
118 
encodeInt32(int32_t n)119 void ArgumentEncoder::encodeInt32(int32_t n)
120 {
121     uint8_t* buffer = grow(sizeof(n), sizeof(n));
122 
123     *reinterpret_cast<int32_t*>(buffer) = n;
124 }
125 
encodeInt64(int64_t n)126 void ArgumentEncoder::encodeInt64(int64_t n)
127 {
128     uint8_t* buffer = grow(sizeof(n), sizeof(n));
129 
130     *reinterpret_cast<int64_t*>(buffer) = n;
131 }
132 
encodeFloat(float n)133 void ArgumentEncoder::encodeFloat(float n)
134 {
135     uint8_t* buffer = grow(sizeof(n), sizeof(n));
136 
137     *reinterpret_cast<float*>(buffer) = n;
138 }
139 
encodeDouble(double n)140 void ArgumentEncoder::encodeDouble(double n)
141 {
142     uint8_t* buffer = grow(sizeof(n), sizeof(n));
143 
144     *reinterpret_cast<double*>(buffer) = n;
145 }
146 
addAttachment(const Attachment & attachment)147 void ArgumentEncoder::addAttachment(const Attachment& attachment)
148 {
149     m_attachments.append(attachment);
150 }
151 
releaseAttachments()152 Vector<Attachment> ArgumentEncoder::releaseAttachments()
153 {
154     Vector<Attachment> newList;
155     newList.swap(m_attachments);
156     return newList;
157 }
158 
159 #ifndef NDEBUG
debug()160 void ArgumentEncoder::debug()
161 {
162     printf("ArgumentEncoder::debug()\n");
163     printf("Number of Attachments: %d\n", (int)m_attachments.size());
164     printf("Size of buffer: %d\n", (int)m_bufferSize);
165 }
166 #endif
167 
168 } // namespace CoreIPC
169