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 <stdlib.h>
0038 #include <string.h>
0039 #include <rtems/posix/shmimpl.h>
0040 
0041 int _POSIX_Shm_Object_create_from_heap(
0042   POSIX_Shm_Object *shm_obj,
0043   size_t size
0044 )
0045 {
0046   void *p = calloc( 1, size ); /* get zero'd memory */
0047   if ( p != NULL ) {
0048     shm_obj->handle = p;
0049     shm_obj->size = size;
0050   } else {
0051     errno = EIO;
0052   }
0053   return 0;
0054 }
0055 
0056 int _POSIX_Shm_Object_delete_from_heap( POSIX_Shm_Object *shm_obj )
0057 {
0058   /* zero out memory before releasing it. */
0059   memset( shm_obj->handle, 0, shm_obj->size );
0060   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_heap(
0067   POSIX_Shm_Object *shm_obj,
0068   size_t size
0069 )
0070 {
0071   void *p;
0072   int err = 0;
0073 
0074   if ( size < shm_obj->size ) {
0075     /* zero out if shrinking */
0076     p = (void*)((uintptr_t)shm_obj->handle + (uintptr_t)size);
0077     memset( p, 0, shm_obj->size - size );
0078   }
0079   p = realloc( shm_obj->handle, size );
0080   if ( p != NULL ) {
0081     shm_obj->handle = p;
0082     if ( size > shm_obj->size ) {
0083       /* initialize added memory */ 
0084       memset( p, 0, size - shm_obj->size );
0085     }
0086     shm_obj->size = size;
0087   } else {
0088     err = EIO;
0089   }
0090   return err;
0091 }
0092 
0093 /* This is identical to _POSIX_Shm_Object_read_from_wkspace */
0094 int _POSIX_Shm_Object_read_from_heap(
0095   POSIX_Shm_Object *shm_obj,
0096   void *buf,
0097   size_t count
0098 )
0099 {
0100   if ( shm_obj == NULL || shm_obj->handle == NULL )
0101     return 0;
0102 
0103   if ( shm_obj->size < count ) {
0104     count = shm_obj->size;
0105   }
0106 
0107   memcpy( buf, shm_obj->handle, count );
0108 
0109   return count;
0110 }
0111 
0112 /* This is identical to _POSIX_Shm_Object_mmap_from_wkspace */
0113 void * _POSIX_Shm_Object_mmap_from_heap(
0114   POSIX_Shm_Object *shm_obj,
0115   size_t len,
0116   int prot,
0117   off_t off
0118 )
0119 {
0120   if ( shm_obj == NULL || shm_obj->handle == NULL )
0121     return 0;
0122 
0123   /* This is already checked by mmap. Maybe make it a debug assert? */
0124   if ( shm_obj->size < len + off ) {
0125     return NULL;
0126   }
0127 
0128   return (char*)shm_obj->handle + off;
0129 }
0130