9
0
Fork 0

Fix error handling with malloc, memalign etc. Introduce xmemalign().

The idea is to panic() when there is no memory available for normal
operation. Exception: code which can consume arbitrary amount of RAM
(example: files allocated in ramfs) must report error instead of panic().

This patch also fixes code which didn't check for NULL from malloc() etc.

Usage: malloc(), memalign() return NULL when out of RAM.
xmalloc(), xmemalign() always return non-NULL or panic().

Signed-off-by: Krzysztof Hałasa <khc@pm.waw.pl>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Krzysztof Halasa 2011-01-06 16:23:01 +01:00 committed by Sascha Hauer
parent c99d28ca56
commit c5baa0edc4
25 changed files with 41 additions and 52 deletions

View File

@ -95,13 +95,7 @@ void setup_dma_coherent(unsigned long offset)
void *dma_alloc_coherent(size_t size)
{
void *mem;
mem = memalign(4096, size);
if (mem)
return mem + dma_coherent_offset;
return NULL;
return xmemalign(4096, size) + dma_coherent_offset;
}
unsigned long virt_to_phys(void *virt)

View File

@ -28,7 +28,7 @@ void *phys_to_virt(unsigned long phys);
#else
static inline void *dma_alloc_coherent(size_t size)
{
return malloc(size);
return xmalloc(size);
}
static inline void dma_free_coherent(void *mem)

View File

@ -184,10 +184,8 @@ static struct line *line_realloc(int len, struct line *line)
{
int size = 32;
if (!line) {
if (!line)
line = xzalloc(sizeof(struct line));
line->data = malloc(32);
}
while (size < len)
size <<= 1;

View File

@ -1333,10 +1333,7 @@ static struct driver_d mci_driver = {
static int mci_init(void)
{
sector_buf = memalign(32, 512);
if (!sector_buf)
return -ENOMEM;
sector_buf = xmemalign(32, 512);
return register_driver(&mci_driver);
}

View File

@ -263,7 +263,7 @@ static int at91rm9200_eth_init (struct device_d *dev)
struct eth_device *edev;
int i;
edev = malloc(sizeof(struct eth_device));
edev = xmalloc(sizeof(struct eth_device));
dev->priv = edev;
edev->open = at91rm9200_eth_open;

View File

@ -440,14 +440,14 @@ static int cs8900_probe(struct device_d *dev)
debug("cs8900_init()\n");
priv = (struct cs8900_priv *)malloc(sizeof(*priv));
priv = (struct cs8900_priv *)xmalloc(sizeof(*priv));
priv->regs = (u16 *)dev->map_base;
if (cs8900_check_id(priv)) {
free(priv);
return -1;
}
edev = (struct eth_device *)malloc(sizeof(struct eth_device));
edev = (struct eth_device *)xmalloc(sizeof(struct eth_device));
dev->type_data = edev;
edev->priv = priv;

View File

@ -661,9 +661,9 @@ int mpc5xxx_fec_probe(struct device_d *dev)
struct eth_device *edev;
mpc5xxx_fec_priv *fec;
edev = (struct eth_device *)malloc(sizeof(struct eth_device));
edev = (struct eth_device *)xmalloc(sizeof(struct eth_device));
dev->type_data = edev;
fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
fec = (mpc5xxx_fec_priv *)xmalloc(sizeof(*fec));
edev->priv = fec;
edev->open = mpc5xxx_fec_open,
edev->init = mpc5xxx_fec_init,

View File

@ -79,7 +79,7 @@ int tap_probe(struct device_d *dev)
struct tap_priv *priv;
int ret = 0;
priv = malloc(sizeof(struct tap_priv));
priv = xmalloc(sizeof(struct tap_priv));
priv->name = "barebox";
priv->fd = tap_alloc(priv->name);
@ -88,7 +88,7 @@ int tap_probe(struct device_d *dev)
goto out;
}
edev = malloc(sizeof(struct eth_device));
edev = xmalloc(sizeof(struct eth_device));
dev->type_data = edev;
edev->priv = priv;

View File

@ -305,7 +305,7 @@ static ulong flash_get_size (struct flash_info *info, ulong base)
#endif
/* first only malloc space for the first sector */
info->start = malloc(sizeof(ulong));
info->start = xmalloc(sizeof(ulong));
info->start[0] = base;
info->protect = 0;
@ -394,8 +394,8 @@ static ulong flash_get_size (struct flash_info *info, ulong base)
cur_offset += erase_region_size * erase_region_count;
/* increase the space malloced for the sector start addresses */
info->start = realloc(info->start, sizeof(ulong) * (erase_region_count + sect_cnt));
info->protect = realloc(info->protect, sizeof(uchar) * (erase_region_count + sect_cnt));
info->start = xrealloc(info->start, sizeof(ulong) * (erase_region_count + sect_cnt));
info->protect = xrealloc(info->protect, sizeof(uchar) * (erase_region_count + sect_cnt));
for (j = 0; j < erase_region_count; j++) {
info->start[sect_cnt] = sector;

View File

@ -157,8 +157,7 @@ static int pl011_probe(struct device_d *dev)
struct amba_uart_port *uart;
struct console_device *cdev;
uart = malloc(sizeof(struct amba_uart_port));
uart = xmalloc(sizeof(struct amba_uart_port));
uart->clk = clk_get(dev, NULL);
if (IS_ERR(uart->clk))

View File

@ -398,8 +398,7 @@ static int atmel_serial_probe(struct device_d *dev)
struct atmel_uart_port *uart;
struct console_device *cdev;
uart = malloc(sizeof(struct atmel_uart_port));
uart = xmalloc(sizeof(struct atmel_uart_port));
cdev = &uart->uart;
dev->type_data = cdev;
cdev->dev = dev;

View File

@ -115,7 +115,7 @@ static int blackfin_serial_probe(struct device_d *dev)
{
struct console_device *cdev;
cdev = malloc(sizeof(struct console_device));
cdev = xmalloc(sizeof(struct console_device));
dev->type_data = cdev;
cdev->dev = dev;
cdev->f_caps = CONSOLE_STDIN | CONSOLE_STDOUT | CONSOLE_STDERR;

View File

@ -319,7 +319,7 @@ static int imx_serial_probe(struct device_d *dev)
struct imx_serial_priv *priv;
uint32_t val;
priv = malloc(sizeof(*priv));
priv = xmalloc(sizeof(*priv));
cdev = &priv->cdev;
priv->regs = (void __force __iomem *)dev->map_base;

View File

@ -140,7 +140,7 @@ static int netx_serial_probe(struct device_d *dev)
{
struct console_device *cdev;
cdev = malloc(sizeof(struct console_device));
cdev = xmalloc(sizeof(struct console_device));
dev->type_data = cdev;
cdev->dev = dev;
cdev->f_caps = CONSOLE_STDIN | CONSOLE_STDOUT | CONSOLE_STDERR;

View File

@ -141,7 +141,7 @@ static int pl010_probe(struct device_d *dev)
{
struct console_device *cdev;
cdev = malloc(sizeof(struct console_device));
cdev = xmalloc(sizeof(struct console_device));
dev->type_data = cdev;
cdev->dev = dev;
cdev->f_caps = CONSOLE_STDIN | CONSOLE_STDOUT | CONSOLE_STDERR;

View File

@ -121,8 +121,7 @@ static int s3c24x0_serial_probe(struct device_d *dev)
{
struct console_device *cdev;
cdev = malloc(sizeof(struct console_device));
cdev = xmalloc(sizeof(struct console_device));
dev->type_data = cdev;
cdev->dev = dev;
cdev->f_caps = CONSOLE_STDIN | CONSOLE_STDOUT | CONSOLE_STDERR;

View File

@ -903,9 +903,6 @@ static int __init composite_bind(struct usb_gadget *gadget)
int status = -ENOMEM;
cdev = xzalloc(sizeof *cdev);
if (!cdev)
return status;
cdev->gadget = gadget;
set_gadget_data(gadget, cdev);
INIT_LIST_HEAD(&cdev->configs);

View File

@ -1016,8 +1016,6 @@ fsl_alloc_request(struct usb_ep *_ep)
struct fsl_req *req;
req = xzalloc(sizeof *req);
if (!req)
return NULL;
INIT_LIST_HEAD(&req->queue);
@ -2095,7 +2093,7 @@ static int struct_udc_setup(struct fsl_udc *udc,
udc->status_req = container_of(fsl_alloc_request(NULL),
struct fsl_req, req);
/* allocate a small amount of memory to get valid address */
udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
udc->status_req->req.buf = xmalloc(8);
udc->resume_state = USB_STATE_NOTATTACHED;
udc->usb_state = USB_STATE_POWERED;
udc->ep0_dir = 0;

View File

@ -205,11 +205,7 @@ gs_alloc_req(struct usb_ep *ep, unsigned len)
if (req != NULL) {
req->length = len;
req->buf = memalign(32, len);
if (req->buf == NULL) {
usb_ep_free_request(ep, req);
return NULL;
}
req->buf = xmemalign(32, len);
}
return req;

View File

@ -392,7 +392,7 @@ struct net_connection {
static inline char *net_alloc_packet(void)
{
return memalign(32, PKTSIZE);
return xmemalign(32, PKTSIZE);
}
struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport,

View File

@ -7,5 +7,6 @@ void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
void *xzalloc(size_t size);
char *xstrdup(const char *s);
void* xmemalign(size_t alignment, size_t bytes);
#endif /* __XFUNCS_H */

View File

@ -70,3 +70,11 @@ char *xstrdup(const char *s)
}
EXPORT_SYMBOL(xstrdup);
void* xmemalign(size_t alignment, size_t bytes)
{
void *p = memalign(alignment, bytes);
if (!p)
panic("ERROR: out of memory\n");
return p;
}
EXPORT_SYMBOL(xmemalign);

View File

@ -365,7 +365,7 @@ static struct net_connection *net_new(IPaddr_t dest, rx_handler_f *handler)
return ERR_PTR(-ENETDOWN);
con = xzalloc(sizeof(*con));
con->packet = memalign(32, PKTSIZE);
con->packet = xmemalign(32, PKTSIZE);
memset(con->packet, 0, PKTSIZE);
con->et = (struct ethernet *)con->packet;
@ -658,7 +658,7 @@ static int net_init(void)
int i;
for (i = 0; i < PKTBUFSRX; i++)
NetRxPackets[i] = memalign(32, PKTSIZE);
NetRxPackets[i] = xmemalign(32, PKTSIZE);
return 0;
}

View File

@ -122,8 +122,11 @@ int symfilecnt = 0;
void add_new_symbol(struct symfile *sym, char * symname)
{
sym->symbollist =
realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
sym->symbollist = realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
if (!sym->symbollist) {
fprintf(stderr, "docproc: out of memory\n");
exit(1);
}
sym->symbollist[sym->symbolcnt++].name = strdup(symname);
}

View File

@ -1317,7 +1317,7 @@ void buf_write(struct buffer *buf, const char *s, int len)
{
if (buf->size - buf->pos < len) {
buf->size += len + SZ;
buf->p = realloc(buf->p, buf->size);
buf->p = NOFAIL(realloc(buf->p, buf->size));
}
strncpy(buf->p + buf->pos, s, len);
buf->pos += len;