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 "DataReference.h"
30#include <algorithm>
31#include <stdio.h>
32
33#if OS(DARWIN)
34#include <sys/mman.h>
35#endif
36
37namespace CoreIPC {
38
39static inline void* allocBuffer(size_t size)
40{
41#if OS(DARWIN)
42    return mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
43#else
44    return fastMalloc(size);
45#endif
46}
47
48static inline void freeBuffer(void* addr, size_t size)
49{
50#if OS(DARWIN)
51    munmap(addr, size);
52#else
53    UNUSED_PARAM(size);
54    fastFree(addr);
55#endif
56}
57
58PassOwnPtr<ArgumentEncoder> ArgumentEncoder::create()
59{
60    return adoptPtr(new ArgumentEncoder);
61}
62
63ArgumentEncoder::ArgumentEncoder()
64    : m_buffer(m_inlineBuffer)
65    , m_bufferPointer(m_inlineBuffer)
66    , m_bufferSize(0)
67    , m_bufferCapacity(sizeof(m_inlineBuffer))
68{
69}
70
71ArgumentEncoder::~ArgumentEncoder()
72{
73    if (m_buffer != m_inlineBuffer)
74        freeBuffer(m_buffer, m_bufferCapacity);
75
76#if !USE(UNIX_DOMAIN_SOCKETS)
77    // FIXME: We need to dispose of the attachments in cases of failure.
78#else
79    for (size_t i = 0; i < m_attachments.size(); ++i)
80        m_attachments[i].dispose();
81#endif
82}
83
84static inline size_t roundUpToAlignment(size_t value, unsigned alignment)
85{
86    return ((value + alignment - 1) / alignment) * alignment;
87}
88
89uint8_t* ArgumentEncoder::grow(unsigned alignment, size_t size)
90{
91    size_t alignedSize = roundUpToAlignment(m_bufferSize, alignment);
92
93    if (alignedSize + size > m_bufferCapacity) {
94        size_t newCapacity = roundUpToAlignment(m_bufferCapacity * 2, 4096);
95        while (newCapacity < alignedSize + size)
96            newCapacity *= 2;
97
98        uint8_t* newBuffer = static_cast<uint8_t*>(allocBuffer(newCapacity));
99        if (!newBuffer)
100            CRASH();
101
102        memcpy(newBuffer, m_buffer, m_bufferSize);
103
104        if (m_buffer != m_inlineBuffer)
105            freeBuffer(m_buffer, m_bufferCapacity);
106
107        m_buffer = newBuffer;
108        m_bufferCapacity = newCapacity;
109    }
110
111    m_bufferSize = alignedSize + size;
112    m_bufferPointer = m_buffer + alignedSize + size;
113
114    return m_buffer + alignedSize;
115}
116
117void ArgumentEncoder::encodeFixedLengthData(const uint8_t* data, size_t size, unsigned alignment)
118{
119    ASSERT(!(reinterpret_cast<uintptr_t>(data) % alignment));
120
121    uint8_t* buffer = grow(alignment, size);
122    memcpy(buffer, data, size);
123}
124
125void ArgumentEncoder::encodeVariableLengthByteArray(const DataReference& dataReference)
126{
127    encode(static_cast<uint64_t>(dataReference.size()));
128    encodeFixedLengthData(dataReference.data(), dataReference.size(), 1);
129}
130
131void ArgumentEncoder::encode(bool n)
132{
133    uint8_t* buffer = grow(sizeof(n), sizeof(n));
134
135    *reinterpret_cast<bool*>(buffer) = n;
136}
137
138void ArgumentEncoder::encode(uint8_t n)
139{
140    uint8_t* buffer = grow(sizeof(n), sizeof(n));
141
142    *reinterpret_cast<uint8_t*>(buffer) = n;
143}
144
145void ArgumentEncoder::encode(uint16_t n)
146{
147    uint8_t* buffer = grow(sizeof(n), sizeof(n));
148
149    *reinterpret_cast<uint16_t*>(buffer) = n;
150}
151
152void ArgumentEncoder::encode(uint32_t n)
153{
154    uint8_t* buffer = grow(sizeof(n), sizeof(n));
155
156    *reinterpret_cast<uint32_t*>(buffer) = n;
157}
158
159void ArgumentEncoder::encode(uint64_t n)
160{
161    uint8_t* buffer = grow(sizeof(n), sizeof(n));
162
163    *reinterpret_cast<uint64_t*>(buffer) = n;
164}
165
166void ArgumentEncoder::encode(int32_t n)
167{
168    uint8_t* buffer = grow(sizeof(n), sizeof(n));
169
170    *reinterpret_cast<int32_t*>(buffer) = n;
171}
172
173void ArgumentEncoder::encode(int64_t n)
174{
175    uint8_t* buffer = grow(sizeof(n), sizeof(n));
176
177    *reinterpret_cast<int64_t*>(buffer) = n;
178}
179
180void ArgumentEncoder::encode(float n)
181{
182    uint8_t* buffer = grow(sizeof(n), sizeof(n));
183
184    *reinterpret_cast<float*>(buffer) = n;
185}
186
187void ArgumentEncoder::encode(double n)
188{
189    uint8_t* buffer = grow(sizeof(n), sizeof(n));
190
191    *reinterpret_cast<double*>(buffer) = n;
192}
193
194void ArgumentEncoder::addAttachment(const Attachment& attachment)
195{
196    m_attachments.append(attachment);
197}
198
199Vector<Attachment> ArgumentEncoder::releaseAttachments()
200{
201    Vector<Attachment> newList;
202    newList.swap(m_attachments);
203    return newList;
204}
205
206} // namespace CoreIPC
207