ucx/ucx/ucx.h

Mon, 04 Feb 2019 17:49:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 04 Feb 2019 17:49:50 +0100
changeset 157
0b33b9396851
child 162
18892c0a9adc
permissions
-rw-r--r--

ucx update

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Main UCX Header providing most common definitions.
 * 
 * @file   ucx.h
 * @author Mike Becker
 * @author Olaf Wintermann
 */

#ifndef UCX_H
#define	UCX_H

/** Major UCX version as integer constant. */
#define UCX_VERSION_MAJOR   2

/** Minor UCX version as integer constant. */
#define UCX_VERSION_MINOR   0

/** Version constant which ensures to increase monotonically. */
#define UCX_VERSION (((UCX_VERSION_MAJOR)<<16)|UCX_VERSION_MINOR)

#include <stdlib.h>
#include <stdint.h>

#ifdef _WIN32
#if !(defined __ssize_t_defined || defined _SSIZE_T_)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#define __ssize_t_defined
#define _SSIZE_T_
#endif /* __ssize_t_defined and _SSIZE_T */
#else /* !_WIN32 */
#include <sys/types.h>
#endif /* _WIN32 */

#ifdef	__cplusplus
extern "C" {
#endif
    

/**
 * A function pointer to a destructor function.
 * @see ucx_mempool_setdestr()
 * @see ucx_mempool_regdestr()
 */
typedef void(*ucx_destructor)(void*);

/**
 * Function pointer to a compare function.
 * 
 * The compare function shall take three arguments: the two values that shall be
 * compared and optional additional data.
 * The function shall then return -1 if the first argument is less than the
 * second argument, 1 if the first argument is greater than the second argument
 * and 0 if both arguments are equal. If the third argument is
 * <code>NULL</code>, it shall be ignored.
 */
typedef int(*cmp_func)(const void*,const void*,void*);

/**
 * Function pointer to a distance function.
 * 
 * The distance function shall take three arguments: the two values for which
 * the distance shall be computed and optional additional data.
 * The function shall then return the signed distance as integer value.
 */
typedef intmax_t(*distance_func)(const void*,const void*,void*);

/**
 * Function pointer to a copy function.
 * 
 * The copy function shall create a copy of the first argument and may use
 * additional data provided by the second argument. If the second argument is
 * <code>NULL</code>, it shall be ignored.

 * <b>Attention:</b> if pointers returned by functions of this type may be
 * passed to <code>free()</code> depends on the implementation of the
 * respective <code>copy_func</code>.
 */
typedef void*(*copy_func)(const void*,void*);

/**
 * Function pointer to a write function.
 * 
 * The signature of the write function shall be compatible to the signature
 * of standard <code>fwrite</code>, though it may use arbitrary data types for
 * source and destination.
 * 
 * The arguments shall contain (in ascending order): a pointer to the source,
 * the length of one element, the element count and a pointer to the
 * destination.
 */
typedef size_t(*write_func)(const void*, size_t, size_t, void*);

/**
 * Function pointer to a read function.
 * 
 * The signature of the read function shall be compatible to the signature
 * of standard <code>fread</code>, though it may use arbitrary data types for
 * source and destination.
 * 
 * The arguments shall contain (in ascending order): a pointer to the
 * destination, the length of one element, the element count and a pointer to
 * the source.
 */
typedef size_t(*read_func)(void*, size_t, size_t, void*);



#if __GNUC__ >= 5 || defined(__clang__)
#define UCX_MUL_BUILTIN

#if __WORDSIZE == 32
/**
 * Alias for <code>__builtin_umul_overflow</code>.
 * 
 * Performs a multiplication of size_t values and checks for overflow.
 * 
 * @param a first operand
 * @param b second operand
 * @param result a pointer to a size_t, where the result should
 * be stored
 * @return zero, if no overflow occurred and the result is correct, non-zero
 * otherwise
 */
#define ucx_szmul(a, b, result) __builtin_umul_overflow(a, b, result)
#else /* __WORDSIZE != 32 */
/**
 * Alias for <code>__builtin_umull_overflow</code>.
 * 
 * Performs a multiplication of size_t values and checks for overflow.
 * 
 * @param a first operand
 * @param b second operand
 * @param result a pointer to a size_t, where the result should
 * be stored
 * @return zero, if no overflow occurred and the result is correct, non-zero
 * otherwise
 */
#define ucx_szmul(a, b, result) __builtin_umull_overflow(a, b, result)
#endif /* __WORDSIZE */

#else /* no GNUC or clang bultin */

/**
 * Performs a multiplication of size_t values and checks for overflow.
 * 
 * This is a custom implementation in case there is no compiler builtin
 * available.
 * 
 * @param a first operand
 * @param b second operand
 * @param result a pointer to a size_t, where the result should
 * be stored
 * @return zero, if no overflow occurred and the result is correct, non-zero
 * otherwise
 */
#define ucx_szmul(a, b, result) ucx_szmul_impl(a, b, result)

int ucx_szmul_impl(size_t a, size_t b, size_t *result);

#endif

#ifdef	__cplusplus
}
#endif

#endif	/* UCX_H */

mercurial