util: Gather some common macros

This gathers macros that have been included across components into util so
that the include chain can be more vertical.  In particular, this makes
util stand on its own without any dependence whatsoever on the rest of
mesa.

Signed-off-by: "Jason Ekstrand" <jason.ekstrand@intel.com>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
This commit is contained in:
Jason Ekstrand
2014-07-23 14:58:52 -07:00
parent 72e55bb688
commit efa0aa8ffc
23 changed files with 154 additions and 106 deletions

View File

@@ -1,5 +1,6 @@
GALLIUM_CFLAGS = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/gallium/include \
-I$(top_srcdir)/src/gallium/auxiliary \
$(DEFINES)

View File

@@ -4,7 +4,7 @@ include Makefile.sources
include $(top_srcdir)/src/gallium/Automake.inc
AM_CFLAGS = \
-I$(top_srcdir)/src/util \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/mesa/program \
-I$(top_srcdir)/src/mesa \
-I$(top_srcdir)/src/glsl \

View File

@@ -67,5 +67,4 @@ COMPILER_TESTS_SOURCES := \
compiler/tests/unit_test.c
HELPER_SOURCES := \
ralloc.c \
register_allocate.c

View File

@@ -33,7 +33,7 @@
#include "main/glheader.h"
#include "program/register_allocate.h"
#include "util/u_memory.h"
#include "ralloc.h"
#include "util/ralloc.h"
#include "r300_fragprog_swizzle.h"
#include "radeon_compiler.h"

View File

@@ -1 +0,0 @@
../../../util/ralloc.c

View File

@@ -11,6 +11,7 @@ env.PkgUseModules(['DRM'])
#env.Append(CPPDEFINES = [('__NOT_HAVE_DRM_H', '1')])
env.Append(CPPPATH = [
'#/src',
'#/src/mapi',
'#/src/mesa',
'#/src/gallium/state_trackers/dri/common',

View File

@@ -6,6 +6,7 @@ Import('*')
env = env.Clone()
env.Append(CPPPATH = [
'#/src',
'#/src/mapi',
'#/src/mesa',
'#/src/mesa/main',

View File

@@ -29,6 +29,7 @@ env.Prepend(LIBS = [
ws_xlib,
mesautil,
glapi,
mesautil,
mesa,
glsl,
gallium,

View File

@@ -96,6 +96,7 @@ nodist_EXTRA_pipe_r300_la_SOURCES = dummy.cpp
pipe_r300_la_LIBADD = \
$(PIPE_LIBS) \
$(top_builddir)/src/gallium/winsys/radeon/drm/libradeonwinsys.la \
$(top_builddir)/src/util/libmesautil.la \
$(top_builddir)/src/gallium/drivers/r300/libr300-helper.la \
$(top_builddir)/src/gallium/drivers/r300/libr300.la \
$(LIBDRM_LIBS) \

View File

@@ -36,6 +36,7 @@
#include "glsl_types.h"
#include "glsl_parser_extras.h"
#include "util/macros.h"
/**
* Declarations of type flyweights (glsl_type::_foo_type) and
@@ -48,7 +49,7 @@
#define STRUCT_TYPE(NAME) \
const glsl_type glsl_type::_struct_##NAME##_type = \
glsl_type(NAME##_fields, Elements(NAME##_fields), #NAME); \
glsl_type(NAME##_fields, ARRAY_SIZE(NAME##_fields), #NAME); \
const glsl_type *const glsl_type::struct_##NAME##_type = \
&glsl_type::_struct_##NAME##_type;
@@ -265,7 +266,7 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
{
struct glsl_symbol_table *symbols = state->symbols;
for (unsigned i = 0; i < Elements(builtin_type_versions); i++) {
for (unsigned i = 0; i < ARRAY_SIZE(builtin_type_versions); i++) {
const struct builtin_type_versions *const t = &builtin_type_versions[i];
if (state->is_version(t->min_gl, t->min_es)) {
add_type(symbols, t->type);
@@ -276,7 +277,7 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
* they're still present. We've removed them in 1.40+ (OpenGL 3.1+).
*/
if (!state->es_shader && state->language_version < 140) {
for (unsigned i = 0; i < Elements(deprecated_types); i++) {
for (unsigned i = 0; i < ARRAY_SIZE(deprecated_types); i++) {
add_type(symbols, deprecated_types[i]);
}
}

View File

@@ -26,6 +26,7 @@ noinst_LTLIBRARIES = libloader.la
libloader_la_CPPFLAGS = \
$(DEFINES) \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src \
$(VISIBILITY_CFLAGS) \
$(LIBUDEV_CFLAGS)

View File

@@ -10,6 +10,7 @@ drienv.Replace(CPPPATH = [
xmlpool_options.dir.dir, # Dir to generated xmlpool/options.h
'#include',
'#include/GL/internal',
'#src',
'#src/mapi',
'#src/gallium/include',
'#src/gallium/auxiliary',

View File

@@ -23,6 +23,7 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/mapi \
-I$(top_builddir)/src/mapi \
-I$(top_srcdir)/src/mesa/ \

View File

@@ -3,6 +3,7 @@ Import('*')
env = env.Clone()
env.Prepend(CPPPATH = [
'#src',
'#src/mapi',
'#src/mesa',
Dir('../../../mapi'), # src/mapi build path for python-generated GL API files/headers

View File

@@ -3,6 +3,7 @@ Import('*')
env = env.Clone()
env.Append(CPPPATH = [
'#/src',
'#/src/mapi',
'#/src/mesa',
'#/src/mesa/main',

View File

@@ -44,6 +44,8 @@
#include <float.h>
#include <stdarg.h>
#include "util/macros.h"
#include "c99_compat.h" /* inline, __func__, etc. */
@@ -130,23 +132,6 @@ extern "C" {
#endif
/**
* __builtin_expect macros
*/
#if !defined(__GNUC__)
# define __builtin_expect(x, y) (x)
#endif
#ifndef likely
# ifdef __GNUC__
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
# else
# define likely(x) (x)
# define unlikely(x) (x)
# endif
#endif
/* XXX: Use standard `__func__` instead */
#ifndef __FUNCTION__
# define __FUNCTION__ __func__
@@ -238,65 +223,16 @@ static INLINE GLuint CPU_TO_LE32(GLuint x)
#endif
/**
* Static (compile-time) assertion.
* Basically, use COND to dimension an array. If COND is false/zero the
* array size will be -1 and we'll get a compilation error.
*/
#define STATIC_ASSERT(COND) \
do { \
(void) sizeof(char [1 - 2*!(COND)]); \
} while (0)
/**
* Unreachable macro. Useful for suppressing "control reaches end of non-void
* function" warnings.
*/
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 5
#define unreachable(str) \
do { \
assert(!str); \
__builtin_unreachable(); \
} while (0)
#elif (defined(__clang__) && defined(__has_builtin))
# if __has_builtin(__builtin_unreachable)
# define unreachable(str) \
do { \
assert(!str); \
__builtin_unreachable(); \
} while (0)
# endif
#endif
#ifndef unreachable
#define unreachable(str)
#endif
/*
* A trick to suppress uninitialized variable warning without generating any
* code
*/
#define uninitialized_var(x) x = x
#if (__GNUC__ >= 3)
#define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
#else
#define PRINTFLIKE(f, a)
#endif
#ifndef NULL
#define NULL 0
#endif
/* Used to optionally mark structures with misaligned elements or size as
* packed, to trade off performance for space.
*/
#if (__GNUC__ >= 3)
#define PACKED __attribute__((__packed__))
#else
#define PACKED
#endif
/**
* LONGSTRING macro
@@ -432,30 +368,6 @@ do { \
#define Elements(x) (sizeof(x)/sizeof(*(x)))
#endif
#ifdef __cplusplus
/**
* Macro function that evaluates to true if T is a trivially
* destructible type -- that is, if its (non-virtual) destructor
* performs no action and all member variables and base classes are
* trivially destructible themselves.
*/
# if defined(__GNUC__)
# if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
# define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
# endif
# elif (defined(__clang__) && defined(__has_feature))
# if __has_feature(has_trivial_destructor)
# define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
# endif
# endif
# ifndef HAS_TRIVIAL_DESTRUCTOR
/* It's always safe (if inefficient) to assume that a
* destructor is non-trivial.
*/
# define HAS_TRIVIAL_DESTRUCTOR(T) (false)
# endif
#endif
#ifdef __cplusplus
}
#endif

View File

@@ -818,7 +818,9 @@ DIFFERENT_SIGNS(GLfloat x, GLfloat y)
#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
/* Compute the size of an array */
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
/* Stringify */
#define STRINGIFY(x) #x

View File

@@ -28,8 +28,6 @@ noinst_LTLIBRARIES = libmesautil.la
libmesautil_la_CPPFLAGS = \
$(DEFINES) \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src/mapi \
-I$(top_srcdir)/src/mesa \
$(VISIBILITY_CFLAGS)
libmesautil_la_SOURCES = $(MESA_UTIL_FILES)

View File

@@ -8,8 +8,6 @@ env = env.Clone()
env.Prepend(CPPPATH = [
'#include',
'#src/mapi',
'#src/mesa',
'#src/util',
])

View File

@@ -44,8 +44,8 @@
#include <string.h>
#include "hash_table.h"
#include "main/macros.h"
#include "ralloc.h"
#include "macros.h"
static const uint32_t deleted_key_value;

View File

@@ -28,10 +28,10 @@
#ifndef _HASH_TABLE_H
#define _HASH_TABLE_H
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include "main/compiler.h"
#include "macros.h"
#ifdef __cplusplus
extern "C" {

128
src/util/macros.h Normal file
View File

@@ -0,0 +1,128 @@
/*
* Copyright © 2014 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef UTIL_MACROS_H
#define UTIL_MACROS_H
/* Compute the size of an array */
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#endif
/**
* __builtin_expect macros
*/
#if !defined(__GNUC__)
# define __builtin_expect(x, y) (x)
#endif
#ifndef likely
# ifdef __GNUC__
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
# else
# define likely(x) (x)
# define unlikely(x) (x)
# endif
#endif
/**
* Static (compile-time) assertion.
* Basically, use COND to dimension an array. If COND is false/zero the
* array size will be -1 and we'll get a compilation error.
*/
#define STATIC_ASSERT(COND) \
do { \
(void) sizeof(char [1 - 2*!(COND)]); \
} while (0)
/**
* Unreachable macro. Useful for suppressing "control reaches end of non-void
* function" warnings.
*/
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 5
#define unreachable(str) \
do { \
assert(!str); \
__builtin_unreachable(); \
} while (0)
#elif (defined(__clang__) && defined(__has_builtin))
# if __has_builtin(__builtin_unreachable)
# define unreachable(str) \
do { \
assert(!str); \
__builtin_unreachable(); \
} while (0)
# endif
#endif
#ifndef unreachable
#define unreachable(str)
#endif
#if (__GNUC__ >= 3)
#define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
#else
#define PRINTFLIKE(f, a)
#endif
/* Used to optionally mark structures with misaligned elements or size as
* packed, to trade off performance for space.
*/
#if (__GNUC__ >= 3)
#define PACKED __attribute__((__packed__))
#else
#define PACKED
#endif
#ifdef __cplusplus
/**
* Macro function that evaluates to true if T is a trivially
* destructible type -- that is, if its (non-virtual) destructor
* performs no action and all member variables and base classes are
* trivially destructible themselves.
*/
# if defined(__GNUC__)
# if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
# define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
# endif
# elif (defined(__clang__) && defined(__has_feature))
# if __has_feature(has_trivial_destructor)
# define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
# endif
# endif
# ifndef HAS_TRIVIAL_DESTRUCTOR
/* It's always safe (if inefficient) to assume that a
* destructor is non-trivial.
*/
# define HAS_TRIVIAL_DESTRUCTOR(T) (false)
# endif
#endif
#endif /* UTIL_MACROS_H */

View File

@@ -53,7 +53,8 @@ extern "C" {
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include "main/compiler.h"
#include "macros.h"
/**
* \def ralloc(ctx, type)