simplify xmalloc & friends

This commit is contained in:
Alexandre Ratchov 2012-11-11 00:47:00 +01:00
parent ca6e1699f3
commit 40cf11d689
11 changed files with 33 additions and 140 deletions

View File

@ -43,9 +43,9 @@ abuf_log(struct abuf *buf)
#endif
void
abuf_init(struct abuf *buf, unsigned int len, char *tag)
abuf_init(struct abuf *buf, unsigned int len)
{
buf->data = xmalloc(len, tag);
buf->data = xmalloc(len);
buf->len = len;
buf->used = 0;
buf->start = 0;

View File

@ -24,7 +24,7 @@ struct abuf {
unsigned char *data;
};
void abuf_init(struct abuf *, unsigned int, char *);
void abuf_init(struct abuf *, unsigned int);
void abuf_done(struct abuf *);
void abuf_log(struct abuf *);
unsigned char *abuf_rgetblk(struct abuf *, int *);

View File

@ -1004,7 +1004,7 @@ dev_new(char *path, struct aparams *par,
log_puts("too many devices\n");
return NULL;
}
d = xmalloc(sizeof(struct dev), "dev");
d = xmalloc(sizeof(struct dev));
d->num = dev_sndnum++;
/*
* XXX
@ -1094,16 +1094,14 @@ dev_open(struct dev *d)
/*
* Create device <-> demuxer buffer
*/
d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t),
"dev_rbuf");
d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
/*
* Insert a converter, if needed.
*/
if (!aparams_native(&d->par)) {
dec_init(&d->dec, &d->par, d->rchan);
d->decbuf = xmalloc(d->round * d->rchan * d->par.bps,
"dev_dec");
d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
} else
d->decbuf = NULL;
}
@ -1111,7 +1109,7 @@ dev_open(struct dev *d)
/*
* Create device <-> mixer buffer
*/
d->pbuf = xmalloc(d->bufsz * d->pchan * sizeof(adata_t), "dev_pbuf");
d->pbuf = xmalloc(d->bufsz * d->pchan * sizeof(adata_t));
d->poffs = 0;
d->mode |= MODE_MON;
@ -1120,7 +1118,7 @@ dev_open(struct dev *d)
*/
if (!aparams_native(&d->par)) {
enc_init(&d->enc, &d->par, d->pchan);
d->encbuf = xmalloc(d->round * d->pchan * d->par.bps, "dev_enc");
d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
} else
d->encbuf = NULL;
}
@ -1303,12 +1301,6 @@ void
dev_wakeup(struct dev *d)
{
if (d->pstate == DEV_INIT) {
#ifdef DEBUG
if (d->mode & MODE_PLAY)
memrnd(d->pbuf, d->bufsz * d->pchan * sizeof(adata_t));
if (d->mode & MODE_REC)
memrnd(d->rbuf, d->round * d->rchan * sizeof(adata_t));
#endif
if (log_level >= 2) {
dev_log(d);
log_puts(": device started\n");
@ -1718,13 +1710,13 @@ slot_attach(struct slot *s)
if (!aparams_native(&s->par)) {
dec_init(&s->mix.dec, &s->par, slot_nch);
s->mix.decbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_mix_dec");
xmalloc(d->round * slot_nch * sizeof(adata_t));
}
if (s->rate != d->rate) {
resamp_init(&s->mix.resamp, s->round, d->round,
slot_nch);
s->mix.resampbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_mix_resamp");
xmalloc(d->round * slot_nch * sizeof(adata_t));
}
#ifdef DEBUG
if (log_level >= 3) {
@ -1773,12 +1765,12 @@ slot_attach(struct slot *s)
resamp_init(&s->sub.resamp, d->round, s->round,
slot_nch);
s->sub.resampbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_sub_resamp");
xmalloc(d->round * slot_nch * sizeof(adata_t));
}
if (!aparams_native(&s->par)) {
enc_init(&s->sub.enc, &s->par, slot_nch);
s->sub.encbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_sub_enc");
xmalloc(d->round * slot_nch * sizeof(adata_t));
}
#ifdef DEBUG
@ -1853,7 +1845,7 @@ slot_start(struct slot *s)
#endif
s->mix.bpf = s->par.bps *
(s->mix.slot_cmax - s->mix.slot_cmin + 1);
abuf_init(&s->mix.buf, bufsz * s->mix.bpf, "slot_mix");
abuf_init(&s->mix.buf, bufsz * s->mix.bpf);
}
if (s->mode & MODE_RECMASK) {
#ifdef DEBUG
@ -1868,7 +1860,7 @@ slot_start(struct slot *s)
#endif
s->sub.bpf = s->par.bps *
(s->sub.slot_cmax - s->sub.slot_cmin + 1);
abuf_init(&s->sub.buf, bufsz * s->sub.bpf, "slot_sub");
abuf_init(&s->sub.buf, bufsz * s->sub.bpf);
}
s->mix.weight = MIDI_TO_ADATA(MIDI_MAXCTL);
#ifdef DEBUG

View File

@ -230,7 +230,7 @@ file_new(struct fileops *ops, void *arg, char *name, unsigned int nfds)
#endif
return NULL;
}
f = xmalloc(sizeof(struct file), "file");
f = xmalloc(sizeof(struct file));
f->nfds = nfds;
f->ops = ops;
f->arg = arg;

View File

@ -109,11 +109,11 @@ listen_new_un(char *path)
goto bad_close;
}
umask(oldumask);
f = xmalloc(sizeof(struct listen), "listen_un");
f = xmalloc(sizeof(struct listen));
f->file = file_new(&listen_fileops, f, path, 1);
if (f->file == NULL)
goto bad_close;
f->path = xstrdup(path, "listen_path");
f->path = xstrdup(path);
if (f->path == NULL) {
perror("strdup");
exit(1);
@ -174,7 +174,7 @@ listen_new_tcp(char *addr, unsigned int port)
perror("listen");
goto bad_close;
}
f = xmalloc(sizeof(struct listen), "listen_tcp");
f = xmalloc(sizeof(struct listen));
f->file = file_new(&listen_fileops, f, addr, 1);
if (f == NULL) {
bad_close:

View File

@ -128,10 +128,10 @@ midi_new(struct midiops *ops, void *arg, int mode)
* to client input
*/
if (ep->mode & MODE_MIDIOUT) {
abuf_init(&ep->ibuf, MIDI_BUFSZ, "midi_ibuf");
abuf_init(&ep->ibuf, MIDI_BUFSZ);
}
if (ep->mode & MODE_MIDIIN) {
abuf_init(&ep->obuf, MIDI_BUFSZ, "midi_obuf");
abuf_init(&ep->obuf, MIDI_BUFSZ);
}
return ep;
}
@ -454,7 +454,7 @@ port_new(char *path, unsigned int mode)
{
struct port *c;
c = xmalloc(sizeof(struct port), "port");
c = xmalloc(sizeof(struct port));
c->path = path;
c->state = PORT_CFG;
c->midi = midi_new(&port_midiops, c, mode);

View File

@ -46,7 +46,7 @@ opt_new(char *name, struct dev *dev,
exit(1);
}
}
o = xmalloc(sizeof(struct opt), "opt");
o = xmalloc(sizeof(struct opt));
if (mode & MODE_PLAY) {
o->pmin = pmin;
o->pmax = pmax;

View File

@ -488,6 +488,5 @@ main(int argc, char **argv)
filelist_done();
rmdir(base);
unsetsig();
xmalloc_exit();
return 0;
}

View File

@ -293,7 +293,7 @@ sock_new(int fd)
{
struct sock *f;
f = xmalloc(sizeof(struct sock), "sock");
f = xmalloc(sizeof(struct sock));
f->pstate = SOCK_AUTH;
f->opt = NULL;
f->slot = NULL;

View File

@ -139,131 +139,35 @@ panic(void)
_exit(1);
}
/*
* return a pseudo-random number
*/
unsigned
rnd(void)
{
static unsigned seed = 1989123;
seed = (seed * 1664525) + 1013904223;
return seed;
}
void
memrnd(void *addr, size_t size)
{
unsigned char *p = addr;
while (size-- > 0)
*(p++) = rnd() >> 24;
}
/*
* header of a memory block
*/
struct xmalloc_hdr {
struct xmalloc_hdr *next; /* next allocated block */
char *tag; /* what the block is used for */
size_t size; /* data chunk size in bytes */
char end[sizeof(void *)]; /* copy of trailer (random bytes) */
};
#define XMALLOC_HDR_SIZE ((sizeof(struct xmalloc_hdr) + 15) & ~15)
struct xmalloc_hdr *xmalloc_list = NULL;
/*
* allocate 'size' bytes of memory (with size > 0). This functions never
* fails (and never returns NULL), if there isn't enough memory then
* we abort the program. The memory block is randomized to break code
* that doesn't initialize the block. We also add a footer and a
* trailer to detect writes outside the block boundaries.
*/
void *
xmalloc(size_t size, char *tag)
xmalloc(size_t size)
{
struct xmalloc_hdr *hdr;
char *p;
void *p;
if (size == 0) {
log_puts(tag);
log_puts(": xmalloc: nbytes = 0\n");
panic();
}
hdr = malloc(size + XMALLOC_HDR_SIZE + sizeof(hdr->end));
if (hdr == NULL) {
log_puts(tag);
log_puts(": xmalloc: failed to allocate ");
p = malloc(size);
if (p == NULL) {
log_puts("failed to allocate ");
log_putx(size);
log_puts(" bytes\n");
panic();
}
p = (char *)hdr + XMALLOC_HDR_SIZE;
hdr->tag = tag;
hdr->size = size;
memrnd(hdr->end, sizeof(hdr->end));
memset(p, 0xd0, size);
memcpy(p + size, hdr->end, sizeof(hdr->end));
hdr->next = xmalloc_list;
xmalloc_list = hdr;
return p;
}
/*
* free a memory block. Also check that the header and the trailer
* weren't changed and randomise the block, so that the block is not
* usable once freed
*/
void
xfree(void *p)
{
struct xmalloc_hdr *hdr, **ph;
hdr = (struct xmalloc_hdr *)((char *)p - XMALLOC_HDR_SIZE);
if (memcmp(hdr->end, (char *)p + hdr->size, sizeof(hdr->end)) != 0) {
log_puts(hdr->tag);
log_puts(": block trailer corrupted\n");
panic();
}
memset(p, 0xdf, hdr->size);
for (ph = &xmalloc_list; *ph != NULL; ph = &(*ph)->next) {
if (*ph == hdr) {
*ph = hdr->next;
free(hdr);
return;
}
}
log_puts(hdr->tag);
log_puts(": not allocated (double free?)\n");
panic();
}
void
xmalloc_exit(void)
{
struct xmalloc_hdr *hdr;
if (xmalloc_list) {
log_puts("allocated memory blocs: ");
for (hdr = xmalloc_list; hdr != NULL; hdr = hdr->next) {
log_puts(hdr->tag);
if (hdr->next)
log_puts(", ");
}
log_puts("\n");
}
free(p);
}
char *
xstrdup(char *s, char *tag)
xstrdup(char *s)
{
size_t size;
void *p;
size = strlen(s) + 1;
p = xmalloc(size, tag);
p = xmalloc(size);
memcpy(p, s, size);
return p;
}

View File

@ -27,12 +27,10 @@ void log_puti(long);
void panic(void);
void log_flush(void);
void *xmalloc(size_t, char *);
char *xstrdup(char *, char *);
void *xmalloc(size_t);
char *xstrdup(char *);
void xfree(void *);
void xmalloc_exit(void);
void memrnd(void *, size_t);
extern unsigned int log_sync;
#endif