nsjail/common.h

57 lines
1.9 KiB
C
Raw Normal View History

2015-05-15 05:44:48 +08:00
/*
nsjail - common macros
2015-05-15 05:44:48 +08:00
-----------------------------------------
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-03-11 09:45:43 +08:00
#ifndef NS_COMMON_H
#define NS_COMMON_H
2015-05-15 05:44:48 +08:00
#define ARRAYSIZE(array) (sizeof(array) / sizeof(*array))
2017-02-08 07:36:32 +08:00
#define UNUSED __attribute__((unused))
2015-05-15 05:44:48 +08:00
2017-10-09 05:00:45 +08:00
#if 0 /* Works, but needs -fblocks and libBlocksRuntime with clang */
2016-03-11 05:56:26 +08:00
/* Go-style defer implementation */
2016-03-09 05:40:29 +08:00
#define __STRMERGE(a, b) a##b
#define _STRMERGE(a, b) __STRMERGE(a, b)
2016-03-09 01:22:50 +08:00
#ifdef __clang__
2016-04-23 10:22:31 +08:00
static void __attribute__ ((unused)) __clang_cleanup_func(void (^*dfunc) (void))
2016-03-09 01:23:26 +08:00
{
2016-03-11 09:45:43 +08:00
(*dfunc) ();
2016-03-09 01:23:26 +08:00
}
#define defer \
void (^_STRMERGE(__defer_f_, __COUNTER__))(void) \
__attribute__((cleanup(__clang_cleanup_func))) __attribute__((unused)) = ^
2016-03-09 01:22:50 +08:00
#else
2016-03-09 01:23:26 +08:00
#define __block
#define _DEFER(a, count) \
auto void _STRMERGE(__defer_f_, count)(void* _defer_arg __attribute__((unused))); \
int _STRMERGE(__defer_var_, count) __attribute__((cleanup(_STRMERGE(__defer_f_, count)))) \
__attribute__((unused)); \
2017-10-09 05:00:45 +08:00
void _STRMERGE(__defer_f_, count)(void* _defer_arg __attribute__((unused)))
2016-04-23 10:22:31 +08:00
#define defer _DEFER(a, __COUNTER__)
2016-03-09 01:22:50 +08:00
#endif
2016-07-29 21:38:22 +08:00
#endif
2016-03-09 01:22:50 +08:00
#define NS_VALSTR_STRUCT(x) \
{ x, #x }
2017-10-09 05:06:40 +08:00
2017-10-09 05:00:45 +08:00
#endif /* NS_COMMON_H */