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
25 #include <sys/types.h>
26 #include <sys/stat.h> /* For mode constants */
27 #include <fcntl.h> /* For O_* constants */
32 #include <lttng/align.h>
37 * Ensure we have the required amount of space available by writing 0
38 * into the entire buffer. Not doing so can trigger SIGBUS when going
39 * beyond the available shm space.
42 int zero_file(int fd
, size_t len
)
50 pagelen
= sysconf(_SC_PAGESIZE
);
53 zeropage
= calloc(pagelen
, 1);
57 while (len
> written
) {
59 retlen
= write(fd
, zeropage
,
60 min_t(size_t, pagelen
, len
- written
));
61 } while (retlen
== -1UL && errno
== EINTR
);
74 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
76 struct shm_object_table
*table
;
78 table
= zmalloc(sizeof(struct shm_object_table
) +
79 max_nb_obj
* sizeof(table
->objects
[0]));
82 table
->size
= max_nb_obj
;
87 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
88 size_t memory_map_size
,
91 int shmfd
, waitfd
[2], ret
, i
;
92 struct shm_object
*obj
;
97 if (table
->allocated_len
>= table
->size
)
99 obj
= &table
->objects
[table
->allocated_len
];
101 /* wait_fd: create pipe */
107 for (i
= 0; i
< 2; i
++) {
108 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
114 /* The write end of the pipe needs to be non-blocking */
115 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
120 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
125 ret
= zero_file(shmfd
, memory_map_size
);
128 goto error_zero_file
;
130 ret
= ftruncate(shmfd
, memory_map_size
);
133 goto error_ftruncate
;
135 obj
->shm_fd_ownership
= 0;
138 /* memory_map: mmap */
139 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
140 MAP_SHARED
, shmfd
, 0);
141 if (memory_map
== MAP_FAILED
) {
145 obj
->type
= SHM_OBJECT_SHM
;
146 obj
->memory_map
= memory_map
;
147 obj
->memory_map_size
= memory_map_size
;
148 obj
->allocated_len
= 0;
149 obj
->index
= table
->allocated_len
++;
157 for (i
= 0; i
< 2; i
++) {
158 ret
= close(waitfd
[i
]);
169 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
170 size_t memory_map_size
)
172 struct shm_object
*obj
;
174 int waitfd
[2], i
, ret
;
176 if (table
->allocated_len
>= table
->size
)
178 obj
= &table
->objects
[table
->allocated_len
];
180 memory_map
= zmalloc(memory_map_size
);
184 /* wait_fd: create pipe */
190 for (i
= 0; i
< 2; i
++) {
191 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
197 /* The write end of the pipe needs to be non-blocking */
198 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
203 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
207 obj
->shm_fd_ownership
= 0;
209 obj
->type
= SHM_OBJECT_MEM
;
210 obj
->memory_map
= memory_map
;
211 obj
->memory_map_size
= memory_map_size
;
212 obj
->allocated_len
= 0;
213 obj
->index
= table
->allocated_len
++;
218 for (i
= 0; i
< 2; i
++) {
219 ret
= close(waitfd
[i
]);
231 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
232 size_t memory_map_size
,
233 enum shm_object_type type
,
238 return _shm_object_table_alloc_shm(table
, memory_map_size
,
241 return _shm_object_table_alloc_mem(table
, memory_map_size
);
248 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
249 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
250 size_t memory_map_size
)
252 struct shm_object
*obj
;
256 if (table
->allocated_len
>= table
->size
)
258 /* streams _must_ be received in sequential order, else fail. */
259 if (stream_nr
+ 1 != table
->allocated_len
)
262 obj
= &table
->objects
[table
->allocated_len
];
264 /* wait_fd: set write end of the pipe. */
265 obj
->wait_fd
[0] = -1; /* read end is unset */
266 obj
->wait_fd
[1] = wakeup_fd
;
267 obj
->shm_fd
= shm_fd
;
268 obj
->shm_fd_ownership
= 1;
270 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
275 /* The write end of the pipe needs to be non-blocking */
276 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
282 /* memory_map: mmap */
283 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
284 MAP_SHARED
, shm_fd
, 0);
285 if (memory_map
== MAP_FAILED
) {
289 obj
->type
= SHM_OBJECT_SHM
;
290 obj
->memory_map
= memory_map
;
291 obj
->memory_map_size
= memory_map_size
;
292 obj
->allocated_len
= memory_map_size
;
293 obj
->index
= table
->allocated_len
++;
303 * Passing ownership of mem to object.
305 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
306 void *mem
, size_t memory_map_size
, int wakeup_fd
)
308 struct shm_object
*obj
;
311 if (table
->allocated_len
>= table
->size
)
313 obj
= &table
->objects
[table
->allocated_len
];
315 obj
->wait_fd
[0] = -1; /* read end is unset */
316 obj
->wait_fd
[1] = wakeup_fd
;
318 obj
->shm_fd_ownership
= 0;
320 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
325 /* The write end of the pipe needs to be non-blocking */
326 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
332 obj
->type
= SHM_OBJECT_MEM
;
333 obj
->memory_map
= mem
;
334 obj
->memory_map_size
= memory_map_size
;
335 obj
->allocated_len
= memory_map_size
;
336 obj
->index
= table
->allocated_len
++;
345 void shmp_object_destroy(struct shm_object
*obj
)
352 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
357 if (obj
->shm_fd_ownership
) {
358 ret
= close(obj
->shm_fd
);
364 for (i
= 0; i
< 2; i
++) {
365 if (obj
->wait_fd
[i
] < 0)
367 ret
= close(obj
->wait_fd
[i
]);
379 for (i
= 0; i
< 2; i
++) {
380 if (obj
->wait_fd
[i
] < 0)
382 ret
= close(obj
->wait_fd
[i
]);
388 free(obj
->memory_map
);
396 void shm_object_table_destroy(struct shm_object_table
*table
)
400 for (i
= 0; i
< table
->allocated_len
; i
++)
401 shmp_object_destroy(&table
->objects
[i
]);
406 * zalloc_shm - allocate memory within a shm object.
408 * Shared memory is already zeroed by shmget.
409 * *NOT* multithread-safe (should be protected by mutex).
410 * Returns a -1, -1 tuple on error.
412 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
415 struct shm_ref shm_ref_error
= { -1, -1 };
417 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
418 return shm_ref_error
;
419 ref
.index
= obj
->index
;
420 ref
.offset
= obj
->allocated_len
;
421 obj
->allocated_len
+= len
;
425 void align_shm(struct shm_object
*obj
, size_t align
)
427 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
428 obj
->allocated_len
+= offset_len
;
This page took 0.066321 seconds and 4 git commands to generate.