WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 1/3] Typesafe allocators

To: Xen Mailing List <xen-devel@xxxxxxxxxxxxxxxxxxxxx>
Subject: [Xen-devel] [PATCH 1/3] Typesafe allocators
From: Rusty Russell <rusty@xxxxxxxxxxxxxxx>
Date: Wed, 02 Feb 2005 16:07:05 +1100
Delivery-date: Wed, 02 Feb 2005 05:09:20 +0000
Envelope-to: xen+James.Bulpin@xxxxxxxxxxxx
List-archive: <http://sourceforge.net/mailarchive/forum.php?forum=xen-devel>
List-help: <mailto:xen-devel-request@lists.sourceforge.net?subject=help>
List-id: List for Xen developers <xen-devel.lists.sourceforge.net>
List-post: <mailto:xen-devel@lists.sourceforge.net>
List-subscribe: <https://lists.sourceforge.net/lists/listinfo/xen-devel>, <mailto:xen-devel-request@lists.sourceforge.net?subject=subscribe>
List-unsubscribe: <https://lists.sourceforge.net/lists/listinfo/xen-devel>, <mailto:xen-devel-request@lists.sourceforge.net?subject=unsubscribe>
Sender: xen-devel-admin@xxxxxxxxxxxxxxxxxxxxx
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.

diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/arch/x86/irq.c xen-unstable-working/xen/arch/x86/irq.c
--- xen-unstable/xen/arch/x86/irq.c     2005-01-30 15:28:43.000000000 +1100
+++ xen-unstable-working/xen/arch/x86/irq.c     2005-01-31 16:53:54.000000000 
+1100
@@ -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);
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/arch/x86/mtrr/generic.c 
xen-unstable-working/xen/arch/x86/mtrr/generic.c
--- xen-unstable/xen/arch/x86/mtrr/generic.c    2005-01-30 15:28:42.000000000 
+1100
+++ xen-unstable-working/xen/arch/x86/mtrr/generic.c    2005-01-31 
16:53:54.000000000 +1100
@@ -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;
        } 
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/arch/x86/mtrr/main.c 
xen-unstable-working/xen/arch/x86/mtrr/main.c
--- xen-unstable/xen/arch/x86/mtrr/main.c       2005-01-30 15:28:43.000000000 
+1100
+++ xen-unstable-working/xen/arch/x86/mtrr/main.c       2005-01-31 
16:53:54.000000000 +1100
@@ -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;
        }
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/arch/x86/shadow.c xen-unstable-working/xen/arch/x86/shadow.c
--- xen-unstable/xen/arch/x86/shadow.c  2005-01-30 15:28:47.000000000 +1100
+++ xen-unstable-working/xen/arch/x86/shadow.c  2005-01-31 16:53:54.000000000 
+1100
@@ -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));
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/arch/x86/smpboot.c xen-unstable-working/xen/arch/x86/smpboot.c
--- xen-unstable/xen/arch/x86/smpboot.c 2005-01-30 15:28:44.000000000 +1100
+++ xen-unstable-working/xen/arch/x86/smpboot.c 2005-01-31 16:53:54.000000000 
+1100
@@ -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];
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/ac_timer.c xen-unstable-working/xen/common/ac_timer.c
--- xen-unstable/xen/common/ac_timer.c  2005-01-30 15:28:46.000000000 +1100
+++ xen-unstable-working/xen/common/ac_timer.c  2005-01-31 16:54:13.000000000 
+1100
@@ -130,7 +130,7 @@ static int add_entry(struct ac_timer **h
     if ( 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);
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/dom0_ops.c xen-unstable-working/xen/common/dom0_ops.c
--- xen-unstable/xen/common/dom0_ops.c  2005-01-30 15:28:43.000000000 +1100
+++ xen-unstable-working/xen/common/dom0_ops.c  2005-01-31 16:54:13.000000000 
+1100
@@ -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);
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/domain.c xen-unstable-working/xen/common/domain.c
--- xen-unstable/xen/common/domain.c    2005-01-30 15:28:49.000000000 +1100
+++ xen-unstable-working/xen/common/domain.c    2005-01-31 16:54:13.000000000 
+1100
@@ -264,7 +264,7 @@ int final_setup_guestos(struct domain *p
     int 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, fu
     if ( 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;
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/event_channel.c 
xen-unstable-working/xen/common/event_channel.c
--- xen-unstable/xen/common/event_channel.c     2005-01-30 15:28:47.000000000 
+1100
+++ xen-unstable-working/xen/common/event_channel.c     2005-01-31 
16:54:13.000000000 +1100
@@ -54,7 +54,7 @@ static int get_free_port(struct exec_dom
         else
             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;
 
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/grant_table.c 
xen-unstable-working/xen/common/grant_table.c
--- xen-unstable/xen/common/grant_table.c       2005-01-30 15:28:49.000000000 
+1100
+++ xen-unstable-working/xen/common/grant_table.c       2005-01-31 
16:54:13.000000000 +1100
@@ -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);
 
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/physdev.c xen-unstable-working/xen/common/physdev.c
--- xen-unstable/xen/common/physdev.c   2005-01-30 15:28:43.000000000 +1100
+++ xen-unstable-working/xen/common/physdev.c   2005-01-31 16:54:13.000000000 
+1100
@@ -98,7 +98,7 @@ static void add_dev_to_task(struct domai
         return;
     }
 
-    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;
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/resource.c xen-unstable-working/xen/common/resource.c
--- xen-unstable/xen/common/resource.c  2005-01-30 15:28:45.000000000 +1100
+++ xen-unstable-working/xen/common/resource.c  2005-01-31 16:54:13.000000000 
+1100
@@ -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));
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/sched_atropos.c 
xen-unstable-working/xen/common/sched_atropos.c
--- xen-unstable/xen/common/sched_atropos.c     2005-01-30 15:28:43.000000000 
+1100
+++ xen-unstable-working/xen/common/sched_atropos.c     2005-01-31 
16:54:13.000000000 +1100
@@ -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;
     
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/common/sched_bvt.c xen-unstable-working/xen/common/sched_bvt.c
--- xen-unstable/xen/common/sched_bvt.c 2005-01-30 15:28:51.000000000 +1100
+++ xen-unstable-working/xen/common/sched_bvt.c 2005-01-31 16:54:13.000000000 
+1100
@@ -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 )
         {
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/drivers/pci/pci.c xen-unstable-working/xen/drivers/pci/pci.c
--- xen-unstable/xen/drivers/pci/pci.c  2005-01-30 15:28:49.000000000 +1100
+++ xen-unstable-working/xen/drivers/pci/pci.c  2005-01-31 16:54:13.000000000 
+1100
@@ -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_devi
        if (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");
        }
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/drivers/pci/setup-res.c 
xen-unstable-working/xen/drivers/pci/setup-res.c
--- xen-unstable/xen/drivers/pci/setup-res.c    2005-01-30 15:28:55.000000000 
+1100
+++ xen-unstable-working/xen/drivers/pci/setup-res.c    2005-01-31 
16:54:13.000000000 +1100
@@ -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;
diff -urpN --exclude TAGS -X 
/home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal 
xen-unstable/xen/include/xen/slab.h xen-unstable-working/xen/include/xen/slab.h
--- xen-unstable/xen/include/xen/slab.h 2005-01-30 15:28:52.000000000 +1100
+++ xen-unstable-working/xen/include/xen/slab.h 2005-01-31 17:07:04.000000000 
+1100
@@ -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__ */

-- 
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
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/xen-devel