sourcemm/metamod_oslink.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 29 15:01:32 2007 +0000 (2007-09-29)
branchsourcemm-1.6.0
changeset 365 afdf36dee1a6
parent 359 9b3c625f3a83
child 367 fc76472b980f
permissions -rw-r--r--
- fixed shutdown crash
- fixed makefile for linux
     1 /* ======== SourceMM ========
     2  * Copyright (C) 2004-2007 Metamod:Source Development Team
     3  * No warranties of any kind
     4  *
     5  * License: zlib/libpng
     6  *
     7  * Author(s): David "BAILOPAN" Anderson
     8  * ============================
     9  */
    10 
    11 /**
    12  * @brief Implements OS-dependant functions from oslink.h
    13  * @file oslink.cpp
    14  */
    15 
    16 #include "metamod_oslink.h"
    17 #include <malloc.h>
    18 #ifdef __linux
    19 #include <errno.h>
    20 #include <stdio.h>
    21 #endif
    22 
    23 #if defined __WIN32__ || defined _WIN32 || defined WIN32
    24 const char *dlerror()
    25 {
    26 	static char buf[1024];
    27 	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    28 		NULL, GetLastError(),
    29 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    30 		(LPTSTR) &buf, 0, NULL);
    31 	return buf;
    32 }
    33 #endif
    34 
    35 #if defined __linux__
    36 int GetLastError()
    37 {
    38 	return errno;
    39 }
    40 
    41 extern "C" void __cxa_guard_acquire(void)
    42 {
    43 }
    44 
    45 extern "C" void __cxa_guard_release(void)
    46 {
    47 }
    48 #endif
    49 
    50 bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
    51 {
    52 #if defined WIN32 || defined _WIN32
    53 	MEMORY_BASIC_INFORMATION mem;
    54 	if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
    55 		return false;
    56 	if (mem.AllocationBase == NULL)
    57 		return false;
    58 	HMODULE dll = (HMODULE)mem.AllocationBase;
    59 	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    60 #elif defined __linux__
    61 	Dl_info info;
    62 	if (!dladdr(pAddr, &info))
    63 		return false;
    64 	if (!info.dli_fbase || !info.dli_fname)
    65 		return false;
    66 	const char *dllpath = info.dli_fname;
    67 	snprintf(buffer, maxlength, "%s", dllpath);
    68 #endif
    69 	return true;
    70 }
    71 
    72 #if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
    73 void * operator new(size_t size) {
    74 	return malloc(size);
    75 }
    76 
    77 void * operator new[](size_t size) {
    78 	return malloc(size);
    79 }
    80 
    81 void operator delete(void * ptr) {
    82 	free(ptr);
    83 }
    84 
    85 void operator delete[](void * ptr) {
    86 	free(ptr);
    87 }
    88 #endif
    89