Changeset 694e946d in rtems


Ignore:
Timestamp:
Sep 6, 2017, 5:31:48 AM (23 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
ead1281f
Parents:
18b32d76
git-author:
Sebastian Huber <sebastian.huber@…> (09/06/17 05:31:48)
git-committer:
Sebastian Huber <sebastian.huber@…> (09/14/17 05:02:28)
Message:

libio: Remove special-case reference count

The top-level IO library structures should contain no special-case data.

Update #2859.

Location:
cpukit
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libcsupport/include/rtems/libio.h

    r18b32d76 r694e946d  
    13211321  off_t                                   offset;    /* current offset into file */
    13221322  uint32_t                                flags;
    1323   uint32_t                                mapping_refcnt; /* current mappings */
    13241323  rtems_filesystem_location_info_t        pathinfo;
    13251324  uint32_t                                data0;     /* private to "driver" */
  • cpukit/libcsupport/include/rtems/libio_.h

    r18b32d76 r694e946d  
    304304  rtems_libio_t *iop
    305305);
    306 
    307 /**
    308  * Garbage collects the free libio in case it was previously freed but there
    309  * were still references to it.
    310  */
    311 void rtems_libio_check_deferred_free( rtems_libio_t *iop );
    312 
    313 /**
    314  * Increment the reference count tracking number of mmap mappings of a file.
    315  * Returns the updated reference count value.
    316  */
    317 static inline uint32_t rtems_libio_increment_mapping_refcnt(rtems_libio_t *iop)
    318 {
    319   uint32_t refcnt;
    320   rtems_libio_lock();
    321   refcnt = ++iop->mapping_refcnt;
    322   rtems_libio_unlock();
    323   return refcnt;
    324 }
    325 
    326 /**
    327  * Decrement the reference count tracking number of mmap mappings of a file.
    328  * Returns the updated reference count value.
    329  */
    330 static inline uint32_t rtems_libio_decrement_mapping_refcnt(rtems_libio_t *iop)
    331 {
    332   uint32_t refcnt;
    333   rtems_libio_lock();
    334   refcnt = --iop->mapping_refcnt;
    335   rtems_libio_unlock();
    336   return refcnt;
    337 }
    338 
    339 static inline bool rtems_libio_is_mapped(rtems_libio_t *iop)
    340 {
    341   bool is_mapped;
    342   rtems_libio_lock();
    343   is_mapped = iop->mapping_refcnt != 0;
    344   rtems_libio_unlock();
    345   return is_mapped;
    346 }
    347306
    348307/*
  • cpukit/libcsupport/src/libio.c

    r18b32d76 r694e946d  
    139139
    140140    iop->flags = 0;
    141     /* If the mapping_refcnt is non-zero, the deferred free will be
    142      * called by munmap. The iop is no longer good to use, but it cannot
    143      * be recycled until the mapped file is unmapped. deferred free knows
    144      * it can recycle the iop in case flags == 0 and iop->data1 == iop,
    145      * since these two conditions are not otherwise satisifed at
    146      * the same time. It may be possible that iop->data1 == iop when
    147      * flags != 0 because data1 is private to the driver. However, flags == 0
    148      * means a freed iop, and an iop on the freelist cannot store a pointer
    149      * to itself in data1, or else the freelist is corrupted. We can't use
    150      * NULL in data1 as an indicator because it is used by the tail of the
    151      * freelist. */
    152     if ( iop->mapping_refcnt == 0 ) {
    153       iop->data1 = rtems_libio_iop_freelist;
    154       rtems_libio_iop_freelist = iop;
    155     } else {
    156       iop->data1 = iop;
    157     }
     141    iop->data1 = rtems_libio_iop_freelist;
     142    rtems_libio_iop_freelist = iop;
    158143
    159144  rtems_libio_unlock();
    160145}
    161 
    162 void rtems_libio_check_deferred_free(
    163   rtems_libio_t *iop
    164 )
    165 {
    166   rtems_libio_lock();
    167     if ( iop->mapping_refcnt == 0 && iop->flags == 0 && iop->data1 == iop) {
    168       /* No mappings and rtems_libio_free already called, recycle the iop */
    169       iop->data1 = rtems_libio_iop_freelist;
    170       rtems_libio_iop_freelist = iop;
    171     }
    172   rtems_libio_unlock();
    173 }
  • cpukit/posix/include/rtems/posix/mmanimpl.h

    r18b32d76 r694e946d  
    1919#include <rtems/libio_.h>
    2020#include <rtems/chain.h> /* FIXME: use score chains for proper layering? */
     21#include <rtems/posix/shm.h>
    2122
    2223#ifdef __cplusplus
     
    3031 */
    3132typedef struct mmap_mappings_s {
    32   rtems_chain_node node;  /**< The mapping chain's node */
    33   void*            addr;  /**< The address of the mapped memory */
    34   size_t           len;   /**< The length of memory mapped */
    35   int              flags; /**< The mapping flags */
    36   rtems_libio_t   *iop;   /**< The mapped object's file descriptor pointer */
    37   bool             is_shared_shm; /**< True if MAP_SHARED of shared memory */
     33  rtems_chain_node   node;  /**< The mapping chain's node */
     34  void*              addr;  /**< The address of the mapped memory */
     35  size_t             len;   /**< The length of memory mapped */
     36  int                flags; /**< The mapping flags */
     37  POSIX_Shm_Control *shm;   /**< The shared memory object or NULL */
    3838} mmap_mapping;
    3939
  • cpukit/posix/src/mmap.c

    r18b32d76 r694e946d  
    4949  bool            map_shared;
    5050  bool            map_private;
     51  bool            is_shared_shm;
    5152  int             err;
    5253
     
    195196  mapping->len = len;
    196197  mapping->flags = flags;
    197   mapping->iop = iop;
    198198
    199199  if ( !map_anonymous ) {
     
    207207         S_ISCHR( sb.st_mode ) || S_ISFIFO( sb.st_mode ) ||
    208208         S_ISSOCK( sb.st_mode ) ) {
    209       mapping->is_shared_shm = false;
     209      is_shared_shm = false;
    210210    } else {
    211       mapping->is_shared_shm = true;
     211      is_shared_shm = true;
    212212    }
    213213  } else {
    214     mapping->is_shared_shm = false;
     214    is_shared_shm = false;
    215215  }
    216216
     
    219219  } else if ( map_private ) {
    220220    /* private mappings of shared memory do not need special treatment. */
    221     mapping->is_shared_shm = false;
     221    is_shared_shm = false;
    222222    posix_memalign( &mapping->addr, PAGE_SIZE, len );
    223223    if ( !mapping->addr ) {
     
    229229
    230230  /* MAP_FIXED is not supported for shared memory objects with MAP_SHARED. */
    231   if ( map_fixed && mapping->is_shared_shm ) {
     231  if ( map_fixed && is_shared_shm ) {
    232232    free( mapping );
    233233    errno = ENOTSUP;
     
    281281    }
    282282  } else if ( map_shared ) {
     283    if ( is_shared_shm ) {
     284      /* FIXME: This use of implementation details is a hack. */
     285      mapping->shm = iop_to_shm( iop );
     286    }
     287
    283288    err = (*iop->pathinfo.handlers->mmap_h)(
    284289        iop, &mapping->addr, len, prot, off );
     
    290295  }
    291296
    292   if ( iop != NULL ) {
    293     /* add an extra reference to the file associated with fildes that
    294      * is not removed by a subsequent close().  This reference shall be removed
    295      * when there are no more mappings to the file. */
    296     rtems_libio_increment_mapping_refcnt(iop);
    297   }
    298 
    299297  rtems_chain_append_unprotected( &mmap_mappings, &mapping->node );
    300298
  • cpukit/posix/src/munmap.c

    r18b32d76 r694e946d  
    1717#include <sys/mman.h>
    1818
    19 #include <rtems/posix/mmanimpl.h> 
     19#include <rtems/posix/mmanimpl.h>
    2020#include <rtems/posix/shmimpl.h>
    21 
    22 static void shm_munmap( rtems_libio_t *iop )
    23 {
    24   POSIX_Shm_Control *shm = iop_to_shm( iop );
    25 
    26   /* decrement mmap's shm reference_count and maybe delete the object */
    27   POSIX_Shm_Attempt_delete(shm);
    28 }
    2921
    3022int munmap(void *addr, size_t len)
    3123{
    32   mmap_mapping      *mapping;
    33   rtems_chain_node  *node;
    34   uint32_t           refcnt;
    35  
     24  mmap_mapping     *mapping;
     25  rtems_chain_node *node;
     26
    3627  /*
    3728   * Clear errno.
     
    6152         ( addr < ( mapping->addr + mapping->len )) ) {
    6253      rtems_chain_extract_unprotected( node );
     54
    6355      /* FIXME: generally need a way to clean-up the backing object, but
    6456       * currently it only matters for MAP_SHARED shm objects. */
    65       if ( mapping->is_shared_shm == true ) {
    66         shm_munmap(mapping->iop);
     57      if ( mapping->shm != NULL ) {
     58        POSIX_Shm_Attempt_delete(mapping->shm);
    6759      }
    68       if ( mapping->iop != NULL ) {
    69         refcnt = rtems_libio_decrement_mapping_refcnt(mapping->iop);
    70         if ( refcnt == 0 ) {
    71           rtems_libio_check_deferred_free(mapping->iop);
    72         }
    73       }
     60
    7461      /* only free the mapping address for non-fixed mapping */
    7562      if (( mapping->flags & MAP_FIXED ) != MAP_FIXED ) {
Note: See TracChangeset for help on using the changeset viewer.