summaryrefslogtreecommitdiff
path: root/include/wolf/library/loader.h
blob: 437fa1bba6370acb3b8c843e554bdf82a7ec6488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
    Copyright (C) 2010 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef WOLF_LOADER_H
#define WOLF_LOADER_H

/**
 * @addtogroup wolf_library Loadable library support
 * @{
 */

/**
 * @file loader.h
 * @brief Portable library loading functions
 * @author Andreas Baumann <abaumann@yahoo.com>
 */

#ifdef __cplusplus
extern "C" {
#endif

#include "port/sys.h"

#include "errors.h"
#include <stddef.h>         /* for size_t */

#if defined _WIN32
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#endif

/* @brief handle representing a shared library
 */
typedef struct wolf_library_t *wolf_library_p;

/* @brief returned by wolf_library_get_func, cast it to the custom function pointer */
#ifdef _WIN32
#define WOLF_LIBRARY_FUNCPTR FARPROC
#else
#define WOLF_LIBRARY_FUNCPTR void *
#endif

/* @brief casts a library symbol to a function pointer of a given type.
 *
 * Reason: conversion from void * to function pointer is illegal by ISO-99
 *         (though it works actually on Posix)
 *
 * for the reasons why naive casting doesn't work with dlsym, see:
 *
 * http://en.wikipedia.org/wiki/Dynamic_loading
 */
#if defined _WIN32
#define WOLF_LIBRARY_FUNC_CAST( symbol__, func_t__, func__ ) func__ = (func_t__)symbol__
#else
#define WOLF_LIBRARY_FUNC_CAST( symbol__, func_t__, func__ ) \
	{ \
		union { func_t__ f__; void *s__; } alias__; \
		alias__.s__ = symbol__; \
		func__ = alias__.f__; \
	}
#endif

/**
 * Loads a shared library.
 *
 * @param error     indicates errors while loading the library
 *
 * @return          the libary object
 */
wolf_library_p wolf_library_load( const char *name, wolf_error_t *error );

/**
 * Free up resources used by the library object.
 *
 * @param library   the library object to free
 *
 * @return          WOLF_OK if library has been unloaded,
 *                  WOLF_ERR_INTERNAL_STATE if the library handle is in an illegal state
 *                  (like double freeing the same library)
 */
wolf_error_t wolf_library_unload( wolf_library_p library );

/**
 * Converts the last error and state of the library object in a human readable message.
 *
 * @param error     the error which happened after calling a library loader function
 * @param library   the library object for which we want a error message
 * @param buf       the buffer which will hold the error message
 * @param buflen    the size of the buffer
 *
 * @return          a pointer to buf for convenience
 */
char *wolf_library_errmsg( const wolf_error_t error, const wolf_library_p library, char *buf, size_t buflen );

/**
 * Gets a symbol (a function) from the library object. It has to be
 * casted properly to the function you are expecting to call.
 *
 * @param library   the library object
 * @param name      name of the symbol to retrieve
 * @param error     WOLF_OK if the symbol could be loaded
 *
 * @return          a pointer to the retrieved symbol
 */
WOLF_LIBRARY_FUNCPTR wolf_library_get_func( const wolf_library_p library, const char *name, wolf_error_t *error );

/** @} */ /* @addtogroup wolf_library */

#endif /* ifndef WOLF_LOADER_H */