view module.c @ 27:02b0ee204042

Added ARRAY_FOREACH_R. Same functionality as ARRAY_FOREACH but reversal.
author David Demelier <markand@malikania.fr>
date Wed, 21 Sep 2011 12:03:23 +0200
parents 726b181b8956
children 23a3ebcbf08e
line wrap: on
line source

/*
 * module.c -- portable functions to manipulate dynamic libraries
 *
 * Copyright (c) 2011, David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "module.h"

int
module_load(struct module *mod, const char *path, int flags)
{
	/* Flags are not supported on Windows */
#if defined(_WIN32) || defined(__WIN32__)
	(void) flags;
	mod->handler = LoadLibrary(path);
#else
	mod->handler = dlopen(path, flags);
#endif

	if (!(mod->handler))
		return -1;

	return 0;
}

int
module_find(struct module *mod, const char *sym)
{
#if defined(_WIN32) || defined(__WIN32__)
	mod->sym = GetProcAddress(mod->handler, sym);
#else
	mod->sym = dlsym(mod->handler, sym);
#endif

	if (!(mod->sym))
		return -1;

	return 0;
}

void
module_free(struct module *mod)
{
#if defined(_WIN32) || defined(__WIN32__)
	FreeLibrary(mod->handler);
#else
	dlclose(mod->handler);
#endif
}