#include <ne-mem.h>
#include <ne-utils.h>
-#include <ne-error.h>
#include <GEMdrvAPI.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
+#include <errno.h>
#include <assert.h>
#include <pthread.h>
#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
} hwmem_type;
/**
- * @brief private data structure for hwmem
+ * @brief private data structure for buffer
*/
typedef struct {
chunk *chunk; /**< associated chunk */
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;
/**
else
goto out_unlock;
}
-
+
target->state = state;
err = 0;
}
if (!state_changed)
- RETURN_ERROR (EINVAL);
+ return -EINVAL;
priv->state = state;
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 {
/**
if (handle == 0) {
close (fd);
- RETURN_ERROR_MSG (ENOMEM, "no available memory");
+ logerr (MEM_TAG, "no available memory\n");
+ return -ENOMEM;
}
/* setup private data */
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++) {
{
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();
*hwmem_p = new_hwmem;
return 0;
} else
- RETURN_ERROR (ENOMEM);
+ return -ENOMEM;
}
/**
* @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();
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();
*
* @note its latency can be increased due to the memory compaction.
*/
- static int
+static int
mem_resize_buffers (uint64_t size)
{
int err = 0;
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);
}
}
out:
MEM_UNLOCK ();
- RETURN_ERROR (err);
+ return -err;
}
/**
/**
* @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.
if (err == 0)
hwmem_deactivate (priv->hwmem);
- RETURN_ERROR (err);
+ return -err;
}
/**
{
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);
* @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);
hwmem_activate (hwmem *hwmem)
{
hwmem_priv *priv;
-
+
if (!hwmem || !hwmem->priv) {
logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n");
return -EINVAL;
hwmem_deactivate (hwmem *hwmem)
{
hwmem_priv *priv;
-
+
if (!hwmem || !hwmem->priv) {
logerr (MEM_TAG, "invalid hwmem; internal structure does not exist\n");
return -EINVAL;
{
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;
mem *
mem_get_instance (void)
{
- return &mem_instance;
+ return &mem_instance;
}