Typesafe allocators
PATCH 1/3 Typesafe allocators
While reading through the sources, I noticed that you've already diverged from "kmalloc" to xmalloc, so I thought it worth trying a typesafe allocator (something I have long missed in Linux).
And here's a patch...
Rusty.
xen/arch/x86/irq.c +1 -1
@@ -260,7 +260,7 @@ int pirq_guest_bind(struct exec_domain * goto out; } - action =xmalloc (sizeof (irq_guest_action_t)); + action =new (irq_guest_action_t);if ( (desc->action = (struct irqaction *)action) ==NULL ) {DPRINTK ("Cannot bind IRQ %d to guest. Out of memory.\n" , irq);
xen/arch/x86/mtrr/generic.c +2 -1
@@ -52,7 +52,8 @@ void __init get_mtrr_state(void)unsigned lo, dummy;if (!mtrr_state.var_ranges) { - mtrr_state.var_ranges =xmalloc (num_var_ranges *sizeof (struct mtrr_var_range )); + mtrr_state.var_ranges = new_array(struct mtrr_var_range, + num_var_ranges);if (!mtrr_state.var_ranges)return ; }
xen/arch/x86/mtrr/main.c +1 -2
@@ -136,8 +136,7 @@ static void __init init_table(void)int i, max; max = num_var_ranges; -if ((usage_table =xmalloc (max *sizeof *usage_table)) - ==NULL ) { + if ((usage_table = new_array(unsigned int, max)) == NULL) {printk (KERN_ERR "mtrr: could not allocate\n" );return ; }
xen/arch/x86/shadow.c +1 -2
@@ -176,8 +176,7 @@ int shadow_mode_enable(struct domain *p, {struct mm_struct *m = &p->exec_domain[0 ]->mm; - m->shadow_ht =xmalloc ( - shadow_ht_buckets *sizeof (struct shadow_status )); + m->shadow_ht = new_array(struct shadow_status, shadow_ht_buckets);if ( m->shadow_ht ==NULL ) goto nomem;memset (m->shadow_ht,0 , shadow_ht_buckets *sizeof (struct shadow_status ));
xen/arch/x86/smpboot.c +1 -1
@@ -409,7 +409,7 @@ void __init start_secondary(void)* At this point, boot CPU has fully initialised the IDT. It is * now safe to make ourselves a private copy. */ - idt_tables[cpu] =xmalloc (IDT_ENTRIES *8 ); + idt_tables[cpu] = new_array(struct desc_struct, IDT_ENTRIES);memcpy (idt_tables[cpu], idt_table,IDT_ENTRIES *8 ); *(unsigned short *)(&idt_load[0 ]) = (IDT_ENTRIES *8 )-1 ; *(unsigned long *)(&idt_load[2 ]) = (unsigned long )idt_tables[cpu];
xen/common/ac_timer.c +2 -3
@@ -130,7 +130,7 @@ static int add_entry(struct ac_timer **hif (unlikely (sz ==GET_HEAP_LIMIT (heap)) ) {int i, limit = (GET_HEAP_LIMIT (heap)+1 ) <<1 ; -struct ac_timer **new_heap =xmalloc (limit*sizeof (struct ac_timer *)); + struct ac_timer **new_heap = new_array(struct ac_timer *, limit);if ( new_heap ==NULL )BUG ();memcpy (new_heap, heap, (limit>>1 )*sizeof (struct ac_timer *));for ( i =0 ; i < smp_num_cpus; i++ )
@@ -278,8 +278,7 @@ void __init ac_timer_init(void)for ( i =0 ; i < smp_num_cpus; i++ ) { - ac_timers[i].heap =xmalloc ( - (DEFAULT_HEAP_LIMIT +1 ) *sizeof (struct ac_timer *)); + ac_timers[i].heap = new_array(struct ac_timer *, DEFAULT_HEAP_LIMIT+1);if ( ac_timers[i].heap ==NULL )BUG ();SET_HEAP_SIZE (ac_timers[i].heap,0 );SET_HEAP_LIMIT (ac_timers[i].heap,DEFAULT_HEAP_LIMIT );
xen/common/dom0_ops.c +1 -1
@@ -383,7 +383,7 @@ long do_dom0_op(dom0_op_t *u_dom0_op)if ( op->u.getdomaininfo.ctxt !=NULL ) { -if ( (c =xmalloc (sizeof (*c))) ==NULL ) +if ( (c =new (full_execution_context_t)) ==NULL ) { ret = -ENOMEM ;put_domain (d);
xen/common/domain.c +2 -2
@@ -264,7 +264,7 @@ int final_setup_guestos(struct domain *pint rc =0 ;full_execution_context_t *c; -if ( (c =xmalloc (sizeof (*c))) ==NULL ) +if ( (c =new (full_execution_context_t)) ==NULL )return -ENOMEM ;if (test_bit (DF_CONSTRUCTED , &p->d_flags) )
@@ -311,7 +311,7 @@ long do_boot_vcpu(unsigned long vcpu, fuif (alloc_exec_domain_struct (d, vcpu) ==NULL )return -ENOMEM ; -if ( (c =xmalloc (sizeof (*c))) ==NULL ) +if ( (c =new (full_execution_context_t)) ==NULL ) { rc = -ENOMEM ; goto out;
xen/common/event_channel.c +1 -1
@@ -54,7 +54,7 @@ static int get_free_port(struct exec_domelse max = port +EVENT_CHANNELS_SPREAD ; - chn =xmalloc (max *sizeof (event_channel_t)); + chn =new_array (event_channel_t, max);if (unlikely (chn ==NULL ) )return -ENOMEM ;
xen/common/grant_table.c +3 -3
@@ -565,7 +565,7 @@ grant_table_create(grant_table_t *t;int i; -if ( (t =xmalloc (sizeof (*t))) ==NULL ) +if ( (t =new (grant_table_t)) ==NULL ) goto no_mem;/* Simple stuff. */
@@ -573,8 +573,8 @@ grant_table_create(spin_lock_init (&t->lock);/* Active grant table. */ -if ( (t->active =xmalloc (sizeof (active_grant_entry_t) * -NR_GRANT_ENTRIES )) ==NULL ) +if ( (t->active =new_array (active_grant_entry_t,NR_GRANT_ENTRIES )) + ==NULL ) goto no_mem;memset (t->active,0 ,sizeof (active_grant_entry_t) *NR_GRANT_ENTRIES );
xen/common/physdev.c +3 -3
@@ -98,7 +98,7 @@ static void add_dev_to_task(struct domaireturn ; } -if ( (pdev =xmalloc (sizeof (phys_dev_t))) ==NULL ) +if ( (pdev =new (phys_dev_t)) ==NULL ) {INFO ("Error allocating pdev structure.\n" );return ;
@@ -174,7 +174,7 @@ int physdev_pci_access_modify(if ( ed->thread.io_bitmap ==NULL ) { -if ( (ed->thread.io_bitmap =xmalloc (IOBMP_BYTES )) ==NULL ) +if ( (ed->thread.io_bitmap =new_array (u8,IOBMP_BYTES )) ==NULL ) { rc = -ENOMEM ; goto out;
@@ -765,7 +765,7 @@ void physdev_init_dom0(struct domain *p)if ( (dev->hdr_type !=PCI_HEADER_TYPE_NORMAL ) && (dev->hdr_type !=PCI_HEADER_TYPE_CARDBUS ) )continue ; - pdev =xmalloc (sizeof (phys_dev_t)); + pdev =new (phys_dev_t); pdev->dev = dev; pdev->flags =ACC_WRITE ; pdev->state =0 ;
xen/common/resource.c +1 -1
@@ -220,7 +220,7 @@ int allocate_resource(struct resource *r*/ struct resource *__request_region (struct resource *parent,unsigned long start,unsigned long n,const char *name) { -struct resource *res =xmalloc (sizeof (*res)); + struct resource *res = new(struct resource);if (res) {memset (res,0 ,sizeof (*res));
xen/common/sched_atropos.c +1 -1
@@ -173,7 +173,7 @@ static int at_alloc_task(struct domain * {ASSERT (p !=NULL ); - p->sched_priv =xmem_cache_alloc (dom_info_cache); + p->sched_priv = new(struct at_dom_info);if ( p->sched_priv ==NULL )return -1 ;
xen/common/sched_bvt.c +1 -1
@@ -557,7 +557,7 @@ int bvt_init_scheduler()for ( i =0 ; i <NR_CPUS ; i++ ) { - schedule_data[i].sched_priv =xmalloc (sizeof (struct bvt_cpu_info )); + schedule_data[i].sched_priv = new(struct bvt_cpu_info);if ( schedule_data[i].sched_priv ==NULL ) {
xen/drivers/pci/pci.c +3 -3
@@ -1126,7 +1126,7 @@ static struct pci_bus * __devinit pci_al {struct pci_bus *b; - b =xmalloc (sizeof (*b)); + b = new(struct pci_bus);if (b) {memset (b,0 ,sizeof (*b));INIT_LIST_HEAD (&b->children);
@@ -1351,7 +1351,7 @@ struct pci_dev * __devinit pci_scan_deviif (l ==0xffffffff || l ==0x00000000 || l ==0x0000ffff || l ==0xffff0000 )return NULL ; - dev =xmalloc (sizeof (*dev)); + dev = new(struct pci_dev);if (!dev)return NULL ;
@@ -1431,7 +1431,7 @@ unsigned int __devinit pci_do_scan_bus(s max = bus->secondary;/* Create a device template */ - dev0 =xmalloc (sizeof (struct pci_dev )); + dev0 = new(struct pci_dev);if (!dev0) {panic ("Out of memory scanning PCI bus!\n" ); }
xen/drivers/pci/setup-res.c +2 -2
@@ -171,10 +171,10 @@ pdev_sort_resources(struct pci_dev *dev, ln->res->start; }if (r_align > align) { - tmp =xmalloc (sizeof (*tmp)); + tmp = new(struct resource_list);if (!tmp)panic ("pdev_sort_resources(): " -"xmalloc() failed!\n" ); +"malloc() failed!\n" ); tmp->next = ln; tmp->res = r; tmp->dev = dev;
xen/include/xen/slab.h +12 -0
@@ -18,6 +18,7 @@ typedef struct xmem_cache_s xmem_cache_t#include <xen/mm.h> #include <xen/cache.h> +#include <xen/types.h> /* Flags to pass to xmem_cache_create(). */ /* NB. The first 3 are only valid when built with SLAB_DEBUG_SUPPORT. */
@@ -52,6 +53,17 @@ extern int xmem_cache_reap(void);extern void dump_slabinfo (); +/* Nicely typesafe for you. */ +#define new (type) ((type *)xmalloc(sizeof(type))) +#define new_array (type, num) ((type *)xmalloc_array(sizeof(type), (num))) + +static inline void *xmalloc_array (size_t size,size_t num) +{ +/* Check for overflow. */ +if (size && num >UINT_MAX / size) +return NULL ; +return xmalloc (size * num); +}#endif /* __ARCH_HAS_SLAB_ALLOCATOR */ #endif /* __SLAB_H__ */
Signature · 9 lines
A bad analogy is like a leaky screwdriver -- Richard Braakman ------------------------------------------------------- This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting Tool for open source databases. Create drag-&-drop reports. Save time by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. Download a FREE copy at http://www.intelliview.com/go/osdn_nl