4 * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <sys/types.h>
27 #include "pims-internal.h"
28 #include "pims-ipc-data.h"
31 * Structure of data with type(4 bytes order)
32 * +------------------------------------------------------------------+
33 * | type | size | data | pad | type | size | data | pad |
34 * +------------------------------------------------------------------+
35 * 4 4 size 0-3 (Size of bytes)
37 * Structure of data without type(4 bytes order)
38 * +----------------------------------------------------+
39 * | size | data | pad | size | data | pad |
40 * +----------------------------------------------------+
41 * 4 size 0-3 (Size of bytes)
44 #define _get_used_size_with_type(data_size) \
45 (sizeof(int) + sizeof(int) + data_size + ((sizeof(int) - (data_size % sizeof(int)))%sizeof(int)))
47 #define _get_used_size(data_size) \
48 (sizeof(int) + data_size + ((sizeof(int) - (data_size % sizeof(int)))%sizeof(int)))
50 API pims_ipc_data_h pims_ipc_data_create_with_size(unsigned int size, int flags)
52 pims_ipc_data_s *handle = NULL;
54 handle = calloc(1, sizeof(pims_ipc_data_s));
60 if (flags & PIMS_IPC_DATA_FLAGS_WITH_TYPE) {
61 ERR("Not supported with-type mode");
66 handle->alloc_size = size;
67 handle->free_size = size;
69 handle->buf = calloc(1, size);
70 if (NULL == handle->buf) {
75 handle->pos = handle->buf;
77 handle->buf_alloced = 1;
82 API void pims_ipc_data_destroy(pims_ipc_data_h data)
84 pims_ipc_data_s *handle = data;
88 if (handle->buf_alloced)
94 API int pims_ipc_data_put(pims_ipc_data_h data, void *buf, unsigned int size)
96 pims_ipc_data_s *handle = data;
97 unsigned int dsize = size;
98 unsigned int used_size = 0;
100 if (handle->created == 0) {
101 ERR("This handle isn't create mode.");
105 if (dsize > 0 && buf == NULL) {
106 ERR("Invalid argument");
110 used_size = _get_used_size(dsize);
111 if (handle->free_size < used_size) {
113 new_size = handle->alloc_size * 2;
115 while (new_size < handle->buf_size + used_size)
117 handle->buf = realloc(handle->buf, new_size);
118 if (NULL == handle->buf) {
119 ERR("realloc() Fail");
122 handle->alloc_size = new_size;
123 handle->free_size = handle->alloc_size - handle->buf_size;
124 handle->pos = handle->buf;
125 handle->pos += handle->buf_size;
126 VERBOSE("free_size [%d] dsize [%d]", handle->free_size, dsize);
129 *(int*)(handle->pos) = dsize;
131 memcpy((handle->pos+sizeof(int)), buf, dsize);
132 int pad_size = used_size-dsize-sizeof(int);
134 memset((handle->pos+sizeof(int)+dsize), 0x0, pad_size);
137 handle->pos += used_size;
138 handle->buf_size += used_size;
139 handle->free_size -= used_size;
141 VERBOSE("data put size [%u] data[%p]", dsize, buf);
146 API void* pims_ipc_data_get(pims_ipc_data_h data, unsigned int *size)
148 pims_ipc_data_s *handle = data;
149 unsigned int dsize = 0;
150 unsigned int used_size = 0;
153 if (handle->created == 1) {
154 ERR("This handle is create mode.");
157 if (handle->buf_size == 0) {
158 ERR("Remain buffer size is 0.");
162 dsize = *(int*)(handle->pos);
163 buf = (handle->pos+sizeof(int));
165 if (dsize == 0) /* current position is next size field becasue data size is 0 */
168 used_size = _get_used_size(dsize);
169 handle->pos += used_size;
170 handle->buf_size -= used_size;
171 handle->free_size += used_size;
173 VERBOSE("data get size [%u] data[%p]", dsize, buf);
178 pims_ipc_data_h pims_ipc_data_steal_unmarshal(void *buf, unsigned int size)
180 pims_ipc_data_s *handle = NULL;
182 VERBOSE("size : %d", size);
183 handle = calloc(1, sizeof(pims_ipc_data_s));
184 if (NULL == handle) {
185 ERR("calloc() handle Fail");
188 handle->buf = calloc(1, size + 1);
189 if (NULL == handle->buf) {
190 ERR("calloc() buf Fail");
194 memcpy(handle->buf, buf, size);
196 handle->alloc_size = size;
197 handle->free_size = 0;
198 handle->buf_size = handle->alloc_size;
199 handle->pos = handle->buf;
201 handle->buf_alloced = 1;
203 VERBOSE("handle[%p]", handle);