jli_util.c revision 5270
4901N/A/*
4901N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
4901N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4901N/A *
4901N/A * This code is free software; you can redistribute it and/or modify it
4901N/A * under the terms of the GNU General Public License version 2 only, as
4901N/A * published by the Free Software Foundation. Oracle designates this
4901N/A * particular file as subject to the "Classpath" exception as provided
4901N/A * by Oracle in the LICENSE file that accompanied this code.
4901N/A *
4901N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4901N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4901N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4901N/A * version 2 for more details (a copy is included in the LICENSE file that
4901N/A * accompanied this code).
4901N/A *
4901N/A * You should have received a copy of the GNU General Public License version
4901N/A * 2 along with this work; if not, write to the Free Software Foundation,
4901N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4901N/A *
4901N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4901N/A * or visit www.oracle.com if you need additional information or have any
4901N/A * questions.
4901N/A */
4901N/A
4901N/A#include <stdio.h>
4901N/A#include <string.h>
4901N/A#include <jni.h>
4901N/A
4901N/A#include "jli_util.h"
4901N/A
4901N/A/*
4901N/A * Returns a pointer to a block of at least 'size' bytes of memory.
4901N/A * Prints error message and exits if the memory could not be allocated.
4901N/A */
4901N/Avoid *
4901N/AJLI_MemAlloc(size_t size)
4901N/A{
4901N/A void *p = malloc(size);
4901N/A if (p == 0) {
4901N/A perror("malloc");
4901N/A exit(1);
4901N/A }
4901N/A return p;
4901N/A}
4901N/A
4901N/A/*
4901N/A * Equivalent to realloc(size).
4901N/A * Prints error message and exits if the memory could not be reallocated.
4901N/A */
4901N/Avoid *
4901N/AJLI_MemRealloc(void *ptr, size_t size)
4901N/A{
4901N/A void *p = realloc(ptr, size);
4901N/A if (p == 0) {
4901N/A perror("realloc");
4901N/A exit(1);
4901N/A }
4901N/A return p;
4901N/A}
4901N/A
4901N/A/*
4901N/A * Wrapper over strdup(3C) which prints an error message and exits if memory
4901N/A * could not be allocated.
4901N/A */
4901N/Achar *
4901N/AJLI_StringDup(const char *s1)
4901N/A{
4901N/A char *s = strdup(s1);
4901N/A if (s == NULL) {
4901N/A perror("strdup");
4901N/A exit(1);
4901N/A }
4901N/A return s;
4901N/A}
4901N/A
4901N/A/*
4901N/A * Very equivalent to free(ptr).
4901N/A * Here to maintain pairing with the above routines.
4901N/A */
4901N/Avoid
4901N/AJLI_MemFree(void *ptr)
4901N/A{
4901N/A free(ptr);
4901N/A}
4901N/A
4901N/A/*
4901N/A * debug helpers we use
4901N/A */
4901N/Astatic jboolean _launcher_debug = JNI_FALSE;
4901N/A
4901N/Avoid
4901N/AJLI_TraceLauncher(const char* fmt, ...)
4901N/A{
4901N/A va_list vl;
4901N/A if (_launcher_debug != JNI_TRUE) return;
4901N/A va_start(vl, fmt);
4901N/A vprintf(fmt,vl);
4901N/A va_end(vl);
4901N/A}
4901N/A
4901N/Avoid
4901N/AJLI_SetTraceLauncher()
4901N/A{
4901N/A if (getenv(JLDEBUG_ENV_ENTRY) != 0) {
4901N/A _launcher_debug = JNI_TRUE;
4901N/A JLI_TraceLauncher("----%s----\n", JLDEBUG_ENV_ENTRY);
4901N/A }
4901N/A}
4901N/A
4901N/Ajboolean
4901N/AJLI_IsTraceLauncher()
4901N/A{
4901N/A return _launcher_debug;
4901N/A}
4901N/A
4901N/Aint
4901N/AJLI_StrCCmp(const char *s1, const char* s2)
4901N/A{
4901N/A return JLI_StrNCmp(s1, s2, JLI_StrLen(s2));
4901N/A}
4901N/A