Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/modules/ModuleSystem/linker.c

Source at commit 1282 created 12 years 8 months ago.
By meklort, Enable arch specfic code in modules
1/*
2 * Copyright 2010 Evan Lojewski. All rights reserved.
3 *
4 */
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include "modules.h"
9
10//extern void start_built_in_modules();
11
12void* symbols_module_start = (void*)0xFFFFFFFF;// Global, value is populated by the makefile with actual address
13
14/** Internal symbols, however there are accessor methods **/
15moduleHook_t* moduleCallbacks = NULL;
16moduleList_t* loadedModules = NULL;
17
18int init_module_system()
19{
20 // Start any modules that were compiled in first.
21extern void start_built_in_modules();
22 start_built_in_modules();
23
24return 1;
25}
26
27void start_built_in_module(const char* name,
28 const char* author,
29 const char* description,
30 UInt32 version,
31 UInt32 compat,
32 void(*start_function)(void))
33{
34 start_function();
35 // Notify the module system that this module really exists, specificaly, let other module link with it
36 module_loaded(name, author, description, version, compat);
37}
38
39
40
41/*
42 * print out the information about the loaded module
43 */
44void module_loaded(const char* name, const char* author, const char* description, UInt32 version, UInt32 compat)
45{
46moduleList_t* new_entry = (moduleList_t*)malloc(sizeof(moduleList_t));
47new_entry->next = loadedModules;
48
49loadedModules = new_entry;
50
51 if(!name) name = "Unknown";
52 if(!author) author = "Unknown";
53 if(!description) description = "";
54
55new_entry->name = name;
56 new_entry->author = author;
57 new_entry->description = description;
58new_entry->version = version;
59 new_entry->compat = compat;
60}
61
62
63/********************************************************************************/
64/*Module Hook Interface*/
65/********************************************************************************/
66
67
68/*
69 *execute_hook( const char* name )
70 *name - Name of the module hook
71 *If any callbacks have been registered for this hook
72 *they will be executed now in the same order that the
73 *hooks were added.
74*/
75int execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4)
76{
77moduleHook_t* hook = (moduleHook_t*)hook_exists(name);
78
79if(hook)
80{
81// Loop through all callbacks for this module
82callbackList_t* callbacks = hook->callbacks;
83
84while(callbacks)
85{
86// Execute callback
87callbacks->callback(arg1, arg2, arg3, arg4);
88callbacks = callbacks->next;
89}
90return 1;
91}
92else
93{
94// Callback for this hook doesn't exist;
95return 0;
96}
97}
98
99
100
101/*
102 *register_hook_callback( const char* name, void(*callback)())
103 *name - Name of the module hook to attach to.
104 *callbacks - The funciton pointer that will be called when the
105 *hook is executed. When registering a new callback name, the callback is added sorted.
106 *NOTE: the hooks take four void* arguments.
107 */
108void register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*))
109{
110moduleHook_t* hook = hook_exists(name);
111
112if(hook)
113{
114// append
115callbackList_t* newCallback = (callbackList_t*)malloc(sizeof(callbackList_t));
116newCallback->next = hook->callbacks;
117hook->callbacks = newCallback;
118newCallback->callback = callback;
119}
120else
121{
122// create new hook
123moduleHook_t* newHook = (moduleHook_t*)malloc(sizeof(moduleHook_t));
124newHook->name = name;
125newHook->callbacks = (callbackList_t*)malloc(sizeof(callbackList_t));
126newHook->callbacks->callback = callback;
127newHook->callbacks->next = NULL;
128
129newHook->next = moduleCallbacks;
130moduleCallbacks = newHook;
131
132}
133}
134
135
136moduleHook_t* hook_exists(const char* name)
137{
138moduleHook_t* hooks = moduleCallbacks;
139
140// look for a hook. If it exists, return the moduleHook_t*,
141// If not, return NULL.
142while(hooks)
143{
144if(strcmp(name, hooks->name) == 0)
145{
146return hooks;
147}
148hooks = hooks->next;
149}
150return NULL;
151
152}
153
154/********************************************************************************/
155/*dyld / Linker Interface*/
156/********************************************************************************/
157void dyld_stub_binder()
158{
159printf("ERROR: dyld_stub_binder was called, should have been take care of by the linker.\n");
160getchar();
161}

Archive Download this file

Revision: 1282