| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * Hypervisor Software File: allocate.c |
| 5 | * |
| 6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 7 | * |
| 8 | * - Do no alter or remove copyright notices |
| 9 | * |
| 10 | * - Redistribution and use of this software in source and binary forms, with |
| 11 | * or without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistribution of source code must retain the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer. |
| 16 | * |
| 17 | * - Redistribution in binary form must reproduce the above copyright notice, |
| 18 | * this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * |
| 21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
| 24 | * |
| 25 | * This software is provided "AS IS," without a warranty of any kind. |
| 26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN |
| 29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR |
| 30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR |
| 31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN |
| 32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR |
| 33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE |
| 34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, |
| 35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF |
| 36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 37 | * |
| 38 | * You acknowledge that this software is not designed, licensed or |
| 39 | * intended for use in the design, construction, operation or maintenance of |
| 40 | * any nuclear facility. |
| 41 | * |
| 42 | * ========== Copyright Header End ============================================ |
| 43 | */ |
| 44 | /* |
| 45 | * Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
| 46 | * Use is subject to license terms. |
| 47 | */ |
| 48 | |
| 49 | #pragma ident "@(#)allocate.c 1.1 04/07/16 SMI" |
| 50 | |
| 51 | #include <string.h> |
| 52 | #include <malloc.h> |
| 53 | #include <unistd.h> |
| 54 | #include <stdlib.h> |
| 55 | |
| 56 | #include "fatal.h" |
| 57 | #include "allocate.h" |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * Simple allocation routines |
| 62 | */ |
| 63 | |
| 64 | void * |
| 65 | xmalloc(int size, int linen, char *filen) |
| 66 | { |
| 67 | void *p; |
| 68 | |
| 69 | if (size <= 0) { |
| 70 | if (size < 0) { |
| 71 | fatal("xmalloc of negative size (%d) at %d in %s", |
| 72 | linen, filen); |
| 73 | } |
| 74 | warning("xmalloc of zero size at %d in %s", linen, filen); |
| 75 | return (NULL); |
| 76 | } |
| 77 | |
| 78 | p = malloc(size); |
| 79 | if (p == NULL) |
| 80 | fatal("malloc of %d at %d in %s", size, linen, filen); |
| 81 | |
| 82 | return (p); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void * |
| 87 | xcalloc(int num, int size, int linen, char *filen) |
| 88 | { |
| 89 | void *p; |
| 90 | |
| 91 | if (size <= 0 || num <= 0) { |
| 92 | fatal("xcalloc(%d,%d) : one of number or size is <= 0 " |
| 93 | "at line %d of %s", num, size, linen, filen); |
| 94 | } |
| 95 | |
| 96 | p = calloc(num, size); |
| 97 | if (p == NULL) { |
| 98 | fatal("calloc of %d of size %d at %d in %s", |
| 99 | num, size, linen, filen); |
| 100 | } |
| 101 | |
| 102 | return (p); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | void |
| 108 | xfree(void *p, int linen, char *filen) |
| 109 | { |
| 110 | if (p == NULL) { |
| 111 | warning("xfree of NULL pointer at %d in %s", linen, filen); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | free(p); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void * |
| 120 | xrealloc(void *oldp, int size, int linen, char *filen) |
| 121 | { |
| 122 | void *p; |
| 123 | |
| 124 | if (size <= 0) { |
| 125 | if (size == 0) { |
| 126 | xfree(oldp, linen, filen); |
| 127 | warning("xrealloc to zero size at %d in %s", |
| 128 | linen, filen); |
| 129 | return (NULL); |
| 130 | } |
| 131 | |
| 132 | fatal("xrealloc to negative size %d at %d in %s", |
| 133 | size, linen, filen); |
| 134 | } |
| 135 | |
| 136 | if (oldp == NULL) { |
| 137 | p = malloc(size); |
| 138 | } else { |
| 139 | p = realloc(oldp, size); |
| 140 | } |
| 141 | if (p == NULL) |
| 142 | fatal("xrealloc failed @ %d in %s", linen, filen); |
| 143 | |
| 144 | return (p); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | char * |
| 150 | xstrdup(char *strp, int linen, char *filen) |
| 151 | { |
| 152 | char *p; |
| 153 | |
| 154 | p = strdup(strp); |
| 155 | if (p == NULL) |
| 156 | fatal("xstrdup @ %d in %s failed", linen, filen); |
| 157 | |
| 158 | return (p); |
| 159 | } |