dataArg.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * This file is part of the PikaScript project.
  3. * http://github.com/pikastech/pikascript
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon 李昂 liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include "dataArg.h"
  28. #include "dataArgs.h"
  29. #include "dataMemory.h"
  30. #include "dataString.h"
  31. #include "stdlib.h"
  32. uint16_t arg_getTotleSize(Arg* self) {
  33. return content_totleSize(self);
  34. }
  35. /**
  36. * time33 hash
  37. */
  38. Hash hash_time33(char* str) {
  39. Hash hash = 5381;
  40. while (*str) {
  41. hash += (hash << 5) + (*str++);
  42. }
  43. return (hash & 0x7FFFFFFF);
  44. }
  45. static uint8_t* content_init_hash(Hash nameHash,
  46. ArgType type,
  47. uint8_t* content,
  48. uint32_t size,
  49. uint8_t* next) {
  50. __arg* self = (__arg*)pikaMalloc(sizeof(__arg) + size);
  51. self->next = (__arg*)next;
  52. self->size = size;
  53. self->name_hash = nameHash;
  54. self->type = type;
  55. if (NULL != content) {
  56. __platform_memcpy(self->content, content, size);
  57. } else {
  58. __platform_memset(self->content, 0, size);
  59. }
  60. return (uint8_t*)self;
  61. }
  62. static uint8_t* content_init(char* name,
  63. ArgType type,
  64. uint8_t* content,
  65. uint16_t size,
  66. uint8_t* next) {
  67. Hash nameHash = hash_time33(name);
  68. return content_init_hash(nameHash, type, content, size, next);
  69. }
  70. uint16_t content_totleSize(uint8_t* self) {
  71. return ((__arg*)self)->size + sizeof(__arg);
  72. }
  73. void arg_freeContent(Arg* self) {
  74. if (NULL != self) {
  75. content_deinit(self);
  76. }
  77. }
  78. uint8_t* content_deinit(uint8_t* self) {
  79. uint16_t totleSize = content_totleSize(self);
  80. pikaFree(self, totleSize);
  81. return 0;
  82. }
  83. uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
  84. if (NULL == self) {
  85. /* malloc */
  86. return content_init("", ARG_TYPE_VOID, content, size, NULL);
  87. }
  88. /* only copy */
  89. if (content_getSize(self) == size) {
  90. __platform_memcpy(((__arg*)self)->content, content, size);
  91. return self;
  92. }
  93. /* realloc */
  94. Hash nameHash = content_getNameHash(self);
  95. ArgType type = content_getType(self);
  96. uint8_t* next = content_getNext(self);
  97. uint8_t* newContent =
  98. content_init_hash(nameHash, type, content, size, next);
  99. content_deinit(self);
  100. return newContent;
  101. }
  102. uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
  103. if (NULL == self) {
  104. return content_init_hash(nameHash, ARG_TYPE_VOID, NULL, 0, NULL);
  105. }
  106. __arg* arg = (__arg*)self;
  107. arg->name_hash = nameHash;
  108. return self;
  109. }
  110. uint8_t* content_setName(uint8_t* self, char* name) {
  111. return content_setNameHash(self, hash_time33(name));
  112. }
  113. uint8_t* content_setType(uint8_t* self, ArgType type) {
  114. if (NULL == self) {
  115. return content_init("", type, NULL, 0, NULL);
  116. }
  117. __arg* arg = (__arg*)self;
  118. arg->type = type;
  119. return self;
  120. }
  121. ArgType content_getType(uint8_t* self) {
  122. __arg* arg = (__arg*)self;
  123. return (ArgType)arg->type;
  124. }
  125. Arg* arg_newContent(Arg* self, uint32_t size) {
  126. uint8_t* newContent = content_init("", ARG_TYPE_VOID, NULL, size, NULL);
  127. arg_freeContent(self);
  128. return newContent;
  129. }
  130. Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) {
  131. return content_setContent(self, content, size);
  132. }
  133. Arg* arg_setStruct(Arg* self,
  134. char* name,
  135. void* struct_ptr,
  136. uint32_t struct_size) {
  137. if (NULL == struct_ptr) {
  138. return NULL;
  139. }
  140. Arg* struct_arg = arg_setContent(NULL, (uint8_t*)struct_ptr, struct_size);
  141. struct_arg = arg_setType(struct_arg, ARG_TYPE_STRUCT);
  142. struct_arg = arg_setName(struct_arg, name);
  143. return struct_arg;
  144. }
  145. Arg* arg_setHeapStruct(Arg* self,
  146. char* name,
  147. void* struct_ptr,
  148. uint32_t struct_size,
  149. void* struct_deinit_fun) {
  150. if (NULL == struct_ptr) {
  151. return NULL;
  152. }
  153. Arg* struct_arg =
  154. arg_setContent(NULL, (uint8_t*)&struct_deinit_fun, sizeof(void*));
  155. struct_arg = arg_append(struct_arg, (uint8_t*)struct_ptr, struct_size);
  156. struct_arg = arg_setType(struct_arg, ARG_TYPE_HEAP_STRUCT);
  157. struct_arg = arg_setName(struct_arg, name);
  158. return struct_arg;
  159. }
  160. void* arg_getHeapStructDeinitFun(Arg* self) {
  161. void* deinit_fun = NULL;
  162. __platform_memcpy(&deinit_fun, arg_getContent(self), sizeof(void*));
  163. return deinit_fun;
  164. }
  165. Arg* arg_setName(Arg* self, char* name) {
  166. return content_setName(self, name);
  167. }
  168. Arg* arg_setNameHash(Arg* self, Hash nameHash) {
  169. return content_setNameHash(self, nameHash);
  170. }
  171. Arg* arg_setType(Arg* self, ArgType type) {
  172. return content_setType(self, type);
  173. }
  174. Arg* arg_setInt(Arg* self, char* name, int64_t val) {
  175. return content_init(name, ARG_TYPE_INT, (uint8_t*)&val, sizeof(val), NULL);
  176. }
  177. Arg* arg_setNull(Arg* self) {
  178. return content_init("", ARG_TYPE_NULL, NULL, 0, NULL);
  179. }
  180. Arg* arg_setFloat(Arg* self, char* name, float val) {
  181. return content_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val),
  182. NULL);
  183. }
  184. float arg_getFloat(Arg* self) {
  185. if (NULL == arg_getContent(self)) {
  186. return -999.999;
  187. }
  188. return *(float*)(((__arg*)self)->content);
  189. }
  190. Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) {
  191. return content_init(name, type, (uint8_t*)&pointer, sizeof(uintptr_t),
  192. NULL);
  193. }
  194. Arg* arg_setStr(Arg* self, char* name, char* string) {
  195. return content_init(name, ARG_TYPE_STRING, (uint8_t*)string,
  196. strGetSize(string) + 1, NULL);
  197. }
  198. int64_t arg_getInt(Arg* self) {
  199. if (NULL == arg_getContent(self)) {
  200. return -999999;
  201. }
  202. return *(int64_t*)(((__arg*)self)->content);
  203. }
  204. void* arg_getPtr(Arg* self) {
  205. if (NULL == arg_getContent(self)) {
  206. return NULL;
  207. }
  208. return *(void**)(((__arg*)self)->content);
  209. }
  210. char* arg_getStr(Arg* self) {
  211. return (char*)arg_getContent(self);
  212. }
  213. Hash arg_getNameHash(Arg* self) {
  214. if (NULL == self) {
  215. return 999999;
  216. }
  217. return content_getNameHash(self);
  218. }
  219. ArgType arg_getType(Arg* self) {
  220. if (NULL == self) {
  221. return ARG_TYPE_NULL;
  222. }
  223. return content_getType(self);
  224. }
  225. uint16_t arg_getContentSize(Arg* self) {
  226. return content_getSize(self);
  227. }
  228. Arg* New_arg(void* voidPointer) {
  229. return NULL;
  230. }
  231. Arg* arg_copy(Arg* argToBeCopy) {
  232. if (NULL == argToBeCopy) {
  233. return NULL;
  234. }
  235. Arg* argCopied = New_arg(NULL);
  236. argCopied = arg_setContent(argCopied, arg_getContent(argToBeCopy),
  237. arg_getContentSize(argToBeCopy));
  238. argCopied = arg_setNameHash(argCopied, arg_getNameHash(argToBeCopy));
  239. argCopied = arg_setType(argCopied, arg_getType(argToBeCopy));
  240. return argCopied;
  241. }
  242. Arg* arg_append(Arg* arg_in, void* new_content, size_t new_size) {
  243. uint8_t* old_content = arg_getContent(arg_in);
  244. size_t old_size = arg_getContentSize(arg_in);
  245. /* create arg_out */
  246. Arg* arg_out = arg_setContent(NULL, NULL, old_size + new_size);
  247. arg_setType(arg_out, arg_getType(arg_in));
  248. arg_setNameHash(arg_out, arg_getNameHash(arg_in));
  249. /* copy old content */
  250. __platform_memcpy(arg_getContent(arg_out), old_content, old_size);
  251. /* copy new content */
  252. __platform_memcpy(arg_getContent(arg_out) + old_size, new_content,
  253. new_size);
  254. arg_deinit(arg_in);
  255. return arg_out;
  256. }
  257. void* arg_getHeapStruct(Arg* self) {
  258. return arg_getContent(self) + sizeof(void*);
  259. }
  260. void arg_deinit(Arg* self) {
  261. if (arg_getType(self) == ARG_TYPE_HEAP_STRUCT) {
  262. /* deinit heap strcut */
  263. StructDeinitFun struct_deinit_fun =
  264. (StructDeinitFun)arg_getHeapStructDeinitFun(self);
  265. struct_deinit_fun(arg_getHeapStruct(self));
  266. }
  267. arg_freeContent(self);
  268. }