xmalloc.c revision 60573
157429Smarkm/*
257429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
357429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
457429Smarkm *                    All rights reserved
557429Smarkm * Created: Mon Mar 20 21:23:10 1995 ylo
657429Smarkm * Versions of malloc and friends that check their results, and never return
757429Smarkm * failure (they call fatal if they encounter an error).
857429Smarkm */
957429Smarkm
1057429Smarkm#include "includes.h"
1160573SkrisRCSID("$Id: xmalloc.c,v 1.6 2000/04/14 10:30:34 markus Exp $");
1257429Smarkm
1357429Smarkm#include "ssh.h"
1457429Smarkm
1557429Smarkmvoid *
1657429Smarkmxmalloc(size_t size)
1757429Smarkm{
1857429Smarkm	void *ptr = malloc(size);
1957429Smarkm	if (ptr == NULL)
2057429Smarkm		fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
2157429Smarkm	return ptr;
2257429Smarkm}
2357429Smarkm
2457429Smarkmvoid *
2557429Smarkmxrealloc(void *ptr, size_t new_size)
2657429Smarkm{
2757429Smarkm	void *new_ptr;
2857429Smarkm
2957429Smarkm	if (ptr == NULL)
3057429Smarkm		fatal("xrealloc: NULL pointer given as argument");
3157429Smarkm	new_ptr = realloc(ptr, new_size);
3257429Smarkm	if (new_ptr == NULL)
3357429Smarkm		fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
3457429Smarkm	return new_ptr;
3557429Smarkm}
3657429Smarkm
3760573Skrisvoid
3857429Smarkmxfree(void *ptr)
3957429Smarkm{
4057429Smarkm	if (ptr == NULL)
4157429Smarkm		fatal("xfree: NULL pointer given as argument");
4257429Smarkm	free(ptr);
4357429Smarkm}
4457429Smarkm
4557429Smarkmchar *
4657429Smarkmxstrdup(const char *str)
4757429Smarkm{
4857429Smarkm	int len = strlen(str) + 1;
4957429Smarkm
5057429Smarkm	char *cp = xmalloc(len);
5157429Smarkm	strlcpy(cp, str, len);
5257429Smarkm	return cp;
5357429Smarkm}
54