Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:22

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  */
0006 
0007 /*
0008  * Copyright (c) 2016 Gedare Bloom.
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  */
0031 
0032 #ifdef HAVE_CONFIG_H
0033 #include "config.h"
0034 #endif
0035 
0036 #include <errno.h>
0037 #include <string.h>
0038 #include <rtems/score/wkspace.h>
0039 #include <rtems/posix/shmimpl.h>
0040 
0041 int _POSIX_Shm_Object_create_from_workspace(
0042   POSIX_Shm_Object *shm_obj,
0043   size_t size
0044 )
0045 {
0046   shm_obj->handle = _Workspace_Allocate( size );
0047   if ( shm_obj->handle == NULL ) {
0048     return ENOMEM;
0049   }
0050 
0051   memset( shm_obj->handle, 0, size );
0052   shm_obj->size = size;
0053   return 0;
0054 }
0055 
0056 int _POSIX_Shm_Object_delete_from_workspace( POSIX_Shm_Object *shm_obj )
0057 {
0058   /* zero out memory before releasing it. */
0059   memset( shm_obj->handle, 0, shm_obj->size );
0060   _Workspace_Free( shm_obj->handle );
0061   shm_obj->handle = NULL;
0062   shm_obj->size = 0;
0063   return 0;
0064 }
0065 
0066 int _POSIX_Shm_Object_resize_from_workspace(
0067   POSIX_Shm_Object *shm_obj,
0068   size_t size
0069 )
0070 {
0071   int err;
0072 
0073   if ( size == 0 ) {
0074     err = _POSIX_Shm_Object_delete_from_workspace( shm_obj );
0075   } else if ( shm_obj->handle == NULL && shm_obj->size == 0 ) {
0076     err = _POSIX_Shm_Object_create_from_workspace( shm_obj, size );
0077   } else {
0078     /* Refuse to resize a workspace object. */
0079     err = EIO;
0080   }
0081   return err;
0082 }
0083 
0084 int _POSIX_Shm_Object_read_from_workspace(
0085   POSIX_Shm_Object *shm_obj,
0086   void *buf,
0087   size_t count
0088 )
0089 {
0090   if ( shm_obj == NULL || shm_obj->handle == NULL )
0091     return 0;
0092 
0093   if ( shm_obj->size < count ) {
0094     count = shm_obj->size;
0095   }
0096 
0097   memcpy( buf, shm_obj->handle, count );
0098 
0099   return count;
0100 }
0101 
0102 void * _POSIX_Shm_Object_mmap_from_workspace(
0103   POSIX_Shm_Object *shm_obj,
0104   size_t len,
0105   int prot,
0106   off_t off
0107 )
0108 {
0109   if ( shm_obj == NULL || shm_obj->handle == NULL )
0110     return 0;
0111 
0112   /* This is already checked by mmap. Maybe make it a debug assert? */
0113   if ( shm_obj->size < len + off ) {
0114     return NULL;
0115   }
0116 
0117   return (char*)shm_obj->handle + off;
0118 }
0119