File: [Platon] / libco / libco / co_util.h (download)
Revision 1.2, Sun Mar 30 10:26:47 2003 UTC (21 years ago) by yenar
Changes since 1.1: +4 -2
lines
fix documentation header [co_util.h, co_debug.h]
|
/***{{{*******************************************************************
* This file is part of libco - object library for C *
* Copyright (c) 2002 *
* Peter Rockai (yenar) <yenar@host.sk> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, *
* Boston, MA 02111-1307 USA *
*******************************************************************}}}***/
/* {{{ file description */
/**
* @file co_util.h
* @brief Collection of useful macros and inline fnc's.
**/
/* }}} */
#ifndef __CO_UTIL_H__
#define __CO_UTIL_H__
/* {{{ includes */
#include <unistd.h>
#include <stdbool.h>
#include <math.h>
#include "co_exception.h"
#include "co_debug.h"
/* }}} */
/* {{{ dmalloc includes */
#ifdef USE_DMALLOC
/// Enable dmalloc function parameter checking. (mostly useless)
#define DMALLOC_FUNC_CHECK
#include <dmalloc.h>
#endif
/* }}} */
/* {{{ */ static inline void *malloc_safe (size_t size)
{
void *p;
p = malloc (size);
//if (! p) COX_THROW (nomem);
if (! p) COX_RETHROW (3); // FIXME
memset (p, 0, size);
return p;
} /* }}} */
/* {{{ min/max macros */
/// Return lesser of two values
#define min(x,y) (((x)<(y))?(x):(y))
/// Return greater of two values
#define max(x,y) (((x)>(y))?(x):(y))
/* }}} */
/* {{{ memory allocation macros */
/// Allocate new storage (for part of object with destructor).
#define new_p(type) ((type *) (malloc_safe (sizeof (type))))
/// Allocate new array storage (for part of object with destructor).
#define new_ar_p(type, n) ((type *) (malloc_safe ((sizeof (type)) * n)))
/// Allocate new storage (and push it to cleanup stack).
#define new(type) ((type *) \
((CO_DEBUG (2, "allocation macro `new' called...")), \
(cox_malloc (sizeof (type)))))
#define renew_ar(old, type, n) \
((type *) (realloc ((old), (n) * (sizeof (type)))))
/// Allocate new array storage (and push it to cleanup stack).
#define new_ar(type, n) ((type *) (cox_malloc ((n) * (sizeof(type)))))
/// Safe free, free pointer if it is not NULL.
#define sfree(a) ((a) ? free (a), 0 : 0)
/* }}} */
/* {{{ fixme macro */
/// Macro to use for marking expression as "to be fixed" (uses CO_DEBUG)
#define FIXME(...) (CO_DEBUG (0, "FIXME: %s", #__VA_ARGS__), __VA_ARGS__)
/* }}} */
/* {{{ */ static inline int zdiv(int x, int y)
/// Integer division with proper rounding
{
float tmp;
tmp = ((float)x)/((float)y);
if (tmp > floor (tmp) + 0.49999) return ceil (tmp);
return floor (tmp);
} /* }}} */
#endif /* __CO_UTIL_H__ */
Platon Group <platon@platon.org> http://platon.org/
|