File: | /home/joel/rtems-4.11-work/build/rtems/c/src/../../cpukit/libblock/src/media-server.c |
Location: | line 151, column 3 |
Description: | Allocated memory never released. Potential memory leak |
1 | /** | ||
2 | * @file | ||
3 | * | ||
4 | * @ingroup RTEMSMedia | ||
5 | * | ||
6 | * @brief Media implementation. | ||
7 | */ | ||
8 | |||
9 | /* | ||
10 | * Copyright (c) 2009, 2010 embedded brains GmbH. All rights reserved. | ||
11 | * | ||
12 | * embedded brains GmbH | ||
13 | * Obere Lagerstr. 30 | ||
14 | * 82178 Puchheim | ||
15 | * Germany | ||
16 | * <rtems@embedded-brains.de> | ||
17 | * | ||
18 | * The license and distribution terms for this file may be | ||
19 | * found in the file LICENSE in this distribution or at | ||
20 | * http://www.rtems.com/license/LICENSE. | ||
21 | */ | ||
22 | |||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <assert.h> | ||
26 | |||
27 | #include <rtems.h> | ||
28 | #include <rtems/chain.h> | ||
29 | #include <rtems/media.h> | ||
30 | |||
31 | #define EVENT0x00002000 RTEMS_EVENT_130x00002000 | ||
32 | |||
33 | typedef struct { | ||
34 | rtems_chain_node node; | ||
35 | rtems_media_event event; | ||
36 | const char *src; | ||
37 | rtems_media_worker worker; | ||
38 | void *worker_arg; | ||
39 | } message; | ||
40 | |||
41 | static RTEMS_CHAIN_DEFINE_EMPTY(message_chain)Chain_Control message_chain = { (Chain_Node *) &(message_chain ).permanent_null, ((void*)0), (Chain_Node *) &(message_chain ) }; | ||
42 | |||
43 | static rtems_id server_id = RTEMS_ID_NONE0; | ||
44 | |||
45 | static void media_server(rtems_task_argument arg __attribute__((unused))) | ||
46 | { | ||
47 | rtems_status_code sc = RTEMS_SUCCESSFUL; | ||
48 | |||
49 | while (true1) { | ||
50 | message *msg = NULL((void*)0); | ||
51 | |||
52 | sc = rtems_chain_get_with_wait( | ||
53 | &message_chain, | ||
54 | EVENT0x00002000, | ||
55 | RTEMS_NO_TIMEOUT0, | ||
56 | (rtems_chain_node **) &msg | ||
57 | ); | ||
58 | assert(sc == RTEMS_SUCCESSFUL)((sc == RTEMS_SUCCESSFUL) ? (void) (0) : __assert_fail ("sc == RTEMS_SUCCESSFUL" , "/home/joel/rtems-4.11-work/build/rtems/c/src/../../cpukit/libblock/src/media-server.c" , 58, __PRETTY_FUNCTION__)); | ||
59 | assert(msg != NULL)((msg != ((void*)0)) ? (void) (0) : __assert_fail ("msg != ((void*)0)" , "/home/joel/rtems-4.11-work/build/rtems/c/src/../../cpukit/libblock/src/media-server.c" , 59, __PRETTY_FUNCTION__)); | ||
60 | |||
61 | rtems_media_post_event( | ||
62 | msg->event, | ||
63 | msg->src, | ||
64 | NULL((void*)0), | ||
65 | msg->worker, | ||
66 | msg->worker_arg | ||
67 | ); | ||
68 | |||
69 | free(msg); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | rtems_status_code rtems_media_server_initialize( | ||
74 | rtems_task_priority priority, | ||
75 | size_t stack_size, | ||
76 | rtems_mode modes, | ||
77 | rtems_attribute attributes | ||
78 | ) | ||
79 | { | ||
80 | rtems_status_code sc = RTEMS_SUCCESSFUL; | ||
81 | |||
82 | if (server_id == RTEMS_ID_NONE0) { | ||
83 | sc = rtems_media_initialize(); | ||
84 | if (sc != RTEMS_SUCCESSFUL) { | ||
85 | goto error; | ||
86 | } | ||
87 | |||
88 | sc = rtems_task_create( | ||
89 | rtems_build_name('M', 'D', 'I', 'A')( (uint32_t)('M') << 24 | (uint32_t)('D') << 16 | (uint32_t)('I') << 8 | (uint32_t)('A') ), | ||
90 | priority, | ||
91 | stack_size, | ||
92 | modes, | ||
93 | attributes, | ||
94 | &server_id | ||
95 | ); | ||
96 | if (sc != RTEMS_SUCCESSFUL) { | ||
97 | goto error; | ||
98 | } | ||
99 | |||
100 | sc = rtems_task_start(server_id, media_server, 0); | ||
101 | if (sc != RTEMS_SUCCESSFUL) { | ||
102 | goto error; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | return RTEMS_SUCCESSFUL; | ||
107 | |||
108 | error: | ||
109 | |||
110 | if (server_id != RTEMS_ID_NONE0) { | ||
111 | rtems_task_delete(server_id); | ||
112 | } | ||
113 | |||
114 | return RTEMS_NO_MEMORY; | ||
115 | } | ||
116 | |||
117 | rtems_status_code rtems_media_server_post_event( | ||
118 | rtems_media_event event, | ||
119 | const char *src, | ||
120 | rtems_media_worker worker, | ||
121 | void *worker_arg | ||
122 | ) | ||
123 | { | ||
124 | rtems_status_code sc = RTEMS_SUCCESSFUL; | ||
125 | size_t src_size = strlen(src) + 1; | ||
126 | message *msg = malloc(sizeof(*msg) + src_size); | ||
127 | |||
128 | if (msg != NULL((void*)0)) { | ||
| |||
129 | char *s = (char *) msg + sizeof(*msg); | ||
130 | |||
131 | memcpy(s, src, src_size); | ||
132 | |||
133 | msg->event = event; | ||
134 | msg->src = s; | ||
135 | msg->worker = worker; | ||
136 | msg->worker_arg = worker_arg; | ||
137 | |||
138 | sc = rtems_chain_append_with_notification( | ||
139 | &message_chain, | ||
140 | &msg->node, | ||
141 | server_id, | ||
142 | EVENT0x00002000 | ||
143 | ); | ||
144 | if (sc != RTEMS_SUCCESSFUL) { | ||
| |||
145 | sc = RTEMS_NOT_CONFIGURED; | ||
146 | } | ||
147 | } else { | ||
148 | sc = RTEMS_NO_MEMORY; | ||
149 | } | ||
150 | |||
151 | return sc; | ||
| |||
152 | } |