7 #include "ns3/mpi-interface.h"
13 #include <sys/types.h>
31 std::list<struct SharedModule *>
deps;
56 std::list<struct Module *>
deps;
66 virtual void *
Load (std::string filename,
int flag);
67 virtual void Unload (
void *module);
68 virtual void *
Lookup (
void *module, std::string symbol);
81 : cache (
"elf-cache", MpiInterface::GetSystemId ())
83 : cache (
"elf-cache", 0)
90 for (std::list<struct SharedModule *>::iterator i =
modules.begin ();
93 struct SharedModule *module = *i;
94 NS_LOG_DEBUG (
"delete shared module " << module);
95 free (module->template_buffer);
96 dlclose (module->handle);
112 for (std::list<struct Module *>::const_iterator i =
m_modules.begin (); i !=
m_modules.end (); ++i)
114 const struct Module *module = *i;
119 if (module->
module->current_buffer != 0)
122 memcpy (module->
module->current_buffer,
123 module->
module->data_buffer,
124 module->
module->buffer_size);
127 memcpy (module->
module->data_buffer,
129 module->
module->buffer_size);
143 for (std::list<struct Module *>::const_iterator i =
m_modules.begin (); i !=
m_modules.end (); ++i)
145 struct Module *module = *i;
148 clonedModule->
module->refcount++;
150 clonedModule->
buffer = malloc (module->
module->buffer_size);
151 memcpy (clonedModule->
buffer,
152 module->
module->data_buffer,
153 clonedModule->
module->buffer_size);
155 for (std::list<struct Module *>::iterator j = module->
deps.begin ();
156 j != module->
deps.end (); ++j)
161 clonedModule->
deps.push_back (cloneDep);
163 NS_LOG_DEBUG (
"add " << clonedModule->
module->id);
164 clone->
m_modules.push_back (clonedModule);
176 return module->
module->handle;
182 for (std::list<struct Module *>::iterator i =
m_modules.begin (); i !=
m_modules.end (); ++i)
184 struct Module *module = *i;
185 if (module->
module->id ==
id)
194 struct SharedModule *
198 for (std::list<struct SharedModule *>::iterator i = ns->
modules.begin ();
209 #define ROUND_DOWN(addr, align) \
210 (((unsigned long)addr) - (((unsigned long)(addr)) % (align)))
212 struct CoojaLoader::Module *
215 NS_LOG_FUNCTION (
this << filename << flag);
218 struct Module *module = 0;
223 if (sharedModule == 0)
226 RTLD_LAZY | RTLD_DEEPBIND | RTLD_LOCAL);
227 NS_ASSERT_MSG (handle != 0,
"Could not open " << cached.
cachedFilename <<
" " << dlerror ());
228 struct link_map *link_map;
229 dlinfo (handle, RTLD_DI_LINKMAP, &link_map);
231 sharedModule =
new SharedModule ();
232 NS_LOG_DEBUG (
"create shared module=" << sharedModule <<
234 " id=" << cached.
id);
235 sharedModule->refcount = 0;
236 sharedModule->id = cached.
id;
237 sharedModule->handle = handle;
239 sharedModule->template_buffer = malloc (cached.
data_p_memsz);
240 sharedModule->data_buffer = (
void *)(link_map->l_addr + cached.
data_p_vaddr);
241 memcpy (sharedModule->template_buffer,
242 sharedModule->data_buffer,
243 sharedModule->buffer_size);
244 sharedModule->current_buffer = 0;
245 for (std::vector<uint32_t>::const_iterator j = cached.
deps.begin ();
246 j != cached.
deps.end (); ++j)
250 sharedModule->deps.push_back (dep);
252 modules->
modules.push_back (sharedModule);
258 NS_LOG_DEBUG (
"Create module for " << sharedModule->handle <<
260 module->
module = sharedModule;
261 sharedModule->refcount++;
263 module->
buffer = malloc (sharedModule->buffer_size);
264 if (sharedModule->current_buffer != 0)
267 memcpy (module->
module->current_buffer,
268 module->
module->data_buffer,
269 module->
module->buffer_size);
272 memcpy (sharedModule->data_buffer,
273 sharedModule->template_buffer,
274 sharedModule->buffer_size);
276 sharedModule->current_buffer = module->
buffer;
278 for (std::vector<uint32_t>::const_iterator j = cached.
deps.begin ();
279 j != cached.
deps.end (); ++j)
283 module->
deps.push_back (dep);
285 NS_LOG_DEBUG (
"add " << module);
294 NS_LOG_FUNCTION (
this << search << search->refcount);
296 for (std::list<struct SharedModule *>::iterator i = ns->
modules.begin ();
299 struct SharedModule *module = *i;
300 if (module == search)
303 if (module->refcount == 0)
305 NS_LOG_DEBUG (
"delete shared module " << module);
306 for (std::list<struct SharedModule *>::iterator j = module->deps.begin ();
307 j != module->deps.end (); ++j)
309 struct SharedModule *dep = *j;
312 dlclose (module->handle);
313 free (module->template_buffer);
324 NS_LOG_FUNCTION (
this);
325 for (std::list<struct Module *>::const_iterator i =
m_modules.begin (); i !=
m_modules.end (); ++i)
327 struct Module *module = *i;
328 NS_LOG_DEBUG (
"Delete module " << module);
331 module->
module->current_buffer = 0;
342 NS_LOG_FUNCTION (
this << handle);
343 for (std::list<struct Module *>::iterator i =
m_modules.begin (); i !=
m_modules.end (); ++i)
345 struct Module *module = *i;
346 if (module->
module->handle == handle)
352 for (std::list<struct Module *>::iterator j = module->
deps.begin ();
353 j != module->
deps.end (); ++j)
359 NS_LOG_DEBUG (
"Delete module for " << module->
module->handle);
362 module->
module->current_buffer = 0;
375 NS_LOG_FUNCTION (
this << module << symbol);
376 void *p = dlsym (module, symbol.c_str ());
382 NS_LOG_FUNCTION (
this);
387 NS_LOG_FUNCTION (
this);
395 static TypeId tid = TypeId (
"ns3::CoojaLoaderFactory")
397 .AddConstructor<CoojaLoaderFactory> ()