common.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. Temelia - generic data structures library written in C.
  3. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef COMMON_H_
  18. #define COMMON_H_
  19. #include "platform.h"
  20. DECLSPEC extern int temelia_errno;
  21. typedef enum
  22. {
  23. NOT_EXCEPTION,
  24. MEMORY_ALLOCATION,
  25. NULL_POINTER,
  26. INVALID_INPUT,
  27. FULL,
  28. EMPTY,
  29. ELEMENT_NOT_FOUND
  30. } Exceptions;
  31. #ifndef MAX
  32. #define MAX(A,B) ((A)>(B)?(A):(B))
  33. #endif
  34. #ifndef MIN
  35. #define MIN(A,B) ((A)<(B)?(A):(B))
  36. #endif
  37. #define DEFAULT_SIZE (64)
  38. #define TEMELIA_INFINITY (1.7976931348623157e+308)
  39. /*!
  40. * @brief Allocator, returns a pointer to a memory address with size bytes allocated.
  41. * @param Memory size.
  42. * It's a wrapper over malloc function, included in CRT.
  43. */
  44. DECLSPEC INLINE void *_new(int size);
  45. /*!
  46. * @brief Reallocator, returns a pointer to a memory address with size bytes allocated;
  47. * it requires the old pointer.
  48. * @param old pointer.
  49. * @param new size.
  50. * It's a wrapper over realloc function, included in CRT.
  51. */
  52. DECLSPEC INLINE void *_realloc(void *old_addr, int size);
  53. /*!
  54. * @brief Deallocator, frees allocated memory.
  55. * @param Pointer to be freed.
  56. * It's a wrapper over free function, included in CRT.
  57. */
  58. DECLSPEC INLINE void _delete(void *memory_address);
  59. /*!
  60. * @brief Compares two pointers after their address. It's used to find memory addresses in
  61. * a data structure.
  62. * @param First memory address.
  63. * @param Second memory address.
  64. */
  65. DECLSPEC INLINE int compare_pointers(void *x, void *y, void *context);
  66. /*
  67. * @brief Random number wrapper generator over standard rand() function.
  68. */
  69. DECLSPEC INLINE int _rand();
  70. DECLSPEC void report_error(CONST char *RESTRICT _format, ...);
  71. /*!
  72. * @brief Initialization and destruction functions.
  73. */
  74. DECLSPEC void temelia_init();
  75. DECLSPEC void temelia_finish();
  76. /*!
  77. * Logging functions; default output stream is = stdout =
  78. * LOGGER is a simple wrapper over standard fprintf function
  79. */
  80. DECLSPEC void _logger_init(void *output_stream);
  81. DECLSPEC void _logger_log(CONST char *RESTRICT _format, ...);
  82. DECLSPEC void _logger_destroy();
  83. /*!
  84. * Empty handlers
  85. */
  86. DECLSPEC void _empty_init(void *output_stream);
  87. DECLSPEC void _empty_log(CONST char *RESTRICT _format, ...);
  88. DECLSPEC void _empty_destroy();
  89. #define LOGGING
  90. // Logging support macros
  91. #ifdef LOGGING
  92. #define INIT _logger_init
  93. #define LOGGER _logger_log
  94. #define DESTROY _logger_destroy
  95. #else
  96. #define INIT _empty_init
  97. #define LOGGER _empty_log
  98. #define DESTROY _empty_destroy
  99. #endif /* LOGGING */
  100. /*
  101. * @brief Assertion macro. It uses the do { ... } while(0) construct because I want
  102. * to force the user to put ";" after the macro invocation.
  103. * In RELEASE, comment the lines between do and while.
  104. */
  105. #define _ASSERT(ITEM, OPERATION, LIMIT_VALUE, ERROR_VALUE, RETURN_VALUE) \
  106. do \
  107. {\
  108. if (ITEM OPERATION LIMIT_VALUE)\
  109. {\
  110. if (ERROR_VALUE != NOT_EXCEPTION)\
  111. LOGGER("assertion failed on (file, line) (%s, %d)\n",\
  112. __FILE__, __LINE__);\
  113. temelia_errno = ERROR_VALUE;\
  114. return RETURN_VALUE;\
  115. }\
  116. } while(0)\
  117. #endif /* COMMON_H */