1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef GRAPHIC_LITE_COMMON_MACROS_H 16 #define GRAPHIC_LITE_COMMON_MACROS_H 17 18 #ifndef _WIN32 19 #define UI_WEAK_SYMBOL __attribute__((weak)) 20 #else 21 #define UI_WEAK_SYMBOL 22 #endif 23 24 #define ALIGN_UP(sz, align) (((sz) + ((align) - 1)) & (-(align))) 25 #define ALIGN_DOWN(sz, align) ((sz) & (-(align))) 26 #define ADDR_ALIGN(ptr, sz, align) \ 27 do { \ 28 uint32_t _sz = ALIGN_DOWN(sz, align); \ 29 (ptr) = decltype(ptr)((uintptr_t)(void*)(ptr) + ((sz)-_sz)); \ 30 (sz) = _sz; \ 31 } while (0) 32 #define STRUCT_ALIGN(sz) alignas(sz) 33 34 #ifdef ALIGNMENT_BYTES 35 #if (ALIGNMENT_BYTES != 0) && ((ALIGNMENT_BYTES & (ALIGNMENT_BYTES - 1)) != 0) 36 #error ALIGNMENT_BYTES should be power of 2. 37 #endif 38 #define UI_ALIGN_UP(size) ALIGN_UP((size), ALIGNMENT_BYTES) 39 #define UI_ALIGN_DOWN(size) ALIGN_DOWN((size), ALIGNMENT_BYTES) 40 #define UI_ADDR_ALIGN(ptr, sz) ADDR_ALIGN(ptr, sz, ALIGNMENT_BYTES) 41 #define UI_STRUCT_ALIGN STRUCT_ALIGN(ALIGNMENT_BYTES) 42 #else 43 #define UI_ALIGN_UP(size) ALIGN_UP((size), 4U) 44 #define UI_ALIGN_DOWN(size) ALIGN_DOWN((size), 4U) 45 #define UI_ADDR_ALIGN(ptr, sz) 46 #define UI_STRUCT_ALIGN 47 #endif 48 49 #endif 50