shithub: rtmp

ref: c9a45262d2655e843fcc0ad7382f08efcc523926
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include "util.h"

void *
emalloc(usize sz)
{
	void *p;

	assert((p = malloc(sz)) != nil);
	setmalloctag(p, getcallerpc(&sz));

	return p;
}

void *
ecalloc(ulong nel, usize elsz)
{
	void *p;

	assert((p = calloc(nel, elsz)) != nil);
	setmalloctag(p, getcallerpc(&nel));

	return p;
}

void *
erealloc(void *p, usize sz)
{
	void *np;

	np = realloc(p, sz);
	assert((sz == 0 && np == nil) || np != nil);
	if(np != nil)
		setrealloctag(np, getcallerpc(&p));

	return np;
}

char *
estrndup(void *d, int n)
{
	char *s;

	s = malloc(n+1);
	assert(s != nil);
	setmalloctag(s, getcallerpc(&d));
	memmove(s, d, n);
	s[n] = 0;

	return s;
}

char *
estrdup(char *s)
{
	char *ns;

	ns = strdup(s);
	assert(ns != nil);
	setmalloctag(ns, getcallerpc(&s));

	return ns;
}