4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
27 #include <sys/stat.h> /* For mode constants */
28 #include <fcntl.h> /* For O_* constants */
33 #include <lttng/align.h>
38 * Ensure we have the required amount of space available by writing 0
39 * into the entire buffer. Not doing so can trigger SIGBUS when going
40 * beyond the available shm space.
43 int zero_file(int fd
, size_t len
)
51 pagelen
= sysconf(_SC_PAGESIZE
);
54 zeropage
= calloc(pagelen
, 1);
58 while (len
> written
) {
60 retlen
= write(fd
, zeropage
,
61 min_t(size_t, pagelen
, len
- written
));
62 } while (retlen
== -1UL && errno
== EINTR
);
75 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
77 struct shm_object_table
*table
;
79 table
= zmalloc(sizeof(struct shm_object_table
) +
80 max_nb_obj
* sizeof(table
->objects
[0]));
83 table
->size
= max_nb_obj
;
88 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
89 size_t memory_map_size
,
92 int shmfd
, waitfd
[2], ret
, i
;
93 struct shm_object
*obj
;
98 if (table
->allocated_len
>= table
->size
)
100 obj
= &table
->objects
[table
->allocated_len
];
102 /* wait_fd: create pipe */
108 for (i
= 0; i
< 2; i
++) {
109 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
115 /* The write end of the pipe needs to be non-blocking */
116 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
121 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
126 ret
= zero_file(shmfd
, memory_map_size
);
129 goto error_zero_file
;
131 ret
= ftruncate(shmfd
, memory_map_size
);
134 goto error_ftruncate
;
136 obj
->shm_fd_ownership
= 0;
139 /* memory_map: mmap */
140 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
141 MAP_SHARED
, shmfd
, 0);
142 if (memory_map
== MAP_FAILED
) {
146 obj
->type
= SHM_OBJECT_SHM
;
147 obj
->memory_map
= memory_map
;
148 obj
->memory_map_size
= memory_map_size
;
149 obj
->allocated_len
= 0;
150 obj
->index
= table
->allocated_len
++;
158 for (i
= 0; i
< 2; i
++) {
159 ret
= close(waitfd
[i
]);
170 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
171 size_t memory_map_size
)
173 struct shm_object
*obj
;
175 int waitfd
[2], i
, ret
;
177 if (table
->allocated_len
>= table
->size
)
179 obj
= &table
->objects
[table
->allocated_len
];
181 memory_map
= zmalloc(memory_map_size
);
185 /* wait_fd: create pipe */
191 for (i
= 0; i
< 2; i
++) {
192 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
198 /* The write end of the pipe needs to be non-blocking */
199 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
204 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
208 obj
->shm_fd_ownership
= 0;
210 obj
->type
= SHM_OBJECT_MEM
;
211 obj
->memory_map
= memory_map
;
212 obj
->memory_map_size
= memory_map_size
;
213 obj
->allocated_len
= 0;
214 obj
->index
= table
->allocated_len
++;
219 for (i
= 0; i
< 2; i
++) {
220 ret
= close(waitfd
[i
]);
232 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
233 size_t memory_map_size
,
234 enum shm_object_type type
,
239 return _shm_object_table_alloc_shm(table
, memory_map_size
,
242 return _shm_object_table_alloc_mem(table
, memory_map_size
);
249 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
250 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
251 size_t memory_map_size
)
253 struct shm_object
*obj
;
257 if (table
->allocated_len
>= table
->size
)
259 /* streams _must_ be received in sequential order, else fail. */
260 if (stream_nr
+ 1 != table
->allocated_len
)
263 obj
= &table
->objects
[table
->allocated_len
];
265 /* wait_fd: set write end of the pipe. */
266 obj
->wait_fd
[0] = -1; /* read end is unset */
267 obj
->wait_fd
[1] = wakeup_fd
;
268 obj
->shm_fd
= shm_fd
;
269 obj
->shm_fd_ownership
= 1;
271 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
276 /* The write end of the pipe needs to be non-blocking */
277 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
283 /* memory_map: mmap */
284 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
285 MAP_SHARED
, shm_fd
, 0);
286 if (memory_map
== MAP_FAILED
) {
290 obj
->type
= SHM_OBJECT_SHM
;
291 obj
->memory_map
= memory_map
;
292 obj
->memory_map_size
= memory_map_size
;
293 obj
->allocated_len
= memory_map_size
;
294 obj
->index
= table
->allocated_len
++;
304 * Passing ownership of mem to object.
306 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
307 void *mem
, size_t memory_map_size
, int wakeup_fd
)
309 struct shm_object
*obj
;
312 if (table
->allocated_len
>= table
->size
)
314 obj
= &table
->objects
[table
->allocated_len
];
316 obj
->wait_fd
[0] = -1; /* read end is unset */
317 obj
->wait_fd
[1] = wakeup_fd
;
319 obj
->shm_fd_ownership
= 0;
321 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
326 /* The write end of the pipe needs to be non-blocking */
327 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
333 obj
->type
= SHM_OBJECT_MEM
;
334 obj
->memory_map
= mem
;
335 obj
->memory_map_size
= memory_map_size
;
336 obj
->allocated_len
= memory_map_size
;
337 obj
->index
= table
->allocated_len
++;
346 void shmp_object_destroy(struct shm_object
*obj
)
353 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
358 if (obj
->shm_fd_ownership
) {
359 ret
= close(obj
->shm_fd
);
365 for (i
= 0; i
< 2; i
++) {
366 if (obj
->wait_fd
[i
] < 0)
368 ret
= close(obj
->wait_fd
[i
]);
380 for (i
= 0; i
< 2; i
++) {
381 if (obj
->wait_fd
[i
] < 0)
383 ret
= close(obj
->wait_fd
[i
]);
389 free(obj
->memory_map
);
397 void shm_object_table_destroy(struct shm_object_table
*table
)
401 for (i
= 0; i
< table
->allocated_len
; i
++)
402 shmp_object_destroy(&table
->objects
[i
]);
407 * zalloc_shm - allocate memory within a shm object.
409 * Shared memory is already zeroed by shmget.
410 * *NOT* multithread-safe (should be protected by mutex).
411 * Returns a -1, -1 tuple on error.
413 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
416 struct shm_ref shm_ref_error
= { -1, -1 };
418 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
419 return shm_ref_error
;
420 ref
.index
= obj
->index
;
421 ref
.offset
= obj
->allocated_len
;
422 obj
->allocated_len
+= len
;
426 void align_shm(struct shm_object
*obj
, size_t align
)
428 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
429 obj
->allocated_len
+= offset_len
;
This page took 0.046741 seconds and 5 git commands to generate.