29 #include <sys/types.h>
44 #include "debug_lock.h"
45 #include "shm_arena.h"
47 #include "arena_lock.h"
63 SPEW_SYS_ERR_RET(-1, _WARN, errno,
64 "fstat(fd=%d,) failed", a->
fd);
75 m_hdr = get_mapping_header(a, mapnum);
76 ASSERT(m_hdr->
map_length*CHUNK >= (uint32_t) getpagesize());
83 if(!(footer->
flags & IS_FREE))
88 (((uint8_t *) footer) -
91 ASSERT(seg->
length*CHUNK <= m_hdr->map_length*CHUNK -
95 if(seg != detached_seg)
101 _shm_detach_free_segment(a, seg, mapnum);
114 ((uintptr_t) seg)) || mapnum == 0)
121 offset_t new_length_used;
123 new_length_used = m_hdr->
length_used - seg->length;
124 st.st_size -= seg->length*CHUNK;
126 if(ftruncate(a->
fd, st.st_size))
128 SPEW_SYS_ERR_RET(-1, _WARN, errno,
129 "ftruncate(fd=%d,) failed", a->
fd);
131 SPEW(_DEBUG,
"Truncated arena file to %lu CHUNKS",
147 SPEW_SYS_ERR_RET(-1, _WARN, errno,
150 if(ftruncate(a->
fd, st.st_size))
152 SPEW_SYS_ERR_RET(-1, _WARN, errno,
153 "ftruncate(fd=%d,) failed", a->
fd);
155 SPEW(_DEBUG,
"Unmapped mapping %d and truncated arena file to %lu CHUNKS",
156 mapnum, (st.st_size)/CHUNK);
164 SPEW_SYS_ERR_RET(-1, _WARN, errno,
"realloc() failed");
191 SPEW(_DEBUG,
"%s(arena=%p, seg=%p, mapnum="MAPNUM_FORMAT
")",
192 __func__, a, seg, mapnum);
194 SPEW(_DEBUG,
"Removing segment \"%s\"", get_seg_name(seg));
196 if(_shm_detach_alloc_segment(a, seg, mapnum))
return 1;
200 m_hdr = get_mapping_header(a, mapnum);
207 ((mapnum == 0)?CHUNKS(
sizeof(
struct arena_header)):0))) <=
211 ASSERT((uintptr_t)(((uint8_t *)(a->
mapping[mapnum].
start)) +
213 (uintptr_t)(((uint8_t *) seg) + seg->
length*CHUNK));
217 ((mapnum == 0)?CHUNKS(
sizeof(
struct arena_header)):0))) !=
222 (((uint8_t *) seg) - CHUNKS(
sizeof(
struct seg_footer)));
224 if(f->
flags & IS_FREE)
231 (((uint8_t *) f) - (f->
length*CHUNK -
236 _shm_detach_free_segment(a, s, mapnum);
246 ((uintptr_t)(((uint8_t *) seg) + seg->length*CHUNK)))
249 s = (
struct seg_header *)(((uint8_t *) seg) + seg->length*CHUNK);
251 (((uint8_t *) s) + (s->
length*CHUNK -
253 if(f->
flags & IS_FREE)
260 _shm_detach_free_segment(a, s, mapnum);
268 seg->user_length = 0;
269 f = get_seg_footer(seg);
275 ((uintptr_t)(((uint8_t *) seg) + seg->length*CHUNK)))
278 return shrink_arena(a, seg);
283 insert_free_segment(a, seg, mapnum);
319 SPEW(_INFO,
"%s(arena=%p, ptr=%p)", __func__, arena, ptr);
321 arena = get_arena_and_autolock(arena, 2, &err IF_SPEW(, __func__));
334 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
345 m_hdr = get_mapping_header(arena, mapnum);
347 if((uintptr_t) ptr > (uintptr_t) arena->
mapping[mapnum].
start &&
348 (uintptr_t) ptr < (uintptr_t)(arena->
mapping[mapnum].
start +
356 SPEW(_WARN,
"A mapping was not found for this pointer=%p", ptr);
361 (((uint8_t *) ptr) - CHUNKS(
sizeof(
struct seg_header)));
363 ret = remove_segment(arena, seg, mapnum);
368 err = arena_autounlock(arena IF_SPEW(, __func__));
408 SPEW(_INFO,
"%s(arena=%p, name=\"%s\")", __func__, arena, name);
410 arena = get_arena_and_autolock(arena, 2, &err IF_SPEW(, __func__));
423 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
424 goto shm_remove_name_ret;
430 seg = find_segment(arena, name, &mapnum);
434 SPEW(_INFO,
"Segment \"%s\" not found", name);
436 goto shm_remove_name_ret;
439 ret = remove_segment(arena, seg, mapnum);
443 err = arena_autounlock(arena IF_SPEW(, __func__));
struct arena_header * header
struct shm_mapping * mapping
int shm_remove(shm_arena_t arena, const void *ptr)
remove a shared memory segment
int shm_remove_name(shm_arena_t arena, const char *name)
remove a shared memory segment by name