First set of licensing changes
[mono.git] / mono / utils / mono-embed.c
1 /*
2  * mono-embed.c: Example code APIs to register a libraries using
3  * mono_dl_fallback_register.  Real implementations should instead
4  * use a binary search for implementing the dl_mapping_open and
5  * dl_mapping_symbol methods here.
6  *
7  * Author:
8  *    Mono Team (http://www.mono-project.com)
9  *
10  * Copyright 2001-2004 Ximian, Inc.
11  * Copyright 2004-2010 Novell, Inc.
12  *
13  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14  */
15 #include "config.h"
16 #include "mono/utils/mono-dl.h"
17 #include "mono/utils/mono-embed.h"
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <glib.h>
24
25 static GHashTable *mono_dls;
26
27 static void *
28 dl_mapping_open (const char *file, int flags, char **err, void *user_data)
29 {
30         MonoDlMapping *mappings;
31         
32         if (mono_dls == NULL){
33                 *err = g_strdup ("Library not registered");
34                 return NULL;
35         }
36                 
37         mappings = (MonoDlMapping *) g_hash_table_lookup (mono_dls, file);
38         *err = g_strdup (mappings == NULL ? "File not registered" : "");
39         return mappings;
40 }
41
42 static void *
43 dl_mapping_symbol (void *handle, const char *symbol, char **err, void *user_data)
44 {
45         MonoDlMapping *mappings = (MonoDlMapping *) handle;
46         
47         for (;mappings->name; mappings++){
48                 if (strcmp (symbol, mappings->name) == 0){
49                         *err = g_strdup ("");
50                         return mappings->addr;
51                 }
52         }
53         *err = g_strdup ("Symbol not found");
54         return NULL;
55 }
56
57 /**
58  * mono_dl_register_library:
59 u * @name: Library name, this is the name used by the DllImport as the external library name
60  * @mappings: the mappings to register for P/Invoke.
61  *
62  * The mappings registered using this function are used as fallbacks if the dynamic linker 
63  * fails, or if the platform doesn't have a dynamic linker.
64  *
65  * Mappings is a pointer to the first element of an array of
66  * MonoDlMapping values.  The list must be terminated with both 
67  * the name and addr fields set to NULL.
68  *
69  * This is typically used like this:
70  * MonoDlMapping sample_library_mappings [] = {
71  *   { "CallMe", CallMe },
72  *   { NULL, NULL }
73  * };
74  *
75  * ...
76  * main ()
77  * {
78  *    ...
79  *    mono_dl_register_library ("sample", sample_library_mappings);
80  *    ...
81  * }
82  *
83  * Then the C# code can use this P/Invoke signature:
84  *
85  *      [DllImport ("sample")]
86  *      extern static int CallMe (int f);
87  */
88 void
89 mono_dl_register_library (const char *name, MonoDlMapping *mappings)
90 {
91         if (mono_dls == NULL){
92                 mono_dls = g_hash_table_new (g_str_hash, g_str_equal);
93                 mono_dl_fallback_register (dl_mapping_open, dl_mapping_symbol, NULL, NULL);
94         }
95         
96         g_hash_table_insert (mono_dls, g_strdup (name), mappings);
97 }
98