ia64/xen-unstable
changeset 3612:beb0887c54bc
bitkeeper revision 1.1159.238.1 (4200c8d8KsGlaM3w6o3y4GHhK1jKjg)
A typesafe allocator submitted by Rusty Russel with trivial renames by me.
Signed-off-by: Rusty Russel <rusty@rustcorp.com.au> (authored)
Signed-off-by: ian.pratt@cl.cam.ac.uk
A typesafe allocator submitted by Rusty Russel with trivial renames by me.
Signed-off-by: Rusty Russel <rusty@rustcorp.com.au> (authored)
Signed-off-by: ian.pratt@cl.cam.ac.uk
author | iap10@labyrinth.cl.cam.ac.uk |
---|---|
date | Wed Feb 02 12:34:32 2005 +0000 (2005-02-02) |
parents | fd1dd0663b09 |
children | f98fa170a9f4 |
files | xen/arch/x86/irq.c xen/arch/x86/mtrr/generic.c xen/arch/x86/mtrr/main.c xen/arch/x86/shadow.c xen/arch/x86/smpboot.c xen/common/ac_timer.c xen/common/dom0_ops.c xen/common/domain.c xen/common/event_channel.c xen/common/grant_table.c xen/common/physdev.c xen/common/resource.c xen/common/sched_atropos.c xen/common/sched_bvt.c xen/drivers/pci/pci.c xen/drivers/pci/setup-res.c xen/include/xen/slab.h |
line diff
1.1 --- a/xen/arch/x86/irq.c Wed Feb 02 00:26:53 2005 +0000 1.2 +++ b/xen/arch/x86/irq.c Wed Feb 02 12:34:32 2005 +0000 1.3 @@ -260,7 +260,7 @@ int pirq_guest_bind(struct exec_domain * 1.4 goto out; 1.5 } 1.6 1.7 - action = xmalloc(sizeof(irq_guest_action_t)); 1.8 + action = xmalloc(irq_guest_action_t); 1.9 if ( (desc->action = (struct irqaction *)action) == NULL ) 1.10 { 1.11 DPRINTK("Cannot bind IRQ %d to guest. Out of memory.\n", irq);
2.1 --- a/xen/arch/x86/mtrr/generic.c Wed Feb 02 00:26:53 2005 +0000 2.2 +++ b/xen/arch/x86/mtrr/generic.c Wed Feb 02 12:34:32 2005 +0000 2.3 @@ -52,7 +52,8 @@ void __init get_mtrr_state(void) 2.4 unsigned lo, dummy; 2.5 2.6 if (!mtrr_state.var_ranges) { 2.7 - mtrr_state.var_ranges = xmalloc(num_var_ranges * sizeof (struct mtrr_var_range)); 2.8 + mtrr_state.var_ranges = xmalloc_array(struct mtrr_var_range, 2.9 + num_var_ranges); 2.10 if (!mtrr_state.var_ranges) 2.11 return; 2.12 }
3.1 --- a/xen/arch/x86/mtrr/main.c Wed Feb 02 00:26:53 2005 +0000 3.2 +++ b/xen/arch/x86/mtrr/main.c Wed Feb 02 12:34:32 2005 +0000 3.3 @@ -136,8 +136,7 @@ static void __init init_table(void) 3.4 int i, max; 3.5 3.6 max = num_var_ranges; 3.7 - if ((usage_table = xmalloc(max * sizeof *usage_table)) 3.8 - == NULL) { 3.9 + if ((usage_table = xmalloc_array(unsigned int, max)) == NULL) { 3.10 printk(KERN_ERR "mtrr: could not allocate\n"); 3.11 return; 3.12 }
4.1 --- a/xen/arch/x86/shadow.c Wed Feb 02 00:26:53 2005 +0000 4.2 +++ b/xen/arch/x86/shadow.c Wed Feb 02 12:34:32 2005 +0000 4.3 @@ -176,8 +176,7 @@ int shadow_mode_enable(struct domain *p, 4.4 { 4.5 struct mm_struct *m = &p->exec_domain[0]->mm; 4.6 4.7 - m->shadow_ht = xmalloc( 4.8 - shadow_ht_buckets * sizeof(struct shadow_status)); 4.9 + m->shadow_ht = xmalloc_array(struct shadow_status, shadow_ht_buckets); 4.10 if ( m->shadow_ht == NULL ) 4.11 goto nomem; 4.12 memset(m->shadow_ht, 0, shadow_ht_buckets * sizeof(struct shadow_status));
5.1 --- a/xen/arch/x86/smpboot.c Wed Feb 02 00:26:53 2005 +0000 5.2 +++ b/xen/arch/x86/smpboot.c Wed Feb 02 12:34:32 2005 +0000 5.3 @@ -409,7 +409,7 @@ void __init start_secondary(void) 5.4 * At this point, boot CPU has fully initialised the IDT. It is 5.5 * now safe to make ourselves a private copy. 5.6 */ 5.7 - idt_tables[cpu] = xmalloc(IDT_ENTRIES*8); 5.8 + idt_tables[cpu] = xmalloc_array(struct desc_struct, IDT_ENTRIES); 5.9 memcpy(idt_tables[cpu], idt_table, IDT_ENTRIES*8); 5.10 *(unsigned short *)(&idt_load[0]) = (IDT_ENTRIES*8)-1; 5.11 *(unsigned long *)(&idt_load[2]) = (unsigned long)idt_tables[cpu];
6.1 --- a/xen/common/ac_timer.c Wed Feb 02 00:26:53 2005 +0000 6.2 +++ b/xen/common/ac_timer.c Wed Feb 02 12:34:32 2005 +0000 6.3 @@ -130,7 +130,7 @@ static int add_entry(struct ac_timer **h 6.4 if ( unlikely(sz == GET_HEAP_LIMIT(heap)) ) 6.5 { 6.6 int i, limit = (GET_HEAP_LIMIT(heap)+1) << 1; 6.7 - struct ac_timer **new_heap = xmalloc(limit*sizeof(struct ac_timer *)); 6.8 + struct ac_timer **new_heap = xmalloc_array(struct ac_timer *, limit); 6.9 if ( new_heap == NULL ) BUG(); 6.10 memcpy(new_heap, heap, (limit>>1)*sizeof(struct ac_timer *)); 6.11 for ( i = 0; i < smp_num_cpus; i++ ) 6.12 @@ -278,8 +278,7 @@ void __init ac_timer_init(void) 6.13 6.14 for ( i = 0; i < smp_num_cpus; i++ ) 6.15 { 6.16 - ac_timers[i].heap = xmalloc( 6.17 - (DEFAULT_HEAP_LIMIT+1) * sizeof(struct ac_timer *)); 6.18 + ac_timers[i].heap = xmalloc_array(struct ac_timer *, DEFAULT_HEAP_LIMIT+1); 6.19 if ( ac_timers[i].heap == NULL ) BUG(); 6.20 SET_HEAP_SIZE(ac_timers[i].heap, 0); 6.21 SET_HEAP_LIMIT(ac_timers[i].heap, DEFAULT_HEAP_LIMIT);
7.1 --- a/xen/common/dom0_ops.c Wed Feb 02 00:26:53 2005 +0000 7.2 +++ b/xen/common/dom0_ops.c Wed Feb 02 12:34:32 2005 +0000 7.3 @@ -383,7 +383,7 @@ long do_dom0_op(dom0_op_t *u_dom0_op) 7.4 7.5 if ( op->u.getdomaininfo.ctxt != NULL ) 7.6 { 7.7 - if ( (c = xmalloc(sizeof(*c))) == NULL ) 7.8 + if ( (c = xmalloc(full_execution_context_t)) == NULL ) 7.9 { 7.10 ret = -ENOMEM; 7.11 put_domain(d);
8.1 --- a/xen/common/domain.c Wed Feb 02 00:26:53 2005 +0000 8.2 +++ b/xen/common/domain.c Wed Feb 02 12:34:32 2005 +0000 8.3 @@ -264,7 +264,7 @@ int final_setup_guestos(struct domain *p 8.4 int rc = 0; 8.5 full_execution_context_t *c; 8.6 8.7 - if ( (c = xmalloc(sizeof(*c))) == NULL ) 8.8 + if ( (c = xmalloc(full_execution_context_t)) == NULL ) 8.9 return -ENOMEM; 8.10 8.11 if ( test_bit(DF_CONSTRUCTED, &p->d_flags) ) 8.12 @@ -311,7 +311,7 @@ long do_boot_vcpu(unsigned long vcpu, fu 8.13 if ( alloc_exec_domain_struct(d, vcpu) == NULL ) 8.14 return -ENOMEM; 8.15 8.16 - if ( (c = xmalloc(sizeof(*c))) == NULL ) 8.17 + if ( (c = xmalloc(full_execution_context_t)) == NULL ) 8.18 { 8.19 rc = -ENOMEM; 8.20 goto out;
9.1 --- a/xen/common/event_channel.c Wed Feb 02 00:26:53 2005 +0000 9.2 +++ b/xen/common/event_channel.c Wed Feb 02 12:34:32 2005 +0000 9.3 @@ -54,7 +54,7 @@ static int get_free_port(struct exec_dom 9.4 else 9.5 max = port + EVENT_CHANNELS_SPREAD; 9.6 9.7 - chn = xmalloc(max * sizeof(event_channel_t)); 9.8 + chn = xmalloc_array(event_channel_t, max); 9.9 if ( unlikely(chn == NULL) ) 9.10 return -ENOMEM; 9.11
10.1 --- a/xen/common/grant_table.c Wed Feb 02 00:26:53 2005 +0000 10.2 +++ b/xen/common/grant_table.c Wed Feb 02 12:34:32 2005 +0000 10.3 @@ -565,7 +565,7 @@ grant_table_create( 10.4 grant_table_t *t; 10.5 int i; 10.6 10.7 - if ( (t = xmalloc(sizeof(*t))) == NULL ) 10.8 + if ( (t = xmalloc(grant_table_t)) == NULL ) 10.9 goto no_mem; 10.10 10.11 /* Simple stuff. */ 10.12 @@ -573,8 +573,8 @@ grant_table_create( 10.13 spin_lock_init(&t->lock); 10.14 10.15 /* Active grant table. */ 10.16 - if ( (t->active = xmalloc(sizeof(active_grant_entry_t) * 10.17 - NR_GRANT_ENTRIES)) == NULL ) 10.18 + if ( (t->active = xmalloc_array(active_grant_entry_t, NR_GRANT_ENTRIES)) 10.19 + == NULL ) 10.20 goto no_mem; 10.21 memset(t->active, 0, sizeof(active_grant_entry_t) * NR_GRANT_ENTRIES); 10.22
11.1 --- a/xen/common/physdev.c Wed Feb 02 00:26:53 2005 +0000 11.2 +++ b/xen/common/physdev.c Wed Feb 02 12:34:32 2005 +0000 11.3 @@ -98,7 +98,7 @@ static void add_dev_to_task(struct domai 11.4 return; 11.5 } 11.6 11.7 - if ( (pdev = xmalloc(sizeof(phys_dev_t))) == NULL ) 11.8 + if ( (pdev = xmalloc(phys_dev_t)) == NULL ) 11.9 { 11.10 INFO("Error allocating pdev structure.\n"); 11.11 return; 11.12 @@ -174,7 +174,7 @@ int physdev_pci_access_modify( 11.13 11.14 if ( ed->thread.io_bitmap == NULL ) 11.15 { 11.16 - if ( (ed->thread.io_bitmap = xmalloc(IOBMP_BYTES)) == NULL ) 11.17 + if ( (ed->thread.io_bitmap = xmalloc_array(u8, IOBMP_BYTES)) == NULL ) 11.18 { 11.19 rc = -ENOMEM; 11.20 goto out; 11.21 @@ -765,7 +765,7 @@ void physdev_init_dom0(struct domain *p) 11.22 if ( (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) && 11.23 (dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) ) 11.24 continue; 11.25 - pdev = xmalloc(sizeof(phys_dev_t)); 11.26 + pdev = xmalloc(phys_dev_t); 11.27 pdev->dev = dev; 11.28 pdev->flags = ACC_WRITE; 11.29 pdev->state = 0;
12.1 --- a/xen/common/resource.c Wed Feb 02 00:26:53 2005 +0000 12.2 +++ b/xen/common/resource.c Wed Feb 02 12:34:32 2005 +0000 12.3 @@ -220,7 +220,7 @@ int allocate_resource(struct resource *r 12.4 */ 12.5 struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name) 12.6 { 12.7 - struct resource *res = xmalloc(sizeof(*res)); 12.8 + struct resource *res = xmalloc(struct resource); 12.9 12.10 if (res) { 12.11 memset(res, 0, sizeof(*res));
13.1 --- a/xen/common/sched_atropos.c Wed Feb 02 00:26:53 2005 +0000 13.2 +++ b/xen/common/sched_atropos.c Wed Feb 02 12:34:32 2005 +0000 13.3 @@ -173,7 +173,7 @@ static int at_alloc_task(struct domain * 13.4 { 13.5 ASSERT(p != NULL); 13.6 13.7 - p->sched_priv = xmem_cache_alloc(dom_info_cache); 13.8 + p->sched_priv = xmalloc(struct at_dom_info); 13.9 if ( p->sched_priv == NULL ) 13.10 return -1; 13.11
14.1 --- a/xen/common/sched_bvt.c Wed Feb 02 00:26:53 2005 +0000 14.2 +++ b/xen/common/sched_bvt.c Wed Feb 02 12:34:32 2005 +0000 14.3 @@ -557,7 +557,7 @@ int bvt_init_scheduler() 14.4 14.5 for ( i = 0; i < NR_CPUS; i++ ) 14.6 { 14.7 - schedule_data[i].sched_priv = xmalloc(sizeof(struct bvt_cpu_info)); 14.8 + schedule_data[i].sched_priv = xmalloc(struct bvt_cpu_info); 14.9 14.10 if ( schedule_data[i].sched_priv == NULL ) 14.11 {
15.1 --- a/xen/drivers/pci/pci.c Wed Feb 02 00:26:53 2005 +0000 15.2 +++ b/xen/drivers/pci/pci.c Wed Feb 02 12:34:32 2005 +0000 15.3 @@ -1126,7 +1126,7 @@ static struct pci_bus * __devinit pci_al 15.4 { 15.5 struct pci_bus *b; 15.6 15.7 - b = xmalloc(sizeof(*b)); 15.8 + b = xmalloc(struct pci_bus); 15.9 if (b) { 15.10 memset(b, 0, sizeof(*b)); 15.11 INIT_LIST_HEAD(&b->children); 15.12 @@ -1351,7 +1351,7 @@ struct pci_dev * __devinit pci_scan_devi 15.13 if (l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000) 15.14 return NULL; 15.15 15.16 - dev = xmalloc(sizeof(*dev)); 15.17 + dev = xmalloc(struct pci_dev); 15.18 if (!dev) 15.19 return NULL; 15.20 15.21 @@ -1431,7 +1431,7 @@ unsigned int __devinit pci_do_scan_bus(s 15.22 max = bus->secondary; 15.23 15.24 /* Create a device template */ 15.25 - dev0 = xmalloc(sizeof(struct pci_dev)); 15.26 + dev0 = xmalloc(struct pci_dev); 15.27 if(!dev0) { 15.28 panic("Out of memory scanning PCI bus!\n"); 15.29 }
16.1 --- a/xen/drivers/pci/setup-res.c Wed Feb 02 00:26:53 2005 +0000 16.2 +++ b/xen/drivers/pci/setup-res.c Wed Feb 02 12:34:32 2005 +0000 16.3 @@ -171,10 +171,10 @@ pdev_sort_resources(struct pci_dev *dev, 16.4 ln->res->start; 16.5 } 16.6 if (r_align > align) { 16.7 - tmp = xmalloc(sizeof(*tmp)); 16.8 + tmp = xmalloc(struct resource_list); 16.9 if (!tmp) 16.10 panic("pdev_sort_resources(): " 16.11 - "xmalloc() failed!\n"); 16.12 + "malloc() failed!\n"); 16.13 tmp->next = ln; 16.14 tmp->res = r; 16.15 tmp->dev = dev;
17.1 --- a/xen/include/xen/slab.h Wed Feb 02 00:26:53 2005 +0000 17.2 +++ b/xen/include/xen/slab.h Wed Feb 02 12:34:32 2005 +0000 17.3 @@ -18,6 +18,7 @@ typedef struct xmem_cache_s xmem_cache_t 17.4 17.5 #include <xen/mm.h> 17.6 #include <xen/cache.h> 17.7 +#include <xen/types.h> 17.8 17.9 /* Flags to pass to xmem_cache_create(). */ 17.10 /* NB. The first 3 are only valid when built with SLAB_DEBUG_SUPPORT. */ 17.11 @@ -52,6 +53,17 @@ extern int xmem_cache_reap(void); 17.12 17.13 extern void dump_slabinfo(); 17.14 17.15 +/* Nicely typesafe for you. */ 17.16 +#define xmalloc(type) ((type *)xmalloc(sizeof(type))) 17.17 +#define xmalloc_array(type, num) ((type *)xmalloc_array(sizeof(type), (num))) 17.18 + 17.19 +static inline void *xmalloc_array(size_t size, size_t num) 17.20 +{ 17.21 + /* Check for overflow. */ 17.22 + if (size && num > UINT_MAX / size) 17.23 + return NULL; 17.24 + return xmalloc(size * num); 17.25 +} 17.26 #endif /* __ARCH_HAS_SLAB_ALLOCATOR */ 17.27 17.28 #endif /* __SLAB_H__ */