| 1 |
/* |
| 2 |
* Copyright (C) 2002 Erik Fears |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to |
| 16 |
* |
| 17 |
* The Free Software Foundation, Inc. |
| 18 |
* 59 Temple Place - Suite 330 |
| 19 |
* Boston, MA 02111-1307, USA. |
| 20 |
* |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <malloc.h> |
| 25 |
#include <assert.h> |
| 26 |
#include <string.h> |
| 27 |
#include "malloc.h" |
| 28 |
|
| 29 |
|
| 30 |
/* MyMalloc |
| 31 |
* |
| 32 |
* A wrapper function for malloc(), for catching memory issues |
| 33 |
* and error handling. |
| 34 |
* |
| 35 |
* Parameters |
| 36 |
* bytes: amount in bytes to allocate |
| 37 |
* |
| 38 |
* Return: |
| 39 |
* Pointer to allocated memory |
| 40 |
*/ |
| 41 |
|
| 42 |
void *MyMalloc(size_t bytes) |
| 43 |
{ |
| 44 |
void *ret = calloc(1, bytes); |
| 45 |
assert(ret); |
| 46 |
|
| 47 |
return ret; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
/* MyFree |
| 53 |
* |
| 54 |
* Free memory allocated with MyMalloc |
| 55 |
* |
| 56 |
* Parameters: |
| 57 |
* var: pointer to memory to free |
| 58 |
* |
| 59 |
* Return: |
| 60 |
* None |
| 61 |
*/ |
| 62 |
|
| 63 |
void _MyFree(void **var) |
| 64 |
{ |
| 65 |
assert(var != NULL); |
| 66 |
|
| 67 |
if(*var != NULL) |
| 68 |
free(*var); |
| 69 |
*var = NULL; |
| 70 |
} |
| 71 |
|
| 72 |
void * |
| 73 |
xstrdup(const char *s) |
| 74 |
{ |
| 75 |
void *ret = malloc(strlen(s) + 1); |
| 76 |
|
| 77 |
assert(ret); |
| 78 |
strcpy(ret, s); |
| 79 |
|
| 80 |
return ret; |
| 81 |
} |