standard.h 2.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 
 * Copyright (c) 2012 Christoph Mueller <ruunhb@googlemail.com>
 * 
 * Crystal is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Crystal 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
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
18
#pragma once
19

20 21 22 23 24
/** 
 * @file standard.h 
 * Sets general operations, definitions and typdefs used by crystal modules (header, implementation)
 */

25
#include <stdlib.h>
26
#include <stdint.h>
27

28
/** typedef to hide void* pointers for clearer interfaces */
29
typedef void* pointer;
30 31

/** typedef to hide void* const_pointers for clearer interfaces */
32
typedef const void* const_pointer;
33

34
/** Smaller identifier for uint8_t */
35 36 37 38
typedef uint8_t byte_t;

/** Two bytes are a word */
typedef uint16_t word_t;
39 40

/** Unicode character type */
41 42 43 44
typedef uint32_t unicode_t;

#define FALSE 0
#define TRUE 1
45

46 47 48 49
/** 
 * Crystal's malloc macro for allocating memory based on a type
 * @param Type Set directly the type for this memory block (dont use sizeof)
 */
50
#define cry_malloc(Type) malloc(sizeof(Type))
51 52 53 54 55 56

/**
 * Crystal's calloc macro for allocating memory based on a type
 * @param Type Set directly the type for this memory block (dont use sizeof)
 * @param num Set the number of elements (of type Type)
 */
57
#define cry_calloc(Type, num) calloc(num, sizeof(Type))
58 59 60 61 62 63 64

/** 
 * Crystal's realloc macro for allocating memory based on a type
 * @param Type Set directly the type for this memory block (dont use sizeof)
 * @param Pointer Set the Adress of the existing memory block that should be reallocated.
 * @param num Set the number of elements (of type Type)
 */
65
#define cry_realloc(Type, Pointer, num) realloc(Pointer, sizeof(Pointer) * num)
66 67

/** 
68
 * Crystal renaming for deallocating memory (equivalent to free(Pointer))
69 70
 * @param Pointer The address of the freeing memory block
 */
71
static void (*cry_free)(pointer data) = free;
72

73 74 75 76 77
/** 
 * Convenient casting operator for cleaner code similar to c++ static_cast in its appearance. 
 * @param Type The type the variable Var should be casted
 * @param Var The given variable that should be casted
 */
78 79
#define cry_cast(Type, Var) ((Type)Var)