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