Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the macros from sys/param.h #1970

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion builtin/builtin_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <nuttx/config.h>

#include <nuttx/lib/builtin.h>
#include <sys/param.h>

#include <sys/stat.h>

Expand Down Expand Up @@ -52,7 +53,7 @@ const struct builtin_s g_builtins[] =
#endif
};

const int g_builtin_count = sizeof(g_builtins) / sizeof(g_builtins[0]);
const int g_builtin_count = nitems(g_builtins);

/****************************************************************************
* Private Data
Expand Down
15 changes: 6 additions & 9 deletions canutils/libcanutils/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <string.h>
#include <stdint.h>

#include <sys/param.h>
#include <sys/socket.h> /* for sa_family_t */
#include <nuttx/can.h>
#include <netpacket/can.h>
Expand Down Expand Up @@ -496,10 +497,6 @@ static const char *protocol_violation_locations[] = {
"unspecified",
};

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif

static int snprintf_error_data(char *buf, size_t len, uint8_t err,
const char **arr, int arr_len)
{
Expand Down Expand Up @@ -537,7 +534,7 @@ static int snprintf_error_ctrl(char *buf, size_t len, const struct canfd_frame *
n += snprintf(buf + n, len - n, "{");
n += snprintf_error_data(buf + n, len - n, cf->data[1],
controller_problems,
ARRAY_SIZE(controller_problems));
nitems(controller_problems));
n += snprintf(buf + n, len - n, "}");

return n;
Expand All @@ -553,10 +550,10 @@ static int snprintf_error_prot(char *buf, size_t len, const struct canfd_frame *
n += snprintf(buf + n, len - n, "{{");
n += snprintf_error_data(buf + n, len - n, cf->data[2],
protocol_violation_types,
ARRAY_SIZE(protocol_violation_types));
nitems(protocol_violation_types));
n += snprintf(buf + n, len - n, "}{");
if (cf->data[3] > 0 &&
cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
cf->data[3] < nitems(protocol_violation_locations))
n += snprintf(buf + n, len - n, "%s",
protocol_violation_locations[cf->data[3]]);
n += snprintf(buf + n, len - n, "}}");
Expand All @@ -575,15 +572,15 @@ void snprintf_can_error_frame(char *buf, size_t len, const struct canfd_frame *c
return;

class = cf->can_id & CAN_EFF_MASK;
if (class > (1 << ARRAY_SIZE(error_classes))) {
if (class > (1 << nitems(error_classes))) {
fprintf(stderr, "Error class %#jx is invalid\n", (uintmax_t)class);
return;
}

if (!sep)
sep = defsep;

for (i = 0; i < (int)ARRAY_SIZE(error_classes); i++) {
for (i = 0; i < (int)nitems(error_classes); i++) {
mask = 1 << i;
if (class & mask) {
if (classes)
Expand Down
9 changes: 4 additions & 5 deletions examples/adxl372_test/adxl372_test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <nuttx/fs/fs.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Expand All @@ -41,7 +41,6 @@
* Pre-processor Definitions
****************************************************************************/

#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
#define PASSED 0

#define SUB_PROMPT "stst >"
Expand Down Expand Up @@ -394,7 +393,7 @@ int main(int argc, FAR char *argv[])
adxl372_test, /* ADXL372 accelerometer tests */
};

FAR char *test_path[ARRAYSIZE(test_ptr_array)];
FAR char *test_path[nitems(test_ptr_array)];

if (argc < 1 || *argv[1] == 0 || *(argv[1] + 1) == 0)
{
Expand Down Expand Up @@ -490,7 +489,7 @@ int main(int argc, FAR char *argv[])
printf("Set to batch mode.\n");
}
}
else if (ui >= ARRAYSIZE(test_ptr_array))
else if (ui >= nitems(test_ptr_array))
{
printf("Huh?\n");
}
Expand All @@ -511,7 +510,7 @@ int main(int argc, FAR char *argv[])
{
printf("ADXL372 sensor diagnostic started in batch mode...\n");

for (ui = 0; ui < ARRAYSIZE(test_ptr_array); ui++)
for (ui = 0; ui < nitems(test_ptr_array); ui++)
{
step_rc = 0;
if (test_ptr_array[ui] != 0)
Expand Down
11 changes: 4 additions & 7 deletions examples/dac/dac_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/param.h>
#include <nuttx/analog/dac.h>
#include <nuttx/arch.h>

Expand All @@ -47,10 +48,6 @@
# define CONFIG_EXAMPLES_DAC_DEVPATH "/dev/dac0"
#endif

#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

/****************************************************************************
* Private Types
****************************************************************************/
Expand Down Expand Up @@ -226,7 +223,7 @@ static int cmd_dac_put(int argc, FAR const char *argv[])
{
msgs[0].am_channel = g_dacstate.channel;
msgs[0].am_data = data;
ret = dac_put(g_dacstate.devpath, msgs, ARRAY_SIZE(msgs), delay);
ret = dac_put(g_dacstate.devpath, msgs, nitems(msgs), delay);
printf("ret=%d\n", ret);
}

Expand Down Expand Up @@ -277,7 +274,7 @@ static void dac_help(void)
"Default: %s Current: %s\n",
CONFIG_EXAMPLES_DAC_DEVPATH,
g_dacstate.devpath ? g_dacstate.devpath : "NONE");
print_cmds("\nCommands:\n", commands, ARRAY_SIZE(commands), "\n");
print_cmds("\nCommands:\n", commands, nitems(commands), "\n");
}

static int arg_string(FAR const char **arg, FAR const char **value)
Expand Down Expand Up @@ -422,7 +419,7 @@ int main(int argc, FAR const char *argv[])
}
else
{
ret = execute_cmd(argc, argv, commands, ARRAY_SIZE(commands));
ret = execute_cmd(argc, argv, commands, nitems(commands));
}

return (ret >= 0) ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down
24 changes: 11 additions & 13 deletions examples/ft80x/ft80x_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <fcntl.h>
#include <errno.h>

#include <sys/param.h>
#include <nuttx/lcd/ft80x.h>

#include "graphics/ft80x.h"
Expand Down Expand Up @@ -60,13 +61,13 @@ struct ft80x_exampleinfo_s
* ft80x_prim_lines LINES Line drawing primitive
* ft80x_prim_linestrip LINE_STRIP Line strip drawing primitive
* ft80x_prim_edgestrip_r EDGE_STRIP_R Edge strip right side drawing
primitive
* primitive
* (To be provided) EDGE_STRIP_L Edge strip left side drawing
primitive
* primitive
* (To be provided) EDGE_STRIP_A Edge strip above side drawing
primitive
* primitive
* (To be provided) EDGE_STRIP_B Edge strip below side drawing
primitive
* primitive
* ft80x_prim_rectangles RECTS Rectangle drawing primitive
* ft80x_prim_scissor SCISSOR Scissor primitive
* ft80x_prim_stencil STENCIL Stencil primitives
Expand All @@ -88,15 +89,14 @@ static const struct ft80x_exampleinfo_s g_primitives[] =
{ "Alpha Blend", ft80x_prim_alphablend }
};

#define NPRIMITIVES (sizeof(g_primitives) / sizeof(struct ft80x_exampleinfo_s))
#endif /* CONFIG_EXAMPLES_FT80X_PRIMITIVES */

/* Co-processor display examples. Only a small, but interesting, subset
* here co-processor command are exercised and these with only a few of the
* possible options.
*
* FUNCTION CoProc CMD USED DESCRIPTION
* ------------------------ --------------- ----------------------------------
* ------------------------ --------------- --------------------------------
* ft80x_coproc_button CMD_BUTTON Draw a button
* ft80x_coproc_clock CMD_CLOCK Draw an analog clock
* ft80x_coproc_gauge CMD_GAUGE Draw a gauge
Expand All @@ -109,12 +109,12 @@ static const struct ft80x_exampleinfo_s g_primitives[] =
* ft80x_coproc_toggle CMD_TOGGLE Draw a toggle switch
* ft80x_coproc_number CMD_NUMBER Draw a decimal number
* ft80x_coproc_calibrate CMD_CALIBRATE Execute the touch screen
calibration routine
* calibration routine
* ft80x_coproc_spinner CMD_SPINNER Start an animated spinner
* ft80x_coproc_screensaver CMD_SCREENSAVER Start an animated screensaver
* (To be provided) CMD_SKETCH Start a continuous sketch update
* (To be provided) CMD_SNAPSHOT Take a snapshot of the current
screen
* screen
* ft80x_coproc_logo CMD_LOGO Play device log animation
*/

Expand All @@ -139,8 +139,6 @@ static const struct ft80x_exampleinfo_s g_coproc[] =
{ "Logo", ft80x_coproc_logo }
};

#define NCOPROC (sizeof(g_coproc) / sizeof(struct ft80x_exampleinfo_s))

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -182,7 +180,7 @@ static int ft80x_showname(int fd, FAR struct ft80x_dlbuffer_s *buffer,
/* Clear the display */

cmds.clearrgb.cmd = FT80X_CLEAR_COLOR_RGB(0, 0, 0x80);
cmds.clear.cmd = FT80X_CLEAR(1 ,1, 1);
cmds.clear.cmd = FT80X_CLEAR(1, 1, 1);
cmds.colorrgb.cmd = FT80X_COLOR_RGB(0xff, 0xff, 0xff);

/* Use the CMD_TEXT co-processor command to show the name of the next
Expand Down Expand Up @@ -308,7 +306,7 @@ int main(int argc, FAR char *argv[])

ft80x_info("FT80x Primitive Functions\n");

for (i = 0; i < NPRIMITIVES; i++)
for (i = 0; i < nitems(g_primitives); i++)
{
ft80x_example(fd, buffer, &g_primitives[i]);
}
Expand All @@ -318,7 +316,7 @@ int main(int argc, FAR char *argv[])

ft80x_info("FT80x Co-processor Functions\n");

for (i = 0; i < NCOPROC; i++)
for (i = 0; i < nitems(g_coproc); i++)
{
ft80x_example(fd, buffer, &g_coproc[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/ftpd/ftpd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <errno.h>
#include <debug.h>

#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Expand Down Expand Up @@ -65,7 +66,6 @@ static const struct fptd_account_s g_ftpdaccounts[] =
{ FTPD_ACCOUNTFLAG_GUEST, "ftp", NULL, NULL },
{ FTPD_ACCOUNTFLAG_GUEST, "anonymous", NULL, NULL },
};
#define NACCOUNTS (sizeof(g_ftpdaccounts) / sizeof(struct fptd_account_s))

/****************************************************************************
* Public Data
Expand Down Expand Up @@ -139,7 +139,7 @@ static void ftpd_accounts(FTPD_SESSION handle)
int i;

printf("Adding accounts:\n");
for (i = 0; i < NACCOUNTS; i++)
for (i = 0; i < nitems(g_ftpdaccounts); i++)
{
account = &g_ftpdaccounts[i];

Expand Down
9 changes: 4 additions & 5 deletions examples/lsm330spi_test/lsm330spi_test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <nuttx/fs/fs.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Expand All @@ -41,7 +41,6 @@
* Pre-processor Definitions
****************************************************************************/

#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
#define PASSED 0

#define SUB_PROMPT "stst >"
Expand Down Expand Up @@ -631,7 +630,7 @@ int main(int argc, FAR char *argv[])
lsm330gyro_test, /* LSM330 gyroscope tests */
};

FAR char *test_path[ARRAYSIZE(test_ptr_array)];
FAR char *test_path[nitems(test_ptr_array)];

if (argc < 2 || *argv[1] == 0 || *(argv[1] + 1) == 0)
{
Expand Down Expand Up @@ -737,7 +736,7 @@ int main(int argc, FAR char *argv[])
printf("Set to batch mode.\n");
}
}
else if (ui >= ARRAYSIZE(test_ptr_array))
else if (ui >= nitems(test_ptr_array))
{
printf("Huh?\n");
}
Expand All @@ -757,7 +756,7 @@ int main(int argc, FAR char *argv[])
else /* not interactive mode */
{
printf("LSM330 sensor diagnostic started in batch mode...\n");
for (ui = 0; ui < ARRAYSIZE(test_ptr_array); ui++)
for (ui = 0; ui < nitems(test_ptr_array); ui++)
{
step_rc = 0;
if (test_ptr_array[ui] != 0)
Expand Down
Loading