hqlstack.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "hqlstack.hpp"
  17. FuncCallStack::FuncCallStack() {
  18. sp = 0;
  19. tos = DEFAULTSTACKSIZE;
  20. stackbuf = (char *)malloc(tos);
  21. numToFree = 0;
  22. #ifdef __64BIT__
  23. numFpRegs = 0;
  24. for (unsigned i=0;i<MAXFPREGS;i++)
  25. fpRegs[i] = 0.0;
  26. #endif
  27. }
  28. FuncCallStack::FuncCallStack(int size) {
  29. if(size < DEFAULTSTACKSIZE)
  30. size = DEFAULTSTACKSIZE;
  31. sp = 0;
  32. tos = size;
  33. stackbuf = (char*) malloc(tos);
  34. #ifdef __64BIT__
  35. numFpRegs = 0;
  36. for (unsigned i=0;i<MAXFPREGS;i++)
  37. fpRegs[i] = 0.0;
  38. #endif
  39. }
  40. FuncCallStack::~FuncCallStack() {
  41. if(stackbuf) {
  42. free(stackbuf);
  43. }
  44. // Free memory used by string/data parameters
  45. for(int i = 0; i < numToFree; i++) {
  46. if(toFree[i]) {
  47. free(toFree[i]);
  48. }
  49. }
  50. }
  51. unsigned FuncCallStack::getSp(){
  52. return sp;
  53. }
  54. char* FuncCallStack::getMem() {
  55. return stackbuf;
  56. }
  57. int FuncCallStack::push(unsigned len, const void * data)
  58. {
  59. int incsize = len;
  60. int inclen = align(incsize);
  61. assure(inclen);
  62. memcpy(stackbuf + sp, data, incsize);
  63. memset(stackbuf+sp+incsize, 0, inclen - incsize);
  64. sp += inclen;
  65. return sp;
  66. }
  67. int FuncCallStack::push(ITypeInfo* argType, IValue* paramValue)
  68. {
  69. unsigned len = 0;
  70. char* str;
  71. int incsize;
  72. int inclen;
  73. Owned<IValue> castParam = paramValue->castTo(argType);
  74. if(!castParam) {
  75. PrintLog("Failed to cast paramValue to argType in FuncCallStack::push");
  76. return -1;
  77. }
  78. switch (argType->getTypeCode())
  79. {
  80. case type_string:
  81. case type_data:
  82. getStringFromIValue(len, str, castParam);
  83. // For STRINGn, len doesn't need to be passed in.
  84. if(argType->getSize() == UNKNOWN_LENGTH) {
  85. push(sizeof(unsigned), &len);
  86. }
  87. push(sizeof(char *), &str);
  88. if(numToFree < MAXARGS) {
  89. toFree[numToFree++] = str;
  90. }
  91. break;
  92. case type_varstring:
  93. getStringFromIValue(len, str, castParam);
  94. push(sizeof(char *), &str);
  95. if(numToFree < MAXARGS) {
  96. toFree[numToFree++] = str;
  97. }
  98. break;
  99. case type_qstring:
  100. case type_unicode:
  101. case type_utf8:
  102. {
  103. unsigned argSize = castParam->getSize();
  104. const void * text = castParam->queryValue();
  105. str = (char *)malloc(argSize);
  106. memcpy(str, text, argSize);
  107. // For STRINGn, len doens't need to be passed in.
  108. if(argType->getSize() == UNKNOWN_LENGTH)
  109. {
  110. len = castParam->queryType()->getStringLen();
  111. push(sizeof(unsigned), &len);
  112. }
  113. push(sizeof(char *), &str);
  114. if(numToFree < MAXARGS) {
  115. toFree[numToFree++] = str;
  116. }
  117. }
  118. break;
  119. case type_varunicode:
  120. UNIMPLEMENTED;
  121. case type_real:
  122. #ifdef __64BIT__
  123. if (numFpRegs==MAXFPREGS) {
  124. PrintLog("Too many floating point registers needed in FuncCallStack::push");
  125. return -1;
  126. }
  127. char tempbuf[MMXREGSIZE];
  128. castParam->toMem(tempbuf);
  129. if (argType->getSize()<=4)
  130. fpRegs[numFpRegs++] = *(float *)&tempbuf;
  131. else
  132. fpRegs[numFpRegs++] = *(double *)&tempbuf;
  133. break;
  134. #else
  135. // fall through
  136. #endif
  137. case type_boolean:
  138. case type_int:
  139. case type_decimal:
  140. case type_date:
  141. case type_char:
  142. case type_enumerated:
  143. case type_swapint:
  144. case type_packedint:
  145. incsize = argType->getSize();
  146. inclen = align(incsize);
  147. assure(inclen);
  148. castParam->toMem(stackbuf+sp);
  149. memset(stackbuf+sp+incsize, 0, inclen - incsize);
  150. sp += inclen;
  151. break;
  152. default:
  153. //code isn't here to pass sets/datasets to external functions....
  154. return -1;
  155. }
  156. return sp;
  157. }
  158. int FuncCallStack::pushPtr(void * val)
  159. {
  160. return push(sizeof(void *), &val);
  161. }
  162. int FuncCallStack::push(char* & val) {
  163. return push(sizeof(char *), &val);
  164. }
  165. int FuncCallStack::pushRef(unsigned& val) {
  166. unsigned* valRef = &val;
  167. return push(sizeof(unsigned *), &valRef);
  168. }
  169. int FuncCallStack::pushRef(char*& val) {
  170. char** valRef = &val;
  171. return push(sizeof(char **), &valRef);
  172. }
  173. void FuncCallStack::assure(int inclen) {
  174. if(sp + inclen >= tos) {
  175. tos += INCREMENTALSIZE;
  176. stackbuf = (char *)realloc(stackbuf, tos);
  177. }
  178. }