1 | /* |
---|
2 | * Copyright (c) 2012 Chris Johns (chrisj@rtems.org) |
---|
3 | * Copyright (c) 2017 Gedare Bloom (gedare@rtems.org) |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <rtems.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <sys/mman.h> |
---|
18 | |
---|
19 | #include <rtems/posix/mmanimpl.h> |
---|
20 | #include <rtems/posix/shmimpl.h> |
---|
21 | |
---|
22 | int munmap(void *addr, size_t len) |
---|
23 | { |
---|
24 | mmap_mapping *mapping; |
---|
25 | rtems_chain_node *node; |
---|
26 | |
---|
27 | /* |
---|
28 | * Clear errno. |
---|
29 | */ |
---|
30 | errno = 0; |
---|
31 | |
---|
32 | /* |
---|
33 | * Length cannot be 0. |
---|
34 | */ |
---|
35 | if ( len == 0 ) { |
---|
36 | errno = EINVAL; |
---|
37 | return -1; |
---|
38 | } |
---|
39 | |
---|
40 | /* Check for illegal addresses. Watch out for address wrap. */ |
---|
41 | if (addr + len < addr) { |
---|
42 | errno = EINVAL; |
---|
43 | return -1; |
---|
44 | } |
---|
45 | |
---|
46 | mmap_mappings_lock_obtain(); |
---|
47 | |
---|
48 | node = rtems_chain_first (&mmap_mappings); |
---|
49 | while ( !rtems_chain_is_tail( &mmap_mappings, node )) { |
---|
50 | mapping = (mmap_mapping*) node; |
---|
51 | if ( ( addr >= mapping->addr ) && |
---|
52 | ( addr < ( mapping->addr + mapping->len )) ) { |
---|
53 | rtems_chain_extract_unprotected( node ); |
---|
54 | |
---|
55 | /* FIXME: generally need a way to clean-up the backing object, but |
---|
56 | * currently it only matters for MAP_SHARED shm objects. */ |
---|
57 | if ( mapping->shm != NULL ) { |
---|
58 | POSIX_Shm_Attempt_delete(mapping->shm); |
---|
59 | } |
---|
60 | |
---|
61 | /* only free the mapping address for non-fixed mapping */ |
---|
62 | if (( mapping->flags & MAP_FIXED ) != MAP_FIXED ) { |
---|
63 | /* only free the mapping address for non-shared mapping, because we |
---|
64 | * re-use the mapping address across all of the shared mappings, and |
---|
65 | * it is memory managed independently... */ |
---|
66 | if (( mapping->flags & MAP_SHARED ) != MAP_SHARED ) { |
---|
67 | free( mapping->addr ); |
---|
68 | } |
---|
69 | } |
---|
70 | free( mapping ); |
---|
71 | break; |
---|
72 | } |
---|
73 | node = rtems_chain_next( node ); |
---|
74 | } |
---|
75 | |
---|
76 | mmap_mappings_lock_release( ); |
---|
77 | return 0; |
---|
78 | } |
---|