mirror of https://github.com/ericonr/sndio.git
better xmalloc from midish
This commit is contained in:
parent
49feb9131a
commit
f29564da69
|
@ -64,7 +64,7 @@ abuf_done(struct abuf *buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
free(buf->data);
|
xfree(buf->data);
|
||||||
buf->data = (void *)0xdeadbeef;
|
buf->data = (void *)0xdeadbeef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1231,7 +1231,7 @@ dev_del(struct dev *d)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
*p = d->next;
|
*p = d->next;
|
||||||
free(d);
|
xfree(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
|
@ -1464,7 +1464,7 @@ slot_new(struct dev *d, char *who, struct slotops *ops, void *arg, int mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* find a free controller slot with the same name/unit
|
* find a xfree controller slot with the same name/unit
|
||||||
*/
|
*/
|
||||||
for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
|
for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
|
||||||
if (s->ops == NULL &&
|
if (s->ops == NULL &&
|
||||||
|
@ -1482,7 +1482,7 @@ slot_new(struct dev *d, char *who, struct slotops *ops, void *arg, int mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* couldn't find a matching slot, pick oldest free slot
|
* couldn't find a matching slot, pick oldest xfree slot
|
||||||
* and set its name/unit
|
* and set its name/unit
|
||||||
*/
|
*/
|
||||||
bestser = 0;
|
bestser = 0;
|
||||||
|
|
|
@ -289,7 +289,7 @@ file_poll(void)
|
||||||
while ((f = *pf) != NULL) {
|
while ((f = *pf) != NULL) {
|
||||||
if (f->state == FILE_ZOMB) {
|
if (f->state == FILE_ZOMB) {
|
||||||
*pf = f->next;
|
*pf = f->next;
|
||||||
free(f);
|
xfree(f);
|
||||||
} else
|
} else
|
||||||
pf = &f->next;
|
pf = &f->next;
|
||||||
}
|
}
|
||||||
|
|
|
@ -430,7 +430,7 @@ port_del(struct port *c)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
*p = c->next;
|
*p = c->next;
|
||||||
free(c);
|
xfree(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -71,7 +71,7 @@ miofile_del(struct miofile *f)
|
||||||
{
|
{
|
||||||
file_del(f->file);
|
file_del(f->file);
|
||||||
mio_close(f->hdl);
|
mio_close(f->hdl);
|
||||||
free(f);
|
xfree(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -277,7 +277,7 @@ siofile_del(struct siofile *f)
|
||||||
#endif
|
#endif
|
||||||
file_del(f->file);
|
file_del(f->file);
|
||||||
sio_close(f->hdl);
|
sio_close(f->hdl);
|
||||||
free(f);
|
xfree(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -154,7 +154,7 @@ sock_close(struct sock *f)
|
||||||
}
|
}
|
||||||
file_del(f->file);
|
file_del(f->file);
|
||||||
close(f->fd);
|
close(f->fd);
|
||||||
free(f);
|
xfree(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -295,7 +295,7 @@ sock_new(int fd)
|
||||||
f->file = file_new(&sock_fileops, f, "sock", 1);
|
f->file = file_new(&sock_fileops, f, "sock", 1);
|
||||||
f->fd = fd;
|
f->fd = fd;
|
||||||
if (f->file == NULL) {
|
if (f->file == NULL) {
|
||||||
free(f);
|
xfree(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
f->next = sock_list;
|
f->next = sock_list;
|
||||||
|
|
133
sndiod/utils.c
133
sndiod/utils.c
|
@ -136,6 +136,7 @@ panic(void)
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
/*
|
/*
|
||||||
* allocate memory, and abort on error
|
* allocate memory, and abort on error
|
||||||
*/
|
*/
|
||||||
|
@ -151,6 +152,7 @@ xmalloc(size_t size)
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* return a random number, will be used to randomize memory bocks
|
* return a random number, will be used to randomize memory bocks
|
||||||
|
@ -172,3 +174,134 @@ memrnd(void *addr, size_t size)
|
||||||
while (size-- > 0)
|
while (size-- > 0)
|
||||||
*(p++) = rnd() >> 24;
|
*(p++) = rnd() >> 24;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* header of a memory block
|
||||||
|
*/
|
||||||
|
struct mem_hdr {
|
||||||
|
char *owner; /* who allocaed the block ? */
|
||||||
|
unsigned words; /* data chunk size expressed in sizeof(int) */
|
||||||
|
#define MAGIC_FREE 0xa55f9811 /* a (hopefully) ``rare'' number */
|
||||||
|
unsigned magic; /* random number, but not MAGIC_FREE */
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned mem_nalloc = 0, mem_nfree = 0, mem_debug = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return a random number, will be used to randomize memory bocks
|
||||||
|
*/
|
||||||
|
unsigned
|
||||||
|
mem_rnd(void)
|
||||||
|
{
|
||||||
|
static unsigned seed = 1989123;
|
||||||
|
|
||||||
|
seed = (seed * 1664525) + 1013904223;
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* allocate 'n' bytes of memory (with n > 0). This functions never
|
||||||
|
* fails (and never returns NULL), if there isn't enough memory then
|
||||||
|
* we abord 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 *
|
||||||
|
mem_alloc(unsigned bytes, char *owner)
|
||||||
|
{
|
||||||
|
unsigned words, i, *p;
|
||||||
|
struct mem_hdr *hdr;
|
||||||
|
|
||||||
|
if (bytes == 0) {
|
||||||
|
log_puts(owner);
|
||||||
|
log_puts(": mem_alloc: nbytes = 0\n");
|
||||||
|
panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* calculates the number of ints corresponding to ``bytes''
|
||||||
|
*/
|
||||||
|
words = (bytes + sizeof(int) - 1) / sizeof(int);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* allocate the header, the data chunk and the trailer
|
||||||
|
*/
|
||||||
|
hdr = malloc(sizeof(struct mem_hdr) + (words + 1) * sizeof(int));
|
||||||
|
if (hdr == NULL) {
|
||||||
|
log_puts(owner);
|
||||||
|
log_puts(": mem_alloc: failed to allocate ");
|
||||||
|
log_putx(words);
|
||||||
|
log_puts(" words\n");
|
||||||
|
panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* find a random magic, but not MAGIC_FREE
|
||||||
|
*/
|
||||||
|
do {
|
||||||
|
hdr->magic = mem_rnd();
|
||||||
|
} while (hdr->magic == MAGIC_FREE);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* randomize data chunk
|
||||||
|
*/
|
||||||
|
p = (unsigned *)(hdr + 1);
|
||||||
|
for (i = words; i > 0; i--)
|
||||||
|
*p++ = mem_rnd();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* trailer is equal to the magic
|
||||||
|
*/
|
||||||
|
*p = hdr->magic;
|
||||||
|
|
||||||
|
hdr->owner = owner;
|
||||||
|
hdr->words = words;
|
||||||
|
mem_nalloc++;
|
||||||
|
return hdr + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* free a memory block. Also check that the header and the trailer
|
||||||
|
* werent changed and randomise the block, so that the block is not
|
||||||
|
* usable once freed
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
mem_free(void *mem)
|
||||||
|
{
|
||||||
|
struct mem_hdr *hdr;
|
||||||
|
unsigned i, *p;
|
||||||
|
|
||||||
|
hdr = (struct mem_hdr *)mem - 1;
|
||||||
|
p = (unsigned *)mem;
|
||||||
|
|
||||||
|
if (hdr->magic == MAGIC_FREE) {
|
||||||
|
log_puts("mem_free: block seems already freed\n");
|
||||||
|
panic();
|
||||||
|
}
|
||||||
|
if (hdr->magic != p[hdr->words]) {
|
||||||
|
log_puts("mem_free: block corrupted\n");
|
||||||
|
panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* randomize block, so it's not usable
|
||||||
|
*/
|
||||||
|
for (i = hdr->words; i > 0; i--)
|
||||||
|
*p++ = mem_rnd();
|
||||||
|
|
||||||
|
hdr->magic = MAGIC_FREE;
|
||||||
|
mem_nfree++;
|
||||||
|
free(hdr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mem_stats(void)
|
||||||
|
{
|
||||||
|
if (mem_debug) {
|
||||||
|
log_puts("mem_stats: used=");
|
||||||
|
log_putu(mem_nalloc - mem_nfree);
|
||||||
|
log_puts(", alloc=");
|
||||||
|
log_putu(mem_nalloc);
|
||||||
|
log_puts("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,14 @@ void log_puti(long);
|
||||||
void panic(void);
|
void panic(void);
|
||||||
void log_flush(void);
|
void log_flush(void);
|
||||||
|
|
||||||
void *xmalloc(size_t);
|
//void *xmalloc(size_t);
|
||||||
|
void *mem_alloc(unsigned, char *);
|
||||||
|
void mem_free(void *);
|
||||||
|
void mem_stats(void);
|
||||||
|
|
||||||
|
#define xmalloc(s) (mem_alloc((s), (char *)__func__))
|
||||||
|
#define xfree(p) (mem_free((p)))
|
||||||
|
|
||||||
void memrnd(void *, size_t);
|
void memrnd(void *, size_t);
|
||||||
|
|
||||||
extern unsigned int log_sync;
|
extern unsigned int log_sync;
|
||||||
|
|
Loading…
Reference in New Issue