File: array.h

package info (click to toggle)
drgn 0.0.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,892 kB
  • sloc: python: 59,081; ansic: 51,400; awk: 423; makefile: 339; sh: 113
file content (43 lines) | stat: -rw-r--r-- 903 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

/**
 * @file
 *
 * Helpers for C arrays.
 */

#ifndef DRGN_ARRAY_H
#define DRGN_ARRAY_H

#include "pp.h"
#include "util.h"

/** @cond */
#define array_for_each_impl(var, arr, unique_end)	\
	for (typeof((arr)[0]) *var = (arr),		\
	     *unique_end = var + array_size(arr);	\
	     var < unique_end; var++)
/** @endcond */

/**
 * Return the number of elements in an array.
 *
 * @hideinitializer
 */
#define array_size(arr)							\
	static_assert_expression(is_array(arr),				\
				 "not an array",			\
				 sizeof(arr) / sizeof((arr)[0]))

/**
 * Iterate over every element in an array.
 *
 * The element is declared as `element_type *var` in the scope of the loop.
 *
 * @hideinitializer
 */
#define array_for_each(var, arr)	\
	array_for_each_impl(var, arr, PP_UNIQUE(end))

#endif /* DRGN_ARRAY_H */