sourcemm/metamod_oslink.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jan 10 19:05:59 2008 +0000 (2008-01-10)
changeset 489 77c6b0da24ec
parent 371 751179604513
child 523 6f1630519b81
permissions -rw-r--r--
fixed FormatMessage() misuse and improver error handling
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2007 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 "metamod_oslink.h"
    29 #include <malloc.h>
    30 #include <stdio.h>
    31 #ifdef __linux
    32 #include <errno.h>
    33 #endif
    34 
    35 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    36 const char *dlerror()
    37 {
    38 	static char buf[1024];
    39 	DWORD num;
    40 
    41 	num = GetLastError();
    42 	
    43 	if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    44 			NULL, 
    45 			num,
    46 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    47 			buf, 
    48 			sizeof(buf), 
    49 			NULL)
    50 		== 0)
    51 	{
    52 		_snprintf(buf, sizeof(buf), "unknown error %x", num);
    53 	}
    54 
    55 	return buf;
    56 }
    57 #endif
    58 
    59 #if defined __linux__
    60 int GetLastError()
    61 {
    62 	return errno;
    63 }
    64 
    65 extern "C" void __cxa_guard_acquire(void)
    66 {
    67 }
    68 
    69 extern "C" void __cxa_guard_release(void)
    70 {
    71 }
    72 #endif
    73 
    74 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    75 {
    76 #if defined WIN32 || defined _WIN32
    77 	MEMORY_BASIC_INFORMATION mem;
    78 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    79 		return false;
    80 	if (mem.AllocationBase == NULL)
    81 		return false;
    82 	HMODULE dll = (HMODULE)mem.AllocationBase;
    83 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    84 #elif defined __linux__
    85 	Dl_info info;
    86 	if (!dladdr(pAddr, &info))
    87 		return false;
    88 	if (!info.dli_fbase || !info.dli_fname)
    89 		return false;
    90 	const char *dllpath = info.dli_fname;
    91 	snprintf(buffer, maxlength, "%s", dllpath);
    92 #endif
    93 	return true;
    94 }
    95 
    96 #if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
    97 void * operator new(size_t size) {
    98 	return malloc(size);
    99 }
   100 
   101 void * operator new[](size_t size) {
   102 	return malloc(size);
   103 }
   104 
   105 void operator delete(void * ptr) {
   106 	free(ptr);
   107 }
   108 
   109 void operator delete[](void * ptr) {
   110 	free(ptr);
   111 }
   112 #endif
   113