563903877f5eb59a62409ce32662a8bd6f7600bd
4 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Dual LGPL v2.1/GPL v2 license.
13 #include <sys/stat.h> /* For mode constants */
14 #include <fcntl.h> /* For O_* constants */
16 #include <ust/align.h>
18 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
20 struct shm_object_table
*table
;
22 table
= zmalloc(sizeof(struct shm_object_table
) +
23 max_nb_obj
* sizeof(table
->objects
[0]));
24 table
->size
= max_nb_obj
;
28 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
29 size_t memory_map_size
)
31 int shmfd
, waitfd
[2], ret
, i
;
32 struct shm_object
*obj
;
35 if (table
->allocated_len
>= table
->size
)
37 obj
= &table
->objects
[table
->allocated_len
];
39 /* wait_fd: create pipe */
45 for (i
= 0; i
< 2; i
++) {
46 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
52 /* The write end of the pipe needs to be non-blocking */
53 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
58 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
60 /* shm_fd: create shm */
63 * Allocate shm, and immediately unlink its shm oject, keeping
64 * only the file descriptor as a reference to the object. If it
65 * already exists (caused by short race window during which the
66 * global object exists in a concurrent shm_open), simply retry.
67 * We specifically do _not_ use the / at the beginning of the
68 * pathname so that some OS implementations can keep it local to
69 * the process (POSIX leaves this implementation-defined).
70 * Ignore the shm_unlink errors, because we handle leaks that
71 * could occur by applications crashing between shm_open and
72 * shm_unlink by unlinking the shm before every open. Therefore,
73 * we can only leak one single shm (and only if the application
74 * crashes between shm_open and the following shm_unlink).
77 ret
= shm_unlink("ust-shm-tmp");
78 if (ret
< 0 && errno
!= ENOENT
) {
80 goto error_shm_unlink
;
82 shmfd
= shm_open("ust-shm-tmp",
83 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
84 } while (shmfd
< 0 && errno
== EEXIST
);
89 ret
= shm_unlink("ust-shm-tmp");
90 if (ret
< 0 && errno
!= ENOENT
) {
92 goto error_shm_release
;
94 ret
= ftruncate(shmfd
, memory_map_size
);
101 /* memory_map: mmap */
102 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
103 MAP_SHARED
, shmfd
, 0);
104 if (memory_map
== MAP_FAILED
) {
108 obj
->memory_map
= memory_map
;
109 obj
->memory_map_size
= memory_map_size
;
110 obj
->allocated_len
= 0;
111 obj
->index
= table
->allocated_len
++;
126 for (i
= 0; i
< 2; i
++) {
127 ret
= close(waitfd
[i
]);
139 void shmp_object_destroy(struct shm_object
*obj
)
143 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
148 ret
= close(obj
->shm_fd
);
153 for (i
= 0; i
< 2; i
++) {
154 ret
= close(obj
->wait_fd
[i
]);
162 void shm_object_table_destroy(struct shm_object_table
*table
)
166 for (i
= 0; i
< table
->allocated_len
; i
++)
167 shmp_object_destroy(&table
->objects
[i
]);
172 * zalloc_shm - allocate memory within a shm object.
174 * Shared memory is already zeroed by shmget.
175 * *NOT* multithread-safe (should be protected by mutex).
176 * Returns a -1, -1 tuple on error.
178 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
181 struct shm_ref shm_ref_error
= { -1, -1 };
183 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
184 return shm_ref_error
;
185 ref
.index
= obj
->index
;
186 ref
.offset
= obj
->allocated_len
;
187 obj
->allocated_len
+= len
;
191 void align_shm(struct shm_object
*obj
, size_t align
)
193 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
194 obj
->allocated_len
+= offset_len
;
This page took 0.032854 seconds and 3 git commands to generate.