source: rtems/cpukit/posix/src/shmheap.c @ b2ed712

5
Last change on this file since b2ed712 was b2ed712, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:58:58

Include missing <string.h>

Update #2133.

  • Property mode set to 100644
File size: 2.2 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 <stdlib.h>
19#include <string.h>
20#include <rtems/posix/shmimpl.h>
21
22int _POSIX_Shm_Object_create_from_heap(
23  POSIX_Shm_Object *shm_obj,
24  size_t size
25)
26{
27  void *p = calloc( 1, size ); /* get zero'd memory */
28  if ( p != NULL ) {
29    shm_obj->handle = p;
30    shm_obj->size = size;
31  } else {
32    errno = EIO;
33  }
34  return 0;
35}
36
37int _POSIX_Shm_Object_delete_from_heap( POSIX_Shm_Object *shm_obj )
38{
39  /* zero out memory before releasing it. */
40  memset( shm_obj->handle, 0, shm_obj->size );
41  free( shm_obj->handle );
42  shm_obj->handle = NULL;
43  shm_obj->size = 0;
44  return 0;
45}
46
47int _POSIX_Shm_Object_resize_from_heap(
48  POSIX_Shm_Object *shm_obj,
49  size_t size
50)
51{
52  void *p;
53  int err = 0;
54
55  if ( size < shm_obj->size ) {
56    /* zero out if shrinking */
57    p = (void*)((uintptr_t)shm_obj->handle + (uintptr_t)size);
58    memset( p, 0, shm_obj->size - size );
59  }
60  p = realloc( shm_obj->handle, size );
61  if ( p != NULL ) {
62    shm_obj->handle = p;
63    if ( size > shm_obj->size ) {
64      /* initialize added memory */
65      memset( p, 0, size - shm_obj->size );
66    }
67    shm_obj->size = size;
68  } else {
69    err = EIO;
70  }
71  return err;
72}
73
74/* This is identical to _POSIX_Shm_Object_read_from_wkspace */
75int _POSIX_Shm_Object_read_from_heap(
76  POSIX_Shm_Object *shm_obj,
77  void *buf,
78  size_t count
79)
80{
81  if ( shm_obj == NULL || shm_obj->handle == NULL )
82    return 0;
83
84  if ( shm_obj->size < count ) {
85    count = shm_obj->size;
86  }
87
88  memcpy( buf, shm_obj->handle, count );
89
90  return count;
91}
92
93/* This is identical to _POSIX_Shm_Object_mmap_from_wkspace */
94void * _POSIX_Shm_Object_mmap_from_heap(
95  POSIX_Shm_Object *shm_obj,
96  size_t len,
97  int prot,
98  off_t off
99)
100{
101  if ( shm_obj == NULL || shm_obj->handle == NULL )
102    return 0;
103
104  /* This is already checked by mmap. Maybe make it a debug assert? */
105  if ( shm_obj->size < len + off ) {
106    return NULL;
107  }
108
109  return (char*)shm_obj->handle + off;
110}
111
Note: See TracBrowser for help on using the repository browser.