00001 #include "slate.hpp"
00002
00003 void error(const char* str) {
00004 fprintf(stderr, "%s", str);
00005 assert(0);
00006 }
00007
00008 void fill_bytes_with(byte_t* dst, word_t n, byte_t value) {
00009 #ifdef SLATE_NO_MEM_PRIMS
00010 while (n > 0)
00011 {
00012 *dst = value;
00013 dst++;
00014 n--;
00015 }
00016 #else
00017 memset(dst, value, n);
00018 #endif
00019 }
00020
00021 void fill_words_with(word_t* dst, word_t n, word_t value) {
00022 #ifdef SLATE_NO_MEM_PRIMS
00023 while (n > 0)
00024 {
00025 *dst = value;
00026 dst++;
00027 n--;
00028 }
00029 #else
00030
00031 while (n > 0)
00032 {
00033 *dst = value;
00034 dst++;
00035 n--;
00036 }
00037 #endif
00038 }
00039
00040 void copy_words_into(void * src, word_t n, void * dst) {
00041 #ifdef SLATE_NO_MEM_PRIMS
00042 if ((src < dst) && ((src + n) > dst))
00043 {
00044 dst = dst + n;
00045 src = src + n;
00046
00047 {
00048 do
00049 {
00050 dst = dst - 1;
00051 src = src - 1;
00052 *dst = *src;
00053 n = n - 1;
00054 }
00055 while (n > 0);
00056 }
00057 }
00058 else
00059 while (n > 0)
00060 {
00061 *dst = *src;
00062 dst = dst + 1;
00063 src = src + 1;
00064 n = n - 1;
00065 }
00066 #else
00067 memcpy(dst, src, n*sizeof(word_t));
00068 #endif
00069 }
00070
00071 void copy_bytes_into(byte_t * src, word_t n, byte_t * dst) {
00072 #ifdef SLATE_NO_MEM_PRIMS
00073 if ((src < dst) && ((src + n) > dst))
00074 {
00075 dst = dst + n;
00076 src = src + n;
00077
00078 {
00079 do
00080 {
00081 dst = dst - 1;
00082 src = src - 1;
00083 *dst = *src;
00084 n = n - 1;
00085 }
00086 while (n > 0);
00087 }
00088 }
00089 else
00090 while (n > 0)
00091 {
00092 *dst = *src;
00093 dst = dst + 1;
00094 src = src + 1;
00095 n = n - 1;
00096 }
00097 #else
00098 memcpy(dst, src, n);
00099 #endif
00100 }
00101
00102
00103 #ifdef WIN32
00104
00105 int uname(struct utsname *un) {
00106 DWORD dwVersion = 0;
00107 DWORD dwMajorVersion = 0;
00108 DWORD dwMinorVersion = 0;
00109 DWORD dwBuild = 0;
00110 #ifdef WIN32
00111 #ifdef WIN64
00112 strcpy(un->sysname,"Win64");
00113 #else
00114 strcpy(un->sysname,"Win32");
00115 #endif
00116 #endif
00117 dwVersion = GetVersion();
00118 if (dwVersion < 0x80000000) dwBuild = (DWORD)(HIWORD(dwVersion)); else dwBuild=0;
00119 sprintf(un->release, "%d", dwBuild);
00120 sprintf(un->version, "%d %d", (DWORD)(LOBYTE(LOWORD(dwVersion))), (DWORD)(HIBYTE(LOWORD(dwVersion))));
00121 #ifdef WIN32
00122 #ifdef WIN64
00123 strcpy(un->machine,"x64");
00124 #else
00125 strcpy(un->machine,"x86");
00126 #endif
00127 #endif
00128 if(gethostname(un->nodename, 256)!=0) strcpy(un->nodename, "localhost");
00129 return 0;
00130 };
00131 #endif
00132
00133 #ifdef WIN32
00134 int getpid() {
00135 return GetCurrentProcessId();
00136 }
00137 #endif
00138
00139 int64_t getTickCount() {
00140 struct timeval tv;
00141 gettimeofday(&tv, NULL);
00142 return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
00143 }
00144
00145
00146 void cache_specials(struct object_heap* heap) {
00147
00148 heap->cached.interpreter = (struct Interpreter*) get_special(heap, SPECIAL_OOP_INTERPRETER);
00149 heap->cached.true_object = (struct Object*) get_special(heap, SPECIAL_OOP_TRUE);
00150 heap->cached.false_object = (struct Object*) get_special(heap, SPECIAL_OOP_FALSE);
00151 heap->cached.nil = (struct Object*) get_special(heap, SPECIAL_OOP_NIL);
00152 heap->cached.primitive_method_window = (struct Object*) get_special(heap, SPECIAL_OOP_PRIMITIVE_METHOD_WINDOW);
00153 heap->cached.compiled_method_window = (struct Object*) get_special(heap, SPECIAL_OOP_COMPILED_METHOD_WINDOW);
00154 heap->cached.closure_method_window = (struct Object*) get_special(heap, SPECIAL_OOP_CLOSURE_WINDOW);
00155
00156 }
00157
00158 #ifndef WIN32
00159 word_t max(word_t x, word_t y) {
00160 if (x > y) return x;
00161 return y;
00162 }
00163 #endif
00164
00165
00166 word_t write_args_into(struct object_heap* oh, char* buffer, word_t limit) {
00167
00168 word_t i, iLen, totalLen;
00169
00170 totalLen = 0;
00171 for (i = 0; i < oh->argcSaved; i++) {
00172 iLen = strlen(oh->argvSaved[i]) + 1;
00173 if (totalLen + iLen >= limit) return totalLen;
00174 memcpy (buffer + totalLen, oh->argvSaved[i], iLen);
00175 totalLen += iLen;
00176 }
00177 return totalLen;
00178
00179 }
00180
00181 word_t byte_array_extract_into(struct ByteArray * fromArray, byte_t* targetBuffer, word_t bufferSize)
00182 {
00183 word_t payloadSize = payload_size((struct Object *) fromArray);
00184 if (bufferSize < payloadSize)
00185 return SLATE_ERROR_RETURN;
00186
00187 copy_bytes_into((byte_t*) fromArray -> elements, bufferSize, targetBuffer);
00188
00189 return payloadSize;
00190 }
00191
00192 word_t calculateMethodCallDepth(struct object_heap* oh) {
00193 struct Interpreter* i = oh->cached.interpreter;
00194 word_t fp = i->framePointer;
00195 word_t depth = 0;
00196 do {
00197
00198 fp = object_to_smallint(i->stack->elements[fp - FRAME_OFFSET_PREVIOUS_FRAME_POINTER]);
00199 if (fp < FUNCTION_FRAME_SIZE) break;
00200 depth++;
00201 } while (fp >= FUNCTION_FRAME_SIZE);
00202
00203 return depth;
00204 }
00205
00206
00207 word_t extractCString(struct ByteArray * array, byte_t* buffer, word_t bufferSize) {
00208 word_t arrayLength = byte_array_extract_into(array, (byte_t*)buffer, bufferSize - 1);
00209
00210 if (arrayLength < 0)
00211 return SLATE_ERROR_RETURN;
00212
00213 buffer [arrayLength] = '\0';
00214 return arrayLength;
00215 }
00216
00217 #ifndef WIN32
00218
00219
00220 int fork2()
00221 {
00222 pid_t pid;
00223 int status;
00224
00225 if (!(pid = fork()))
00226 {
00227 switch (fork())
00228 {
00229 case 0: return 0;
00230 case -1: _exit(errno);
00231 default: _exit(0);
00232 }
00233 }
00234
00235 if (pid < 0 || waitpid(pid,&status,0) < 0)
00236 return -1;
00237
00238 if (WIFEXITED(status))
00239 if (WEXITSTATUS(status) == 0)
00240 return 1;
00241 else
00242 errno = WEXITSTATUS(status);
00243 else
00244 errno = EINTR;
00245
00246 return -1;
00247 }
00248 #endif
00249