[77] | 1 | /* $Id: xmalloc.c 13 2011-04-20 15:41:43Z mmamonski $ */ |
---|
| 2 | /* |
---|
| 3 | * PSNC DRMAA 2.0 utilities library |
---|
| 4 | * Copyright (C) 2012 Poznan Supercomputing and Networking Center |
---|
| 5 | * |
---|
| 6 | * This program is free software: you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * @file xmalloc.c |
---|
| 22 | * Memory allocation/deallocation routines. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifdef HAVE_CONFIG_H |
---|
| 26 | # include <config.h> |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <limits.h> |
---|
| 31 | #include <stdlib.h> |
---|
| 32 | #include <string.h> |
---|
| 33 | |
---|
| 34 | #include <drmaa_utils/xmalloc.h> |
---|
| 35 | #include <drmaa_utils/compat.h> |
---|
| 36 | #include <drmaa_utils/exception.h> |
---|
| 37 | #include <drmaa_utils/logging.h> |
---|
| 38 | |
---|
| 39 | #ifndef SIZE_T_MAX |
---|
| 40 | # define SIZE_T_MAX ((size_t) -1) |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | #ifndef lint |
---|
| 44 | static char rcsid[] |
---|
| 45 | # ifdef __GNUC__ |
---|
| 46 | __attribute__ ((unused)) |
---|
| 47 | # endif |
---|
| 48 | = "$Id: xmalloc.c 13 2011-04-20 15:41:43Z mmamonski $"; |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | void * |
---|
| 53 | fsd_malloc_( void **p, size_t size ) |
---|
| 54 | { |
---|
| 55 | void *ptr = NULL; |
---|
| 56 | if( size ) |
---|
| 57 | { |
---|
| 58 | ptr = malloc( size ); |
---|
| 59 | if( ptr ) |
---|
| 60 | memset( ptr, 0, size ); |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | *p = NULL; |
---|
| 64 | fsd_exc_raise_sys( errno=ENOMEM ); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | *p = ptr; |
---|
| 68 | return ptr; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | void * |
---|
| 73 | fsd_calloc_( void **p, size_t n, size_t size ) |
---|
| 74 | { |
---|
| 75 | void *ptr = NULL; |
---|
| 76 | |
---|
| 77 | /*fsd_log_enter(("%d", (int)n));*/ |
---|
| 78 | |
---|
| 79 | if( n && size ) |
---|
| 80 | { |
---|
| 81 | if( n <= SIZE_T_MAX / size ) |
---|
| 82 | ptr = calloc( n, size ); |
---|
| 83 | if( !ptr ) |
---|
| 84 | { |
---|
| 85 | *p = NULL; |
---|
| 86 | fsd_exc_raise_sys( errno=ENOMEM ); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | *p = ptr; |
---|
| 90 | return ptr; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | void * |
---|
| 95 | fsd_realloc_( void **p, size_t size ) |
---|
| 96 | { |
---|
| 97 | void *ptr = *p; |
---|
| 98 | |
---|
| 99 | if( size ) |
---|
| 100 | { |
---|
| 101 | if( ptr ) |
---|
| 102 | ptr = realloc( ptr, size ); |
---|
| 103 | else |
---|
| 104 | ptr = malloc( size ); |
---|
| 105 | |
---|
| 106 | if( ptr != NULL ) |
---|
| 107 | *p = ptr; |
---|
| 108 | else |
---|
| 109 | fsd_exc_raise_sys( errno=ENOMEM ); |
---|
| 110 | } |
---|
| 111 | else if( ptr != NULL ) |
---|
| 112 | { |
---|
| 113 | free( ptr ); |
---|
| 114 | *p = ptr = NULL; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | return ptr; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | void |
---|
| 122 | fsd_free( void *p ) |
---|
| 123 | { |
---|
| 124 | if( p ) |
---|
| 125 | free( p ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | int |
---|
| 130 | fsd_malloc_noraise_( void **p, size_t size ) |
---|
| 131 | { |
---|
| 132 | int result = 0; |
---|
| 133 | void *ptr = NULL; |
---|
| 134 | if( size ) |
---|
| 135 | { |
---|
| 136 | ptr = malloc( size ); |
---|
| 137 | if( ptr ) |
---|
| 138 | memset( ptr, 0, size ); |
---|
| 139 | else |
---|
| 140 | result = ENOMEM; |
---|
| 141 | } |
---|
| 142 | *p = ptr; |
---|
| 143 | return result; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | int |
---|
| 148 | fsd_calloc_noraise_( void **p, size_t n, size_t size ) |
---|
| 149 | { |
---|
| 150 | int result = 0; |
---|
| 151 | void *ptr = NULL; |
---|
| 152 | if( n && size ) |
---|
| 153 | { |
---|
| 154 | if( n <= SIZE_T_MAX / size ) |
---|
| 155 | ptr = calloc( n, size ); |
---|
| 156 | if( !ptr ) |
---|
| 157 | result = ENOMEM; |
---|
| 158 | } |
---|
| 159 | *p = ptr; |
---|
| 160 | return result; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | int |
---|
| 165 | fsd_realloc_noraise_( void **p, size_t size ) |
---|
| 166 | { |
---|
| 167 | int result = 0; |
---|
| 168 | void *ptr = *p; |
---|
| 169 | |
---|
| 170 | if( size ) |
---|
| 171 | { |
---|
| 172 | if( ptr ) |
---|
| 173 | ptr = realloc( ptr, size ); |
---|
| 174 | else |
---|
| 175 | ptr = malloc( size ); |
---|
| 176 | |
---|
| 177 | if( ptr != NULL ) |
---|
| 178 | *p = ptr; |
---|
| 179 | else |
---|
| 180 | result = ENOMEM; |
---|
| 181 | } |
---|
| 182 | else if( ptr != NULL ) |
---|
| 183 | { |
---|
| 184 | free( ptr ); |
---|
| 185 | *p = ptr = NULL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | return result; |
---|
| 189 | } |
---|
| 190 | |
---|