# HG changeset patch # User Francisco J Ballesteros # Date 1317230267 0 # Node ID 45c559389f50ee579a164a83fed9de51c7ed7b84 # Parent 86f0809cfdd50e38e4c7532fa2cc57cc399b1dd5 liberror: tiny error handling tools. emalloc, erealloc, ... they are found in so many programs that they deserve to be in a library. R=nix-dev, john CC=nix-dev http://codereview.appspot.com/5133046 diff -r 86f0809cfdd5 -r 45c559389f50 sys/include/error.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/include/error.h Wed Sep 28 17:17:47 2011 +0000 @@ -0,0 +1,15 @@ +#pragma lib "liberror.a" +#pragma src "/sys/src/liberror" + +typedef struct Channel Channel; + +#pragma incomplete Channel; + +#pragma varargck argpos esmprint 1 + +char* estrdup(char*); +void* emalloc(int); +void* emallocz(int, int); +void* erealloc(void*,int); +char* esmprint(char *fmt, ...); +Channel*echancreate(int, int); diff -r 86f0809cfdd5 -r 45c559389f50 sys/man/2/error --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/man/2/error Wed Sep 28 17:17:47 2011 +0000 @@ -0,0 +1,48 @@ +.TH ERROR 2 +.SH NAME +estrdup, emalloc, emallocz, erealloc, esmprint, echancreate \- error handling utilities +.SH SYNOPSIS +.B #include +.br +.B #include +.br +.B #include +.PP +.B +char* estrdup(char*) +.br +.B +void* emalloc(int); +.br +.B +void* emallocz(int, int); +.br +.B +void* erealloc(void*,int) +.br +.B +void* esmprint(char *fmt, ...) +.br +.B +Channel* echancreate(int, int) +.br +.SH DESCRIPTION +These routiles are tools for error handling verions of memory allocation +functions found elsewhere. +.PP +Their are named like standard functions with the letter +.B e +prepended, and behave as expected for the +standard function in each case. However, they check that the standard routine did its +work and call +.IR sysfatal (2) +otherwise. +All of them set the caller PC as the tag, using +.IR setmalloctag (2). +.PP +.I Echancreate +requires including the header for +.I thread (2). +Other ones do not. +.SH SOURCE +.B /sys/src/liberror diff -r 86f0809cfdd5 -r 45c559389f50 sys/src/liberror/echancreate.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/src/liberror/echancreate.c Wed Sep 28 17:17:47 2011 +0000 @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +Channel* +echancreate(int sz, int n) +{ + Channel *c; + + c = chancreate(sz, n); + if(c == nil) + sysfatal("chancreate: %r"); + return c; +} diff -r 86f0809cfdd5 -r 45c559389f50 sys/src/liberror/emalloc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/src/liberror/emalloc.c Wed Sep 28 17:17:47 2011 +0000 @@ -0,0 +1,63 @@ +#include +#include +#include + +char* +estrdup(char* s) +{ + s = strdup(s); + if(s == nil) + sysfatal("estrdup: not enough memory"); + setmalloctag(s, getcallerpc(&s)); + return s; +} + +void* +emalloc(int sz) +{ + void* s; + + s = mallocz(sz, 1); + if(s == nil) + sysfatal("emalloc: not enough memory"); + setmalloctag(s, getcallerpc(&sz)); + return s; +} + +void* +emallocz(int sz, int zero) +{ + void* s; + + s = mallocz(sz, zero); + if(s == nil) + sysfatal("emalloc: not enough memory"); + setmalloctag(s, getcallerpc(&sz)); + return s; +} + +void* +erealloc(void* p, int sz) +{ + + p = realloc(p, sz); + if(p == nil) + sysfatal("erealloc: not enough memory"); + setmalloctag(p, getcallerpc(&p)); + return p; +} + +char* +esmprint(char *fmt, ...) +{ + va_list arg; + char *m; + + va_start(arg, fmt); + m = vsmprint(fmt, arg); + va_end(arg); + if(m == nil) + sysfatal("smprint: %r"); + setmalloctag(m, getcallerpc(&fmt)); + return m; +} diff -r 86f0809cfdd5 -r 45c559389f50 sys/src/liberror/mkfile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/src/liberror/mkfile Wed Sep 28 17:17:47 2011 +0000 @@ -0,0 +1,16 @@ +