common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. Temelia - Common interface implementation source file.
  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. #include "include/common.h"
  18. #include "include/stack.h"
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. // Common interface implementation.
  23. DECLSPEC int temelia_errno;
  24. static FILE *fout = NULL;
  25. void *_new(int size)
  26. {
  27. void *memory_allocated;
  28. if ((memory_allocated = malloc(size)) == NULL)
  29. temelia_errno = MEMORY_ALLOCATION;
  30. #ifdef VERBOSE
  31. LOGGER("memory (%p) = malloc (%d)\n", memory_allocated, size);
  32. #endif
  33. return memory_allocated;
  34. }
  35. void *_realloc(void *old_addr, int size)
  36. {
  37. void *memory_allocated;
  38. if ((memory_allocated = realloc(old_addr, size)) == NULL)
  39. temelia_errno = MEMORY_ALLOCATION;
  40. #ifdef VERBOSE
  41. LOGGER("memory (%p) = realloc (%p, %d)\n", memory_allocated, old_addr, size);
  42. #endif
  43. return memory_allocated;
  44. }
  45. void _delete(void *memory_address)
  46. {
  47. _ASSERT(memory_address, ==, NULL, NULL_POINTER,);
  48. #ifdef VERBOSE
  49. LOGGER("free (%p)\n", memory_address);
  50. #endif
  51. free(memory_address);
  52. }
  53. int compare_pointers(void *x, void *y, void *context)
  54. {
  55. return (int) x - (int) y;
  56. }
  57. int _rand()
  58. {
  59. return rand();
  60. }
  61. void report_error(CONST char *RESTRICT _format, ...)
  62. {
  63. va_list args;
  64. va_start(args, _format);
  65. vfprintf(stderr, _format, args);
  66. va_end(args);
  67. }
  68. //#ifdef DLL
  69. //#define RLS_PRINT(__VA_ARGS__) fprintf(stdout, __VA_ARGS__)
  70. //#else
  71. #define RLS_PRINT(fmt, args...) fprintf(stdout, fmt, ##args)
  72. //#endif
  73. //#ifdef DLL
  74. //#ifdef DEBUG
  75. //#define DBG_PRINT(__VA_ARGS__) RLS_PRINT(" [DBG] " __VA_ARGS__)
  76. //#else
  77. //#define DBG_PRINT(__VA_ARGS__)
  78. //#endif
  79. //#else
  80. #ifdef DEBUG
  81. #define DBG_PRINT(fmt, args...) RLS_PRINT(" [DBG] " fmt, ##args)
  82. #else
  83. #define DBG_PRINT(fmt, args...)
  84. #endif
  85. //#endif
  86. #define INCONSISTENT_ERROR() \
  87. do\
  88. {\
  89. fprintf(stderr, "[%s : %d] INCONSISTENT ERROR!\n", __FILE__, __LINE__);\
  90. exit(1);\
  91. } while(0)
  92. void _logger_init(void *output_stream)
  93. {
  94. fprintf(output_stream, "[logger] initializing logger (%p)\n", output_stream);
  95. fout = output_stream;
  96. }
  97. void _logger_log(CONST char *RESTRICT _format, ...)
  98. {
  99. va_list args;
  100. if (fout == NULL)
  101. return;
  102. va_start(args, _format);
  103. fprintf(fout, "[logger] ");
  104. vfprintf(fout, _format, args);
  105. va_end(args);
  106. fflush(fout);
  107. }
  108. void _logger_destroy()
  109. {
  110. _ASSERT(fout, ==, NULL, NULL_POINTER,);
  111. fprintf(fout, "[logger] destroying logger\n");
  112. if (fout == stdout)
  113. fout = NULL;
  114. else
  115. fclose(fout);
  116. }
  117. void _empty_init(void *output_stream)
  118. {
  119. }
  120. void _empty_log(CONST char *RESTRICT _format, ...)
  121. {
  122. }
  123. void _empty_destroy()
  124. {
  125. }
  126. // Declared in binary_tree.c: line 29
  127. extern stack_t iterating_handlers;
  128. extern stack_t compare_stack;
  129. int temelia_inited = 0;
  130. void temelia_init()
  131. {
  132. /*
  133. * Various initializations come here
  134. */
  135. // Already inited?
  136. if (temelia_inited)
  137. return;
  138. if (!iterating_handlers)
  139. iterating_handlers = stack_new(DEFAULT_SIZE);
  140. if (!compare_stack)
  141. compare_stack = stack_new(DEFAULT_SIZE);
  142. temelia_inited = 1;
  143. }
  144. void temelia_finish()
  145. {
  146. if (iterating_handlers)
  147. {
  148. stack_delete(iterating_handlers);
  149. iterating_handlers = NULL;
  150. }
  151. if (compare_stack)
  152. {
  153. stack_delete(compare_stack);
  154. compare_stack = NULL;
  155. }
  156. }