1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
/*++
/* NAME
/* mymalloc 3
/* SUMMARY
/* memory management wrappers
/* SYNOPSIS
/* #include <mymalloc.h>
/*
/* char *mymalloc(len)
/* int len;
/*
/* char *myrealloc(ptr, len)
/* char *ptr;
/* int len;
/*
/* char *mystrdup(str)
/* const char *str;
/* DESCRIPTION
/* This module performs low-level memory management with error
/* handling. A call of these functions either succeeds or it does
/* not return at all.
/*
/* mymalloc() allocates the requested amount of memory. The memory
/* is not set to zero.
/*
/* myrealloc() resizes memory obtained from mymalloc() or myrealloc()
/* to the requested size. The result pointer value may differ from
/* that given via the \fBptr\fR argument.
/*
/* mystrdup() returns a dynamic-memory copy of its null-terminated
/* argument. This routine uses mymalloc().
/* SEE ALSO
/* error(3) error reporting module.
/* DIAGNOSTICS
/* Fatal errors: the requested amount of memory is not available.
/* LICENSE
/* This software is distributed under the IBM Public License.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/* Utility library. */
#include "error.h"
#include "mymalloc.h"
/* mymalloc - allocate memory or bust */
char *mymalloc(int len)
{
char *ptr;
if ((ptr = malloc(len)) == 0)
error("Insufficient memory: %m");
return (ptr);
}
/* myrealloc - reallocate memory or bust */
char *myrealloc(char *ptr, int len)
{
if ((ptr = realloc(ptr, len)) == 0)
error("Insufficient memory: %m");
return (ptr);
}
/* mystrdup - save string to heap */
char *mystrdup(const char *str)
{
return (strcpy(mymalloc(strlen(str) + 1), str));
}
|