Changeset - r18056:a3aa5f0ca569
[Not reviewed]
master
0 3 0
frosch - 13 years ago 2011-09-03 12:45:45
frosch@openttd.org
(svn r22881) -Fix (r22875): GCC warnings on 64bit systems.
3 files changed with 28 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/core/alloc_func.hpp
Show inline comments
 
@@ -20,12 +20,35 @@
 
 */
 

	
 
void NORETURN MallocError(size_t size);
 
void NORETURN ReallocError(size_t size);
 

	
 
/**
 
 * Checks whether allocating memory would overflow size_t.
 
 *
 
 * @param element_size Size of the structure to allocate.
 
 * @param num_elements Number of elements to allocate.
 
 */
 
static inline void CheckAllocationConstraints(size_t element_size, size_t num_elements)
 
{
 
	if (num_elements > SIZE_MAX / element_size) MallocError(SIZE_MAX);
 
}
 

	
 
/**
 
 * Checks whether allocating memory would overflow size_t.
 
 *
 
 * @tparam T Structure to allocate.
 
 * @param num_elements Number of elements to allocate.
 
 */
 
template <typename T>
 
static inline void CheckAllocationConstraints(size_t num_elements)
 
{
 
	CheckAllocationConstraints(sizeof(T), num_elements);
 
}
 

	
 
/**
 
 * Simplified allocation function that allocates the specified number of
 
 * elements of the given type. It also explicitly casts it to the requested
 
 * type.
 
 * @note throws an error when there is no memory anymore.
 
 * @note the memory contains garbage data (i.e. possibly non-zero values).
 
 * @tparam T the type of the variable(s) to allocation.
 
@@ -40,13 +63,13 @@ static FORCEINLINE T *MallocT(size_t num
 
	 * returns NULL. So we do that for *all* allocations, thus causing it
 
	 * to behave the same on all OSes.
 
	 */
 
	if (num_elements == 0) return NULL;
 

	
 
	/* Ensure the size does not overflow. */
 
	if (num_elements > SIZE_MAX / sizeof(T)) MallocError(SIZE_MAX);
 
	CheckAllocationConstraints<T>(num_elements);
 

	
 
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
 
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
 
	return t_ptr;
 
}
 

	
 
@@ -97,19 +120,19 @@ static FORCEINLINE T *ReallocT(T *t_ptr,
 
	if (num_elements == 0) {
 
		free(t_ptr);
 
		return NULL;
 
	}
 

	
 
	/* Ensure the size does not overflow. */
 
	if (num_elements > SIZE_MAX / sizeof(T)) MallocError(SIZE_MAX);
 
	CheckAllocationConstraints<T>(num_elements);
 

	
 
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
 
	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
 
	return t_ptr;
 
}
 

	
 
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
 
#define AllocaM(T, num_elements) \
 
	((num_elements) > SIZE_MAX / sizeof(T) && (MallocError(SIZE_MAX), NULL), \
 
	(CheckAllocationConstraints<T>(num_elements), \
 
	(T*)alloca((num_elements) * sizeof(T)))
 

	
 
#endif /* ALLOC_FUNC_HPP */
src/pathfinder/npf/queue.cpp
Show inline comments
 
@@ -232,13 +232,13 @@ void BinaryHeap::Init(uint max_size)
 
void Hash::Init(Hash_HashProc *hash, uint num_buckets)
 
{
 
	/* Allocate space for the Hash, the buckets and the bucket flags */
 
	uint i;
 

	
 
	/* Ensure the size won't overflow. */
 
	assert(num_buckets < SIZE_MAX / (sizeof(*this->buckets) + sizeof(*this->buckets_in_use)));
 
	CheckAllocationConstraints(sizeof(*this->buckets) + sizeof(*this->buckets_in_use), num_buckets);
 

	
 
	this->hash = hash;
 
	this->size = 0;
 
	this->num_buckets = num_buckets;
 
	this->buckets = (HashNode*)MallocT<byte>(num_buckets * (sizeof(*this->buckets) + sizeof(*this->buckets_in_use)));
 
	this->buckets_in_use = (bool*)(this->buckets + num_buckets);
src/stdafx.h
Show inline comments
 
@@ -336,12 +336,13 @@ typedef unsigned char byte;
 

	
 
/* Check if the types have the bitsizes like we are using them */
 
assert_compile(sizeof(uint64) == 8);
 
assert_compile(sizeof(uint32) == 4);
 
assert_compile(sizeof(uint16) == 2);
 
assert_compile(sizeof(uint8)  == 1);
 
assert_compile(SIZE_MAX >= UINT32_MAX);
 

	
 
#ifndef M_PI_2
 
#define M_PI_2 1.57079632679489661923
 
#define M_PI   3.14159265358979323846
 
#endif /* M_PI_2 */
 

	
0 comments (0 inline, 0 general)