1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/metamod_oslink.cpp Fri Sep 28 21:31:02 2007 +0000
1.3 @@ -0,0 +1,89 @@
1.4 +/* ======== SourceMM ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * ============================
1.12 + */
1.13 +
1.14 +/**
1.15 + * @brief Implements OS-dependant functions from oslink.h
1.16 + * @file oslink.cpp
1.17 + */
1.18 +
1.19 +#include "metamod_oslink.h"
1.20 +#include <malloc.h>
1.21 +#ifdef __linux
1.22 +#include <errno.h>
1.23 +#include <stdio.h>
1.24 +#endif
1.25 +
1.26 +#if defined __WIN32__ || defined _WIN32 || defined WIN32
1.27 +const char *dlerror()
1.28 +{
1.29 + static char buf[1024];
1.30 + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1.31 + NULL, GetLastError(),
1.32 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1.33 + (LPTSTR) &buf, 0, NULL);
1.34 + return buf;
1.35 +}
1.36 +#endif
1.37 +
1.38 +#if defined __linux__
1.39 +int GetLastError()
1.40 +{
1.41 + return errno;
1.42 +}
1.43 +
1.44 +extern "C" void __cxa_guard_acquire(void)
1.45 +{
1.46 +}
1.47 +
1.48 +extern "C" void __cxa_guard_release(void)
1.49 +{
1.50 +}
1.51 +#endif
1.52 +
1.53 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
1.54 +{
1.55 +#if defined WIN32 || defined _WIN32
1.56 + MEMORY_BASIC_INFORMATION mem;
1.57 + if (!VirtualQuery(pAddr, &mem, sizeof(mem)))
1.58 + return false;
1.59 + if (mem.AllocationBase == NULL)
1.60 + return false;
1.61 + HMODULE dll = (HMODULE)mem.AllocationBase;
1.62 + GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
1.63 +#elif defined __linux__
1.64 + Dl_info info;
1.65 + if (!dladdr(pAddr, &info))
1.66 + return false;
1.67 + if (!info.dli_fbase || !info.dli_fname)
1.68 + return false;
1.69 + const char *dllpath = info.dli_fname;
1.70 + snprintf(buffer, maxlength, "%s", dllpath);
1.71 +#endif
1.72 + return true;
1.73 +}
1.74 +
1.75 +#if defined __GNUC__ && (__GNUC__ == 3 || __GNUC__ == 4)
1.76 +void * operator new(size_t size) {
1.77 + return malloc(size);
1.78 +}
1.79 +
1.80 +void * operator new[](size_t size) {
1.81 + return malloc(size);
1.82 +}
1.83 +
1.84 +void operator delete(void * ptr) {
1.85 + free(ptr);
1.86 +}
1.87 +
1.88 +void operator delete[](void * ptr) {
1.89 + free(ptr);
1.90 +}
1.91 +#endif
1.92 +