/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- add_descriptor
- remove_descriptor
- get_descriptor
- count_descriptors
- remove_timer_descriptor
- remove_inotify_watch_descriptor
1 /****************************************************************************
2 * Copyright (C) 2012 by Onyeka Igabari *
3 * *
4 * This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src). *
5 * *
6 * DMTCP:dmtcp/src is free software: you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public License as *
8 * published by the Free Software Foundation, either version 3 of the *
9 * License, or (at your option) any later version. *
10 * *
11 * DMTCP:dmtcp/src is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with DMTCP:dmtcp/src. If not, see *
18 * <http://www.gnu.org/licenses/>. *
19 ****************************************************************************/
20
21
22 /******************************************************************
23 * File: util_descriptor.cpp
24 *
25 * Author: onyeka Igabari
26 *
27 * Description: Implements a class for handling memory allocation
28 * for system calls with different descriptors
29 *
30 * Created on July 05, 2012, 11:52 PM
31 ******************************************************************/
32
33 #include <iostream>
34 #include <stdio.h>
35 #include <sys/mman.h>
36 #include <stdbool.h>
37 #include "util.h"
38 #include "util_descriptor.h"
39 #include "../jalib/jalloc.h"
40 #include "../jalib/jassert.h"
41
42 using namespace dmtcp;
43
44 /******************************************************************
45 * Class Variables: is_initialized, descriptor_counter,
46 * descript_types_p
47 *
48 * Description: Initialize variables belonging to the Descriptor class
49 *
50 * Parameters: NONE
51 *
52 * Return: NONE
53 ******************************************************************/
54 bool Util::Descriptor::is_initialized = false;
55 unsigned int Util::Descriptor::descriptor_counter = 0;
56 descriptor_types_u* Util::Descriptor::descrip_types_p[MAX_DESCRIPTORS] = {0};
57
58 /******************************************************************
59 * Class Function: Descriptor
60 *
61 * Description: Constructor of the Descriptor class
62 *
63 * Parameters: NONE
64 *
65 * Return: NONE
66 ******************************************************************/
67 Util::Descriptor::Descriptor()
68 {
69 if(false == is_initialized)
70 {
71 descriptor_counter = 0;
72 is_initialized = true;
73
74 JTRACE(" initializing the Descriptor class object");
75 // allocate memory for MAX_DESCRIPTORS that would be stored
76 for(int i = 0; i < MAX_DESCRIPTORS; i++)
77 {
78 void* mem_ptr = jalib::JAllocDispatcher::allocate( sizeof( descriptor_types_u) );
79 if(MAP_FAILED == mem_ptr)
80 {
81 JTRACE("memory allocation failed!");
82 break;
83 }
84 else
85 {
86 descrip_types_p[i] = (descriptor_types_u *)mem_ptr;
87 }
88 }
89 }
90 else
91 {
92 JTRACE(" object has been initialized before");
93 }
94 }
95
96 /******************************************************************
97 * Class Function: ~Descriptor
98 *
99 * Description: Destructor of the Descriptor class
100 *
101 * Parameters: NONE
102 *
103 * Return: NONE
104 ******************************************************************/
105 Util::Descriptor::~Descriptor()
106 {
107 if(true == is_initialized)
108 {
109 JTRACE("Destructor being called");
110 }
111 }
112
113 /******************************************************************
114 * Class Function: add_descriptor
115 *
116 * Description: Adds descriptors
117 *
118 * Parameters: desctriptor
119 *
120 * Return: NONE
121 ******************************************************************/
122 void Util::Descriptor::add_descriptor(descriptor_types_u* descriptor)
123 {
124 JASSERT(descriptor != NULL);
125 if(descriptor_counter < MAX_DESCRIPTORS)
126 {
127 JTRACE ( "Adding new descriptor" ) (descriptor_counter) (descrip_types_p[descriptor_counter]);
128 memcpy(descrip_types_p[descriptor_counter],
129 descriptor, sizeof(descriptor_types_u));
130 descriptor_counter++;
131 }
132 else
133 {
134 JTRACE ( "No more memory to store additional descriptors" ) ;
135 }
136 }
137
138 /******************************************************************
139 * Class Function: remove_descriptor
140 *
141 * Description: Removes descriptors
142 *
143 * Parameters: type - descriptor type
144 * descriptor - descriptor to be removed
145 *
146 * Return: NONE
147 ******************************************************************/
148 int Util::Descriptor::remove_descriptor(descriptor_type_e type,
149 void* descriptor)
150 {
151 int ret_val = FAILURE;
152 JASSERT(descriptor != NULL).Text ( "descriptor is NULL" );
153
154 // determine which descriptor needs to be removed
155 switch(type)
156 {
157 case TIMER_CREATE_DECRIPTOR:
158 {
159 timer_t timer_id;
160 memcpy(&timer_id, descriptor, sizeof(timer_t) );
161
162 //calling timer function
163 ret_val = remove_timer_descriptor(timer_id);
164 break;
165 }
166 case INOTIFY_ADD_WATCH_DESCRIPTOR:
167 {
168 int watch_descriptor;
169 memcpy(&watch_descriptor, descriptor, sizeof(int) );
170
171 //calling timer function
172 ret_val = remove_inotify_watch_descriptor(watch_descriptor);
173 break;
174 }
175 default:
176 {
177 JTRACE("Unknown descriptor type") (type);
178 break;
179 }
180 }
181
182 return ret_val;
183 }
184
185 /******************************************************************
186 * Class Function: get_descriptor
187 *
188 * Description: Returns the descriptor object if it is of the
189 * type specified
190 *
191 * Parameters: index - index to descriptor objects
192 * type - descriptor type
193 * descriptor - descriptor structure returned
194 *
195 * Return: true or false
196 ******************************************************************/
197 bool Util::Descriptor::get_descriptor(unsigned int index,
198 descriptor_type_e type,
199 descriptor_types_u* descriptor)
200 {
201 bool ret_val = false;
202 JASSERT(descriptor != NULL).Text ( "descriptor is NULL" );
203 JTRACE("get descriptor") (index) (type);
204 if((descrip_types_p[index])->add_watch.type == type)
205 {
206 memcpy(descriptor, descrip_types_p[index], sizeof(descriptor_types_u));
207 ret_val = true;
208 }
209 else
210 {
211 JTRACE("descriptor type is different from type saved") (type) ((descrip_types_p[index])->add_watch.type);
212 }
213
214 return ret_val;
215 }
216
217 /******************************************************************
218 * Class Function: count_descriptors
219 *
220 * Description: Returns the number of descriptor
221 * objects stored in dmtcp
222 *
223 * Parameters: None
224 *
225 * Return: the number stored
226 ******************************************************************/
227 unsigned int Util::Descriptor::count_descriptors()
228 {
229 unsigned int count = descriptor_counter;
230
231 return count;
232 }
233
234 /******************************************************************
235 * Class Function: remove_timer_descriptor
236 *
237 * Description: Removes timer descriptors
238 *
239 * Parameters: timer_id - timer descriptor
240 *
241 * Return: NONE
242 ******************************************************************/
243 int Util::Descriptor::remove_timer_descriptor(timer_t timer_id)
244 {
245 int i;
246 int ret_val = FAILURE;
247
248 for(i = 0; i < MAX_DESCRIPTORS; i++)
249 {
250 if((descrip_types_p[i])->create_timer.type == TIMER_CREATE_DECRIPTOR)
251 {
252 JTRACE("find the saved timerid that corresponds to the one passed in");
253 if((descrip_types_p[i])->create_timer.timerid == timer_id)
254 {
255 JTRACE("timer id found...now delete it!");
256 memset(descrip_types_p[i], 0, sizeof(descriptor_types_u) );
257 (descrip_types_p[i])->create_timer.type = UNUSED_DESCRIPTOR;
258
259 //set the return value
260 ret_val = SUCCESS;
261 break;
262 }
263 }
264 }
265
266 return ret_val;
267 }
268
269 /******************************************************************
270 * Class Function: remove_inotify_watch_descriptor
271 *
272 * Description: Removes inotify watch descriptors
273 *
274 * Parameters: watch_descriptor - watch descriptor
275 *
276 * Return: NONE
277 ******************************************************************/
278 int Util::Descriptor::remove_inotify_watch_descriptor(int watch_descriptor)
279 {
280 int i;
281 int ret_val = FAILURE;
282
283 for(i = 0; i < MAX_DESCRIPTORS; i++)
284 {
285 if((descrip_types_p[i])->add_watch.type == INOTIFY_ADD_WATCH_DESCRIPTOR)
286 {
287 JTRACE("find the saved watch descriptor that corresponds to the one passed in");
288
289 if((descrip_types_p[i])->add_watch.watch_descriptor == watch_descriptor)
290 {
291 JTRACE("watch descriptor found...now delete it!") (watch_descriptor);
292 memset((descrip_types_p[i]), 0, sizeof(descriptor_types_u) );
293 (descrip_types_p[i])->add_watch.type = UNUSED_DESCRIPTOR;
294
295 //set the return value
296 ret_val = SUCCESS;
297 break;
298 }
299 }
300 }
301
302 return ret_val;
303 }