1
0
forked from 0ad/0ad

# fix warnings and 64-bit bugs in headerless.cpp (workspace update required)

(hopefully fixes often-reported crashes on 64-bit Linux)
remove lib/precompiled.cpp to simplify PCH handling in build system

This was SVN commit r7006.
This commit is contained in:
janwas 2009-07-16 23:53:46 +00:00
parent 6146db7a3d
commit 2a0025bc28
13 changed files with 100 additions and 63 deletions

View File

@ -97,7 +97,7 @@ class DynHashTbl
throw std::bad_alloc();
}
max_entries += max_entries;
max_entries *= 2;
// must be set before get_slot
// newly initialized, nothing to copy - done

View File

@ -27,7 +27,7 @@
#include "lib/bits.h"
static const size_t minAlignment = 16;
static const size_t minAlignment = 32;
static const bool performSanityChecks = true;
// shared by the Impl::Allocate and FreedBlock::Validate
@ -51,7 +51,7 @@ public:
{
}
FreedBlock(u32 id, size_t size)
FreedBlock(uintptr_t id, size_t size)
: m_magic(s_magic), m_size(size), m_id(id)
{
}
@ -61,7 +61,7 @@ public:
// clear all fields to prevent accidental reuse
prev = next = 0;
m_id = 0;
m_size = ~(size_t)0u;
m_size = ~size_t(0);
m_magic = 0;
}
@ -74,7 +74,7 @@ public:
* @return whether this appears to be a FreedBlock instance with the
* desired ID. for additional safety, also call Validate().
**/
bool IsFreedBlock(u32 id) const
bool IsFreedBlock(uintptr_t id) const
{
if(m_id != id)
return false;
@ -86,7 +86,7 @@ public:
/**
* warn if any invariant doesn't hold.
**/
void Validate(u32 id) const
void Validate(uintptr_t id) const
{
if(!performSanityChecks) return;
@ -103,8 +103,8 @@ public:
private:
// note: the magic and ID fields are stored at both ends of this
// class to increase the chance of detecting memory corruption.
static const uintptr_t s_magic = 0xFF55AA00;
uintptr_t m_magic;
static const u64 s_magic = 0xFF55AA00BBCCDDEEull;
u64 m_magic;
FreedBlock* prev;
FreedBlock* next;
@ -113,7 +113,7 @@ private:
size_t m_size;
// this differentiates between headers and footers.
u32 m_id;
uintptr_t m_id;
};
@ -211,7 +211,7 @@ public:
m_freeBytes -= freedBlock->Size();
}
void Validate(u32 id) const
void Validate(uintptr_t id) const
{
if(!performSanityChecks) return;
@ -284,7 +284,7 @@ public:
const size_t sizeClass = SizeClass(freedBlock->Size());
m_rangeLists[sizeClass].Insert<AddressOrder>(freedBlock);
m_bitmap |= Bit<u32>(sizeClass);
m_bitmap |= Bit<uintptr_t>(sizeClass);
}
/**
@ -295,7 +295,7 @@ public:
// iterate over all large enough, non-empty size classes
// (zero overhead for empty size classes)
const size_t minSizeClass = SizeClass(minSize);
size_t sizeClassBits = m_bitmap & (~0u << minSizeClass);
size_t sizeClassBits = m_bitmap & ((~size_t(0)) << minSizeClass);
while(sizeClassBits)
{
const size_t size = ValueOfLeastSignificantOneBit(sizeClassBits);
@ -309,7 +309,7 @@ public:
// apparently all classes above minSizeClass are empty,
// or the above would have succeeded.
debug_assert(m_bitmap < Bit<u32>(minSizeClass+1));
debug_assert(m_bitmap < Bit<uintptr_t>(minSizeClass+1));
return 0;
}
@ -320,17 +320,17 @@ public:
// (masking with !IsEmpty() << sizeClass would probably be faster)
if(m_rangeLists[sizeClass].IsEmpty())
m_bitmap &= ~Bit<u32>(sizeClass);
m_bitmap &= ~Bit<uintptr_t>(sizeClass);
}
void Validate(u32 id) const
void Validate(uintptr_t id) const
{
for(size_t i = 0; i < numRangeLists; i++)
{
m_rangeLists[i].Validate(id);
// both bitmap and list must agree on whether they are empty
debug_assert(((m_bitmap & Bit<u32>(i)) == 0) == m_rangeLists[i].IsEmpty());
debug_assert(((m_bitmap & Bit<uintptr_t>(i)) == 0) == m_rangeLists[i].IsEmpty());
}
}
@ -357,7 +357,7 @@ private:
**/
static size_t SizeClass(size_t size)
{
return ceil_log2((size_t)size);
return ceil_log2(size);
}
static uintptr_t ValueOfLeastSignificantOneBit(uintptr_t x)
@ -371,7 +371,7 @@ private:
// bit i set <==> size class i's freelist is not empty.
// this allows finding a non-empty list in O(1).
u32 m_bitmap;
uintptr_t m_bitmap;
};
@ -465,8 +465,8 @@ public:
}
// (generated via GUID)
static const u32 s_headerId = 0x111E8E6Fu;
static const u32 s_footerId = 0x4D745342u;
static const uintptr_t s_headerId = 0x111E8E6Fu;
static const uintptr_t s_footerId = 0x4D745342u;
private:
void Validate(FreedBlock* freedBlock) const

View File

@ -75,7 +75,7 @@ T bit_mask(size_t numBits)
// - though bulky, the below statements avoid sign-conversion warnings.
const T bitsInT = sizeof(T)*CHAR_BIT;
T mask(0);
mask = ~mask;
mask = T(~mask);
mask >>= T(bitsInT-numBits);
return mask;
}

View File

@ -55,15 +55,17 @@ u32 FAT_from_time_t(time_t time)
// (values are adjusted for DST)
struct tm* t = localtime(&time);
u16 fat_time = 0;
fat_time |= u16(t->tm_sec/2); // 5
fat_time |= u16(t->tm_min) << 5; // 6
fat_time |= u16(t->tm_hour) << 11; // 5
const u16 fat_time = u16(
(t->tm_sec/2) | // 5
(u16(t->tm_min) << 5) | // 6
(u16(t->tm_hour) << 11) // 5
);
u16 fat_date = 0;
fat_date |= u16(t->tm_mday); // 5
fat_date |= u16(t->tm_mon+1) << 5; // 4
fat_date |= u16(t->tm_year-80) << 9; // 7
const u16 fat_date = u16(
(t->tm_mday) | // 5
(u16(t->tm_mon+1) << 5) | // 4
(u16(t->tm_year-80) << 9) // 7
);
u32 fat_timedate = u32_from_u16(fat_date, fat_time);
return fat_timedate;

View File

@ -33,7 +33,7 @@ static u8 ComputeChecksum(PCV_u8 buf, size_t numBytes)
// (can't use std::accumulate - we need 8-bit wraparound)
u8 sum = 0;
for(PCV_u8 p = buf; p < buf+numBytes; p++)
sum += *p;
sum = u8(sum + *p);
return sum;
}

View File

@ -176,7 +176,7 @@ static size_t NumUniqueMaskedValues(const u8* apicIds, u8 mask)
for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
{
const u8 apicId = apicIds[processor];
const u8 field = apicId & mask;
const u8 field = u8(apicId & mask);
ids.insert(field);
}
@ -412,7 +412,7 @@ static void DetermineCachesProcessorMask(const u8* apicIds, uintptr_t* cachesPro
for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
{
const u8 apicId = apicIds[processor];
const u8 cacheId = apicId & cacheIdMask;
const u8 cacheId = u8(apicId & cacheIdMask);
cacheRelations.Add(cacheId, processor);
}
cacheRelations.StoreProcessorMasks(cachesProcessorMask);

View File

@ -24,6 +24,9 @@
#include "lib/external_libraries/sdl.h"
#include "lib/ogl.h"
#if OS_WIN
# include "lib/sysdep/os/win/wgfx.h"
#endif
char gfx_card[GFX_CARD_LEN] = "";
@ -41,7 +44,6 @@ void gfx_detect()
// try platform-specific version: they return more
// detailed information, and don't require OpenGL to be ready.
#if OS_WIN
extern LibError win_get_gfx_info();
if(win_get_gfx_info() < 0)
#endif
{

View File

@ -15,4 +15,9 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#ifndef INCLUDED_WGFX
#define INCLUDED_WGFX
extern LibError win_get_gfx_info();
#endif // #ifndef INCLUDED_WGFX

View File

@ -187,7 +187,7 @@ int open(const char* fn, int oflag, ...)
if(is_com_port)
{
char port[] = "COM1";
const char digit = fn[8]+1;
const char digit = char(fn[8]+1);
// PCs only support COM1..COM4.
if(!('1' <= digit && digit <= '4'))
return -1;

View File

@ -437,9 +437,9 @@ static void active_change_state(SdlActivationType type, Uint8 changed_app_state)
Uint8 old_app_state = app_state;
if(type == GAIN)
app_state |= changed_app_state;
app_state = Uint8(app_state | changed_app_state);
else
app_state &= ~changed_app_state;
app_state = Uint8(app_state & ~changed_app_state);
// generate an event - but only if the given state flags actually changed.
if((old_app_state & changed_app_state) != (app_state & changed_app_state))
@ -714,7 +714,7 @@ static void queue_mouse_event(int x, int y)
static void queue_button_event(int button, int state, int x, int y)
{
SDL_Event ev;
ev.type = (state == SDL_PRESSED)? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
ev.type = Uint8((state == SDL_PRESSED)? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP);
ev.button.button = (u8)button;
ev.button.state = (u8)state;
debug_assert(unsigned(x|y) <= USHRT_MAX);

View File

@ -0,0 +1,23 @@
/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_WSND
#define INCLUDED_WSND
extern LibError win_get_snd_info();
#endif // #ifndef INCLUDED_WSND

View File

@ -457,8 +457,7 @@ static HWND hAppWindow;
static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM UNUSED(lParam))
{
DWORD pid;
const DWORD tid = GetWindowThreadProcessId(hWnd, &pid);
UNUSED2(tid); // the function can't fail
(void)GetWindowThreadProcessId(hWnd, &pid); // (function always succeeds)
if(pid == GetCurrentProcessId())
{
@ -469,15 +468,18 @@ static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM UNUSED(lParam))
return TRUE; // keep calling
}
HWND wutil_AppWindow()
{
if(!hAppWindow)
{
const DWORD ret = EnumWindows(FindAppWindowByPid, 0);
// the callback returns FALSE when it has found the window
// (so as not to waste time); EnumWindows then returns 0.
// we therefore cannot check for errors.
UNUSED2(ret);
// to avoid wasting time, FindAppWindowByPid returns FALSE after
// finding the desired window, which causes EnumWindows to 'fail'.
// we detect actual errors by checking GetLastError.
WinScopedPreserveLastError s;
SetLastError(0);
(void)EnumWindows(FindAppWindowByPid, 0); // (see above)
debug_assert(GetLastError() == 0);
}
return hAppWindow;

View File

@ -22,6 +22,10 @@
#include "precompiled.h"
#include "snd.h"
#if OS_WIN
# include "lib/sysdep/os/win/wsnd.h"
#endif
char snd_card[SND_CARD_LEN];
char snd_drv_ver[SND_DRV_VER_LEN];
@ -32,7 +36,6 @@ void snd_detect()
// OpenAL API version and renderer (e.g. "Software").
#if OS_WIN
extern LibError win_get_snd_info();
win_get_snd_info();
#else
// At least reset the values for unhandled platforms.