declare ARRAY_SIZE once and use C++11 feature

This commit is contained in:
RadWolfie 2020-03-30 02:23:35 -05:00
parent af3858c0d8
commit 9e1077fe9f
7 changed files with 48 additions and 25 deletions

View File

@ -90,6 +90,7 @@ file (GLOB CXBXR_HEADER_COMMON
"${CXBXR_ROOT_DIR}/src/common/util/CPUID.h"
"${CXBXR_ROOT_DIR}/src/common/util/crc32c.h"
"${CXBXR_ROOT_DIR}/src/common/util/CxbxUtil.h"
"${CXBXR_ROOT_DIR}/src/common/util/std_extend.hpp"
"${CXBXR_ROOT_DIR}/src/common/util/strConverter.hpp"
"${CXBXR_ROOT_DIR}/src/common/win32/AlignPosfix1.h"
"${CXBXR_ROOT_DIR}/src/common/win32/AlignPrefix1.h"

View File

@ -25,6 +25,7 @@
// *
// ******************************************************************
#include "util/std_extend.hpp"
#include "AddressRanges.h"
bool AddressRangeMatchesFlags(const int index, const unsigned int flags)

View File

@ -47,10 +47,6 @@ const int BLOCK_SIZE = KB(64);
// One allocation block consists of 16 pages (since PAGE_SIZE is 4 kilobytes)
const int PAGE_SIZE = KB(4);
#define ARRAY_SIZE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
const struct {
uint32_t Start;
#ifdef DEBUG

View File

@ -34,6 +34,7 @@
#endif
#include <cstdint> // For uint32_t
#include "util/std_extend.hpp"
#include "AddressRanges.h"
// Reserve an address range up to the extend of what the host allows.
@ -203,7 +204,7 @@ void FreeMemoryRange(int index, uint32_t blocks_reserved[384])
bool ReserveAddressRanges(const unsigned int system, uint32_t blocks_reserved[384]) {
// Loop over all Xbox address ranges
for (int i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
for (size_t i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
// Skip address ranges that don't match the given flags
if (!AddressRangeMatchesFlags(i, system))
continue;
@ -227,7 +228,7 @@ void FreeAddressRanges(const unsigned int system, unsigned int release_systems,
return;
}
// Loop over all Xbox address ranges
for (int i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
for (size_t i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
// Skip address ranges that do match specific flag
if (AddressRangeMatchesFlags(i, system))
continue;
@ -243,10 +244,10 @@ void FreeAddressRanges(const unsigned int system, unsigned int release_systems,
bool AttemptReserveAddressRanges(unsigned int* p_reserved_systems, uint32_t blocks_reserved[384]) {
int iLast = 0;
size_t iLast = 0;
unsigned int reserved_systems = *p_reserved_systems, clear_systems = 0;
// Loop over all Xbox address ranges
for (int i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
for (size_t i = 0; i < ARRAY_SIZE(XboxAddressRanges); i++) {
// Once back to original spot, let's resume.
if (i == iLast && clear_systems) {

View File

@ -38,7 +38,7 @@ EmuDevice::EmuDevice(int type, HWND hwnd)
case to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_DUKE):
case to_underlying(XBOX_INPUT_DEVICE::MS_CONTROLLER_S): {
m_hwnd = hwnd;
for (int i = 0; i < ARRAY_SIZE(button_xbox_ctrl_id); i++) {
for (size_t i = 0; i < ARRAY_SIZE(button_xbox_ctrl_id); i++) {
m_buttons.push_back(new Button(button_xbox_ctrl_id[i], i, hwnd));
// Install the subclass for the button control

View File

@ -31,6 +31,7 @@
#include <assert.h>
#include <string>
#include <type_traits>
#include "std_extend.hpp" // for ARRAY_SIZE
/* This is a linux struct for vectored I/O. See readv() and writev() */
struct IoVec
@ -76,22 +77,6 @@ constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept
#define GET_WORD_LOW(value) (uint8_t)((value) & 0xFF)
#define GET_WORD_HIGH(value) (uint8_t)(((value) >> 8) & 0xFF)
#ifdef __cplusplus
template <size_t N> struct ArraySizeHelper { char _[N]; };
template <typename T, size_t N>
ArraySizeHelper<N> makeArraySizeHelper(T(&)[N]);
# define ARRAY_SIZE(a) sizeof(makeArraySizeHelper(a))
#else
// The expression ARRAY_SIZE(a) is a compile-time constant of type
// size_t which represents the number of elements of the given
// array. You should only use ARRAY_SIZE on statically allocated
// arrays.
#define ARRAY_SIZE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
#endif
/*! round dwValue to the nearest multiple of dwMult */
static uint32_t RoundUp(uint32_t dwValue, uint32_t dwMult)
{

View File

@ -0,0 +1,39 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// ******************************************************************
// *
// * This is free and unencumbered software released into the public domain.
// *
// * Anyone is free to copy, modify, publish, use, compile, sell, or
// * distribute this software, either in source code form or as a compiled
// * binary, for any purpose, commercial or non-commercial, and by any
// * means.
// *
// * In jurisdictions that recognize copyright laws, the author or authors
// * of this software dedicate any and all copyright interest in the
// * software to the public domain. We make this dedication for the benefit
// * of the public at large and to the detriment of our heirs and
// * successors. We intend this dedication to be an overt act of
// * relinquishment in perpetuity of all present and future rights to this
// * software under copyright law.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// * OTHER DEALINGS IN THE SOFTWARE.
// *
// * For more information, please refer to <http://unlicense.org/>
// *
// ******************************************************************
#pragma once
#include <stddef.h>
template <typename T, size_t N>
constexpr size_t ARRAY_SIZE(T(&)[N])
{
return N;
}