2011-03-14 03:32:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// stdafx.cpp : source file that includes just the standard includes
|
|
|
|
// GSdx.pch will be the pre-compiled header
|
|
|
|
// stdafx.obj will contain the pre-compiled type information
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
// TODO: reference any additional headers you need in STDAFX.H
|
|
|
|
// and not in this file
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
string format(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
int result = -1, length = 256;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
char* buffer = NULL;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
while(result == -1)
|
|
|
|
{
|
|
|
|
if(buffer) delete [] buffer;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
buffer = new char[length + 1];
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
memset(buffer, 0, length + 1);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
result = vsnprintf(buffer, length, fmt, args);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
length *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
string s(buffer);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
delete [] buffer;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
return s;
|
|
|
|
}
|
2011-02-18 01:56:05 +00:00
|
|
|
|
2011-02-19 10:27:10 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
void* vmalloc(size_t size, bool code)
|
|
|
|
{
|
2011-02-21 23:25:25 +00:00
|
|
|
return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, code ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
|
2011-02-19 03:36:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 10:27:10 +00:00
|
|
|
void vmfree(void* ptr, size_t size)
|
2011-02-19 03:36:30 +00:00
|
|
|
{
|
2011-02-21 23:25:25 +00:00
|
|
|
VirtualFree(ptr, 0, MEM_RELEASE);
|
2011-02-19 10:27:10 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
#else
|
2011-02-19 10:27:10 +00:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
void* vmalloc(size_t size, bool code)
|
|
|
|
{
|
2011-02-21 23:25:25 +00:00
|
|
|
size_t mask = getpagesize() - 1;
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
size = (size + mask) & ~mask;
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
int flags = PROT_READ | PROT_WRITE;
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
if(code)
|
|
|
|
{
|
|
|
|
flags |= PROT_EXEC;
|
|
|
|
}
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
return mmap(NULL, size, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2011-02-19 10:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vmfree(void* ptr, size_t size)
|
|
|
|
{
|
2011-02-21 23:25:25 +00:00
|
|
|
size_t mask = getpagesize() - 1;
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
size = (size + mask) & ~mask;
|
2011-02-19 10:27:10 +00:00
|
|
|
|
2011-02-21 23:25:25 +00:00
|
|
|
munmap(ptr, size);
|
2011-02-19 03:36:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 10:27:10 +00:00
|
|
|
#endif
|
|
|
|
|
2011-02-18 01:56:05 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(HAVE_ALIGNED_MALLOC)
|
|
|
|
|
|
|
|
// declare linux equivalents (alignment must be power of 2 (1,2,4...2^15)
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
void* _aligned_malloc(size_t size, size_t alignment)
|
2011-02-18 01:56:05 +00:00
|
|
|
{
|
2011-02-18 03:50:12 +00:00
|
|
|
ASSERT(alignment <= 0x8000);
|
2011-02-19 03:36:30 +00:00
|
|
|
size_t r = (size_t)malloc(size + --alignment + 2);
|
|
|
|
size_t o = (r + 2 + alignment) & ~(size_t)alignment;
|
|
|
|
if(!r) return NULL;
|
|
|
|
((uint16*)o)[-1] = (uint16)(o-r);
|
2011-02-18 01:56:05 +00:00
|
|
|
return (void*)o;
|
|
|
|
}
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
void _aligned_free(void* p)
|
2011-02-18 01:56:05 +00:00
|
|
|
{
|
2011-02-19 03:36:30 +00:00
|
|
|
if(!p) return;
|
|
|
|
free((void*)((size_t)p-((uint16*)p)[-1]));
|
2011-02-18 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|