1 /* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdio.h> 27 #include <string.h> 28 #include <stdarg.h> 29 #include "jli_util.h" 30 31 /* 32 * Returns a pointer to a block of at least 'size' bytes of memory. 33 * Prints error message and exits if the memory could not be allocated. 34 */ 35 void * 36 JLI_MemAlloc(size_t size) 37 { 38 void *p = malloc(size); 39 if (p == 0) { 40 perror("malloc"); 41 exit(1); 42 } 43 return p; 44 } 45 46 /* 47 * Equivalent to realloc(size). 48 * Prints error message and exits if the memory could not be reallocated. 49 */ 50 void * 51 JLI_MemRealloc(void *ptr, size_t size) 52 { 53 void *p = realloc(ptr, size); 54 if (p == 0) { 55 perror("realloc"); 56 exit(1); 57 } 58 return p; 59 } 60 61 /* 62 * Wrapper over strdup(3C) which prints an error message and exits if memory 63 * could not be allocated. 64 */ 65 char * 66 JLI_StringDup(const char *s1) 67 { 68 char *s = strdup(s1); 69 if (s == NULL) { 70 perror("strdup"); 71 exit(1); 72 } 73 return s; 74 } 75 76 /* 77 * Very equivalent to free(ptr). 78 * Here to maintain pairing with the above routines. 79 */ 80 void 81 JLI_MemFree(void *ptr) 82 { 83 free(ptr); 84 } 85 86 /* 87 * debug helpers we use 88 */ 89 static jboolean _launcher_debug = JNI_FALSE; 90 91 void 92 JLI_TraceLauncher(const char* fmt, ...) 93 { 94 va_list vl; 95 if (_launcher_debug != JNI_TRUE) return; 96 va_start(vl, fmt); 97 vprintf(fmt,vl); 98 va_end(vl); 99 } 100 101 void 102 JLI_SetTraceLauncher() 103 { 104 if (getenv(JLDEBUG_ENV_ENTRY) != 0) { 105 _launcher_debug = JNI_TRUE; 106 JLI_TraceLauncher("----%s----\n", JLDEBUG_ENV_ENTRY); 107 } 108 } 109 110 jboolean 111 JLI_IsTraceLauncher() 112 { 113 return _launcher_debug; 114 } 115 116 int 117 JLI_StrCCmp(const char *s1, const char* s2) 118 { 119 return JLI_StrNCmp(s1, s2, JLI_StrLen(s2)); 120 } 121 122 JLI_List 123 JLI_List_new(int capacity) 124 { 125 JLI_List l = (JLI_List) JLI_MemAlloc(sizeof(struct JLI_List_)); 126 l->capacity = capacity; 127 l->elements = (char **) JLI_MemAlloc(capacity * sizeof(l->elements[0])); 128 l->size = 0; 129 return l; 130 } 131 132 void 133 JLI_List_free(JLI_List sl) 134 { 135 if (sl) { 136 if (sl->elements) { 137 int i; 138 for (i = 0; i < sl->size; i++) 139 JLI_MemFree(sl->elements[i]); 140 JLI_MemFree(sl->elements); 141 } 142 JLI_MemFree(sl); 143 } 144 } 145 146 void 147 JLI_List_ensureCapacity(JLI_List sl, int capacity) 148 { 149 if (sl->capacity < capacity) { 150 while (sl->capacity < capacity) 151 sl->capacity *= 2; 152 sl->elements = JLI_MemRealloc(sl->elements, 153 sl->capacity * sizeof(sl->elements[0])); 154 } 155 } 156 157 void 158 JLI_List_add(JLI_List sl, char *str) 159 { 160 JLI_List_ensureCapacity(sl, sl->size+1); 161 sl->elements[sl->size++] = str; 162 } 163 164 void 165 JLI_List_addSubstring(JLI_List sl, const char *beg, size_t len) 166 { 167 char *str = (char *) JLI_MemAlloc(len+1); 168 memcpy(str, beg, len); 169 str[len] = '\0'; 170 JLI_List_ensureCapacity(sl, sl->size+1); 171 sl->elements[sl->size++] = str; 172 } 173 174 char * 175 JLI_List_combine(JLI_List sl) 176 { 177 int i; 178 int size; 179 char *str; 180 char *p; 181 for (i = 0, size = 1; i < sl->size; i++) 182 size += (int)JLI_StrLen(sl->elements[i]); 183 184 str = JLI_MemAlloc(size); 185 186 for (i = 0, p = str; i < sl->size; i++) { 187 int len = (int)JLI_StrLen(sl->elements[i]); 188 memcpy(p, sl->elements[i], len); 189 p += len; 190 } 191 *p = '\0'; 192 193 return str; 194 } 195 196 char * 197 JLI_List_join(JLI_List sl, char sep) 198 { 199 int i; 200 int size; 201 char *str; 202 char *p; 203 for (i = 0, size = 1; i < sl->size; i++) 204 size += (int)JLI_StrLen(sl->elements[i]) + 1; 205 206 str = JLI_MemAlloc(size); 207 208 for (i = 0, p = str; i < sl->size; i++) { 209 int len = (int)JLI_StrLen(sl->elements[i]); 210 if (i > 0) *p++ = sep; 211 memcpy(p, sl->elements[i], len); 212 p += len; 213 } 214 *p = '\0'; 215 216 return str; 217 } 218 219 JLI_List 220 JLI_List_split(const char *str, char sep) 221 { 222 const char *p, *q; 223 size_t len = JLI_StrLen(str); 224 int count; 225 JLI_List sl; 226 for (count = 1, p = str; p < str + len; p++) 227 count += (*p == sep); 228 sl = JLI_List_new(count); 229 for (p = str;;) { 230 for (q = p; q <= str + len; q++) { 231 if (*q == sep || *q == '\0') { 232 JLI_List_addSubstring(sl, p, q - p); 233 if (*q == '\0') 234 return sl; 235 p = q + 1; 236 } 237 } 238 } 239 }