source: rtems/cpukit/posix/src/shmwkspace.c @ ba776282

5
Last change on this file since ba776282 was ba776282, checked in by Gedare Bloom <gedare@…>, on 08/12/16 at 19:25:10

posix: shared memory support

Add POSIX shared memory manager (Shm). Includes a hook-based
approach for the backing memory storage that defaults to the
Workspace, and a test is provided using the heap. A test is
also provided for the basic use of mmap'ing a shared memory
object. This test currently fails at the mmap stage due to
no support for mmap.

  • Property mode set to 100644
File size: 1.5 KB
Line 
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 <rtems/score/wkspace.h>
19#include <rtems/posix/shmimpl.h>
20
21int _POSIX_Shm_Object_create_from_workspace(
22  POSIX_Shm_Object *shm_obj,
23  size_t size
24)
25{
26  shm_obj->handle = _Workspace_Allocate_or_fatal_error( size );
27  memset( shm_obj->handle, 0, size );
28  shm_obj->size = size;
29  return 0;
30}
31
32int _POSIX_Shm_Object_delete_from_workspace( POSIX_Shm_Object *shm_obj )
33{
34  /* zero out memory before releasing it. */
35  memset( shm_obj->handle, 0, shm_obj->size );
36  _Workspace_Free( shm_obj->handle );
37  shm_obj->handle = NULL;
38  shm_obj->size = 0;
39  return 0;
40}
41
42int _POSIX_Shm_Object_resize_from_workspace(
43  POSIX_Shm_Object *shm_obj,
44  size_t size
45)
46{
47  int err;
48
49  if ( size == 0 ) {
50    err = _POSIX_Shm_Object_delete_from_workspace( shm_obj );
51  } else if ( shm_obj->handle == NULL && shm_obj->size == 0 ) {
52    err = _POSIX_Shm_Object_create_from_workspace( shm_obj, size );
53  } else {
54    /* Refuse to resize a workspace object. */
55    err = EIO;
56  }
57  return err;
58}
59
60int _POSIX_Shm_Object_read_from_workspace(
61  POSIX_Shm_Object *shm_obj,
62  void *buf,
63  size_t count
64)
65{
66  if ( shm_obj == NULL || shm_obj->handle == NULL )
67    return 0;
68
69  if ( shm_obj->size < count ) {
70    count = shm_obj->size;
71  }
72
73  memcpy( buf, shm_obj->handle, count );
74
75  return count;
76}
77
78
Note: See TracBrowser for help on using the repository browser.