Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
c6a86ac7
Commit
c6a86ac7
authored
Feb 17, 2009
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finally, ARRAY_SIZE!
parent
456d1311
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
0 deletions
+134
-0
ccan/array_size/_info.c
ccan/array_size/_info.c
+41
-0
ccan/array_size/array_size.h
ccan/array_size/array_size.h
+25
-0
ccan/array_size/test/compile_fail-function-param.c
ccan/array_size/test/compile_fail-function-param.c
+21
-0
ccan/array_size/test/compile_fail.c
ccan/array_size/test/compile_fail.c
+14
-0
ccan/array_size/test/run.c
ccan/array_size/test/run.c
+33
-0
No files found.
ccan/array_size/_info.c
0 → 100644
View file @
c6a86ac7
#include <stdio.h>
#include <string.h>
#include "config.h"
/**
* array_size - routine for safely deriving the size of a visible array.
*
* This provides a simple ARRAY_SIZE() macro, which (given a good compiler)
* will also break compile if you try to use it on a pointer.
*
* This can ensure your code is robust to changes, without needing a gratuitous
* macro or constant.
*
* Example:
* #include <ccan/array_size/array_size.h>
* #include <stdlib.h>
*
* // We currently use 32 random values.
* static unsigned int vals[32];
*
* void init_values(void)
* {
* unsigned int i;
* for (i = 0; i < ARRAY_SIZE(vals); i++)
* vals[i] = random();
* }
*
* Licence: LGPL (2 or any later version)
*/
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
return
1
;
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
{
printf
(
"ccan/build_assert
\n
"
);
return
0
;
}
return
1
;
}
ccan/array_size/array_size.h
0 → 100644
View file @
c6a86ac7
#ifndef CCAN_ARRAY_SIZE_H
#define CCAN_ARRAY_SIZE_H
#include "config.h"
#include <ccan/build_assert/build_assert.h>
/**
* ARRAY_SIZE - get the number of elements in a visible array
* @arr: the array whose size you want.
*
* This does not work on pointers, or arrays declared as [], or
* function parameters. With correct compiler support, such usage
* will cause a build error (see build_assert).
*/
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
/* Two gcc extensions.
* &a[0] degrades to a pointer: a different type from an array */
#define _array_size_chk(arr) \
EXPR_BUILD_ASSERT(!__builtin_types_compatible_p(typeof(arr), \
typeof(&(arr)[0])))
#else
#define _array_size_chk(arr) 0
#endif
#endif
/* CCAN_ALIGNOF_H */
ccan/array_size/test/compile_fail-function-param.c
0 → 100644
View file @
c6a86ac7
#include <ccan/array_size/array_size.h>
#include <stdlib.h>
struct
foo
{
unsigned
int
a
,
b
;
};
int
check_parameter
(
const
struct
foo
array
[
4
]);
int
check_parameter
(
const
struct
foo
array
[
4
])
{
#ifdef FAIL
return
(
ARRAY_SIZE
(
array
)
==
4
);
#else
return
sizeof
(
array
)
==
4
*
sizeof
(
struct
foo
);
#endif
}
int
main
(
int
argc
,
char
*
argv
[])
{
return
check_parameter
(
NULL
);
}
ccan/array_size/test/compile_fail.c
0 → 100644
View file @
c6a86ac7
#include "array_size/array_size.h"
int
main
(
int
argc
,
char
*
argv
[
8
])
{
char
array
[
100
];
#ifdef FAIL
return
ARRAY_SIZE
(
argv
)
+
ARRAY_SIZE
(
array
);
#if !HAVE_TYPEOF || !HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if _array_size_chk is a noop."
#endif
#else
return
ARRAY_SIZE
(
array
);
#endif
}
ccan/array_size/test/run.c
0 → 100644
View file @
c6a86ac7
#include "array_size/array_size.h"
#include "tap/tap.h"
static
char
array1
[
1
];
static
int
array2
[
2
];
static
unsigned
long
array3
[
3
][
5
];
struct
foo
{
unsigned
int
a
,
b
;
char
string
[
100
];
};
static
struct
foo
array4
[
4
];
/* Make sure they can be used in initializers. */
static
int
array1_size
=
ARRAY_SIZE
(
array1
);
static
int
array2_size
=
ARRAY_SIZE
(
array2
);
static
int
array3_size
=
ARRAY_SIZE
(
array3
);
static
int
array4_size
=
ARRAY_SIZE
(
array4
);
int
main
(
int
argc
,
char
*
argv
[])
{
plan_tests
(
8
);
ok1
(
array1_size
==
1
);
ok1
(
array2_size
==
2
);
ok1
(
array3_size
==
3
);
ok1
(
array4_size
==
4
);
ok1
(
ARRAY_SIZE
(
array1
)
==
1
);
ok1
(
ARRAY_SIZE
(
array2
)
==
2
);
ok1
(
ARRAY_SIZE
(
array3
)
==
3
);
ok1
(
ARRAY_SIZE
(
array4
)
==
4
);
return
exit_status
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment