1 /* 2 * Copyright (c) 2010, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include <stdio.h> 26 #include <string.h> 27 #include "jli_util.h" 28 29 /* 30 * Returns a pointer to a block of at least 'size' bytes of memory. 31 * Prints error message and exits if the memory could not be allocated. 32 */ 33 void * 34 JLI_MemAlloc(size_t size) 35 { 36 void *p = malloc(size); 37 if (p == 0) { 38 perror("malloc"); 39 exit(1); 40 } 41 return p; 42 } 43 44 /* 45 * Equivalent to realloc(size). 46 * Prints error message and exits if the memory could not be reallocated. 47 */ 48 void * 49 JLI_MemRealloc(void *ptr, size_t size) 50 { 51 void *p = realloc(ptr, size); 52 if (p == 0) { 53 perror("realloc"); 54 exit(1); 55 } 56 return p; 57 } 58 59 /* 60 * Wrapper over strdup(3C) which prints an error message and exits if memory 61 * could not be allocated. 62 */ 63 char * 64 JLI_StringDup(const char *s1) 65 { 66 char *s = _strdup(s1); 67 if (s == NULL) { 68 perror("strdup"); 69 exit(1); 70 } 71 return s; 72 } 73 74 /* 75 * Very equivalent to free(ptr). 76 * Here to maintain pairing with the above routines. 77 */ 78 void 79 JLI_MemFree(void *ptr) 80 { 81 free(ptr); 82 }