2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdlib.h>
2N/A#include <alloca.h>
2N/A
2N/A#include <stdio.h>
2N/A#include <strings.h>
2N/A#include <time.h>
2N/A
2N/A#include "asr_err.h"
2N/A#include "asr_mem.h"
2N/A
2N/A/*
2N/A * Wrapper for memory allocation.
2N/A */
2N/Avoid *
2N/Aasr_alloc(size_t size)
2N/A{
2N/A void *mem;
2N/A
2N/A if (size == 0) {
2N/A (void) asr_set_errno(EASR_ZEROSIZE);
2N/A return (NULL);
2N/A }
2N/A if (size > ASR_MEM_MAX_SIZE) {
2N/A (void) asr_set_errno(EASR_OVERSIZE);
2N/A return (NULL);
2N/A }
2N/A if ((mem = malloc(size)) == NULL)
2N/A (void) asr_set_errno(EASR_NOMEM);
2N/A
2N/A return (mem);
2N/A}
2N/A
2N/A/*
2N/A * Wrapper for zero initialized memory allocation.
2N/A */
2N/Avoid *
2N/Aasr_zalloc(size_t size)
2N/A{
2N/A void *mem = asr_alloc(size);
2N/A
2N/A if (mem != NULL)
2N/A bzero(mem, size);
2N/A
2N/A return (mem);
2N/A}
2N/A
2N/A/*
2N/A * Wrapper for string duplication
2N/A */
2N/Achar *
2N/Aasr_strdup(const char *str)
2N/A{
2N/A size_t len;
2N/A char *new;
2N/A
2N/A if (str == NULL) {
2N/A (void) asr_set_errno(EASR_NULLDATA);
2N/A return (NULL);
2N/A }
2N/A
2N/A len = strlen(str) + 1;
2N/A new = asr_alloc(len);
2N/A
2N/A if (new == NULL)
2N/A return (NULL);
2N/A
2N/A bcopy(str, new, len);
2N/A return (new);
2N/A}
2N/A
2N/A/*
2N/A * Erase string following Oracle security guidlines and then free it.
2N/A */
2N/Avoid
2N/Aasr_strfree_secure(char *str)
2N/A{
2N/A size_t len, i;
2N/A int d = 0xff;
2N/A if (str == NULL)
2N/A return;
2N/A len = strlen(str);
2N/A (void) memset(str, d, len);
2N/A for (i = 0; i < len; i++)
2N/A if (str[i] != d)
2N/A break;
2N/A free(str);
2N/A}