core/metamod_oslink.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat May 26 15:43:00 2012 -0400 (2012-05-26)
changeset 778 faee01f77160
parent 705 a1001281dcb1
permissions -rw-r--r--
Remove std string include from core and core-legacy headers (bug 5304, r=DS).
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #include <stdio.h>
    29 #include "metamod_oslink.h"
    30 #include <stdlib.h>
    31 #include <stdio.h>
    32 #if defined __linux__ || defined __APPLE__
    33 #include <errno.h>
    34 #endif
    35 
    36 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    37 const char *dlerror()
    38 {
    39 	static char buf[1024];
    40 	DWORD num;
    41 
    42 	num = GetLastError();
    43 	
    44 	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    45 			NULL, 
    46 			num,
    47 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    48 			buf, 
    49 			sizeof(buf), 
    50 			NULL)
    51 		== 0)
    52 	{
    53 		_snprintf(buf, sizeof(buf), "unknown error %x", num);
    54 	}
    55 
    56 	return buf;
    57 }
    58 #endif
    59 
    60 #if defined __linux_ || defined __APPLE___
    61 int GetLastError()
    62 {
    63 	return errno;
    64 }
    65 
    66 extern "C" void __cxa_guard_acquire(void)
    67 {
    68 }
    69 
    70 extern "C" void __cxa_guard_release(void)
    71 {
    72 }
    73 #endif
    74 
    75 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    76 {
    77 #if defined WIN32 || defined _WIN32
    78 	MEMORY_BASIC_INFORMATION mem;
    79 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    80 		return false;
    81 	if (mem.AllocationBase == NULL)
    82 		return false;
    83 	HMODULE dll = (HMODULE)mem.AllocationBase;
    84 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    85 #elif defined __linux__ || defined __APPLE__
    86 	Dl_info info;
    87 	if (!dladdr(pAddr, &info))
    88 		return false;
    89 	if (!info.dli_fbase || !info.dli_fname)
    90 		return false;
    91 	const char *dllpath = info.dli_fname;
    92 	snprintf(buffer, maxlength, "%s", dllpath);
    93 #endif
    94 	return true;
    95 }
    96 
    97 #if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
    98 void * operator new(size_t size) {
    99 	return malloc(size);
   100 }
   101 
   102 void * operator new[](size_t size) {
   103 	return malloc(size);
   104 }
   105 
   106 void operator delete(void * ptr) {
   107 	free(ptr);
   108 }
   109 
   110 void operator delete[](void * ptr) {
   111 	free(ptr);
   112 }
   113 #endif
   114