From c30fc13033f431475540d381c8da4e28cfeb869d Mon Sep 17 00:00:00 2001 From: Dongju Chae Date: Fri, 12 Jul 2019 15:02:16 +0900 Subject: [PATCH] [N71/Errno] Remove ne-error.h and use the log utility in ne-utils.h This commit changes codes to use the log utility in ne-utils.h Signed-off-by: Dongju Chae --- core/npu-engine/src/ne-error.h | 28 ---------- core/npu-engine/src/ne-mem.c | 118 ++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 72 deletions(-) delete mode 100644 core/npu-engine/src/ne-error.h diff --git a/core/npu-engine/src/ne-error.h b/core/npu-engine/src/ne-error.h deleted file mode 100644 index d79511b..0000000 --- a/core/npu-engine/src/ne-error.h +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Proprietary - * Copyright (C) 2019 Samsung Electronics - * Copyright (C) 2019 Dongju Chae - */ -/** - * @file NE-error.h - * @date 19 June 2019 - * @brief Error codes for NPU components. - * @author Dongju Chae - * @bug No known bugs except for NYI items - */ - -#ifndef __NPU_ENGINE_ERROR_H__ -#define __NPU_ENGINE_ERROR_H__ - -#include - -#define RETURN_ERROR_MSG(errnum, msg, ...) do {\ - fprintf(stderr, "[%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__);\ - return -errnum;\ - } while (0); - -#define RETURN_ERROR(errnum) do {\ - return -errnum;\ - } while (0); - -#endif /* __NPU_ENGINE_ERROR_H__ */ diff --git a/core/npu-engine/src/ne-mem.c b/core/npu-engine/src/ne-mem.c index b8f943b..0a8204b 100644 --- a/core/npu-engine/src/ne-mem.c +++ b/core/npu-engine/src/ne-mem.c @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -22,6 +21,7 @@ #include #include #include +#include #include #include @@ -36,6 +36,11 @@ #define MEM_WAIT() pthread_cond_wait (&mpriv.cond, &mpriv.mutex); /** + * @brief log tag for N71 + */ +#define MEM_TAG _N71 + +/** * @brief memory chunk state * @note here, 'modified' state means all kinds of behaviors (e.g., compaction, resize) * which can change the offset and size of used chunks @@ -69,7 +74,7 @@ typedef enum { } hwmem_type; /** - * @brief private data structure for hwmem + * @brief private data structure for buffer */ typedef struct { chunk *chunk; /**< associated chunk */ @@ -82,8 +87,6 @@ typedef struct { typedef struct { hwmem *hwmem; /**< inherit hwmem */ buffer_state state; /**< buffer state */ - pthread_mutex_t mutex; /**< mutex for locking */ - pthread_cond_t cond; /**< cond for broadcasting */ } buffer_priv; /** @@ -179,7 +182,7 @@ chunk_set_state (chunk *target, chunk_state state, bool sync) else goto out_unlock; } - + target->state = state; err = 0; @@ -751,7 +754,7 @@ buffer_change_state (buffer_priv *priv, buffer_state state) } if (!state_changed) - RETURN_ERROR (EINVAL); + return -EINVAL; priv->state = state; @@ -780,11 +783,15 @@ mem_init (uint64_t size_in, uint64_t *size_out) uint32_t handle; int fd; - if (size_in % MEM_BASE_SIZE != 0 || size_in > UINT32_MAX) - RETURN_ERROR_MSG (EINVAL, "invalid memory size"); + if (size_in % MEM_BASE_SIZE != 0 || size_in > UINT32_MAX) { + logerr(MEM_TAG, "Invalid memory size: 0x%lx\n", size_in); + return -EINVAL; + } - if ((fd = gem_open (GEM_NAME)) <= 0) - RETURN_ERROR_MSG (EACCES, "fail to open a GEM driver"); + if ((fd = gem_open (GEM_NAME)) <= 0) { + logerr(MEM_TAG, "Fail to open a GEM driver\n"); + return -EACCES; + } do { /** @@ -803,7 +810,8 @@ mem_init (uint64_t size_in, uint64_t *size_out) if (handle == 0) { close (fd); - RETURN_ERROR_MSG (ENOMEM, "no available memory"); + logerr (MEM_TAG, "no available memory\n"); + return -ENOMEM; } /* setup private data */ @@ -850,9 +858,11 @@ mem_fini (void) chunk *chunk, *tmp; int idx; - if (!(mpriv.fd > 0 && mpriv.dmabuf > 0)) - RETURN_ERROR_MSG (EBADF, "bad file descriptors for memory allocator" - "(maybe not initialized)"); + if (!(mpriv.fd > 0 && mpriv.dmabuf > 0)) { + logerr(MEM_TAG, "bad file descriptors for memory allocator" + "(maybe not initialized)\n"); + return -EBADF; + } /* destroy buffers */ for (idx = 0; idx < MEM_NUM_BUFFERS; idx++) { @@ -901,8 +911,10 @@ mem_alloc (uint64_t size, hwmem **hwmem_p) { hwmem *new_hwmem; - if (size == 0 || !hwmem_p) - RETURN_ERROR_MSG (EINVAL, "invalid arguments"); + if (size == 0 || !hwmem_p) { + logerr (MEM_TAG, "invalid arguments\n"); + return -EINVAL; + } MEM_LOCK(); @@ -918,7 +930,7 @@ mem_alloc (uint64_t size, hwmem **hwmem_p) *hwmem_p = new_hwmem; return 0; } else - RETURN_ERROR (ENOMEM); + return -ENOMEM; } /** @@ -926,14 +938,15 @@ mem_alloc (uint64_t size, hwmem **hwmem_p) * @param[in] hwmem the created hwmem instance * @param[in] size the new memory size * @return 0 if no error, otherwise a negative error value - * * @note its latency can be increased due to the memory compaction. */ static int mem_realloc (hwmem * hwmem, uint64_t size) { - if (!hwmem || size == 0) - RETURN_ERROR_MSG (EINVAL, "invalid arguments"); + if (!hwmem || size == 0) { + logerr (MEM_TAG, "invalid arguments\n"); + return -EINVAL; + } MEM_LOCK(); @@ -952,8 +965,10 @@ mem_realloc (hwmem * hwmem, uint64_t size) static int mem_dealloc (hwmem * hwmem) { - if (!hwmem) - RETURN_ERROR_MSG (EINVAL, "invalid arguments"); + if (!hwmem) { + logerr (MEM_TAG, "invalid arguments\n"); + return -EINVAL; + } MEM_LOCK(); @@ -971,7 +986,7 @@ mem_dealloc (hwmem * hwmem) * * @note its latency can be increased due to the memory compaction. */ - static int +static int mem_resize_buffers (uint64_t size) { int err = 0; @@ -993,8 +1008,10 @@ mem_resize_buffers (uint64_t size) mpriv.buffer[idx]->size = size; if (mpriv.buffer_size == 0) { /* it's the first resize; we need to initialize buffers */ - mpriv.buffer_idx[idx] = BUFFER_STATE_INVAL; - buffer_change_state (mpriv.buffer[idx]->priv, BUFFER_STATE_EMPTY); + buffer_priv *priv = mpriv.buffer[idx]->priv; + + mpriv.buffer_head = 0; + buffer_change_state (priv, BUFFER_STATE_EMPTY); } } @@ -1004,7 +1021,7 @@ mem_resize_buffers (uint64_t size) out: MEM_UNLOCK (); - RETURN_ERROR (err); + return -err; } /** @@ -1117,7 +1134,7 @@ out: /** * @brief return the buffer for next requesters - * @param[in] buffer the buffer instance + * @param[in] buf the buffer instance * @return 0 if no error, otherwise negative values * * @note this should be called after finishing some processing with this buffer. @@ -1155,7 +1172,7 @@ mem_return_buffer (buffer *buf) if (err == 0) hwmem_deactivate (priv->hwmem); - RETURN_ERROR (err); + return -err; } /** @@ -1208,11 +1225,15 @@ hwmem_get_offset (const hwmem *hwmem, uint64_t *offset) { hwmem_priv *priv; - if (!hwmem || !hwmem->priv) - RETURN_ERROR_MSG (EINVAL, "invalid hwmem; internal structure does not exist"); + if (!hwmem || !hwmem->priv) { + logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n"); + return -EINVAL; + } - if (!offset) - RETURN_ERROR_MSG (EINVAL, "invalid offset pointer"); + if (!offset) { + logerr (MEM_TAG, "invalid offset pointer\n"); + return -EINVAL; + } priv = hwmem->priv; assert (priv->chunk); @@ -1228,15 +1249,20 @@ hwmem_get_offset (const hwmem *hwmem, uint64_t *offset) * @param[out] ptr the data pointer * @return 0 if no error, otherwise a negative error value */ -int hwmem_get_data (const hwmem *hwmem, void **ptr) +int +hwmem_get_data (const hwmem *hwmem, void **ptr) { hwmem_priv *priv; - if (!hwmem || !hwmem->priv) - RETURN_ERROR_MSG (EINVAL, "invalid hwmem; internal structure does not exist"); + if (!hwmem || !hwmem->priv) { + logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n"); + return -EINVAL; + } - if (!ptr) - RETURN_ERROR_MSG (EINVAL, "invalid data pointer"); + if (!ptr) { + logerr (MEM_TAG, "invalid data pointer\n"); + return -EINVAL; + } priv = hwmem->priv; assert (priv->chunk); @@ -1257,7 +1283,7 @@ int hwmem_activate (hwmem *hwmem) { hwmem_priv *priv; - + if (!hwmem || !hwmem->priv) { logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n"); return -EINVAL; @@ -1278,7 +1304,7 @@ int hwmem_deactivate (hwmem *hwmem) { hwmem_priv *priv; - + if (!hwmem || !hwmem->priv) { logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n"); return -EINVAL; @@ -1301,11 +1327,15 @@ buffer_get_hwmem (buffer *buffer, hwmem **hwmem) { buffer_priv *priv; - if (!buffer || !buffer->priv) - RETURN_ERROR_MSG (EINVAL, "invalid hwmem; internal structure does not exist"); + if (!buffer || !buffer->priv) { + logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n"); + return -EINVAL; + } - if (!hwmem) - RETURN_ERROR_MSG (EINVAL, "invalid hwmem pointer"); + if (!hwmem) { + logerr (MEM_TAG, "invalid hwmem pointer\n"); + return -EINVAL; + } priv = buffer->priv; *hwmem = priv->hwmem; @@ -1338,5 +1368,5 @@ buffer_get_state (buffer *buffer) mem * mem_get_instance (void) { - return &mem_instance; + return &mem_instance; } -- 2.7.4