Blame opae-libs/libopae-c/opae_int.h

Packit 534379
// Copyright(c) 2018-2020, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
Packit 534379
#ifndef __OPAE_OPAE_INT_H__
Packit 534379
#define __OPAE_OPAE_INT_H__
Packit 534379
Packit 534379
#include <stdint.h>
Packit 534379
#include <stdlib.h>
Packit 534379
#include <string.h>
Packit 534379
#include <errno.h>
Packit 534379
Packit 534379
#include <opae/types.h>
Packit 534379
#include <opae/log.h>
Packit 534379
Packit 534379
#ifndef __USE_GNU
Packit 534379
#define __USE_GNU
Packit 534379
#endif // __USE_GNU
Packit 534379
#include <pthread.h>
Packit 534379
Packit 534379
#define ASSERT_NOT_NULL_MSG_RESULT(__arg, __msg, __result) \
Packit 534379
	do {                                                   \
Packit 534379
		if (!__arg) {                                      \
Packit 534379
			OPAE_ERR(__msg);                               \
Packit 534379
			return __result;                               \
Packit 534379
		}                                                  \
Packit 534379
	} while (0)
Packit 534379
Packit 534379
/*
Packit 534379
 * Check if argument is NULL and return FPGA_INVALID_PARAM and a message
Packit 534379
 */
Packit 534379
#define ASSERT_NOT_NULL_MSG(__arg, __msg) \
Packit 534379
	ASSERT_NOT_NULL_MSG_RESULT(__arg, __msg, FPGA_INVALID_PARAM)
Packit 534379
Packit 534379
#define ASSERT_NOT_NULL(__arg) ASSERT_NOT_NULL_MSG(__arg, #__arg " is NULL")
Packit 534379
Packit 534379
#define ASSERT_NOT_NULL_RESULT(__arg, __result) \
Packit 534379
	ASSERT_NOT_NULL_MSG_RESULT(__arg, #__arg " is NULL", __result)
Packit 534379
Packit 534379
#define ASSERT_RESULT(__result)    \
Packit 534379
	do {                           \
Packit 534379
		if ((__result) != FPGA_OK) \
Packit 534379
			return __result;       \
Packit 534379
	} while (0)
Packit 534379
Packit 534379
Packit 534379
#define UNUSED_PARAM(x) ((void)x)
Packit 534379
Packit 534379
Packit 534379
#define opae_mutex_lock(__res, __mtx_ptr)             \
Packit 534379
	({                                                \
Packit 534379
		(__res) = pthread_mutex_lock(__mtx_ptr);      \
Packit 534379
		if (__res)                                    \
Packit 534379
			OPAE_ERR("pthread_mutex_lock failed: %s", \
Packit 534379
				 strerror(errno));                    \
Packit 534379
		__res;                                        \
Packit 534379
	})
Packit 534379
Packit 534379
#define opae_mutex_unlock(__res, __mtx_ptr)             \
Packit 534379
	({                                                  \
Packit 534379
		(__res) = pthread_mutex_unlock(__mtx_ptr);      \
Packit 534379
		if (__res)                                      \
Packit 534379
			OPAE_ERR("pthread_mutex_unlock failed: %s", \
Packit 534379
				 strerror(errno));                      \
Packit 534379
		__res;                                          \
Packit 534379
	})
Packit 534379
Packit 534379
Packit 534379
#ifndef __OPAE_ADAPTER_H__
Packit 534379
typedef struct _opae_api_adapter_table opae_api_adapter_table;
Packit 534379
#endif // __OPAE_ADAPTER_H__
Packit 534379
Packit 534379
//                                  k o t w
Packit 534379
#define OPAE_WRAPPED_TOKEN_MAGIC 0x6b6f7477
Packit 534379
Packit 534379
typedef struct _opae_wrapped_token {
Packit 534379
	uint32_t magic;
Packit 534379
	fpga_token opae_token;
Packit 534379
	opae_api_adapter_table *adapter_table;
Packit 534379
} opae_wrapped_token;
Packit 534379
Packit 534379
opae_wrapped_token *
Packit 534379
opae_allocate_wrapped_token(fpga_token token,
Packit 534379
			    const opae_api_adapter_table *adapter);
Packit 534379
Packit 534379
static inline opae_wrapped_token *opae_validate_wrapped_token(fpga_token t)
Packit 534379
{
Packit 534379
	opae_wrapped_token *wt;
Packit 534379
	if (!t)
Packit 534379
		return NULL;
Packit 534379
	wt = (opae_wrapped_token *)t;
Packit 534379
	return (wt->magic == OPAE_WRAPPED_TOKEN_MAGIC) ? wt : NULL;
Packit 534379
}
Packit 534379
Packit 534379
static inline void opae_destroy_wrapped_token(opae_wrapped_token *wt)
Packit 534379
{
Packit 534379
	wt->magic = 0;
Packit 534379
	free(wt);
Packit 534379
}
Packit 534379
Packit 534379
//                                   n a h w
Packit 534379
#define OPAE_WRAPPED_HANDLE_MAGIC 0x6e616877
Packit 534379
Packit 534379
typedef struct _opae_wrapped_handle {
Packit 534379
	uint32_t magic;
Packit 534379
	opae_wrapped_token *wrapped_token;
Packit 534379
	fpga_handle opae_handle;
Packit 534379
	opae_api_adapter_table *adapter_table;
Packit 534379
} opae_wrapped_handle;
Packit 534379
Packit 534379
opae_wrapped_handle *
Packit 534379
opae_allocate_wrapped_handle(opae_wrapped_token *wt, fpga_handle opae_handle,
Packit 534379
			     opae_api_adapter_table *adapter);
Packit 534379
Packit 534379
static inline opae_wrapped_handle *opae_validate_wrapped_handle(fpga_handle h)
Packit 534379
{
Packit 534379
	opae_wrapped_handle *wh;
Packit 534379
	if (!h)
Packit 534379
		return NULL;
Packit 534379
	wh = (opae_wrapped_handle *)h;
Packit 534379
	return (wh->magic == OPAE_WRAPPED_HANDLE_MAGIC) ? wh : NULL;
Packit 534379
}
Packit 534379
Packit 534379
static inline void opae_destroy_wrapped_handle(opae_wrapped_handle *wh)
Packit 534379
{
Packit 534379
	wh->magic = 0;
Packit 534379
	free(wh);
Packit 534379
}
Packit 534379
Packit 534379
//                                         e v e w
Packit 534379
#define OPAE_WRAPPED_EVENT_HANDLE_MAGIC 0x65766577
Packit 534379
Packit 534379
#define OPAE_WRAPPED_EVENT_HANDLE_CREATED 0x00000001
Packit 534379
Packit 534379
typedef struct _opae_wrapped_event_handle {
Packit 534379
	uint32_t magic;
Packit 534379
	pthread_mutex_t lock;
Packit 534379
	uint32_t flags;
Packit 534379
	fpga_event_handle opae_event_handle;
Packit 534379
	opae_api_adapter_table *adapter_table;
Packit 534379
} opae_wrapped_event_handle;
Packit 534379
Packit 534379
opae_wrapped_event_handle *
Packit 534379
opae_allocate_wrapped_event_handle(fpga_event_handle opae_event_handle,
Packit 534379
				   opae_api_adapter_table *adapter);
Packit 534379
Packit 534379
static inline opae_wrapped_event_handle *
Packit 534379
opae_validate_wrapped_event_handle(fpga_event_handle h)
Packit 534379
{
Packit 534379
	opae_wrapped_event_handle *we;
Packit 534379
	if (!h)
Packit 534379
		return NULL;
Packit 534379
	we = (opae_wrapped_event_handle *)h;
Packit 534379
	return (we->magic == OPAE_WRAPPED_EVENT_HANDLE_MAGIC) ? we : NULL;
Packit 534379
}
Packit 534379
Packit 534379
static inline void
Packit 534379
opae_destroy_wrapped_event_handle(opae_wrapped_event_handle *we)
Packit 534379
{
Packit 534379
	int err;
Packit 534379
	opae_mutex_lock(err, &we->lock);
Packit 534379
	we->magic = 0;
Packit 534379
	opae_mutex_unlock(err, &we->lock);
Packit 534379
	if (pthread_mutex_destroy(&we->lock))
Packit 534379
		OPAE_ERR("pthread_mutex_destroy() failed");
Packit 534379
	free(we);
Packit 534379
}
Packit 534379
Packit 534379
//                                   j b o w
Packit 534379
#define OPAE_WRAPPED_OBJECT_MAGIC 0x6a626f77
Packit 534379
Packit 534379
typedef struct _opae_wrapped_object {
Packit 534379
	uint32_t magic;
Packit 534379
	fpga_object opae_object;
Packit 534379
	opae_api_adapter_table *adapter_table;
Packit 534379
} opae_wrapped_object;
Packit 534379
Packit 534379
opae_wrapped_object *
Packit 534379
opae_allocate_wrapped_object(fpga_object opae_object,
Packit 534379
			     opae_api_adapter_table *adapter);
Packit 534379
Packit 534379
static inline opae_wrapped_object *opae_validate_wrapped_object(fpga_object o)
Packit 534379
{
Packit 534379
	opae_wrapped_object *wo;
Packit 534379
	if (!o)
Packit 534379
		return NULL;
Packit 534379
	wo = (opae_wrapped_object *)o;
Packit 534379
	return (wo->magic == OPAE_WRAPPED_OBJECT_MAGIC) ? wo : NULL;
Packit 534379
}
Packit 534379
Packit 534379
static inline void opae_destroy_wrapped_object(opae_wrapped_object *wo)
Packit 534379
{
Packit 534379
	wo->magic = 0;
Packit 534379
	free(wo);
Packit 534379
}
Packit 534379
Packit 534379
#endif // ___OPAE_OPAE_INT_H__