common.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*!
  2. Temelia - generic data structures library written in C.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.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. * Logging functions; default output stream is = stdout =
  73. * LOGGER is a simple wrapper over standard fprintf function
  74. */
  75. DECLSPEC void _temelia_init(void *output_stream);
  76. DECLSPEC void _temelia_logger(CONST char *RESTRICT _format, ...);
  77. DECLSPEC void _temelia_destroy();
  78. /*!
  79. * Empty handlers
  80. */
  81. DECLSPEC void _empty_init(void *output_stream);
  82. DECLSPEC void _empty_logger(CONST char *RESTRICT _format, ...);
  83. DECLSPEC void _empty_destroy();
  84. #define LOGGING
  85. // Logging support macros
  86. #ifdef LOGGING
  87. #define INIT _temelia_init
  88. #define LOGGER _temelia_logger
  89. #define DESTROY _temelia_destroy
  90. #else
  91. #define INIT _empty_init
  92. #define LOGGER _empty_logger
  93. #define DESTROY _empty_destroy
  94. #endif /* LOGGING */
  95. /*
  96. * @brief Assertion macro. It uses the do { ... } while(0) construct because I want
  97. * to force the user to put ";" after the macro invocation.
  98. * In RELEASE, comment the lines between do and while.
  99. */
  100. #define _ASSERT(ITEM, OPERATION, LIMIT_VALUE, ERROR_VALUE, RETURN_VALUE) \
  101. do \
  102. {\
  103. if (ITEM OPERATION LIMIT_VALUE)\
  104. {\
  105. if (ERROR_VALUE != NOT_EXCEPTION)\
  106. LOGGER("assertion failed on (file, line) (%s, %d)\n",\
  107. __FILE__, __LINE__);\
  108. temelia_errno = ERROR_VALUE;\
  109. return RETURN_VALUE;\
  110. }\
  111. } while(0)\
  112. /*
  113. * @brief Initializes special features of library:
  114. * red-black stack handling functions for nested traversal
  115. * and others.
  116. */
  117. DECLSPEC void temelia_init();
  118. DECLSPEC void temelia_finish();
  119. #endif /* COMMON_H */