source: rtems/cpukit/libblock/src/ramdisk-config.c

Last change on this file was 24b94c4, checked in by Sebastian Huber <sebastian.huber@…>, on 07/30/18 at 04:39:09

ramdisk: Use rtems_blkdev_create()

Update #3358.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_ramdisk
5 *
6 * @brief RAM disk block device implementation.
7 */
8
9/*
10 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 * Author: Victor V. Vengerov <vvv@oktet.ru>
12 */
13
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdlib.h>
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <rtems/ramdisk.h>
23
24rtems_device_driver
25ramdisk_initialize(
26    rtems_device_major_number major RTEMS_UNUSED,
27    rtems_device_minor_number minor RTEMS_UNUSED,
28    void *arg RTEMS_UNUSED)
29{
30    rtems_device_minor_number i;
31    rtems_ramdisk_config *c = rtems_ramdisk_configuration;
32    struct ramdisk *r;
33    rtems_status_code rc;
34
35    /*
36     * Coverity Id 27 notes that this calloc() is a resource leak.
37     *
38     * This is allocating memory for a RAM disk which will persist for
39     * the life of the system. RTEMS has no "de-initialize" driver call
40     * so there is no corresponding free(r).  Coverity is correct that
41     * it is never freed but this is not a problem.
42     */
43    r = calloc(rtems_ramdisk_configuration_size, sizeof(struct ramdisk));
44    r->trace = false;
45    for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++)
46    {
47        char name [] = RAMDISK_DEVICE_BASE_NAME "a";
48        name [sizeof(RAMDISK_DEVICE_BASE_NAME) - 1] += i;
49        r->block_size = c->block_size;
50        r->block_num = c->block_num;
51        if (c->location == NULL)
52        {
53            r->malloced = true;
54            r->area = malloc(r->block_size * r->block_num);
55            if (r->area == NULL) /* No enough memory for this disk */
56            {
57                r->initialized = false;
58                continue;
59            }
60            else
61            {
62                r->initialized = true;
63            }
64        }
65        else
66        {
67            r->malloced = false;
68            r->initialized = true;
69            r->area = c->location;
70        }
71        rc = rtems_blkdev_create(name, c->block_size, c->block_num,
72                                 ramdisk_ioctl, r);
73        if (rc != RTEMS_SUCCESSFUL)
74        {
75            if (r->malloced)
76            {
77                free(r->area);
78            }
79            r->initialized = false;
80        }
81    }
82    return RTEMS_SUCCESSFUL;
83}
Note: See TracBrowser for help on using the repository browser.