Back to home page

LXR

 
 

    


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

0001 /**
0002  * @file
0003  *
0004  * @ingroup rtems_ramdisk
0005  *
0006  * @brief RAM disk block device implementation.
0007  */
0008 
0009 /*
0010  * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
0011  * Author: Victor V. Vengerov <vvv@oktet.ru>
0012  */
0013 
0014 #ifdef HAVE_CONFIG_H
0015 #include "config.h"
0016 #endif
0017 
0018 #include <stdlib.h>
0019 
0020 #include <rtems.h>
0021 #include <rtems/libio.h>
0022 #include <rtems/ramdisk.h>
0023 
0024 rtems_device_driver
0025 ramdisk_initialize(
0026     rtems_device_major_number major RTEMS_UNUSED,
0027     rtems_device_minor_number minor RTEMS_UNUSED,
0028     void *arg RTEMS_UNUSED)
0029 {
0030     rtems_device_minor_number i;
0031     rtems_ramdisk_config *c = rtems_ramdisk_configuration;
0032     struct ramdisk *r;
0033     rtems_status_code rc;
0034 
0035     /*
0036      * Coverity Id 27 notes that this calloc() is a resource leak.
0037      *
0038      * This is allocating memory for a RAM disk which will persist for
0039      * the life of the system. RTEMS has no "de-initialize" driver call
0040      * so there is no corresponding free(r).  Coverity is correct that
0041      * it is never freed but this is not a problem.
0042      */
0043     r = calloc(rtems_ramdisk_configuration_size, sizeof(struct ramdisk));
0044     r->trace = false;
0045     for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++)
0046     {
0047         char name [] = RAMDISK_DEVICE_BASE_NAME "a";
0048         name [sizeof(RAMDISK_DEVICE_BASE_NAME) - 1] += i;
0049         r->block_size = c->block_size;
0050         r->block_num = c->block_num;
0051         if (c->location == NULL)
0052         {
0053             r->malloced = true;
0054             r->area = malloc(r->block_size * r->block_num);
0055             if (r->area == NULL) /* No enough memory for this disk */
0056             {
0057                 r->initialized = false;
0058                 continue;
0059             }
0060             else
0061             {
0062                 r->initialized = true;
0063             }
0064         }
0065         else
0066         {
0067             r->malloced = false;
0068             r->initialized = true;
0069             r->area = c->location;
0070         }
0071         rc = rtems_blkdev_create(name, c->block_size, c->block_num,
0072                                  ramdisk_ioctl, r);
0073         if (rc != RTEMS_SUCCESSFUL)
0074         {
0075             if (r->malloced)
0076             {
0077                 free(r->area);
0078             }
0079             r->initialized = false;
0080         }
0081     }
0082     return RTEMS_SUCCESSFUL;
0083 }