d4dag-unloop.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright t lefering
  3. * Copyright Simon Speich 2013
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * These are the four essential freedoms with GNU GPL software:
  19. * 1: freedom to run the program, for any purpose
  20. * 2: freedom to study how the program works, and change it to make it do what you wish
  21. * 3: freedom to redistribute copies to help your Free Software friends
  22. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  23. * , ,
  24. * / \
  25. * ((__-^^-,-^^-__))
  26. * `-_---' `---_-'
  27. * `--|o` 'o|--'
  28. * \ ` /
  29. * ): :(
  30. * :o_o:
  31. * "-"
  32. *
  33. * SPDX-License-Identifier: GPL-3.0+
  34. * License-Filename: LICENSE
  35. */
  36. /**
  37. * all routines have prefix d4d_
  38. * all static routines and vars have prefix d4d___
  39. * the prefix d4d_ has no special meaning but hopefully
  40. * does not problems because of limited C namespace
  41. */
  42. /* only in debug version linkage needed to printf */
  43. #if D4D_DEBUG
  44. #include <stdio.h>
  45. #define print_debug(str, ...) printf(str" - %s:%u\n", ##__VA_ARGS__, __FILE__, __LINE__);
  46. #endif
  47. /* datatypoes used are
  48. * int, as signed 32bits integer value
  49. * unsigned int, as 32bits integer value
  50. * char, as signed 8bits integer value
  51. * unsigned char, as 8bits integer value
  52. */
  53. /**
  54. * set here version number as int
  55. */
  56. #define D4DAG_VERSION 10
  57. /* defs and prototypes of routines for user program */
  58. #include "d4dag.h"
  59. /* mallocer/freeer */
  60. typedef void *(*malloc_fn) (unsigned int n);
  61. typedef void (*free_fn) (void *ptr);
  62. /* toplevel control struct */
  63. struct d4d__maing
  64. {
  65. /* wrappers for malloc/free and all malloc's will be below 4Gb at once */
  66. malloc_fn d4d__malloc;
  67. free_fn d4d__free;
  68. };
  69. /* main control */
  70. static struct d4d__maing *d4d__main = (struct d4d__maing *) 0;
  71. /* forward decl. */
  72. static void d4d__memzero (void *ptr, unsigned int n);
  73. /**
  74. * returns version number as int
  75. */
  76. int
  77. d4d_version (void)
  78. {
  79. return (D4DAG_VERSION);
  80. }
  81. /**
  82. *
  83. * returns:
  84. * 0 if oke
  85. * -1 if missing malloc/free pointer
  86. * -2 if malloc failed
  87. * For embedded devices this source has no linkage to libraries
  88. * which means here pointer to malloc/free must be supplied.
  89. * when malloc returns zero then these routines crashes
  90. */
  91. int
  92. d4d_init (void *(*mallocer) (unsigned int n), void (*freeer) (void *ptr))
  93. {
  94. /* malloc/free pointers must be specified */
  95. {
  96. if (!(!mallocer))
  97. {
  98. goto d4dag__unloop_1;
  99. }
  100. {
  101. return (-1);
  102. }
  103. d4dag__unloop_1:;
  104. }
  105. {
  106. if (!(!freeer))
  107. {
  108. goto d4dag__unloop_2;
  109. }
  110. {
  111. return (-1);
  112. }
  113. d4dag__unloop_2:;
  114. }
  115. d4d__main =
  116. (struct d4d__maing *) (*mallocer) ((unsigned int)
  117. sizeof (struct d4d__maing));
  118. {
  119. if (!(!d4d__main))
  120. {
  121. goto d4dag__unloop_3;
  122. }
  123. {
  124. return (-2);
  125. }
  126. d4dag__unloop_3:;
  127. }
  128. /* set pointers to malloc/free in toplevel control */
  129. d4d__memzero (d4d__main, sizeof (struct d4d__maing));
  130. d4d__main->d4d__malloc = mallocer;
  131. d4d__main->d4d__free = freeer;
  132. return (0);
  133. }
  134. /**
  135. *
  136. */
  137. int
  138. d4d_deinit (void)
  139. {
  140. /* check if active */
  141. {
  142. if (!(!d4d__main))
  143. {
  144. goto d4dag__unloop_4;
  145. }
  146. {
  147. return (0);
  148. }
  149. d4dag__unloop_4:;
  150. }
  151. /* free structs */
  152. /* free control */
  153. d4d__main->d4d__free (d4d__main);
  154. d4d__main = (struct d4d__maing *) 0;
  155. return (0);
  156. }
  157. /* like memset(ptr,0,n) */
  158. static void
  159. d4d__memzero (void *ptr, unsigned int n)
  160. {
  161. unsigned char *p = (unsigned char *) 0;
  162. p = (unsigned char *) ptr;
  163. {
  164. d4dag__unloop_5:
  165. if (!(n))
  166. {
  167. goto d4dag__unloop_6;
  168. }
  169. {
  170. *p = 0;
  171. p++;
  172. n--;
  173. }
  174. goto d4dag__unloop_5;
  175. d4dag__unloop_6:;
  176. }
  177. return;
  178. }
  179. /* end. */