core/metamod_oslink.cpp
changeset 539 db914129ae01
parent 534 eb2d7d18441c
child 705 a1001281dcb1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/metamod_oslink.cpp	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,114 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     1.9 + * All rights reserved.
    1.10 + * ======================================================
    1.11 + *
    1.12 + * This software is provided 'as-is', without any express or implied warranty.
    1.13 + * In no event will the authors be held liable for any damages arising from 
    1.14 + * the use of this software.
    1.15 + * 
    1.16 + * Permission is granted to anyone to use this software for any purpose, 
    1.17 + * including commercial applications, and to alter it and redistribute it 
    1.18 + * freely, subject to the following restrictions:
    1.19 + *
    1.20 + * 1. The origin of this software must not be misrepresented; you must not 
    1.21 + * claim that you wrote the original software. If you use this software in a 
    1.22 + * product, an acknowledgment in the product documentation would be 
    1.23 + * appreciated but is not required.
    1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
    1.25 + * misrepresented as being the original software.
    1.26 + * 3. This notice may not be removed or altered from any source distribution.
    1.27 + *
    1.28 + * Version: $Id$
    1.29 + */
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include "metamod_oslink.h"
    1.33 +#include <malloc.h>
    1.34 +#include <stdio.h>
    1.35 +#ifdef __linux
    1.36 +#include <errno.h>
    1.37 +#endif
    1.38 +
    1.39 +#if defined __WIN32__ || defined _WIN32 || defined WIN32
    1.40 +const char *dlerror()
    1.41 +{
    1.42 +	static char buf[1024];
    1.43 +	DWORD num;
    1.44 +
    1.45 +	num = GetLastError();
    1.46 +	
    1.47 +	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    1.48 +			NULL, 
    1.49 +			num,
    1.50 +			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    1.51 +			buf, 
    1.52 +			sizeof(buf), 
    1.53 +			NULL)
    1.54 +		== 0)
    1.55 +	{
    1.56 +		_snprintf(buf, sizeof(buf), "unknown error %x", num);
    1.57 +	}
    1.58 +
    1.59 +	return buf;
    1.60 +}
    1.61 +#endif
    1.62 +
    1.63 +#if defined __linux__
    1.64 +int GetLastError()
    1.65 +{
    1.66 +	return errno;
    1.67 +}
    1.68 +
    1.69 +extern "C" void __cxa_guard_acquire(void)
    1.70 +{
    1.71 +}
    1.72 +
    1.73 +extern "C" void __cxa_guard_release(void)
    1.74 +{
    1.75 +}
    1.76 +#endif
    1.77 +
    1.78 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    1.79 +{
    1.80 +#if defined WIN32 || defined _WIN32
    1.81 +	MEMORY_BASIC_INFORMATION mem;
    1.82 +	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    1.83 +		return false;
    1.84 +	if (mem.AllocationBase == NULL)
    1.85 +		return false;
    1.86 +	HMODULE dll = (HMODULE)mem.AllocationBase;
    1.87 +	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    1.88 +#elif defined __linux__
    1.89 +	Dl_info info;
    1.90 +	if (!dladdr(pAddr, &info))
    1.91 +		return false;
    1.92 +	if (!info.dli_fbase || !info.dli_fname)
    1.93 +		return false;
    1.94 +	const char *dllpath = info.dli_fname;
    1.95 +	snprintf(buffer, maxlength, "%s", dllpath);
    1.96 +#endif
    1.97 +	return true;
    1.98 +}
    1.99 +
   1.100 +#if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
   1.101 +void * operator new(size_t size) {
   1.102 +	return malloc(size);
   1.103 +}
   1.104 +
   1.105 +void * operator new[](size_t size) {
   1.106 +	return malloc(size);
   1.107 +}
   1.108 +
   1.109 +void operator delete(void * ptr) {
   1.110 +	free(ptr);
   1.111 +}
   1.112 +
   1.113 +void operator delete[](void * ptr) {
   1.114 +	free(ptr);
   1.115 +}
   1.116 +#endif
   1.117 +