Merge: Codechange: Use null pointer literal instead of the NULL macro
This commit is contained in:
@@ -53,23 +53,23 @@ static inline void CheckAllocationConstraints(size_t num_elements)
|
||||
* @note the memory contains garbage data (i.e. possibly non-zero values).
|
||||
* @tparam T the type of the variable(s) to allocation.
|
||||
* @param num_elements the number of elements to allocate of the given type.
|
||||
* @return NULL when num_elements == 0, non-NULL otherwise.
|
||||
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline T *MallocT(size_t num_elements)
|
||||
{
|
||||
/*
|
||||
* MorphOS cannot handle 0 elements allocations, or rather that always
|
||||
* returns NULL. So we do that for *all* allocations, thus causing it
|
||||
* returns nullptr. So we do that for *all* allocations, thus causing it
|
||||
* to behave the same on all OSes.
|
||||
*/
|
||||
if (num_elements == 0) return NULL;
|
||||
if (num_elements == 0) return nullptr;
|
||||
|
||||
/* Ensure the size does not overflow. */
|
||||
CheckAllocationConstraints<T>(num_elements);
|
||||
|
||||
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
|
||||
if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
|
||||
if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
|
||||
return t_ptr;
|
||||
}
|
||||
|
||||
@@ -81,20 +81,20 @@ static inline T *MallocT(size_t num_elements)
|
||||
* @note the memory contains all zero values.
|
||||
* @tparam T the type of the variable(s) to allocation.
|
||||
* @param num_elements the number of elements to allocate of the given type.
|
||||
* @return NULL when num_elements == 0, non-NULL otherwise.
|
||||
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline T *CallocT(size_t num_elements)
|
||||
{
|
||||
/*
|
||||
* MorphOS cannot handle 0 elements allocations, or rather that always
|
||||
* returns NULL. So we do that for *all* allocations, thus causing it
|
||||
* returns nullptr. So we do that for *all* allocations, thus causing it
|
||||
* to behave the same on all OSes.
|
||||
*/
|
||||
if (num_elements == 0) return NULL;
|
||||
if (num_elements == 0) return nullptr;
|
||||
|
||||
T *t_ptr = (T*)calloc(num_elements, sizeof(T));
|
||||
if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
|
||||
if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
|
||||
return t_ptr;
|
||||
}
|
||||
|
||||
@@ -107,26 +107,26 @@ static inline T *CallocT(size_t num_elements)
|
||||
* @tparam T the type of the variable(s) to allocation.
|
||||
* @param t_ptr the previous allocation to extend/shrink.
|
||||
* @param num_elements the number of elements to allocate of the given type.
|
||||
* @return NULL when num_elements == 0, non-NULL otherwise.
|
||||
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline T *ReallocT(T *t_ptr, size_t num_elements)
|
||||
{
|
||||
/*
|
||||
* MorphOS cannot handle 0 elements allocations, or rather that always
|
||||
* returns NULL. So we do that for *all* allocations, thus causing it
|
||||
* returns nullptr. So we do that for *all* allocations, thus causing it
|
||||
* to behave the same on all OSes.
|
||||
*/
|
||||
if (num_elements == 0) {
|
||||
free(t_ptr);
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Ensure the size does not overflow. */
|
||||
CheckAllocationConstraints<T>(num_elements);
|
||||
|
||||
t_ptr = (T*)realloc(static_cast<void *>(t_ptr), num_elements * sizeof(T));
|
||||
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
|
||||
if (t_ptr == nullptr) ReallocError(num_elements * sizeof(T));
|
||||
return t_ptr;
|
||||
}
|
||||
|
||||
|
@@ -73,7 +73,7 @@ private:
|
||||
|
||||
public:
|
||||
/** Create a new buffer */
|
||||
ReusableBuffer() : buffer(NULL), count(0) {}
|
||||
ReusableBuffer() : buffer(nullptr), count(0) {}
|
||||
/** Clear the buffer */
|
||||
~ReusableBuffer() { free(this->buffer); }
|
||||
|
||||
|
@@ -108,11 +108,11 @@ class Kdtree {
|
||||
std::vector<T> elements = this->FreeSubtree(this->root);
|
||||
elements.push_back(root_element);
|
||||
|
||||
if (include_element != NULL) {
|
||||
if (include_element != nullptr) {
|
||||
elements.push_back(*include_element);
|
||||
initial_count++;
|
||||
}
|
||||
if (exclude_element != NULL) {
|
||||
if (exclude_element != nullptr) {
|
||||
typename std::vector<T>::iterator removed = std::remove(elements.begin(), elements.end(), *exclude_element);
|
||||
elements.erase(removed, elements.end());
|
||||
initial_count--;
|
||||
@@ -377,7 +377,7 @@ public:
|
||||
*/
|
||||
void Rebuild()
|
||||
{
|
||||
this->Rebuild(NULL, NULL);
|
||||
this->Rebuild(nullptr, nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -390,7 +390,7 @@ public:
|
||||
if (this->Count() == 0) {
|
||||
this->root = this->AddNode(element);
|
||||
} else {
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(&element, NULL)) {
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(&element, nullptr)) {
|
||||
this->InsertRecursive(element, this->root, 0);
|
||||
this->IncrementUnbalanced();
|
||||
}
|
||||
@@ -408,7 +408,7 @@ public:
|
||||
{
|
||||
size_t count = this->Count();
|
||||
if (count == 0) return;
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(NULL, &element)) {
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(nullptr, &element)) {
|
||||
/* If the removed element is the root node, this modifies this->root */
|
||||
this->root = this->RemoveRecursive(element, this->root, 0);
|
||||
this->IncrementUnbalanced();
|
||||
|
@@ -78,7 +78,7 @@ static inline int MemCmpT(const T *ptr1, const T *ptr2, size_t num = 1)
|
||||
template <typename T>
|
||||
static inline void MemReverseT(T *ptr1, T *ptr2)
|
||||
{
|
||||
assert(ptr1 != NULL && ptr2 != NULL);
|
||||
assert(ptr1 != nullptr && ptr2 != nullptr);
|
||||
assert(ptr1 < ptr2);
|
||||
|
||||
do {
|
||||
@@ -95,7 +95,7 @@ static inline void MemReverseT(T *ptr1, T *ptr2)
|
||||
template <typename T>
|
||||
static inline void MemReverseT(T *ptr, size_t num)
|
||||
{
|
||||
assert(ptr != NULL);
|
||||
assert(ptr != nullptr);
|
||||
|
||||
MemReverseT(ptr, ptr + (num - 1));
|
||||
}
|
||||
|
@@ -39,8 +39,8 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) :
|
||||
checked(0),
|
||||
#endif /* OTTD_ASSERT */
|
||||
cleaning(false),
|
||||
data(NULL),
|
||||
alloc_cache(NULL)
|
||||
data(nullptr),
|
||||
alloc_cache(nullptr)
|
||||
{ }
|
||||
|
||||
/**
|
||||
@@ -71,7 +71,7 @@ DEFINE_POOL_METHOD(inline size_t)::FindFirstFree()
|
||||
size_t index = this->first_free;
|
||||
|
||||
for (; index < this->first_unused; index++) {
|
||||
if (this->data[index] == NULL) return index;
|
||||
if (this->data[index] == nullptr) return index;
|
||||
}
|
||||
|
||||
if (index < this->size) {
|
||||
@@ -96,17 +96,17 @@ DEFINE_POOL_METHOD(inline size_t)::FindFirstFree()
|
||||
* @param size size of item
|
||||
* @param index index of item
|
||||
* @pre index < this->size
|
||||
* @pre this->Get(index) == NULL
|
||||
* @pre this->Get(index) == nullptr
|
||||
*/
|
||||
DEFINE_POOL_METHOD(inline void *)::AllocateItem(size_t size, size_t index)
|
||||
{
|
||||
assert(this->data[index] == NULL);
|
||||
assert(this->data[index] == nullptr);
|
||||
|
||||
this->first_unused = max(this->first_unused, index + 1);
|
||||
this->items++;
|
||||
|
||||
Titem *item;
|
||||
if (Tcache && this->alloc_cache != NULL) {
|
||||
if (Tcache && this->alloc_cache != nullptr) {
|
||||
assert(sizeof(Titem) == size);
|
||||
item = (Titem *)this->alloc_cache;
|
||||
this->alloc_cache = this->alloc_cache->next;
|
||||
@@ -164,7 +164,7 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
|
||||
|
||||
if (index >= this->size) this->ResizeFor(index);
|
||||
|
||||
if (this->data[index] != NULL) {
|
||||
if (this->data[index] != nullptr) {
|
||||
SlErrorCorruptFmt("%s index " PRINTF_SIZE " already in use", this->name, index);
|
||||
}
|
||||
|
||||
@@ -174,13 +174,13 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index)
|
||||
/**
|
||||
* Deallocates memory used by this index and marks item as free
|
||||
* @param index item to deallocate
|
||||
* @pre unit is allocated (non-NULL)
|
||||
* @note 'delete NULL' doesn't cause call of this function, so it is safe
|
||||
* @pre unit is allocated (non-nullptr)
|
||||
* @note 'delete nullptr' doesn't cause call of this function, so it is safe
|
||||
*/
|
||||
DEFINE_POOL_METHOD(void)::FreeItem(size_t index)
|
||||
{
|
||||
assert(index < this->size);
|
||||
assert(this->data[index] != NULL);
|
||||
assert(this->data[index] != nullptr);
|
||||
if (Tcache) {
|
||||
AllocCache *ac = (AllocCache *)this->data[index];
|
||||
ac->next = this->alloc_cache;
|
||||
@@ -188,7 +188,7 @@ DEFINE_POOL_METHOD(void)::FreeItem(size_t index)
|
||||
} else {
|
||||
free(this->data[index]);
|
||||
}
|
||||
this->data[index] = NULL;
|
||||
this->data[index] = nullptr;
|
||||
this->first_free = min(this->first_free, index);
|
||||
this->items--;
|
||||
if (!this->cleaning) Titem::PostDestructor(index);
|
||||
@@ -200,16 +200,16 @@ DEFINE_POOL_METHOD(void)::CleanPool()
|
||||
this->cleaning = true;
|
||||
Titem::PreCleanPool();
|
||||
for (size_t i = 0; i < this->first_unused; i++) {
|
||||
delete this->Get(i); // 'delete NULL;' is very valid
|
||||
delete this->Get(i); // 'delete nullptr;' is very valid
|
||||
}
|
||||
assert(this->items == 0);
|
||||
free(this->data);
|
||||
this->first_unused = this->first_free = this->size = 0;
|
||||
this->data = NULL;
|
||||
this->data = nullptr;
|
||||
this->cleaning = false;
|
||||
|
||||
if (Tcache) {
|
||||
while (this->alloc_cache != NULL) {
|
||||
while (this->alloc_cache != nullptr) {
|
||||
AllocCache *ac = this->alloc_cache;
|
||||
this->alloc_cache = ac->next;
|
||||
free(ac);
|
||||
|
@@ -91,7 +91,7 @@ struct Pool : PoolBase {
|
||||
size_t size; ///< Current allocated size
|
||||
size_t first_free; ///< No item with index lower than this is free (doesn't say anything about this one!)
|
||||
size_t first_unused; ///< This and all higher indexes are free (doesn't say anything about first_unused-1 !)
|
||||
size_t items; ///< Number of used indexes (non-NULL)
|
||||
size_t items; ///< Number of used indexes (non-nullptr)
|
||||
#ifdef OTTD_ASSERT
|
||||
size_t checked; ///< Number of items we checked for
|
||||
#endif /* OTTD_ASSERT */
|
||||
@@ -115,13 +115,13 @@ struct Pool : PoolBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether given index can be used to get valid (non-NULL) Titem
|
||||
* Tests whether given index can be used to get valid (non-nullptr) Titem
|
||||
* @param index index to examine
|
||||
* @return true if PoolItem::Get(index) will return non-NULL pointer
|
||||
* @return true if PoolItem::Get(index) will return non-nullptr pointer
|
||||
*/
|
||||
inline bool IsValidID(size_t index)
|
||||
{
|
||||
return index < this->first_unused && this->Get(index) != NULL;
|
||||
return index < this->first_unused && this->Get(index) != nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,7 +150,7 @@ struct Pool : PoolBase {
|
||||
* Allocates space for new Titem
|
||||
* @param size size of Titem
|
||||
* @return pointer to allocated memory
|
||||
* @note can never fail (return NULL), use CanAllocate() to check first!
|
||||
* @note can never fail (return nullptr), use CanAllocate() to check first!
|
||||
*/
|
||||
inline void *operator new(size_t size)
|
||||
{
|
||||
@@ -164,7 +164,7 @@ struct Pool : PoolBase {
|
||||
*/
|
||||
inline void operator delete(void *p)
|
||||
{
|
||||
if (p == NULL) return;
|
||||
if (p == nullptr) return;
|
||||
Titem *pn = (Titem *)p;
|
||||
assert_msg(pn == Tpool->Get(pn->index), "name: %s", Tpool->name);
|
||||
Tpool->FreeItem(pn->index);
|
||||
@@ -175,7 +175,7 @@ struct Pool : PoolBase {
|
||||
* @param size size of Titem
|
||||
* @param index index of item
|
||||
* @return pointer to allocated memory
|
||||
* @note can never fail (return NULL), use CanAllocate() to check first!
|
||||
* @note can never fail (return nullptr), use CanAllocate() to check first!
|
||||
* @pre index has to be unused! Else it will crash
|
||||
*/
|
||||
inline void *operator new(size_t size, size_t index)
|
||||
@@ -228,9 +228,9 @@ struct Pool : PoolBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether given index can be used to get valid (non-NULL) Titem
|
||||
* Tests whether given index can be used to get valid (non-nullptr) Titem
|
||||
* @param index index to examine
|
||||
* @return true if PoolItem::Get(index) will return non-NULL pointer
|
||||
* @return true if PoolItem::Get(index) will return non-nullptr pointer
|
||||
*/
|
||||
static inline bool IsValidID(size_t index)
|
||||
{
|
||||
@@ -252,11 +252,11 @@ struct Pool : PoolBase {
|
||||
* Returns Titem with given index
|
||||
* @param index of item to get
|
||||
* @return pointer to Titem
|
||||
* @note returns NULL for invalid index
|
||||
* @note returns nullptr for invalid index
|
||||
*/
|
||||
static inline Titem *GetIfValid(size_t index)
|
||||
{
|
||||
return index < Tpool->first_unused ? Tpool->Get(index) : NULL;
|
||||
return index < Tpool->first_unused ? Tpool->Get(index) : nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,7 +282,7 @@ struct Pool : PoolBase {
|
||||
* Dummy function called after destructor of each member.
|
||||
* If you want to use it, override it in PoolItem's subclass.
|
||||
* @param index index of deleted item
|
||||
* @note when this function is called, PoolItem::Get(index) == NULL.
|
||||
* @note when this function is called, PoolItem::Get(index) == nullptr.
|
||||
* @note it's called only when !CleaningPool()
|
||||
*/
|
||||
static inline void PostDestructor(size_t index) { }
|
||||
@@ -321,8 +321,8 @@ private:
|
||||
};
|
||||
|
||||
#define FOR_ALL_ITEMS_FROM(type, iter, var, start) \
|
||||
for (size_t iter = start; var = NULL, iter < type::GetPoolSize(); iter++) \
|
||||
if ((var = type::Get(iter)) != NULL)
|
||||
for (size_t iter = start; var = nullptr, iter < type::GetPoolSize(); iter++) \
|
||||
if ((var = type::Get(iter)) != nullptr)
|
||||
|
||||
#define FOR_ALL_ITEMS(type, iter, var) FOR_ALL_ITEMS_FROM(type, iter, var, 0)
|
||||
|
||||
|
@@ -46,13 +46,13 @@ protected:
|
||||
|
||||
public:
|
||||
|
||||
SmallMatrix() : data(NULL), width(0), height(0), capacity(0) {}
|
||||
SmallMatrix() : data(nullptr), width(0), height(0), capacity(0) {}
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* @param other The other matrix to copy.
|
||||
*/
|
||||
SmallMatrix(const SmallMatrix &other) : data(NULL), width(0), height(0), capacity(0)
|
||||
SmallMatrix(const SmallMatrix &other) : data(nullptr), width(0), height(0), capacity(0)
|
||||
{
|
||||
this->Assign(other);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ public:
|
||||
this->width = 0;
|
||||
this->capacity = 0;
|
||||
free(this->data);
|
||||
this->data = NULL;
|
||||
this->data = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,8 +216,8 @@ public:
|
||||
inline void Resize(uint new_width, uint new_height)
|
||||
{
|
||||
uint new_capacity = new_width * new_height;
|
||||
T *new_data = NULL;
|
||||
void (*copy)(T *dest, const T *src, size_t count) = NULL;
|
||||
T *new_data = nullptr;
|
||||
void (*copy)(T *dest, const T *src, size_t count) = nullptr;
|
||||
if (new_capacity > this->capacity) {
|
||||
/* If the data doesn't fit into current capacity, resize and copy ... */
|
||||
new_data = MallocT<T>(new_capacity);
|
||||
|
@@ -53,8 +53,8 @@ static inline void GSortT(T *base, size_t num, int (CDECL *comparator)(const T*,
|
||||
{
|
||||
if (num < 2) return;
|
||||
|
||||
assert(base != NULL);
|
||||
assert(comparator != NULL);
|
||||
assert(base != nullptr);
|
||||
assert(comparator != nullptr);
|
||||
|
||||
T *a = base;
|
||||
T *b = base + 1;
|
||||
|
Reference in New Issue
Block a user