sourcemm/metamod_oslink.cpp
author David Anderson <dvander@alliedmods.net>
Sat Oct 06 16:47:58 2007 +0000 (2007-10-06)
changeset 376 f2d55f90f697
parent 371 751179604513
child 489 77c6b0da24ec
permissions -rw-r--r--
added format/formatargs api
     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 #ifdef __linux
    31 #include <errno.h>
    32 #include <stdio.h>
    33 #endif
    34 
    35 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    36 const char *dlerror()
    37 {
    38 	static char buf[1024];
    39 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    40 		NULL, GetLastError(),
    41 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    42 		(LPTSTR) &buf, 0, NULL);
    43 	return buf;
    44 }
    45 #endif
    46 
    47 #if defined __linux__
    48 int GetLastError()
    49 {
    50 	return errno;
    51 }
    52 
    53 extern "C" void __cxa_guard_acquire(void)
    54 {
    55 }
    56 
    57 extern "C" void __cxa_guard_release(void)
    58 {
    59 }
    60 #endif
    61 
    62 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    63 {
    64 #if defined WIN32 || defined _WIN32
    65 	MEMORY_BASIC_INFORMATION mem;
    66 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    67 		return false;
    68 	if (mem.AllocationBase == NULL)
    69 		return false;
    70 	HMODULE dll = (HMODULE)mem.AllocationBase;
    71 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    72 #elif defined __linux__
    73 	Dl_info info;
    74 	if (!dladdr(pAddr, &info))
    75 		return false;
    76 	if (!info.dli_fbase || !info.dli_fname)
    77 		return false;
    78 	const char *dllpath = info.dli_fname;
    79 	snprintf(buffer, maxlength, "%s", dllpath);
    80 #endif
    81 	return true;
    82 }
    83 
    84 #if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
    85 void * operator new(size_t size) {
    86 	return malloc(size);
    87 }
    88 
    89 void * operator new[](size_t size) {
    90 	return malloc(size);
    91 }
    92 
    93 void operator delete(void * ptr) {
    94 	free(ptr);
    95 }
    96 
    97 void operator delete[](void * ptr) {
    98 	free(ptr);
    99 }
   100 #endif
   101