Jim Cownie | 33f7b24 | 2014-04-09 15:40:23 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.txt for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #if defined(LINUX) || defined(FREEBSD) |
| 12 | #include <mm_malloc.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "offload_common.h" |
| 16 | |
| 17 | // The debug routines |
| 18 | |
| 19 | #if OFFLOAD_DEBUG > 0 |
| 20 | |
| 21 | void __dump_bytes( |
| 22 | int trace_level, |
| 23 | const void *data, |
| 24 | int len |
| 25 | ) |
| 26 | { |
| 27 | if (console_enabled > trace_level) { |
| 28 | const uint8_t *arr = (const uint8_t*) data; |
| 29 | char buffer[4096]; |
| 30 | char *bufferp; |
| 31 | int count = 0; |
| 32 | |
| 33 | bufferp = buffer; |
| 34 | while (len--) { |
| 35 | sprintf(bufferp, "%02x", *arr++); |
| 36 | bufferp += 2; |
| 37 | count++; |
| 38 | if ((count&3) == 0) { |
| 39 | sprintf(bufferp, " "); |
| 40 | bufferp++; |
| 41 | } |
| 42 | if ((count&63) == 0) { |
| 43 | OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer); |
| 44 | bufferp = buffer; |
| 45 | count = 0; |
| 46 | } |
| 47 | } |
| 48 | if (count) { |
| 49 | OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | #endif // OFFLOAD_DEBUG |
| 54 | |
| 55 | // The Marshaller and associated routines |
| 56 | |
| 57 | void Marshaller::send_data( |
| 58 | const void *data, |
| 59 | int64_t length |
| 60 | ) |
| 61 | { |
| 62 | OFFLOAD_DEBUG_TRACE(2, "send_data(%p, %lld)\n", |
| 63 | data, length); |
| 64 | memcpy(buffer_ptr, data, (size_t)length); |
| 65 | buffer_ptr += length; |
| 66 | tfr_size += length; |
| 67 | } |
| 68 | |
| 69 | void Marshaller::receive_data( |
| 70 | void *data, |
| 71 | int64_t length |
| 72 | ) |
| 73 | { |
| 74 | OFFLOAD_DEBUG_TRACE(2, "receive_data(%p, %lld)\n", |
| 75 | data, length); |
| 76 | memcpy(data, buffer_ptr, (size_t)length); |
| 77 | buffer_ptr += length; |
| 78 | tfr_size += length; |
| 79 | } |
| 80 | |
| 81 | // Send function pointer |
| 82 | void Marshaller::send_func_ptr( |
| 83 | const void* data |
| 84 | ) |
| 85 | { |
| 86 | const char* name; |
| 87 | size_t length; |
| 88 | |
| 89 | if (data != 0) { |
| 90 | name = __offload_funcs.find_name(data); |
| 91 | if (name == 0) { |
| 92 | #if OFFLOAD_DEBUG > 0 |
| 93 | if (console_enabled > 2) { |
| 94 | __offload_funcs.dump(); |
| 95 | } |
| 96 | #endif // OFFLOAD_DEBUG > 0 |
| 97 | |
| 98 | LIBOFFLOAD_ERROR(c_send_func_ptr, data); |
| 99 | exit(1); |
| 100 | } |
| 101 | length = strlen(name) + 1; |
| 102 | } |
| 103 | else { |
| 104 | name = ""; |
| 105 | length = 1; |
| 106 | } |
| 107 | |
| 108 | memcpy(buffer_ptr, name, length); |
| 109 | buffer_ptr += length; |
| 110 | tfr_size += length; |
| 111 | } |
| 112 | |
| 113 | // Receive function pointer |
| 114 | void Marshaller::receive_func_ptr( |
| 115 | const void** data |
| 116 | ) |
| 117 | { |
| 118 | const char* name; |
| 119 | size_t length; |
| 120 | |
| 121 | name = (const char*) buffer_ptr; |
| 122 | if (name[0] != '\0') { |
| 123 | *data = __offload_funcs.find_addr(name); |
| 124 | if (*data == 0) { |
| 125 | #if OFFLOAD_DEBUG > 0 |
| 126 | if (console_enabled > 2) { |
| 127 | __offload_funcs.dump(); |
| 128 | } |
| 129 | #endif // OFFLOAD_DEBUG > 0 |
| 130 | |
| 131 | LIBOFFLOAD_ERROR(c_receive_func_ptr, name); |
| 132 | exit(1); |
| 133 | } |
| 134 | length = strlen(name) + 1; |
| 135 | } |
| 136 | else { |
| 137 | *data = 0; |
| 138 | length = 1; |
| 139 | } |
| 140 | |
| 141 | buffer_ptr += length; |
| 142 | tfr_size += length; |
| 143 | } |
| 144 | |
| 145 | // End of the Marshaller and associated routines |
| 146 | |
| 147 | extern void *OFFLOAD_MALLOC( |
| 148 | size_t size, |
| 149 | size_t align |
| 150 | ) |
| 151 | { |
| 152 | void *ptr; |
| 153 | int err; |
| 154 | |
| 155 | OFFLOAD_DEBUG_TRACE(2, "%s(%lld, %lld)\n", __func__, size, align); |
| 156 | |
| 157 | if (align < sizeof(void*)) { |
| 158 | align = sizeof(void*); |
| 159 | } |
| 160 | |
| 161 | ptr = _mm_malloc(size, align); |
| 162 | if (ptr == NULL) { |
| 163 | LIBOFFLOAD_ERROR(c_offload_malloc, size, align); |
| 164 | exit(1); |
| 165 | } |
| 166 | |
| 167 | OFFLOAD_DEBUG_TRACE(2, "%s returned %p\n", __func__, ptr); |
| 168 | |
| 169 | return ptr; |
| 170 | } |