Skip to content

Commit

Permalink
HevCompiler: Init.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed May 19, 2021
1 parent 1cf3a91 commit a3d5e3b
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/misc/hev-compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
============================================================================
Name : hev-compiler.h
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 everyone.
Description : Compiler
============================================================================
*/

#ifndef __HEV_COMPILER_H__
#define __HEV_COMPILER_H__

#ifdef __cplusplus
extern "C" {
#endif

#define ALIGN_UP(addr, align) \
((addr + (typeof (addr))align - 1) & ~((typeof (addr))align - 1))

#define ALIGN_DOWN(addr, align) ((addr) & ~((typeof (addr))align - 1))

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

#ifndef EXPORT_SYMBOL
#define EXPORT_SYMBOL __attribute__ ((visibility ("default")))
#endif

#define barrier() __asm__ __volatile__("" : : : "memory")

static inline void
__read_once_size (void *dst, const volatile void *src, int size)
{
switch (size) {
case sizeof (char):
*(char *)dst = *(volatile char *)src;
break;
case sizeof (short):
*(short *)dst = *(volatile short *)src;
break;
case sizeof (int):
*(int *)dst = *(volatile int *)src;
break;
default:
barrier ();
__builtin_memcpy ((void *)dst, (const void *)src, size);
barrier ();
}
}

static inline void
__write_once_size (volatile void *dst, const void *src, int size)
{
switch (size) {
case sizeof (char):
*(volatile char *)dst = *(char *)src;
break;
case sizeof (short):
*(volatile short *)dst = *(short *)src;
break;
case sizeof (int):
*(volatile int *)dst = *(int *)src;
break;
default:
barrier ();
__builtin_memcpy ((void *)dst, (const void *)src, size);
barrier ();
}
}

#define READ_ONCE(x) \
({ \
union \
{ \
typeof (x) __val; \
char __c[1]; \
} __u; \
__read_once_size (__u.__c, &(x), sizeof (x)); \
__u.__val; \
})

#define WRITE_ONCE(x, val) \
({ \
union \
{ \
typeof (x) __val; \
char __c[1]; \
} __u = { .__val = (typeof (x)) (val) }; \
__write_once_size (&(x), __u.__c, sizeof (x)); \
__u.__val; \
})

#ifndef container_of
#define container_of(ptr, type, member) \
({ \
void *__mptr = (void *)(ptr); \
((type *)(__mptr - offsetof (type, member))); \
})
#endif

#ifdef __cplusplus
}
#endif

#endif /* __HEV_COMPILER_H__ */

0 comments on commit a3d5e3b

Please sign in to comment.