/** * @file mmpa_linux.h * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. * 内部基础模块,所有参数合法性由调用者保证 */ #ifndef MMPA_LINUX_MMPA_LINUX_H #define MMPA_LINUX_MMPA_LINUX_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __cplusplus #if __cplusplus extern "C" { #endif // __cpluscplus #endif // __cpluscplus #define EN_OK 0 #define EN_ERROR (-1) #define EN_INVALID_PARAM (-2) #define MMPA_MAX_PATH PATH_MAX #define MM_R_OK R_OK /* Test for read permission. */ #define MM_W_OK W_OK /* Test for write permission. */ #define MM_X_OK X_OK /* Test for execute permission. */ #define MM_F_OK F_OK /* Test for existence. */ typedef FILE mmFileHandle; #define MM_SEEK_FILE_BEGIN SEEK_SET #define MM_SEEK_CUR_POS SEEK_CUR #define MM_SEEK_FILE_END SEEK_END #define MM_TASK_ID_INVALID 0 typedef enum { FILE_READ = 0, FILE_READ_BIN, FILE_MODE_BUTT } MM_FILE_MODE; typedef pthread_mutex_t mmMutex_t; typedef uint32_t mmAtomicType; typedef uint64_t mmAtomicType64; static inline uint32_t mmSetData(mmAtomicType *ptr, uint32_t value) { return __sync_lock_test_and_set(ptr, value); } static inline uint64_t mmSetData64(mmAtomicType64 *ptr, uint64_t value) { return __sync_lock_test_and_set(ptr, value); } static inline uint32_t mmValueInc(mmAtomicType *ptr, uint32_t value) { return __sync_fetch_and_add(ptr, value); } static inline bool mmCompareAndSwap(mmAtomicType *ptr, uint32_t oldval, uint32_t newval) { return __sync_bool_compare_and_swap(ptr, oldval, newval); } static inline bool mmCompareAndSwap64(mmAtomicType64 *ptr, uint64_t oldval, uint64_t newval) { return __sync_bool_compare_and_swap(ptr, oldval, newval); } static inline void mmValueStore(mmAtomicType *ptr, uint32_t value) { __atomic_store(ptr, &value, __ATOMIC_SEQ_CST); } static inline int32_t mmMutexInit(mmMutex_t *mutex) { return pthread_mutex_init(mutex, NULL); } static inline int32_t mmMutexLock(mmMutex_t *mutex) { return pthread_mutex_lock(mutex); } static inline int32_t mmMutexUnLock(mmMutex_t *mutex) { return pthread_mutex_unlock(mutex); } static inline int32_t mmMutexDestroy(mmMutex_t *mutex) { return pthread_mutex_destroy(mutex); } static inline void mmSchedYield(void) { (void)sched_yield(); } static inline uint64_t mmGetTaskId(void) { return (uint64_t)(syscall(SYS_gettid)); } static inline size_t mmReadFile(void *ptr, int32_t size, int32_t nitems, mmFileHandle *fd) { return fread(ptr, (size_t)size, (size_t)nitems, fd); } static inline mmFileHandle *mmOpenFile(const char *fileName, int32_t mode) { mmFileHandle *fd = NULL; if (mode == FILE_READ) { fd = fopen(fileName, "r"); } if (mode == FILE_READ_BIN) { fd = fopen(fileName, "rb"); } return fd; } static inline int32_t mmCloseFile(mmFileHandle *fd) { return fclose(fd); } static inline int32_t mmRealPath(const char *path, char *realPath, int32_t realPathLen) { (void)realPathLen; return realpath(path, realPath) == NULL ? EN_ERROR : EN_OK; } static inline int32_t mmAccess(const char *pathName, int32_t mode) { return access(pathName, mode); } static inline int32_t mmSeekFile(mmFileHandle *fd, int64_t offset, int32_t seekFlag) { return fseek(fd, (long)offset, seekFlag); } static inline long mmTellFile(mmFileHandle *fd) { return ftell(fd); } static inline void *mmMalloc(unsigned long long size) { return malloc(size); } static inline void mmFree(void *ptr) { free(ptr); } #ifdef __cplusplus #if __cplusplus } #endif /* __cpluscplus */ #endif // __cpluscplus #endif // MMPA_LINUX_MMPA_LINUX_H_