1 | /** |
---|
2 | * @file |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2016 Gedare Bloom. |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.org/license/LICENSE. |
---|
11 | */ |
---|
12 | |
---|
13 | #if HAVE_CONFIG_H |
---|
14 | #include "config.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <errno.h> |
---|
18 | #include <string.h> |
---|
19 | #include <rtems/score/wkspace.h> |
---|
20 | #include <rtems/posix/shmimpl.h> |
---|
21 | |
---|
22 | int _POSIX_Shm_Object_create_from_workspace( |
---|
23 | POSIX_Shm_Object *shm_obj, |
---|
24 | size_t size |
---|
25 | ) |
---|
26 | { |
---|
27 | shm_obj->handle = _Workspace_Allocate_or_fatal_error( size ); |
---|
28 | memset( shm_obj->handle, 0, size ); |
---|
29 | shm_obj->size = size; |
---|
30 | return 0; |
---|
31 | } |
---|
32 | |
---|
33 | int _POSIX_Shm_Object_delete_from_workspace( POSIX_Shm_Object *shm_obj ) |
---|
34 | { |
---|
35 | /* zero out memory before releasing it. */ |
---|
36 | memset( shm_obj->handle, 0, shm_obj->size ); |
---|
37 | _Workspace_Free( shm_obj->handle ); |
---|
38 | shm_obj->handle = NULL; |
---|
39 | shm_obj->size = 0; |
---|
40 | return 0; |
---|
41 | } |
---|
42 | |
---|
43 | int _POSIX_Shm_Object_resize_from_workspace( |
---|
44 | POSIX_Shm_Object *shm_obj, |
---|
45 | size_t size |
---|
46 | ) |
---|
47 | { |
---|
48 | int err; |
---|
49 | |
---|
50 | if ( size == 0 ) { |
---|
51 | err = _POSIX_Shm_Object_delete_from_workspace( shm_obj ); |
---|
52 | } else if ( shm_obj->handle == NULL && shm_obj->size == 0 ) { |
---|
53 | err = _POSIX_Shm_Object_create_from_workspace( shm_obj, size ); |
---|
54 | } else { |
---|
55 | /* Refuse to resize a workspace object. */ |
---|
56 | err = EIO; |
---|
57 | } |
---|
58 | return err; |
---|
59 | } |
---|
60 | |
---|
61 | int _POSIX_Shm_Object_read_from_workspace( |
---|
62 | POSIX_Shm_Object *shm_obj, |
---|
63 | void *buf, |
---|
64 | size_t count |
---|
65 | ) |
---|
66 | { |
---|
67 | if ( shm_obj == NULL || shm_obj->handle == NULL ) |
---|
68 | return 0; |
---|
69 | |
---|
70 | if ( shm_obj->size < count ) { |
---|
71 | count = shm_obj->size; |
---|
72 | } |
---|
73 | |
---|
74 | memcpy( buf, shm_obj->handle, count ); |
---|
75 | |
---|
76 | return count; |
---|
77 | } |
---|
78 | |
---|
79 | void * _POSIX_Shm_Object_mmap_from_workspace( |
---|
80 | POSIX_Shm_Object *shm_obj, |
---|
81 | size_t len, |
---|
82 | int prot, |
---|
83 | off_t off |
---|
84 | ) |
---|
85 | { |
---|
86 | if ( shm_obj == NULL || shm_obj->handle == NULL ) |
---|
87 | return 0; |
---|
88 | |
---|
89 | /* This is already checked by mmap. Maybe make it a debug assert? */ |
---|
90 | if ( shm_obj->size < len + off ) { |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | return (char*)shm_obj->handle + off; |
---|
95 | } |
---|
96 | |
---|