ia64/xen-unstable

changeset 6555:dfaf788ab18c

Merge.
author adsharma@los-vmm.sc.intel.com
date Fri Aug 26 13:47:16 2005 -0700 (2005-08-26)
parents de3576a1c62c 3bbc9384be3f
children a698bd49931b df589c4cf1ad
files .hgignore Config.mk Makefile buildconfigs/Rules.mk buildconfigs/mk.linux-2.6-xen buildconfigs/mk.linux-2.6-xen0 buildconfigs/mk.linux-2.6-xenU docs/src/user.tex extras/mini-os/include/hypervisor.h extras/mini-os/include/lib.h extras/mini-os/include/mm.h extras/mini-os/include/time.h extras/mini-os/include/types.h extras/mini-os/include/xmalloc.h extras/mini-os/kernel.c extras/mini-os/lib/xmalloc.c extras/mini-os/mm.c extras/mini-os/time.c linux-2.4-xen-sparse/Makefile linux-2.4-xen-sparse/arch/xen/Makefile linux-2.4-xen-sparse/arch/xen/boot/Makefile linux-2.4-xen-sparse/arch/xen/config.in linux-2.4-xen-sparse/arch/xen/defconfig-xen0 linux-2.4-xen-sparse/arch/xen/defconfig-xenU linux-2.4-xen-sparse/arch/xen/drivers/balloon/Makefile linux-2.4-xen-sparse/arch/xen/drivers/blkif/Makefile linux-2.4-xen-sparse/arch/xen/drivers/blkif/backend/Makefile linux-2.4-xen-sparse/arch/xen/drivers/blkif/frontend/Makefile linux-2.4-xen-sparse/arch/xen/drivers/blkif/frontend/common.h linux-2.4-xen-sparse/arch/xen/drivers/blkif/frontend/vbd.c linux-2.4-xen-sparse/arch/xen/drivers/console/Makefile linux-2.4-xen-sparse/arch/xen/drivers/dom0/Makefile linux-2.4-xen-sparse/arch/xen/drivers/evtchn/Makefile linux-2.4-xen-sparse/arch/xen/drivers/netif/Makefile linux-2.4-xen-sparse/arch/xen/drivers/netif/backend/Makefile linux-2.4-xen-sparse/arch/xen/drivers/netif/frontend/Makefile linux-2.4-xen-sparse/arch/xen/kernel/Makefile linux-2.4-xen-sparse/arch/xen/kernel/entry.S linux-2.4-xen-sparse/arch/xen/kernel/head.S linux-2.4-xen-sparse/arch/xen/kernel/i386_ksyms.c linux-2.4-xen-sparse/arch/xen/kernel/irq.c linux-2.4-xen-sparse/arch/xen/kernel/ldt.c linux-2.4-xen-sparse/arch/xen/kernel/pci-pc.c linux-2.4-xen-sparse/arch/xen/kernel/process.c linux-2.4-xen-sparse/arch/xen/kernel/setup.c linux-2.4-xen-sparse/arch/xen/kernel/signal.c linux-2.4-xen-sparse/arch/xen/kernel/time.c linux-2.4-xen-sparse/arch/xen/kernel/traps.c linux-2.4-xen-sparse/arch/xen/lib/Makefile linux-2.4-xen-sparse/arch/xen/lib/delay.c linux-2.4-xen-sparse/arch/xen/mm/Makefile linux-2.4-xen-sparse/arch/xen/mm/fault.c linux-2.4-xen-sparse/arch/xen/mm/init.c linux-2.4-xen-sparse/arch/xen/mm/ioremap.c linux-2.4-xen-sparse/arch/xen/vmlinux.lds linux-2.4-xen-sparse/drivers/block/ll_rw_blk.c linux-2.4-xen-sparse/drivers/char/Makefile linux-2.4-xen-sparse/drivers/char/mem.c linux-2.4-xen-sparse/drivers/char/tty_io.c linux-2.4-xen-sparse/drivers/scsi/aic7xxx/Makefile linux-2.4-xen-sparse/include/asm-xen/bugs.h linux-2.4-xen-sparse/include/asm-xen/desc.h linux-2.4-xen-sparse/include/asm-xen/fixmap.h linux-2.4-xen-sparse/include/asm-xen/highmem.h linux-2.4-xen-sparse/include/asm-xen/hw_irq.h linux-2.4-xen-sparse/include/asm-xen/io.h linux-2.4-xen-sparse/include/asm-xen/irq.h linux-2.4-xen-sparse/include/asm-xen/keyboard.h linux-2.4-xen-sparse/include/asm-xen/mmu_context.h linux-2.4-xen-sparse/include/asm-xen/module.h linux-2.4-xen-sparse/include/asm-xen/page.h linux-2.4-xen-sparse/include/asm-xen/pci.h linux-2.4-xen-sparse/include/asm-xen/pgalloc.h linux-2.4-xen-sparse/include/asm-xen/pgtable-2level.h linux-2.4-xen-sparse/include/asm-xen/pgtable.h linux-2.4-xen-sparse/include/asm-xen/processor.h linux-2.4-xen-sparse/include/asm-xen/queues.h linux-2.4-xen-sparse/include/asm-xen/segment.h linux-2.4-xen-sparse/include/asm-xen/smp.h linux-2.4-xen-sparse/include/asm-xen/system.h linux-2.4-xen-sparse/include/asm-xen/vga.h linux-2.4-xen-sparse/include/asm-xen/xor.h linux-2.4-xen-sparse/include/linux/blk.h linux-2.4-xen-sparse/include/linux/highmem.h linux-2.4-xen-sparse/include/linux/irq.h linux-2.4-xen-sparse/include/linux/mm.h linux-2.4-xen-sparse/include/linux/sched.h linux-2.4-xen-sparse/include/linux/skbuff.h linux-2.4-xen-sparse/include/linux/timer.h linux-2.4-xen-sparse/kernel/time.c linux-2.4-xen-sparse/kernel/timer.c linux-2.4-xen-sparse/mkbuildtree linux-2.4-xen-sparse/mm/highmem.c linux-2.4-xen-sparse/mm/memory.c linux-2.4-xen-sparse/mm/mprotect.c linux-2.4-xen-sparse/mm/mremap.c linux-2.4-xen-sparse/mm/page_alloc.c linux-2.4-xen-sparse/net/core/skbuff.c linux-2.6-xen-sparse/arch/xen/Kconfig linux-2.6-xen-sparse/arch/xen/Kconfig.drivers linux-2.6-xen-sparse/arch/xen/Makefile linux-2.6-xen-sparse/arch/xen/boot/Makefile linux-2.6-xen-sparse/arch/xen/configs/xen0_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xen0_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/configs/xenU_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xenU_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/configs/xen_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xen_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/i386/Kconfig linux-2.6-xen-sparse/arch/xen/i386/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/acpi/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/acpi/boot.c linux-2.6-xen-sparse/arch/xen/i386/kernel/apic.c linux-2.6-xen-sparse/arch/xen/i386/kernel/cpu/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/cpu/common.c linux-2.6-xen-sparse/arch/xen/i386/kernel/cpu/mtrr/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/cpu/mtrr/main.c linux-2.6-xen-sparse/arch/xen/i386/kernel/entry.S linux-2.6-xen-sparse/arch/xen/i386/kernel/head.S linux-2.6-xen-sparse/arch/xen/i386/kernel/i386_ksyms.c linux-2.6-xen-sparse/arch/xen/i386/kernel/init_task.c linux-2.6-xen-sparse/arch/xen/i386/kernel/io_apic.c linux-2.6-xen-sparse/arch/xen/i386/kernel/ioport.c linux-2.6-xen-sparse/arch/xen/i386/kernel/irq.c linux-2.6-xen-sparse/arch/xen/i386/kernel/ldt.c linux-2.6-xen-sparse/arch/xen/i386/kernel/microcode.c linux-2.6-xen-sparse/arch/xen/i386/kernel/mpparse.c linux-2.6-xen-sparse/arch/xen/i386/kernel/pci-dma.c linux-2.6-xen-sparse/arch/xen/i386/kernel/process.c linux-2.6-xen-sparse/arch/xen/i386/kernel/setup.c linux-2.6-xen-sparse/arch/xen/i386/kernel/signal.c linux-2.6-xen-sparse/arch/xen/i386/kernel/smp.c linux-2.6-xen-sparse/arch/xen/i386/kernel/smpboot.c linux-2.6-xen-sparse/arch/xen/i386/kernel/swiotlb.c linux-2.6-xen-sparse/arch/xen/i386/kernel/time.c linux-2.6-xen-sparse/arch/xen/i386/kernel/traps.c linux-2.6-xen-sparse/arch/xen/i386/kernel/vsyscall.S linux-2.6-xen-sparse/arch/xen/i386/mach-default/Makefile linux-2.6-xen-sparse/arch/xen/i386/mm/Makefile linux-2.6-xen-sparse/arch/xen/i386/mm/fault.c linux-2.6-xen-sparse/arch/xen/i386/mm/highmem.c linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c linux-2.6-xen-sparse/arch/xen/i386/mm/init.c linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c linux-2.6-xen-sparse/arch/xen/i386/mm/pgtable.c linux-2.6-xen-sparse/arch/xen/i386/pci/Makefile linux-2.6-xen-sparse/arch/xen/i386/pci/irq.c linux-2.6-xen-sparse/arch/xen/kernel/Makefile linux-2.6-xen-sparse/arch/xen/kernel/ctrl_if.c linux-2.6-xen-sparse/arch/xen/kernel/devmem.c linux-2.6-xen-sparse/arch/xen/kernel/evtchn.c linux-2.6-xen-sparse/arch/xen/kernel/fixup.c linux-2.6-xen-sparse/arch/xen/kernel/gnttab.c linux-2.6-xen-sparse/arch/xen/kernel/reboot.c linux-2.6-xen-sparse/arch/xen/kernel/skbuff.c linux-2.6-xen-sparse/arch/xen/kernel/smp.c linux-2.6-xen-sparse/arch/xen/kernel/xen_proc.c linux-2.6-xen-sparse/arch/xen/x86_64/Kconfig linux-2.6-xen-sparse/arch/xen/x86_64/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/ia32/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/ia32/ia32entry.S linux-2.6-xen-sparse/arch/xen/x86_64/ia32/syscall32.c linux-2.6-xen-sparse/arch/xen/x86_64/ia32/vsyscall-int80.S linux-2.6-xen-sparse/arch/xen/x86_64/kernel/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/kernel/acpi/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/kernel/apic.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/e820.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/early_printk.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/entry.S linux-2.6-xen-sparse/arch/xen/x86_64/kernel/genapic.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/genapic_xen.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/head.S linux-2.6-xen-sparse/arch/xen/x86_64/kernel/head64.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/io_apic.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/ioport.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/irq.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/ldt.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/mpparse.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/pci-nommu.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/process.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/setup.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/setup64.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/signal.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/smp.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/smpboot.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/traps.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/vsyscall.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/x8664_ksyms.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/xen_entry.S linux-2.6-xen-sparse/arch/xen/x86_64/mm/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/mm/fault.c linux-2.6-xen-sparse/arch/xen/x86_64/mm/init.c linux-2.6-xen-sparse/arch/xen/x86_64/mm/pageattr.c linux-2.6-xen-sparse/arch/xen/x86_64/pci/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/pci/Makefile-BUS linux-2.6-xen-sparse/drivers/Makefile linux-2.6-xen-sparse/drivers/acpi/tables.c linux-2.6-xen-sparse/drivers/char/mem.c linux-2.6-xen-sparse/drivers/char/tty_io.c linux-2.6-xen-sparse/drivers/xen/Makefile linux-2.6-xen-sparse/drivers/xen/balloon/Makefile linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c linux-2.6-xen-sparse/drivers/xen/blkback/Makefile linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c linux-2.6-xen-sparse/drivers/xen/blkback/common.h linux-2.6-xen-sparse/drivers/xen/blkback/interface.c linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c linux-2.6-xen-sparse/drivers/xen/blkfront/Kconfig linux-2.6-xen-sparse/drivers/xen/blkfront/Makefile linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c linux-2.6-xen-sparse/drivers/xen/blkfront/block.h linux-2.6-xen-sparse/drivers/xen/blkfront/vbd.c linux-2.6-xen-sparse/drivers/xen/blktap/Makefile linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap.h linux-2.6-xen-sparse/drivers/xen/blktap/blktap_controlmsg.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap_datapath.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c linux-2.6-xen-sparse/drivers/xen/console/Makefile linux-2.6-xen-sparse/drivers/xen/console/console.c linux-2.6-xen-sparse/drivers/xen/evtchn/Makefile linux-2.6-xen-sparse/drivers/xen/evtchn/evtchn.c linux-2.6-xen-sparse/drivers/xen/netback/Makefile linux-2.6-xen-sparse/drivers/xen/netback/common.h linux-2.6-xen-sparse/drivers/xen/netback/interface.c linux-2.6-xen-sparse/drivers/xen/netback/loopback.c linux-2.6-xen-sparse/drivers/xen/netback/netback.c linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c linux-2.6-xen-sparse/drivers/xen/netfront/Kconfig linux-2.6-xen-sparse/drivers/xen/netfront/Makefile linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c linux-2.6-xen-sparse/drivers/xen/privcmd/Makefile linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c linux-2.6-xen-sparse/drivers/xen/usbback/common.h linux-2.6-xen-sparse/drivers/xen/usbback/control.c linux-2.6-xen-sparse/drivers/xen/usbback/interface.c linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c linux-2.6-xen-sparse/drivers/xen/usbfront/usbfront.c linux-2.6-xen-sparse/drivers/xen/usbfront/xhci.h linux-2.6-xen-sparse/drivers/xen/xenbus/Makefile linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_comms.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_comms.h linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c linux-2.6-xen-sparse/include/asm-generic/pgtable.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/agp.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/desc.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/dma-mapping.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/fixmap.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/floppy.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/highmem.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/hw_irq.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/hypercall.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/io.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/kmap_types.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mach-xen/irq_vectors.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mach-xen/setup_arch_post.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mach-xen/setup_arch_pre.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mach-xen/smpboot_hooks.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mmu.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mmu_context.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/page.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/param.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pci.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgalloc.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable-2level.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable-3level.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/processor.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/ptrace.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/scatterlist.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/segment.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/setup.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/spinlock.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/swiotlb.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/synch_bitops.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/system.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/tlbflush.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/vga.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/arch_hooks.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/bootsetup.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/desc.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/dma-mapping.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/fixmap.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/floppy.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/hw_irq.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/hypercall.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/io.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/irq.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/io_ports.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/irq_vectors.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/mach_timer.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/setup_arch_post.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/setup_arch_pre.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mach-xen/smpboot_hooks.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mmu.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mmu_context.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/page.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/param.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pci.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pgalloc.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pgtable.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/processor.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/ptrace.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/segment.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/smp.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/system.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/timer.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/tlbflush.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/vga.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/xor.h linux-2.6-xen-sparse/include/asm-xen/balloon.h linux-2.6-xen-sparse/include/asm-xen/ctrl_if.h linux-2.6-xen-sparse/include/asm-xen/evtchn.h linux-2.6-xen-sparse/include/asm-xen/foreign_page.h linux-2.6-xen-sparse/include/asm-xen/gnttab.h linux-2.6-xen-sparse/include/asm-xen/hypervisor.h linux-2.6-xen-sparse/include/asm-xen/linux-public/privcmd.h linux-2.6-xen-sparse/include/asm-xen/linux-public/suspend.h linux-2.6-xen-sparse/include/asm-xen/queues.h linux-2.6-xen-sparse/include/asm-xen/synch_bitops.h linux-2.6-xen-sparse/include/asm-xen/xen_proc.h linux-2.6-xen-sparse/include/asm-xen/xenbus.h linux-2.6-xen-sparse/include/linux/gfp.h linux-2.6-xen-sparse/include/linux/highmem.h linux-2.6-xen-sparse/include/linux/irq.h linux-2.6-xen-sparse/include/linux/mm.h linux-2.6-xen-sparse/include/linux/skbuff.h linux-2.6-xen-sparse/kernel/irq/manage.c linux-2.6-xen-sparse/mkbuildtree linux-2.6-xen-sparse/mm/highmem.c linux-2.6-xen-sparse/mm/memory.c linux-2.6-xen-sparse/mm/mmap.c linux-2.6-xen-sparse/mm/page_alloc.c linux-2.6-xen-sparse/net/core/dev.c linux-2.6-xen-sparse/net/core/skbuff.c patches/linux-2.6.12/i386-cpu-hotplug-updated-for-mm.patch patches/linux-2.6.12/net-csum.patch patches/linux-2.6.12/patch-2.6.12.5 patches/linux-2.6.12/rcu-nohz.patch patches/linux-2.6.12/smp-alts.patch tools/Makefile tools/Rules.mk tools/blktap/blktaplib.c tools/blktap/blktaplib.h tools/blktap/parallax/Makefile tools/blktap/parallax/block-async.h tools/blktap/parallax/blockstore.h tools/console/Makefile tools/console/client/main.c tools/console/daemon/io.c tools/console/daemon/io.h tools/console/daemon/main.c tools/console/daemon/utils.c tools/console/daemon/utils.h tools/console/testsuite/Makefile tools/console/testsuite/README tools/console/testsuite/console-dom0.c tools/console/testsuite/console-domU.c tools/console/testsuite/procpipe.c tools/debugger/gdb/gdb-6.2.1-xen-sparse/gdb/gdbserver/linux-xen-low.c tools/debugger/gdb/gdbbuild tools/debugger/libxendebug/Makefile tools/debugger/libxendebug/xendebug.c tools/debugger/libxendebug/xendebug.h tools/debugger/pdb/Domain.ml tools/debugger/pdb/Domain.mli tools/debugger/pdb/Makefile tools/debugger/pdb/PDB.ml tools/debugger/pdb/Process.ml tools/debugger/pdb/Process.mli tools/debugger/pdb/Util.ml tools/debugger/pdb/Xen_domain.ml tools/debugger/pdb/Xen_domain.mli tools/debugger/pdb/debugger.ml tools/debugger/pdb/linux-2.6-module/Makefile tools/debugger/pdb/linux-2.6-module/debug.c tools/debugger/pdb/linux-2.6-module/module.c tools/debugger/pdb/linux-2.6-module/pdb_debug.h tools/debugger/pdb/linux-2.6-module/pdb_module.h tools/debugger/pdb/linux-2.6-patches/Makefile tools/debugger/pdb/linux-2.6-patches/i386_ksyms.patch tools/debugger/pdb/linux-2.6-patches/kdebug.patch tools/debugger/pdb/linux-2.6-patches/makefile.patch tools/debugger/pdb/linux-2.6-patches/ptrace.patch tools/debugger/pdb/linux-2.6-patches/traps.patch tools/debugger/pdb/pdb_caml_domain.c tools/debugger/pdb/pdb_caml_evtchn.c tools/debugger/pdb/pdb_caml_process.c tools/debugger/pdb/pdb_caml_xc.c tools/debugger/pdb/pdb_caml_xcs.c tools/debugger/pdb/pdb_caml_xen.h tools/debugger/pdb/pdb_xen.c tools/debugger/pdb/readme tools/debugger/pdb/server.ml tools/examples/Makefile tools/examples/README tools/examples/backend.hotplug tools/examples/network-bridge tools/examples/vif-bridge tools/examples/xend-config.sxp tools/examples/xmexample.vmx tools/firmware/acpi/acpi2_0.h tools/firmware/rombios/rombios.c tools/firmware/vmxassist/vm86.c tools/ioemu/cpu-all.h tools/ioemu/exec.c tools/ioemu/hw/i8254.c tools/ioemu/hw/i8259.c tools/ioemu/hw/ide.c tools/ioemu/hw/ioapic.h tools/ioemu/hw/pc.c tools/ioemu/hw/pckbd.c tools/ioemu/hw/vga.c tools/ioemu/monitor.c tools/ioemu/target-i386-dm/Makefile tools/ioemu/target-i386-dm/helper2.c tools/ioemu/target-i386-dm/qemu-dm.debug tools/ioemu/vl.c tools/ioemu/vl.h tools/ioemu/vnc.c tools/libxc/Makefile tools/libxc/linux_boot_params.h tools/libxc/xc_core.c tools/libxc/xc_domain.c tools/libxc/xc_gnttab.c tools/libxc/xc_linux_build.c tools/libxc/xc_linux_restore.c tools/libxc/xc_linux_save.c tools/libxc/xc_load_aout9.c tools/libxc/xc_load_bin.c tools/libxc/xc_load_elf.c tools/libxc/xc_private.c tools/libxc/xc_private.h tools/libxc/xc_ptrace.c tools/libxc/xc_vmx_build.c tools/libxc/xenctrl.h tools/libxc/xenguest.h tools/libxc/xg_private.c tools/libxc/xg_private.h tools/misc/Makefile tools/misc/cpuperf/Makefile tools/misc/cpuperf/cpuperf_xeno.h tools/misc/xc_shadow.c tools/misc/xend tools/misc/xenperf.c tools/python/setup.py tools/python/xen/lowlevel/xc/xc.c tools/python/xen/lowlevel/xs/xs.c tools/python/xen/lowlevel/xu/xu.c tools/python/xen/sv/CreateDomain.py tools/python/xen/sv/DomInfo.py tools/python/xen/sv/GenTabbed.py tools/python/xen/sv/HTMLBase.py tools/python/xen/sv/Main.py tools/python/xen/sv/NodeInfo.py tools/python/xen/sv/RestoreDomain.py tools/python/xen/sv/Wizard.py tools/python/xen/sv/__init__.py tools/python/xen/sv/util.py tools/python/xen/util/Brctl.py tools/python/xen/web/SrvBase.py tools/python/xen/web/SrvDir.py tools/python/xen/web/__init__.py tools/python/xen/web/connection.py tools/python/xen/web/httpserver.py tools/python/xen/web/protocol.py tools/python/xen/web/reactor.py tools/python/xen/web/resource.py tools/python/xen/web/static.py tools/python/xen/web/tcp.py tools/python/xen/web/unix.py tools/python/xen/xend/Args.py tools/python/xen/xend/EventServer.py tools/python/xen/xend/PrettyPrint.py tools/python/xen/xend/Vifctl.py tools/python/xen/xend/XendCheckpoint.py tools/python/xen/xend/XendClient.py tools/python/xen/xend/XendDB.py tools/python/xen/xend/XendDmesg.py tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/XendError.py tools/python/xen/xend/XendLogging.py tools/python/xen/xend/XendNode.py tools/python/xen/xend/XendProtocol.py tools/python/xen/xend/XendRoot.py tools/python/xen/xend/XendVnet.py tools/python/xen/xend/encode.py tools/python/xen/xend/image.py tools/python/xen/xend/scheduler.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xend/server/SrvDmesg.py tools/python/xen/xend/server/SrvDomain.py tools/python/xen/xend/server/SrvDomainDir.py tools/python/xen/xend/server/SrvNode.py tools/python/xen/xend/server/SrvRoot.py tools/python/xen/xend/server/SrvServer.py tools/python/xen/xend/server/SrvVnetDir.py tools/python/xen/xend/server/SrvXendLog.py tools/python/xen/xend/server/blkif.py tools/python/xen/xend/server/channel.py tools/python/xen/xend/server/controller.py tools/python/xen/xend/server/event.py tools/python/xen/xend/server/messages.py tools/python/xen/xend/server/netif.py tools/python/xen/xend/server/params.py tools/python/xen/xend/server/pciif.py tools/python/xen/xend/server/relocate.py tools/python/xen/xend/sxp.py tools/python/xen/xend/uuid.py tools/python/xen/xend/xenstore/__init__.py tools/python/xen/xend/xenstore/xsnode.py tools/python/xen/xend/xenstore/xsobj.py tools/python/xen/xend/xenstore/xsresource.py tools/python/xen/xm/create.py tools/python/xen/xm/destroy.py tools/python/xen/xm/help.py tools/python/xen/xm/main.py tools/python/xen/xm/migrate.py tools/python/xen/xm/opts.py tools/python/xen/xm/shutdown.py tools/python/xen/xm/sysrq.py tools/security/Makefile tools/security/example.txt tools/security/install.txt tools/security/policies/chwall/chwall-security_label_template.xml tools/security/policies/chwall/chwall-security_policy.xml tools/security/policies/chwall_ste/chwall_ste-security_label_template.xml tools/security/policies/chwall_ste/chwall_ste-security_policy.xml tools/security/policies/null/null-security_label_template.xml tools/security/policies/null/null-security_policy.xml tools/security/policies/security_policy.xsd tools/security/policies/ste/ste-security_label_template.xml tools/security/policies/ste/ste-security_policy.xml tools/security/policy.txt tools/security/readme.txt tools/security/secpol_compat.h tools/security/secpol_tool.c tools/security/secpol_xml2bin.c tools/security/secpol_xml2bin.h tools/security/setlabel.sh tools/security/updategrub.sh tools/sv/Makefile tools/sv/images/destroy.png tools/sv/images/finish.png tools/sv/images/next.png tools/sv/images/pause.png tools/sv/images/previous.png tools/sv/images/reboot.png tools/sv/images/shutdown.png tools/sv/images/small-destroy.png tools/sv/images/small-pause.png tools/sv/images/small-unpause.png tools/sv/images/unpause.png tools/sv/images/xen.png tools/sv/inc/script.js tools/sv/inc/style.css tools/sv/index.psp tools/vnet/00INSTALL tools/vnet/Makefile tools/vnet/doc/vnet-module.txt tools/vnet/doc/vnet-xend.txt tools/vnet/examples/Makefile tools/vnet/examples/network-vnet tools/vnet/examples/vnet97.sxp tools/vnet/examples/vnet98.sxp tools/vnet/examples/vnet99.sxp tools/vnet/libxutil/Makefile tools/vnet/libxutil/debug.h tools/vnet/libxutil/sxpr.c tools/vnet/libxutil/sxpr.h tools/vnet/libxutil/sxpr_parser.c tools/vnet/libxutil/sxpr_parser.h tools/vnet/libxutil/sys_string.c tools/vnet/libxutil/sys_string.h tools/vnet/vnet-module/00README tools/vnet/vnet-module/Makefile tools/vnet/vnet-module/Makefile-2.4 tools/vnet/vnet-module/Makefile-2.6 tools/vnet/vnet-module/Makefile.ver tools/vnet/vnet-module/Makefile.vnet tools/vnet/vnet-module/etherip.c tools/vnet/vnet-module/if_etherip.h tools/vnet/vnet-module/if_varp.h tools/vnet/vnet-module/skb_util.h tools/vnet/vnet-module/tunnel.c tools/vnet/vnet-module/tunnel.h tools/vnet/vnet-module/varp.c tools/vnet/vnet-module/varp.h tools/vnet/vnet-module/varp_socket.c tools/vnet/vnet-module/vif.c tools/vnet/vnet-module/vif.h tools/vnet/vnet-module/vnet.c tools/vnet/vnet-module/vnet.h tools/vnet/vnet-module/vnet_dev.c tools/vnet/vnet-module/vnet_dev.h tools/vnet/vnet-module/vnet_ioctl.c tools/vnet/vnetd/Makefile tools/vnet/vnetd/vcache.c tools/vnet/vnetd/vcache.h tools/vnet/vnetd/vnetd.c tools/vnet/vnetd/vnetd.h tools/xcs/Makefile tools/xcs/dump.h tools/xcs/xcs.h tools/xcs/xcsdump.c tools/xcutils/Makefile tools/xcutils/xc_restore.c tools/xcutils/xc_save.c tools/xenstat/Makefile tools/xenstat/libxenstat/COPYING tools/xenstat/libxenstat/Makefile tools/xenstat/libxenstat/bindings/swig/perl/.empty tools/xenstat/libxenstat/bindings/swig/python/.empty tools/xenstat/libxenstat/bindings/swig/xenstat.i tools/xenstat/libxenstat/src/xen-interface.c tools/xenstat/libxenstat/src/xen-interface.h tools/xenstat/libxenstat/src/xenstat.c tools/xenstat/libxenstat/src/xenstat.h tools/xenstat/xentop/Makefile tools/xenstat/xentop/TODO tools/xenstat/xentop/xentop.1 tools/xenstat/xentop/xentop.c tools/xenstore/COPYING tools/xenstore/Makefile tools/xenstore/TODO tools/xenstore/testsuite/01simple.test tools/xenstore/testsuite/02directory.test tools/xenstore/testsuite/03write.test tools/xenstore/testsuite/04rm.test tools/xenstore/testsuite/05filepermissions.test tools/xenstore/testsuite/06dirpermissions.test tools/xenstore/testsuite/07watch.test tools/xenstore/testsuite/08transaction.slowtest tools/xenstore/testsuite/08transaction.test tools/xenstore/testsuite/09domain.test tools/xenstore/testsuite/10domain-homedir.test tools/xenstore/testsuite/11domain-watch.test tools/xenstore/testsuite/12readonly.test tools/xenstore/testsuite/13watch-ack.test tools/xenstore/testsuite/14complexperms.test tools/xenstore/testsuite/15nowait.test tools/xenstore/testsuite/test.sh tools/xenstore/testsuite/vg-suppressions tools/xenstore/utils.c tools/xenstore/utils.h tools/xenstore/xenstored.h tools/xenstore/xenstored_core.c tools/xenstore/xenstored_core.h tools/xenstore/xenstored_domain.c tools/xenstore/xenstored_domain.h tools/xenstore/xenstored_transaction.c tools/xenstore/xenstored_transaction.h tools/xenstore/xenstored_watch.c tools/xenstore/xenstored_watch.h tools/xenstore/xs.c tools/xenstore/xs.h tools/xenstore/xs_crashme.c tools/xenstore/xs_dom0_test.c tools/xenstore/xs_lib.c tools/xenstore/xs_lib.h tools/xenstore/xs_random.c tools/xenstore/xs_test.c tools/xentrace/Makefile tools/xentrace/formats tools/xentrace/xenctx.c tools/xentrace/xentrace.c xen/Makefile xen/Rules.mk xen/acm/acm_core.c xen/acm/acm_policy.c xen/arch/ia64/Makefile xen/arch/ia64/Rules.mk xen/arch/ia64/asm-offsets.c xen/arch/ia64/asm-xsi-offsets.c xen/arch/ia64/dom_fw.c xen/arch/ia64/domain.c xen/arch/ia64/grant_table.c xen/arch/ia64/hypercall.c xen/arch/ia64/hyperprivop.S xen/arch/ia64/ivt.S xen/arch/ia64/linux-xen/entry.S xen/arch/ia64/linux-xen/head.S xen/arch/ia64/linux-xen/irq_ia64.c xen/arch/ia64/linux-xen/setup.c xen/arch/ia64/linux/minstate.h xen/arch/ia64/mmio.c xen/arch/ia64/pal_emul.c xen/arch/ia64/pcdp.c xen/arch/ia64/process.c xen/arch/ia64/regionreg.c xen/arch/ia64/tools/mkbuildtree xen/arch/ia64/vcpu.c xen/arch/ia64/vlsapic.c xen/arch/ia64/vmmu.c xen/arch/ia64/vmx_hypercall.c xen/arch/ia64/vmx_init.c xen/arch/ia64/vmx_ivt.S xen/arch/ia64/vmx_phy_mode.c xen/arch/ia64/vmx_support.c xen/arch/ia64/vmx_vcpu.c xen/arch/ia64/vmx_virt.c xen/arch/ia64/vtlb.c xen/arch/ia64/xenasm.S xen/arch/ia64/xenirq.c xen/arch/ia64/xenmem.c xen/arch/ia64/xenmisc.c xen/arch/ia64/xensetup.c xen/arch/ia64/xentime.c xen/arch/x86/Makefile xen/arch/x86/acpi/boot.c xen/arch/x86/apic.c xen/arch/x86/audit.c xen/arch/x86/cpu/amd.c xen/arch/x86/cpu/common.c xen/arch/x86/dom0_ops.c xen/arch/x86/domain.c xen/arch/x86/domain_build.c xen/arch/x86/genapic/es7000plat.c xen/arch/x86/i8259.c xen/arch/x86/io_apic.c xen/arch/x86/mm.c xen/arch/x86/mpparse.c xen/arch/x86/physdev.c xen/arch/x86/setup.c xen/arch/x86/shadow.c xen/arch/x86/shadow32.c xen/arch/x86/shadow_public.c xen/arch/x86/smpboot.c xen/arch/x86/time.c xen/arch/x86/traps.c xen/arch/x86/vmx.c xen/arch/x86/vmx_intercept.c xen/arch/x86/vmx_io.c xen/arch/x86/vmx_platform.c xen/arch/x86/vmx_vmcs.c xen/arch/x86/x86_32/asm-offsets.c xen/arch/x86/x86_32/entry.S xen/arch/x86/x86_32/mm.c xen/arch/x86/x86_32/traps.c xen/arch/x86/x86_64/asm-offsets.c xen/arch/x86/x86_64/entry.S xen/arch/x86/x86_64/mm.c xen/arch/x86/x86_64/traps.c xen/common/ac_timer.c xen/common/dom0_ops.c xen/common/dom_mem_ops.c xen/common/domain.c xen/common/event_channel.c xen/common/grant_table.c xen/common/kernel.c xen/common/lib.c xen/common/multicall.c xen/common/page_alloc.c xen/common/perfc.c xen/common/sched_sedf.c xen/common/schedule.c xen/common/symbols.c xen/common/trace.c xen/common/xmalloc.c xen/drivers/char/console.c xen/drivers/char/ns16550.c xen/include/acm/acm_core.h xen/include/acm/acm_hooks.h xen/include/asm-ia64/config.h xen/include/asm-ia64/domain.h xen/include/asm-ia64/event.h xen/include/asm-ia64/ia64_int.h xen/include/asm-ia64/linux-xen/asm/pal.h xen/include/asm-ia64/linux-xen/asm/processor.h xen/include/asm-ia64/mmu_context.h xen/include/asm-ia64/privop.h xen/include/asm-ia64/regionreg.h xen/include/asm-ia64/regs.h xen/include/asm-ia64/serial.h xen/include/asm-ia64/tlb.h xen/include/asm-ia64/vcpu.h xen/include/asm-ia64/vmmu.h xen/include/asm-ia64/vmx.h xen/include/asm-ia64/vmx_uaccess.h xen/include/asm-ia64/vmx_vcpu.h xen/include/asm-ia64/vmx_vpd.h xen/include/asm-ia64/xenprocessor.h xen/include/asm-ia64/xensystem.h xen/include/asm-x86/apicdef.h xen/include/asm-x86/config.h xen/include/asm-x86/e820.h xen/include/asm-x86/event.h xen/include/asm-x86/fixmap.h xen/include/asm-x86/genapic.h xen/include/asm-x86/hpet.h xen/include/asm-x86/io.h xen/include/asm-x86/mach-bigsmp/mach_apic.h xen/include/asm-x86/mach-default/mach_apic.h xen/include/asm-x86/mach-es7000/mach_apic.h xen/include/asm-x86/mach-generic/mach_apic.h xen/include/asm-x86/mach-summit/mach_apic.h xen/include/asm-x86/mach-summit/mach_mpparse.h xen/include/asm-x86/mm.h xen/include/asm-x86/page.h xen/include/asm-x86/shadow.h xen/include/asm-x86/shadow_64.h xen/include/asm-x86/shadow_public.h xen/include/asm-x86/time.h xen/include/asm-x86/types.h xen/include/asm-x86/uaccess.h xen/include/asm-x86/vmx.h xen/include/asm-x86/vmx_virpit.h xen/include/asm-x86/vmx_vmcs.h xen/include/asm-x86/x86_32/page-3level.h xen/include/asm-x86/x86_32/uaccess.h xen/include/asm-x86/x86_64/page.h xen/include/public/arch-ia64.h xen/include/public/arch-x86_32.h xen/include/public/arch-x86_64.h xen/include/public/dom0_ops.h xen/include/public/grant_table.h xen/include/public/io/blkif.h xen/include/public/io/domain_controller.h xen/include/public/io/netif.h xen/include/public/physdev.h xen/include/public/trace.h xen/include/public/xen.h xen/include/xen/ac_timer.h xen/include/xen/domain.h xen/include/xen/event.h xen/include/xen/grant_table.h xen/include/xen/mm.h xen/include/xen/perfc_defn.h xen/include/xen/sched.h xen/include/xen/serial.h xen/include/xen/symbols.h xen/include/xen/time.h xen/include/xen/trace.h xen/tools/Makefile xen/tools/symbols.c
line diff
    10.1 --- a/extras/mini-os/include/lib.h	Thu Aug 25 13:52:38 2005 -0700
    10.2 +++ b/extras/mini-os/include/lib.h	Fri Aug 26 13:47:16 2005 -0700
    10.3 @@ -79,36 +79,4 @@ char  *strchr(const char *s, int c);
    10.4  char  *strstr(const char *s1, const char *s2);
    10.5  
    10.6  
    10.7 -/* dlmalloc functions */
    10.8 -struct mallinfo {
    10.9 -  int arena;    /* non-mmapped space allocated from system */
   10.10 -  int ordblks;  /* number of free chunks */
   10.11 -  int smblks;   /* number of fastbin blocks */
   10.12 -  int hblks;    /* number of mmapped regions */
   10.13 -  int hblkhd;   /* space in mmapped regions */
   10.14 -  int usmblks;  /* maximum total allocated space */
   10.15 -  int fsmblks;  /* space available in freed fastbin blocks */
   10.16 -  int uordblks; /* total allocated space */
   10.17 -  int fordblks; /* total free space */
   10.18 -  int keepcost; /* top-most, releasable (via malloc_trim) space */
   10.19 -};
   10.20 -
   10.21 -void *malloc(size_t n);
   10.22 -void *calloc(size_t n_elements, size_t element_size);
   10.23 -void  free(void* p);
   10.24 -void *realloc(void* p, size_t n);
   10.25 -void *memalign(size_t alignment, size_t n);
   10.26 -void *valloc(size_t n);
   10.27 -struct mallinfo mallinfo(void);
   10.28 -int  mallopt(int parameter_number, int parameter_value);
   10.29 -
   10.30 -void **independent_calloc(size_t n_elements, size_t size, void* chunks[]);
   10.31 -void **independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
   10.32 -void *pvalloc(size_t n);
   10.33 -void cfree(void* p);
   10.34 -int malloc_trim(size_t pad);
   10.35 -size_t malloc_usable_size(void* p);
   10.36 -void malloc_stats(void);
   10.37 -
   10.38 -
   10.39  #endif /* _LIB_H_ */
    11.1 --- a/extras/mini-os/include/mm.h	Thu Aug 25 13:52:38 2005 -0700
    11.2 +++ b/extras/mini-os/include/mm.h	Fri Aug 26 13:47:16 2005 -0700
    11.3 @@ -126,6 +126,18 @@ static __inline__ unsigned long machine_
    11.4  
    11.5  void init_mm(void);
    11.6  unsigned long alloc_pages(int order);
    11.7 -int is_mfn_mapped(unsigned long mfn);
    11.8 +#define alloc_page()    alloc_pages(0);
    11.9 +void free_pages(void *pointer, int order);
   11.10 +//int is_mfn_mapped(unsigned long mfn);
   11.11 +
   11.12 +static __inline__ int get_order(unsigned long size)
   11.13 +{
   11.14 +    int order;
   11.15 +    size = (size-1) >> PAGE_SHIFT;
   11.16 +    for ( order = 0; size; order++ )
   11.17 +        size >>= 1;
   11.18 +    return order;
   11.19 +}
   11.20 +
   11.21  
   11.22  #endif /* _MM_H_ */
    13.1 --- a/extras/mini-os/include/types.h	Thu Aug 25 13:52:38 2005 -0700
    13.2 +++ b/extras/mini-os/include/types.h	Fri Aug 26 13:47:16 2005 -0700
    13.3 @@ -49,4 +49,6 @@ typedef long                quad_t;
    13.4  typedef unsigned long       u_quad_t;
    13.5  typedef unsigned long       uintptr_t;
    13.6  #endif
    13.7 +
    13.8 +#define UINT_MAX            (~0U)
    13.9  #endif /* _TYPES_H_ */
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/extras/mini-os/include/xmalloc.h	Fri Aug 26 13:47:16 2005 -0700
    14.3 @@ -0,0 +1,23 @@
    14.4 +#ifndef __XMALLOC_H__
    14.5 +#define __XMALLOC_H__
    14.6 +
    14.7 +/* Allocate space for typed object. */
    14.8 +#define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
    14.9 +
   14.10 +/* Allocate space for array of typed objects. */
   14.11 +#define xmalloc_array(_type, _num) ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
   14.12 +
   14.13 +/* Free any of the above. */
   14.14 +extern void xfree(const void *);
   14.15 +
   14.16 +/* Underlying functions */
   14.17 +extern void *_xmalloc(size_t size, size_t align);
   14.18 +static inline void *_xmalloc_array(size_t size, size_t align, size_t num)
   14.19 +{
   14.20 +	/* Check for overflow. */
   14.21 +	if (size && num > UINT_MAX / size)
   14.22 +		return NULL;
   14.23 + 	return _xmalloc(size * num, align);
   14.24 +}
   14.25 +
   14.26 +#endif /* __XMALLOC_H__ */
    16.1 --- a/extras/mini-os/lib/malloc.c	Thu Aug 25 13:52:38 2005 -0700
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,5697 +0,0 @@
    16.4 -/* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
    16.5 - ****************************************************************************
    16.6 - * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
    16.7 - ****************************************************************************
    16.8 - *
    16.9 - *        File: malloc.c
   16.10 - *      Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
   16.11 - *     Changes: 
   16.12 - *              
   16.13 - *        Date: Aug 2003
   16.14 - * 
   16.15 - * Environment: Xen Minimal OS
   16.16 - * Description: Library functions, maloc at al
   16.17 - *
   16.18 - ****************************************************************************
   16.19 - * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
   16.20 - ****************************************************************************
   16.21 - */
   16.22 -
   16.23 -#include <os.h>
   16.24 -#include <mm.h>
   16.25 -#include <types.h>
   16.26 -#include <lib.h>
   16.27 -
   16.28 -/* standard compile option */
   16.29 -#define HAVE_MEMCOPY                1
   16.30 -#define USE_MEMCPY                  1
   16.31 -#undef  HAVE_MMAP
   16.32 -#undef  MMAP_CLEARS
   16.33 -#undef  HAVE_MREMAP
   16.34 -#define malloc_getpagesize          PAGE_SIZE
   16.35 -#undef  HAVE_USR_INCLUDE_MALLOC_H   
   16.36 -#define LACKS_UNISTD_H              1
   16.37 -#define LACKS_SYS_PARAM_H           1
   16.38 -#define LACKS_SYS_MMAN_H            1
   16.39 -#define LACKS_FCNTL_H               1
   16.40 -
   16.41 -
   16.42 -/* page allocator interface */
   16.43 -#define MORECORE             more_core
   16.44 -#define MORECORE_CONTIGUOUS  0
   16.45 -#define MORECORE_FAILURE     0
   16.46 -#define MORECORE_CANNOT_TRIM 1
   16.47 -
   16.48 -static void *more_core(size_t n)
   16.49 -{
   16.50 -    static void *last;
   16.51 -    unsigned long order, num_pages;
   16.52 -    void *ret;
   16.53 -
   16.54 -    if (n == 0)
   16.55 -        return last;
   16.56 -    
   16.57 -    n = PFN_UP(n);
   16.58 -    for ( order = 0; n > 1; order++ )
   16.59 -        n >>= 1;
   16.60 -    ret = (void *)alloc_pages(order);
   16.61 -
   16.62 -    /* work out pointer to end of chunk */
   16.63 -    if ( ret )
   16.64 -    {
   16.65 -        num_pages = 1 << order;
   16.66 -        last = (char *)ret + (num_pages * PAGE_SIZE);
   16.67 -    }
   16.68 -
   16.69 -    return ret;      
   16.70 -}
   16.71 -
   16.72 -/* other options commented out below */
   16.73 -#define __STD_C     1
   16.74 -#define Void_t      void
   16.75 -#define assert(x) ((void)0)
   16.76 -
   16.77 -#define CHUNK_SIZE_T unsigned long
   16.78 -#define PTR_UINT unsigned long
   16.79 -#define INTERNAL_SIZE_T size_t
   16.80 -#define SIZE_SZ                (sizeof(INTERNAL_SIZE_T))
   16.81 -#define MALLOC_ALIGNMENT       (2 * SIZE_SZ)
   16.82 -#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
   16.83 -#define TRIM_FASTBINS  0
   16.84 -
   16.85 -#define M_MXFAST            1    
   16.86 -#define DEFAULT_MXFAST     64
   16.87 -#define M_TRIM_THRESHOLD       -1
   16.88 -#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
   16.89 -#define M_TOP_PAD              -2
   16.90 -#define DEFAULT_TOP_PAD        (0)
   16.91 -#define M_MMAP_THRESHOLD      -3
   16.92 -#define DEFAULT_MMAP_THRESHOLD (256 * 1024)
   16.93 -#define M_MMAP_MAX             -4
   16.94 -#define DEFAULT_MMAP_MAX       (0)
   16.95 -#define MALLOC_FAILURE_ACTION   printf("malloc failure\n")
   16.96 -
   16.97 -#define cALLOc      public_cALLOc
   16.98 -#define fREe        public_fREe
   16.99 -#define cFREe       public_cFREe
  16.100 -#define mALLOc      public_mALLOc
  16.101 -#define mEMALIGn    public_mEMALIGn
  16.102 -#define rEALLOc     public_rEALLOc
  16.103 -#define vALLOc      public_vALLOc
  16.104 -#define pVALLOc     public_pVALLOc
  16.105 -#define mALLINFo    public_mALLINFo
  16.106 -#define mALLOPt     public_mALLOPt
  16.107 -#define mTRIm       public_mTRIm
  16.108 -#define mSTATs      public_mSTATs
  16.109 -#define mUSABLe     public_mUSABLe
  16.110 -#define iCALLOc     public_iCALLOc
  16.111 -#define iCOMALLOc   public_iCOMALLOc
  16.112 -
  16.113 -#define public_cALLOc    calloc
  16.114 -#define public_fREe      free
  16.115 -#define public_cFREe     cfree
  16.116 -#define public_mALLOc    malloc
  16.117 -#define public_mEMALIGn  memalign
  16.118 -#define public_rEALLOc   realloc
  16.119 -#define public_vALLOc    valloc
  16.120 -#define public_pVALLOc   pvalloc
  16.121 -#define public_mALLINFo  mallinfo
  16.122 -#define public_mALLOPt   mallopt
  16.123 -#define public_mTRIm     malloc_trim
  16.124 -#define public_mSTATs    malloc_stats
  16.125 -#define public_mUSABLe   malloc_usable_size
  16.126 -#define public_iCALLOc   independent_calloc
  16.127 -#define public_iCOMALLOc independent_comalloc
  16.128 -
  16.129 -
  16.130 -/*
  16.131 -  This is a version (aka dlmalloc) of malloc/free/realloc written by
  16.132 -  Doug Lea and released to the public domain.  Use, modify, and
  16.133 -  redistribute this code without permission or acknowledgement in any
  16.134 -  way you wish.  Send questions, comments, complaints, performance
  16.135 -  data, etc to dl@cs.oswego.edu
  16.136 -
  16.137 -* VERSION 2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
  16.138 -
  16.139 -   Note: There may be an updated version of this malloc obtainable at
  16.140 -           ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  16.141 -         Check before installing!
  16.142 -
  16.143 -* Quickstart
  16.144 -
  16.145 -  This library is all in one file to simplify the most common usage:
  16.146 -  ftp it, compile it (-O), and link it into another program. All
  16.147 -  of the compile-time options default to reasonable values for use on
  16.148 -  most unix platforms. Compile -DWIN32 for reasonable defaults on windows.
  16.149 -  You might later want to step through various compile-time and dynamic
  16.150 -  tuning options.
  16.151 -
  16.152 -  For convenience, an include file for code using this malloc is at:
  16.153 -     ftp://gee.cs.oswego.edu/pub/misc/malloc-2.7.1.h
  16.154 -  You don't really need this .h file unless you call functions not
  16.155 -  defined in your system include files.  The .h file contains only the
  16.156 -  excerpts from this file needed for using this malloc on ANSI C/C++
  16.157 -  systems, so long as you haven't changed compile-time options about
  16.158 -  naming and tuning parameters.  If you do, then you can create your
  16.159 -  own malloc.h that does include all settings by cutting at the point
  16.160 -  indicated below.
  16.161 -
  16.162 -* Why use this malloc?
  16.163 -
  16.164 -  This is not the fastest, most space-conserving, most portable, or
  16.165 -  most tunable malloc ever written. However it is among the fastest
  16.166 -  while also being among the most space-conserving, portable and tunable.
  16.167 -  Consistent balance across these factors results in a good general-purpose
  16.168 -  allocator for malloc-intensive programs.
  16.169 -
  16.170 -  The main properties of the algorithms are:
  16.171 -  * For large (>= 512 bytes) requests, it is a pure best-fit allocator,
  16.172 -    with ties normally decided via FIFO (i.e. least recently used).
  16.173 -  * For small (<= 64 bytes by default) requests, it is a caching
  16.174 -    allocator, that maintains pools of quickly recycled chunks.
  16.175 -  * In between, and for combinations of large and small requests, it does
  16.176 -    the best it can trying to meet both goals at once.
  16.177 -  * For very large requests (>= 128KB by default), it relies on system
  16.178 -    memory mapping facilities, if supported.
  16.179 -
  16.180 -  For a longer but slightly out of date high-level description, see
  16.181 -     http://gee.cs.oswego.edu/dl/html/malloc.html
  16.182 -
  16.183 -  You may already by default be using a C library containing a malloc
  16.184 -  that is  based on some version of this malloc (for example in
  16.185 -  linux). You might still want to use the one in this file in order to
  16.186 -  customize settings or to avoid overheads associated with library
  16.187 -  versions.
  16.188 -
  16.189 -* Contents, described in more detail in "description of public routines" below.
  16.190 -
  16.191 -  Standard (ANSI/SVID/...)  functions:
  16.192 -    malloc(size_t n);
  16.193 -    calloc(size_t n_elements, size_t element_size);
  16.194 -    free(Void_t* p);
  16.195 -    realloc(Void_t* p, size_t n);
  16.196 -    memalign(size_t alignment, size_t n);
  16.197 -    valloc(size_t n);
  16.198 -    mallinfo()
  16.199 -    mallopt(int parameter_number, int parameter_value)
  16.200 -
  16.201 -  Additional functions:
  16.202 -    independent_calloc(size_t n_elements, size_t size, Void_t* chunks[]);
  16.203 -    independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]);
  16.204 -    pvalloc(size_t n);
  16.205 -    cfree(Void_t* p);
  16.206 -    malloc_trim(size_t pad);
  16.207 -    malloc_usable_size(Void_t* p);
  16.208 -    malloc_stats();
  16.209 -
  16.210 -* Vital statistics:
  16.211 -
  16.212 -  Supported pointer representation:       4 or 8 bytes
  16.213 -  Supported size_t  representation:       4 or 8 bytes 
  16.214 -       Note that size_t is allowed to be 4 bytes even if pointers are 8.
  16.215 -       You can adjust this by defining INTERNAL_SIZE_T
  16.216 -
  16.217 -  Alignment:                              2 * sizeof(size_t) (default)
  16.218 -       (i.e., 8 byte alignment with 4byte size_t). This suffices for
  16.219 -       nearly all current machines and C compilers. However, you can
  16.220 -       define MALLOC_ALIGNMENT to be wider than this if necessary.
  16.221 -
  16.222 -  Minimum overhead per allocated chunk:   4 or 8 bytes
  16.223 -       Each malloced chunk has a hidden word of overhead holding size
  16.224 -       and status information.
  16.225 -
  16.226 -  Minimum allocated size: 4-byte ptrs:  16 bytes    (including 4 overhead)
  16.227 -                          8-byte ptrs:  24/32 bytes (including, 4/8 overhead)
  16.228 -
  16.229 -       When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
  16.230 -       ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
  16.231 -       needed; 4 (8) for a trailing size field and 8 (16) bytes for
  16.232 -       free list pointers. Thus, the minimum allocatable size is
  16.233 -       16/24/32 bytes.
  16.234 -
  16.235 -       Even a request for zero bytes (i.e., malloc(0)) returns a
  16.236 -       pointer to something of the minimum allocatable size.
  16.237 -
  16.238 -       The maximum overhead wastage (i.e., number of extra bytes
  16.239 -       allocated than were requested in malloc) is less than or equal
  16.240 -       to the minimum size, except for requests >= mmap_threshold that
  16.241 -       are serviced via mmap(), where the worst case wastage is 2 *
  16.242 -       sizeof(size_t) bytes plus the remainder from a system page (the
  16.243 -       minimal mmap unit); typically 4096 or 8192 bytes.
  16.244 -
  16.245 -  Maximum allocated size:  4-byte size_t: 2^32 minus about two pages 
  16.246 -                           8-byte size_t: 2^64 minus about two pages
  16.247 -
  16.248 -       It is assumed that (possibly signed) size_t values suffice to
  16.249 -       represent chunk sizes. `Possibly signed' is due to the fact
  16.250 -       that `size_t' may be defined on a system as either a signed or
  16.251 -       an unsigned type. The ISO C standard says that it must be
  16.252 -       unsigned, but a few systems are known not to adhere to this.
  16.253 -       Additionally, even when size_t is unsigned, sbrk (which is by
  16.254 -       default used to obtain memory from system) accepts signed
  16.255 -       arguments, and may not be able to handle size_t-wide arguments
  16.256 -       with negative sign bit.  Generally, values that would
  16.257 -       appear as negative after accounting for overhead and alignment
  16.258 -       are supported only via mmap(), which does not have this
  16.259 -       limitation.
  16.260 -
  16.261 -       Requests for sizes outside the allowed range will perform an optional
  16.262 -       failure action and then return null. (Requests may also
  16.263 -       also fail because a system is out of memory.)
  16.264 -
  16.265 -  Thread-safety: NOT thread-safe unless USE_MALLOC_LOCK defined
  16.266 -
  16.267 -       When USE_MALLOC_LOCK is defined, wrappers are created to
  16.268 -       surround every public call with either a pthread mutex or
  16.269 -       a win32 spinlock (depending on WIN32). This is not
  16.270 -       especially fast, and can be a major bottleneck.
  16.271 -       It is designed only to provide minimal protection
  16.272 -       in concurrent environments, and to provide a basis for
  16.273 -       extensions.  If you are using malloc in a concurrent program,
  16.274 -       you would be far better off obtaining ptmalloc, which is
  16.275 -       derived from a version of this malloc, and is well-tuned for
  16.276 -       concurrent programs. (See http://www.malloc.de) Note that
  16.277 -       even when USE_MALLOC_LOCK is defined, you can can guarantee
  16.278 -       full thread-safety only if no threads acquire memory through 
  16.279 -       direct calls to MORECORE or other system-level allocators.
  16.280 -
  16.281 -  Compliance: I believe it is compliant with the 1997 Single Unix Specification
  16.282 -       (See http://www.opennc.org). Also SVID/XPG, ANSI C, and probably 
  16.283 -       others as well.
  16.284 -
  16.285 -* Synopsis of compile-time options:
  16.286 -
  16.287 -    People have reported using previous versions of this malloc on all
  16.288 -    versions of Unix, sometimes by tweaking some of the defines
  16.289 -    below. It has been tested most extensively on Solaris and
  16.290 -    Linux. It is also reported to work on WIN32 platforms.
  16.291 -    People also report using it in stand-alone embedded systems.
  16.292 -
  16.293 -    The implementation is in straight, hand-tuned ANSI C.  It is not
  16.294 -    at all modular. (Sorry!)  It uses a lot of macros.  To be at all
  16.295 -    usable, this code should be compiled using an optimizing compiler
  16.296 -    (for example gcc -O3) that can simplify expressions and control
  16.297 -    paths. (FAQ: some macros import variables as arguments rather than
  16.298 -    declare locals because people reported that some debuggers
  16.299 -    otherwise get confused.)
  16.300 -
  16.301 -    OPTION                     DEFAULT VALUE
  16.302 -
  16.303 -    Compilation Environment options:
  16.304 -
  16.305 -    __STD_C                    derived from C compiler defines
  16.306 -    WIN32                      NOT defined
  16.307 -    HAVE_MEMCPY                defined
  16.308 -    USE_MEMCPY                 1 if HAVE_MEMCPY is defined
  16.309 -    HAVE_MMAP                  defined as 1 
  16.310 -    MMAP_CLEARS                1
  16.311 -    HAVE_MREMAP                0 unless linux defined
  16.312 -    malloc_getpagesize         derived from system #includes, or 4096 if not
  16.313 -    HAVE_USR_INCLUDE_MALLOC_H  NOT defined
  16.314 -    LACKS_UNISTD_H             NOT defined unless WIN32
  16.315 -    LACKS_SYS_PARAM_H          NOT defined unless WIN32
  16.316 -    LACKS_SYS_MMAN_H           NOT defined unless WIN32
  16.317 -    LACKS_FCNTL_H              NOT defined
  16.318 -
  16.319 -    Changing default word sizes:
  16.320 -
  16.321 -    INTERNAL_SIZE_T            size_t
  16.322 -    MALLOC_ALIGNMENT           2 * sizeof(INTERNAL_SIZE_T)
  16.323 -    PTR_UINT                   unsigned long
  16.324 -    CHUNK_SIZE_T               unsigned long
  16.325 -
  16.326 -    Configuration and functionality options:
  16.327 -
  16.328 -    USE_DL_PREFIX              NOT defined
  16.329 -    USE_PUBLIC_MALLOC_WRAPPERS NOT defined
  16.330 -    USE_MALLOC_LOCK            NOT defined
  16.331 -    DEBUG                      NOT defined
  16.332 -    REALLOC_ZERO_BYTES_FREES   NOT defined
  16.333 -    MALLOC_FAILURE_ACTION      errno = ENOMEM, if __STD_C defined, else no-op
  16.334 -    TRIM_FASTBINS              0
  16.335 -    FIRST_SORTED_BIN_SIZE      512
  16.336 -
  16.337 -    Options for customizing MORECORE:
  16.338 -
  16.339 -    MORECORE                   sbrk
  16.340 -    MORECORE_CONTIGUOUS        1 
  16.341 -    MORECORE_CANNOT_TRIM       NOT defined
  16.342 -    MMAP_AS_MORECORE_SIZE      (1024 * 1024) 
  16.343 -
  16.344 -    Tuning options that are also dynamically changeable via mallopt:
  16.345 -
  16.346 -    DEFAULT_MXFAST             64
  16.347 -    DEFAULT_TRIM_THRESHOLD     256 * 1024
  16.348 -    DEFAULT_TOP_PAD            0
  16.349 -    DEFAULT_MMAP_THRESHOLD     256 * 1024
  16.350 -    DEFAULT_MMAP_MAX           65536
  16.351 -
  16.352 -    There are several other #defined constants and macros that you
  16.353 -    probably don't want to touch unless you are extending or adapting malloc.
  16.354 -*/
  16.355 -
  16.356 -/* RN: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  16.357 -#if 0
  16.358 -
  16.359 -/*
  16.360 -  WIN32 sets up defaults for MS environment and compilers.
  16.361 -  Otherwise defaults are for unix.
  16.362 -*/
  16.363 -
  16.364 -/* #define WIN32 */
  16.365 -
  16.366 -#ifdef WIN32
  16.367 -
  16.368 -#define WIN32_LEAN_AND_MEAN
  16.369 -#include <windows.h>
  16.370 -
  16.371 -/* Win32 doesn't supply or need the following headers */
  16.372 -#define LACKS_UNISTD_H
  16.373 -#define LACKS_SYS_PARAM_H
  16.374 -#define LACKS_SYS_MMAN_H
  16.375 -
  16.376 -/* Use the supplied emulation of sbrk */
  16.377 -#define MORECORE sbrk
  16.378 -#define MORECORE_CONTIGUOUS 1
  16.379 -#define MORECORE_FAILURE    ((void*)(-1))
  16.380 -
  16.381 -/* Use the supplied emulation of mmap and munmap */
  16.382 -#define HAVE_MMAP 1
  16.383 -#define MUNMAP_FAILURE  (-1)
  16.384 -#define MMAP_CLEARS 1
  16.385 -
  16.386 -/* These values don't really matter in windows mmap emulation */
  16.387 -#define MAP_PRIVATE 1
  16.388 -#define MAP_ANONYMOUS 2
  16.389 -#define PROT_READ 1
  16.390 -#define PROT_WRITE 2
  16.391 -
  16.392 -/* Emulation functions defined at the end of this file */
  16.393 -
  16.394 -/* If USE_MALLOC_LOCK, use supplied critical-section-based lock functions */
  16.395 -#ifdef USE_MALLOC_LOCK
  16.396 -static int slwait(int *sl);
  16.397 -static int slrelease(int *sl);
  16.398 -#endif
  16.399 -
  16.400 -static long getpagesize(void);
  16.401 -static long getregionsize(void);
  16.402 -static void *sbrk(long size);
  16.403 -static void *mmap(void *ptr, long size, long prot, long type, long handle, long arg);
  16.404 -static long munmap(void *ptr, long size);
  16.405 -
  16.406 -static void vminfo (unsigned long*free, unsigned long*reserved, unsigned long*committed);
  16.407 -static int cpuinfo (int whole, unsigned long*kernel, unsigned long*user);
  16.408 -
  16.409 -#endif
  16.410 -
  16.411 -/*
  16.412 -  __STD_C should be nonzero if using ANSI-standard C compiler, a C++
  16.413 -  compiler, or a C compiler sufficiently close to ANSI to get away
  16.414 -  with it.
  16.415 -*/
  16.416 -
  16.417 -#ifndef __STD_C
  16.418 -#if defined(__STDC__) || defined(_cplusplus)
  16.419 -#define __STD_C     1
  16.420 -#else
  16.421 -#define __STD_C     0
  16.422 -#endif 
  16.423 -#endif /*__STD_C*/
  16.424 -
  16.425 -
  16.426 -/*
  16.427 -  Void_t* is the pointer type that malloc should say it returns
  16.428 -*/
  16.429 -
  16.430 -#ifndef Void_t
  16.431 -#if (__STD_C || defined(WIN32))
  16.432 -#define Void_t      void
  16.433 -#else
  16.434 -#define Void_t      char
  16.435 -#endif
  16.436 -#endif /*Void_t*/
  16.437 -
  16.438 -#if __STD_C
  16.439 -#include <stddef.h>   /* for size_t */
  16.440 -#else
  16.441 -#include <sys/types.h>
  16.442 -#endif
  16.443 -
  16.444 -#ifdef __cplusplus
  16.445 -extern "C" {
  16.446 -#endif
  16.447 -
  16.448 -/* define LACKS_UNISTD_H if your system does not have a <unistd.h>. */
  16.449 -
  16.450 -/* #define  LACKS_UNISTD_H */
  16.451 -
  16.452 -#ifndef LACKS_UNISTD_H
  16.453 -#include <unistd.h>
  16.454 -#endif
  16.455 -
  16.456 -/* define LACKS_SYS_PARAM_H if your system does not have a <sys/param.h>. */
  16.457 -
  16.458 -/* #define  LACKS_SYS_PARAM_H */
  16.459 -
  16.460 -
  16.461 -#include <stdio.h>    /* needed for malloc_stats */
  16.462 -#include <errno.h>    /* needed for optional MALLOC_FAILURE_ACTION */
  16.463 -
  16.464 -
  16.465 -/*
  16.466 -  Debugging:
  16.467 -
  16.468 -  Because freed chunks may be overwritten with bookkeeping fields, this
  16.469 -  malloc will often die when freed memory is overwritten by user
  16.470 -  programs.  This can be very effective (albeit in an annoying way)
  16.471 -  in helping track down dangling pointers.
  16.472 -
  16.473 -  If you compile with -DDEBUG, a number of assertion checks are
  16.474 -  enabled that will catch more memory errors. You probably won't be
  16.475 -  able to make much sense of the actual assertion errors, but they
  16.476 -  should help you locate incorrectly overwritten memory.  The
  16.477 -  checking is fairly extensive, and will slow down execution
  16.478 -  noticeably. Calling malloc_stats or mallinfo with DEBUG set will
  16.479 -  attempt to check every non-mmapped allocated and free chunk in the
  16.480 -  course of computing the summmaries. (By nature, mmapped regions
  16.481 -  cannot be checked very much automatically.)
  16.482 -
  16.483 -  Setting DEBUG may also be helpful if you are trying to modify
  16.484 -  this code. The assertions in the check routines spell out in more
  16.485 -  detail the assumptions and invariants underlying the algorithms.
  16.486 -
  16.487 -  Setting DEBUG does NOT provide an automated mechanism for checking
  16.488 -  that all accesses to malloced memory stay within their
  16.489 -  bounds. However, there are several add-ons and adaptations of this
  16.490 -  or other mallocs available that do this.
  16.491 -*/
  16.492 -
  16.493 -#if DEBUG
  16.494 -#include <assert.h>
  16.495 -#else
  16.496 -#define assert(x) ((void)0)
  16.497 -#endif
  16.498 -
  16.499 -/*
  16.500 -  The unsigned integer type used for comparing any two chunk sizes.
  16.501 -  This should be at least as wide as size_t, but should not be signed.
  16.502 -*/
  16.503 -
  16.504 -#ifndef CHUNK_SIZE_T
  16.505 -#define CHUNK_SIZE_T unsigned long
  16.506 -#endif
  16.507 -
  16.508 -/* 
  16.509 -  The unsigned integer type used to hold addresses when they are are
  16.510 -  manipulated as integers. Except that it is not defined on all
  16.511 -  systems, intptr_t would suffice.
  16.512 -*/
  16.513 -#ifndef PTR_UINT
  16.514 -#define PTR_UINT unsigned long
  16.515 -#endif
  16.516 -
  16.517 -
  16.518 -/*
  16.519 -  INTERNAL_SIZE_T is the word-size used for internal bookkeeping
  16.520 -  of chunk sizes.
  16.521 -
  16.522 -  The default version is the same as size_t.
  16.523 -
  16.524 -  While not strictly necessary, it is best to define this as an
  16.525 -  unsigned type, even if size_t is a signed type. This may avoid some
  16.526 -  artificial size limitations on some systems.
  16.527 -
  16.528 -  On a 64-bit machine, you may be able to reduce malloc overhead by
  16.529 -  defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
  16.530 -  expense of not being able to handle more than 2^32 of malloced
  16.531 -  space. If this limitation is acceptable, you are encouraged to set
  16.532 -  this unless you are on a platform requiring 16byte alignments. In
  16.533 -  this case the alignment requirements turn out to negate any
  16.534 -  potential advantages of decreasing size_t word size.
  16.535 -
  16.536 -  Implementors: Beware of the possible combinations of:
  16.537 -     - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
  16.538 -       and might be the same width as int or as long
  16.539 -     - size_t might have different width and signedness as INTERNAL_SIZE_T
  16.540 -     - int and long might be 32 or 64 bits, and might be the same width
  16.541 -  To deal with this, most comparisons and difference computations
  16.542 -  among INTERNAL_SIZE_Ts should cast them to CHUNK_SIZE_T, being
  16.543 -  aware of the fact that casting an unsigned int to a wider long does
  16.544 -  not sign-extend. (This also makes checking for negative numbers
  16.545 -  awkward.) Some of these casts result in harmless compiler warnings
  16.546 -  on some systems.
  16.547 -*/
  16.548 -
  16.549 -#ifndef INTERNAL_SIZE_T
  16.550 -#define INTERNAL_SIZE_T size_t
  16.551 -#endif
  16.552 -
  16.553 -/* The corresponding word size */
  16.554 -#define SIZE_SZ                (sizeof(INTERNAL_SIZE_T))
  16.555 -
  16.556 -
  16.557 -
  16.558 -/*
  16.559 -  MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
  16.560 -  It must be a power of two at least 2 * SIZE_SZ, even on machines
  16.561 -  for which smaller alignments would suffice. It may be defined as
  16.562 -  larger than this though. Note however that code and data structures
  16.563 -  are optimized for the case of 8-byte alignment.
  16.564 -*/
  16.565 -
  16.566 -
  16.567 -#ifndef MALLOC_ALIGNMENT
  16.568 -#define MALLOC_ALIGNMENT       (2 * SIZE_SZ)
  16.569 -#endif
  16.570 -
  16.571 -/* The corresponding bit mask value */
  16.572 -#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
  16.573 -
  16.574 -
  16.575 -
  16.576 -/*
  16.577 -  REALLOC_ZERO_BYTES_FREES should be set if a call to
  16.578 -  realloc with zero bytes should be the same as a call to free.
  16.579 -  Some people think it should. Otherwise, since this malloc
  16.580 -  returns a unique pointer for malloc(0), so does realloc(p, 0).
  16.581 -*/
  16.582 -
  16.583 -/*   #define REALLOC_ZERO_BYTES_FREES */
  16.584 -
  16.585 -/*
  16.586 -  TRIM_FASTBINS controls whether free() of a very small chunk can
  16.587 -  immediately lead to trimming. Setting to true (1) can reduce memory
  16.588 -  footprint, but will almost always slow down programs that use a lot
  16.589 -  of small chunks.
  16.590 -
  16.591 -  Define this only if you are willing to give up some speed to more
  16.592 -  aggressively reduce system-level memory footprint when releasing
  16.593 -  memory in programs that use many small chunks.  You can get
  16.594 -  essentially the same effect by setting MXFAST to 0, but this can
  16.595 -  lead to even greater slowdowns in programs using many small chunks.
  16.596 -  TRIM_FASTBINS is an in-between compile-time option, that disables
  16.597 -  only those chunks bordering topmost memory from being placed in
  16.598 -  fastbins.
  16.599 -*/
  16.600 -
  16.601 -#ifndef TRIM_FASTBINS
  16.602 -#define TRIM_FASTBINS  0
  16.603 -#endif
  16.604 -
  16.605 -
  16.606 -/*
  16.607 -  USE_DL_PREFIX will prefix all public routines with the string 'dl'.
  16.608 -  This is necessary when you only want to use this malloc in one part 
  16.609 -  of a program, using your regular system malloc elsewhere.
  16.610 -*/
  16.611 -
  16.612 -/* #define USE_DL_PREFIX */
  16.613 -
  16.614 -
  16.615 -/*
  16.616 -  USE_MALLOC_LOCK causes wrapper functions to surround each
  16.617 -  callable routine with pthread mutex lock/unlock.
  16.618 -
  16.619 -  USE_MALLOC_LOCK forces USE_PUBLIC_MALLOC_WRAPPERS to be defined
  16.620 -*/
  16.621 -
  16.622 -
  16.623 -/* #define USE_MALLOC_LOCK */
  16.624 -
  16.625 -
  16.626 -/*
  16.627 -  If USE_PUBLIC_MALLOC_WRAPPERS is defined, every public routine is
  16.628 -  actually a wrapper function that first calls MALLOC_PREACTION, then
  16.629 -  calls the internal routine, and follows it with
  16.630 -  MALLOC_POSTACTION. This is needed for locking, but you can also use
  16.631 -  this, without USE_MALLOC_LOCK, for purposes of interception,
  16.632 -  instrumentation, etc. It is a sad fact that using wrappers often
  16.633 -  noticeably degrades performance of malloc-intensive programs.
  16.634 -*/
  16.635 -
  16.636 -#ifdef USE_MALLOC_LOCK
  16.637 -#define USE_PUBLIC_MALLOC_WRAPPERS
  16.638 -#else
  16.639 -/* #define USE_PUBLIC_MALLOC_WRAPPERS */
  16.640 -#endif
  16.641 -
  16.642 -
  16.643 -/* 
  16.644 -   Two-phase name translation.
  16.645 -   All of the actual routines are given mangled names.
  16.646 -   When wrappers are used, they become the public callable versions.
  16.647 -   When DL_PREFIX is used, the callable names are prefixed.
  16.648 -*/
  16.649 -
  16.650 -#ifndef USE_PUBLIC_MALLOC_WRAPPERS
  16.651 -#define cALLOc      public_cALLOc
  16.652 -#define fREe        public_fREe
  16.653 -#define cFREe       public_cFREe
  16.654 -#define mALLOc      public_mALLOc
  16.655 -#define mEMALIGn    public_mEMALIGn
  16.656 -#define rEALLOc     public_rEALLOc
  16.657 -#define vALLOc      public_vALLOc
  16.658 -#define pVALLOc     public_pVALLOc
  16.659 -#define mALLINFo    public_mALLINFo
  16.660 -#define mALLOPt     public_mALLOPt
  16.661 -#define mTRIm       public_mTRIm
  16.662 -#define mSTATs      public_mSTATs
  16.663 -#define mUSABLe     public_mUSABLe
  16.664 -#define iCALLOc     public_iCALLOc
  16.665 -#define iCOMALLOc   public_iCOMALLOc
  16.666 -#endif
  16.667 -
  16.668 -#ifdef USE_DL_PREFIX
  16.669 -#define public_cALLOc    dlcalloc
  16.670 -#define public_fREe      dlfree
  16.671 -#define public_cFREe     dlcfree
  16.672 -#define public_mALLOc    dlmalloc
  16.673 -#define public_mEMALIGn  dlmemalign
  16.674 -#define public_rEALLOc   dlrealloc
  16.675 -#define public_vALLOc    dlvalloc
  16.676 -#define public_pVALLOc   dlpvalloc
  16.677 -#define public_mALLINFo  dlmallinfo
  16.678 -#define public_mALLOPt   dlmallopt
  16.679 -#define public_mTRIm     dlmalloc_trim
  16.680 -#define public_mSTATs    dlmalloc_stats
  16.681 -#define public_mUSABLe   dlmalloc_usable_size
  16.682 -#define public_iCALLOc   dlindependent_calloc
  16.683 -#define public_iCOMALLOc dlindependent_comalloc
  16.684 -#else /* USE_DL_PREFIX */
  16.685 -#define public_cALLOc    calloc
  16.686 -#define public_fREe      free
  16.687 -#define public_cFREe     cfree
  16.688 -#define public_mALLOc    malloc
  16.689 -#define public_mEMALIGn  memalign
  16.690 -#define public_rEALLOc   realloc
  16.691 -#define public_vALLOc    valloc
  16.692 -#define public_pVALLOc   pvalloc
  16.693 -#define public_mALLINFo  mallinfo
  16.694 -#define public_mALLOPt   mallopt
  16.695 -#define public_mTRIm     malloc_trim
  16.696 -#define public_mSTATs    malloc_stats
  16.697 -#define public_mUSABLe   malloc_usable_size
  16.698 -#define public_iCALLOc   independent_calloc
  16.699 -#define public_iCOMALLOc independent_comalloc
  16.700 -#endif /* USE_DL_PREFIX */
  16.701 -
  16.702 -
  16.703 -/*
  16.704 -  HAVE_MEMCPY should be defined if you are not otherwise using
  16.705 -  ANSI STD C, but still have memcpy and memset in your C library
  16.706 -  and want to use them in calloc and realloc. Otherwise simple
  16.707 -  macro versions are defined below.
  16.708 -
  16.709 -  USE_MEMCPY should be defined as 1 if you actually want to
  16.710 -  have memset and memcpy called. People report that the macro
  16.711 -  versions are faster than libc versions on some systems.
  16.712 -  
  16.713 -  Even if USE_MEMCPY is set to 1, loops to copy/clear small chunks
  16.714 -  (of <= 36 bytes) are manually unrolled in realloc and calloc.
  16.715 -*/
  16.716 -
  16.717 -#define HAVE_MEMCPY
  16.718 -
  16.719 -#ifndef USE_MEMCPY
  16.720 -#ifdef HAVE_MEMCPY
  16.721 -#define USE_MEMCPY 1
  16.722 -#else
  16.723 -#define USE_MEMCPY 0
  16.724 -#endif
  16.725 -#endif
  16.726 -
  16.727 -
  16.728 -#if (__STD_C || defined(HAVE_MEMCPY))
  16.729 -
  16.730 -#ifdef WIN32
  16.731 -/* On Win32 memset and memcpy are already declared in windows.h */
  16.732 -#else
  16.733 -#if __STD_C
  16.734 -void* memset(void*, int, size_t);
  16.735 -void* memcpy(void*, const void*, size_t);
  16.736 -#else
  16.737 -Void_t* memset();
  16.738 -Void_t* memcpy();
  16.739 -#endif
  16.740 -#endif
  16.741 -#endif
  16.742 -
  16.743 -/*
  16.744 -  MALLOC_FAILURE_ACTION is the action to take before "return 0" when
  16.745 -  malloc fails to be able to return memory, either because memory is
  16.746 -  exhausted or because of illegal arguments.
  16.747 -  
  16.748 -  By default, sets errno if running on STD_C platform, else does nothing.  
  16.749 -*/
  16.750 -
  16.751 -#ifndef MALLOC_FAILURE_ACTION
  16.752 -#if __STD_C
  16.753 -#define MALLOC_FAILURE_ACTION \
  16.754 -   errno = ENOMEM;
  16.755 -
  16.756 -#else
  16.757 -#define MALLOC_FAILURE_ACTION
  16.758 -#endif
  16.759 -#endif
  16.760 -
  16.761 -/*
  16.762 -  MORECORE-related declarations. By default, rely on sbrk
  16.763 -*/
  16.764 -
  16.765 -
  16.766 -#ifdef LACKS_UNISTD_H
  16.767 -#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
  16.768 -#if __STD_C
  16.769 -extern Void_t*     sbrk(ptrdiff_t);
  16.770 -#else
  16.771 -extern Void_t*     sbrk();
  16.772 -#endif
  16.773 -#endif
  16.774 -#endif
  16.775 -
  16.776 -/*
  16.777 -  MORECORE is the name of the routine to call to obtain more memory
  16.778 -  from the system.  See below for general guidance on writing
  16.779 -  alternative MORECORE functions, as well as a version for WIN32 and a
  16.780 -  sample version for pre-OSX macos.
  16.781 -*/
  16.782 -
  16.783 -#ifndef MORECORE
  16.784 -#define MORECORE sbrk
  16.785 -#endif
  16.786 -
  16.787 -/*
  16.788 -  MORECORE_FAILURE is the value returned upon failure of MORECORE
  16.789 -  as well as mmap. Since it cannot be an otherwise valid memory address,
  16.790 -  and must reflect values of standard sys calls, you probably ought not
  16.791 -  try to redefine it.
  16.792 -*/
  16.793 -
  16.794 -#ifndef MORECORE_FAILURE
  16.795 -#define MORECORE_FAILURE (-1)
  16.796 -#endif
  16.797 -
  16.798 -/*
  16.799 -  If MORECORE_CONTIGUOUS is true, take advantage of fact that
  16.800 -  consecutive calls to MORECORE with positive arguments always return
  16.801 -  contiguous increasing addresses.  This is true of unix sbrk.  Even
  16.802 -  if not defined, when regions happen to be contiguous, malloc will
  16.803 -  permit allocations spanning regions obtained from different
  16.804 -  calls. But defining this when applicable enables some stronger
  16.805 -  consistency checks and space efficiencies. 
  16.806 -*/
  16.807 -
  16.808 -#ifndef MORECORE_CONTIGUOUS
  16.809 -#define MORECORE_CONTIGUOUS 1
  16.810 -#endif
  16.811 -
  16.812 -/*
  16.813 -  Define MORECORE_CANNOT_TRIM if your version of MORECORE
  16.814 -  cannot release space back to the system when given negative
  16.815 -  arguments. This is generally necessary only if you are using
  16.816 -  a hand-crafted MORECORE function that cannot handle negative arguments.
  16.817 -*/
  16.818 -
  16.819 -/* #define MORECORE_CANNOT_TRIM */
  16.820 -
  16.821 -
  16.822 -/*
  16.823 -  Define HAVE_MMAP as true to optionally make malloc() use mmap() to
  16.824 -  allocate very large blocks.  These will be returned to the
  16.825 -  operating system immediately after a free(). Also, if mmap
  16.826 -  is available, it is used as a backup strategy in cases where
  16.827 -  MORECORE fails to provide space from system.
  16.828 -
  16.829 -  This malloc is best tuned to work with mmap for large requests.
  16.830 -  If you do not have mmap, operations involving very large chunks (1MB
  16.831 -  or so) may be slower than you'd like.
  16.832 -*/
  16.833 -
  16.834 -#ifndef HAVE_MMAP
  16.835 -#define HAVE_MMAP 1
  16.836 -#endif
  16.837 -
  16.838 -#if HAVE_MMAP
  16.839 -/* 
  16.840 -   Standard unix mmap using /dev/zero clears memory so calloc doesn't
  16.841 -   need to.
  16.842 -*/
  16.843 -
  16.844 -#ifndef MMAP_CLEARS
  16.845 -#define MMAP_CLEARS 1
  16.846 -#endif
  16.847 -
  16.848 -#else /* no mmap */
  16.849 -#ifndef MMAP_CLEARS
  16.850 -#define MMAP_CLEARS 0
  16.851 -#endif
  16.852 -#endif
  16.853 -
  16.854 -
  16.855 -/* 
  16.856 -   MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
  16.857 -   sbrk fails, and mmap is used as a backup (which is done only if
  16.858 -   HAVE_MMAP).  The value must be a multiple of page size.  This
  16.859 -   backup strategy generally applies only when systems have "holes" in
  16.860 -   address space, so sbrk cannot perform contiguous expansion, but
  16.861 -   there is still space available on system.  On systems for which
  16.862 -   this is known to be useful (i.e. most linux kernels), this occurs
  16.863 -   only when programs allocate huge amounts of memory.  Between this,
  16.864 -   and the fact that mmap regions tend to be limited, the size should
  16.865 -   be large, to avoid too many mmap calls and thus avoid running out
  16.866 -   of kernel resources.
  16.867 -*/
  16.868 -
  16.869 -#ifndef MMAP_AS_MORECORE_SIZE
  16.870 -#define MMAP_AS_MORECORE_SIZE (1024 * 1024)
  16.871 -#endif
  16.872 -
  16.873 -/*
  16.874 -  Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
  16.875 -  large blocks.  This is currently only possible on Linux with
  16.876 -  kernel versions newer than 1.3.77.
  16.877 -*/
  16.878 -
  16.879 -#ifndef HAVE_MREMAP
  16.880 -#ifdef linux
  16.881 -#define HAVE_MREMAP 1
  16.882 -#else
  16.883 -#define HAVE_MREMAP 0
  16.884 -#endif
  16.885 -
  16.886 -#endif /* HAVE_MMAP */
  16.887 -
  16.888 -
  16.889 -/*
  16.890 -  The system page size. To the extent possible, this malloc manages
  16.891 -  memory from the system in page-size units.  Note that this value is
  16.892 -  cached during initialization into a field of malloc_state. So even
  16.893 -  if malloc_getpagesize is a function, it is only called once.
  16.894 -
  16.895 -  The following mechanics for getpagesize were adapted from bsd/gnu
  16.896 -  getpagesize.h. If none of the system-probes here apply, a value of
  16.897 -  4096 is used, which should be OK: If they don't apply, then using
  16.898 -  the actual value probably doesn't impact performance.
  16.899 -*/
  16.900 -
  16.901 -
  16.902 -#ifndef malloc_getpagesize
  16.903 -
  16.904 -#ifndef LACKS_UNISTD_H
  16.905 -#  include <unistd.h>
  16.906 -#endif
  16.907 -
  16.908 -#  ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
  16.909 -#    ifndef _SC_PAGE_SIZE
  16.910 -#      define _SC_PAGE_SIZE _SC_PAGESIZE
  16.911 -#    endif
  16.912 -#  endif
  16.913 -
  16.914 -#  ifdef _SC_PAGE_SIZE
  16.915 -#    define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
  16.916 -#  else
  16.917 -#    if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
  16.918 -       extern size_t getpagesize();
  16.919 -#      define malloc_getpagesize getpagesize()
  16.920 -#    else
  16.921 -#      ifdef WIN32 /* use supplied emulation of getpagesize */
  16.922 -#        define malloc_getpagesize getpagesize() 
  16.923 -#      else
  16.924 -#        ifndef LACKS_SYS_PARAM_H
  16.925 -#          include <sys/param.h>
  16.926 -#        endif
  16.927 -#        ifdef EXEC_PAGESIZE
  16.928 -#          define malloc_getpagesize EXEC_PAGESIZE
  16.929 -#        else
  16.930 -#          ifdef NBPG
  16.931 -#            ifndef CLSIZE
  16.932 -#              define malloc_getpagesize NBPG
  16.933 -#            else
  16.934 -#              define malloc_getpagesize (NBPG * CLSIZE)
  16.935 -#            endif
  16.936 -#          else
  16.937 -#            ifdef NBPC
  16.938 -#              define malloc_getpagesize NBPC
  16.939 -#            else
  16.940 -#              ifdef PAGESIZE
  16.941 -#                define malloc_getpagesize PAGESIZE
  16.942 -#              else /* just guess */
  16.943 -#                define malloc_getpagesize (4096) 
  16.944 -#              endif
  16.945 -#            endif
  16.946 -#          endif
  16.947 -#        endif
  16.948 -#      endif
  16.949 -#    endif
  16.950 -#  endif
  16.951 -#endif
  16.952 -
  16.953 -/*
  16.954 -  This version of malloc supports the standard SVID/XPG mallinfo
  16.955 -  routine that returns a struct containing usage properties and
  16.956 -  statistics. It should work on any SVID/XPG compliant system that has
  16.957 -  a /usr/include/malloc.h defining struct mallinfo. (If you'd like to
  16.958 -  install such a thing yourself, cut out the preliminary declarations
  16.959 -  as described above and below and save them in a malloc.h file. But
  16.960 -  there's no compelling reason to bother to do this.)
  16.961 -
  16.962 -  The main declaration needed is the mallinfo struct that is returned
  16.963 -  (by-copy) by mallinfo().  The SVID/XPG malloinfo struct contains a
  16.964 -  bunch of fields that are not even meaningful in this version of
  16.965 -  malloc.  These fields are are instead filled by mallinfo() with
  16.966 -  other numbers that might be of interest.
  16.967 -
  16.968 -  HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
  16.969 -  /usr/include/malloc.h file that includes a declaration of struct
  16.970 -  mallinfo.  If so, it is included; else an SVID2/XPG2 compliant
  16.971 -  version is declared below.  These must be precisely the same for
  16.972 -  mallinfo() to work.  The original SVID version of this struct,
  16.973 -  defined on most systems with mallinfo, declares all fields as
  16.974 -  ints. But some others define as unsigned long. If your system
  16.975 -  defines the fields using a type of different width than listed here,
  16.976 -  you must #include your system version and #define
  16.977 -  HAVE_USR_INCLUDE_MALLOC_H.
  16.978 -*/
  16.979 -
  16.980 -/* #define HAVE_USR_INCLUDE_MALLOC_H */
  16.981 -
  16.982 -#ifdef HAVE_USR_INCLUDE_MALLOC_H
  16.983 -#include "/usr/include/malloc.h"
  16.984 -#else
  16.985 -
  16.986 -/* SVID2/XPG mallinfo structure */
  16.987 -
  16.988 -struct mallinfo {
  16.989 -  int arena;    /* non-mmapped space allocated from system */
  16.990 -  int ordblks;  /* number of free chunks */
  16.991 -  int smblks;   /* number of fastbin blocks */
  16.992 -  int hblks;    /* number of mmapped regions */
  16.993 -  int hblkhd;   /* space in mmapped regions */
  16.994 -  int usmblks;  /* maximum total allocated space */
  16.995 -  int fsmblks;  /* space available in freed fastbin blocks */
  16.996 -  int uordblks; /* total allocated space */
  16.997 -  int fordblks; /* total free space */
  16.998 -  int keepcost; /* top-most, releasable (via malloc_trim) space */
  16.999 -};
 16.1000 -
 16.1001 -/*
 16.1002 -  SVID/XPG defines four standard parameter numbers for mallopt,
 16.1003 -  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
 16.1004 -  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
 16.1005 -  so setting them has no effect. But this malloc also supports other
 16.1006 -  options in mallopt described below.
 16.1007 -*/
 16.1008 -#endif
 16.1009 -
 16.1010 -
 16.1011 -/* ---------- description of public routines ------------ */
 16.1012 -
 16.1013 -/*
 16.1014 -  malloc(size_t n)
 16.1015 -  Returns a pointer to a newly allocated chunk of at least n bytes, or null
 16.1016 -  if no space is available. Additionally, on failure, errno is
 16.1017 -  set to ENOMEM on ANSI C systems.
 16.1018 -
 16.1019 -  If n is zero, malloc returns a minumum-sized chunk. (The minimum
 16.1020 -  size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
 16.1021 -  systems.)  On most systems, size_t is an unsigned type, so calls
 16.1022 -  with negative arguments are interpreted as requests for huge amounts
 16.1023 -  of space, which will often fail. The maximum supported value of n
 16.1024 -  differs across systems, but is in all cases less than the maximum
 16.1025 -  representable value of a size_t.
 16.1026 -*/
 16.1027 -#if __STD_C
 16.1028 -Void_t*  public_mALLOc(size_t);
 16.1029 -#else
 16.1030 -Void_t*  public_mALLOc();
 16.1031 -#endif
 16.1032 -
 16.1033 -/*
 16.1034 -  free(Void_t* p)
 16.1035 -  Releases the chunk of memory pointed to by p, that had been previously
 16.1036 -  allocated using malloc or a related routine such as realloc.
 16.1037 -  It has no effect if p is null. It can have arbitrary (i.e., bad!)
 16.1038 -  effects if p has already been freed.
 16.1039 -
 16.1040 -  Unless disabled (using mallopt), freeing very large spaces will
 16.1041 -  when possible, automatically trigger operations that give
 16.1042 -  back unused memory to the system, thus reducing program footprint.
 16.1043 -*/
 16.1044 -#if __STD_C
 16.1045 -void     public_fREe(Void_t*);
 16.1046 -#else
 16.1047 -void     public_fREe();
 16.1048 -#endif
 16.1049 -
 16.1050 -/*
 16.1051 -  calloc(size_t n_elements, size_t element_size);
 16.1052 -  Returns a pointer to n_elements * element_size bytes, with all locations
 16.1053 -  set to zero.
 16.1054 -*/
 16.1055 -#if __STD_C
 16.1056 -Void_t*  public_cALLOc(size_t, size_t);
 16.1057 -#else
 16.1058 -Void_t*  public_cALLOc();
 16.1059 -#endif
 16.1060 -
 16.1061 -/*
 16.1062 -  realloc(Void_t* p, size_t n)
 16.1063 -  Returns a pointer to a chunk of size n that contains the same data
 16.1064 -  as does chunk p up to the minimum of (n, p's size) bytes, or null
 16.1065 -  if no space is available. 
 16.1066 -
 16.1067 -  The returned pointer may or may not be the same as p. The algorithm
 16.1068 -  prefers extending p when possible, otherwise it employs the
 16.1069 -  equivalent of a malloc-copy-free sequence.
 16.1070 -
 16.1071 -  If p is null, realloc is equivalent to malloc.  
 16.1072 -
 16.1073 -  If space is not available, realloc returns null, errno is set (if on
 16.1074 -  ANSI) and p is NOT freed.
 16.1075 -
 16.1076 -  if n is for fewer bytes than already held by p, the newly unused
 16.1077 -  space is lopped off and freed if possible.  Unless the #define
 16.1078 -  REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
 16.1079 -  zero (re)allocates a minimum-sized chunk.
 16.1080 -
 16.1081 -  Large chunks that were internally obtained via mmap will always
 16.1082 -  be reallocated using malloc-copy-free sequences unless
 16.1083 -  the system supports MREMAP (currently only linux).
 16.1084 -
 16.1085 -  The old unix realloc convention of allowing the last-free'd chunk
 16.1086 -  to be used as an argument to realloc is not supported.
 16.1087 -*/
 16.1088 -#if __STD_C
 16.1089 -Void_t*  public_rEALLOc(Void_t*, size_t);
 16.1090 -#else
 16.1091 -Void_t*  public_rEALLOc();
 16.1092 -#endif
 16.1093 -
 16.1094 -/*
 16.1095 -  memalign(size_t alignment, size_t n);
 16.1096 -  Returns a pointer to a newly allocated chunk of n bytes, aligned
 16.1097 -  in accord with the alignment argument.
 16.1098 -
 16.1099 -  The alignment argument should be a power of two. If the argument is
 16.1100 -  not a power of two, the nearest greater power is used.
 16.1101 -  8-byte alignment is guaranteed by normal malloc calls, so don't
 16.1102 -  bother calling memalign with an argument of 8 or less.
 16.1103 -
 16.1104 -  Overreliance on memalign is a sure way to fragment space.
 16.1105 -*/
 16.1106 -#if __STD_C
 16.1107 -Void_t*  public_mEMALIGn(size_t, size_t);
 16.1108 -#else
 16.1109 -Void_t*  public_mEMALIGn();
 16.1110 -#endif
 16.1111 -
 16.1112 -/*
 16.1113 -  valloc(size_t n);
 16.1114 -  Equivalent to memalign(pagesize, n), where pagesize is the page
 16.1115 -  size of the system. If the pagesize is unknown, 4096 is used.
 16.1116 -*/
 16.1117 -#if __STD_C
 16.1118 -Void_t*  public_vALLOc(size_t);
 16.1119 -#else
 16.1120 -Void_t*  public_vALLOc();
 16.1121 -#endif
 16.1122 -
 16.1123 -
 16.1124 -
 16.1125 -/*
 16.1126 -  mallopt(int parameter_number, int parameter_value)
 16.1127 -  Sets tunable parameters The format is to provide a
 16.1128 -  (parameter-number, parameter-value) pair.  mallopt then sets the
 16.1129 -  corresponding parameter to the argument value if it can (i.e., so
 16.1130 -  long as the value is meaningful), and returns 1 if successful else
 16.1131 -  0.  SVID/XPG/ANSI defines four standard param numbers for mallopt,
 16.1132 -  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
 16.1133 -  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
 16.1134 -  so setting them has no effect. But this malloc also supports four
 16.1135 -  other options in mallopt. See below for details.  Briefly, supported
 16.1136 -  parameters are as follows (listed defaults are for "typical"
 16.1137 -  configurations).
 16.1138 -
 16.1139 -  Symbol            param #   default    allowed param values
 16.1140 -  M_MXFAST          1         64         0-80  (0 disables fastbins)
 16.1141 -  M_TRIM_THRESHOLD -1         256*1024   any   (-1U disables trimming)
 16.1142 -  M_TOP_PAD        -2         0          any  
 16.1143 -  M_MMAP_THRESHOLD -3         256*1024   any   (or 0 if no MMAP support)
 16.1144 -  M_MMAP_MAX       -4         65536      any   (0 disables use of mmap)
 16.1145 -*/
 16.1146 -#if __STD_C
 16.1147 -int      public_mALLOPt(int, int);
 16.1148 -#else
 16.1149 -int      public_mALLOPt();
 16.1150 -#endif
 16.1151 -
 16.1152 -
 16.1153 -/*
 16.1154 -  mallinfo()
 16.1155 -  Returns (by copy) a struct containing various summary statistics:
 16.1156 -
 16.1157 -  arena:     current total non-mmapped bytes allocated from system 
 16.1158 -  ordblks:   the number of free chunks 
 16.1159 -  smblks:    the number of fastbin blocks (i.e., small chunks that
 16.1160 -               have been freed but not use resused or consolidated)
 16.1161 -  hblks:     current number of mmapped regions 
 16.1162 -  hblkhd:    total bytes held in mmapped regions 
 16.1163 -  usmblks:   the maximum total allocated space. This will be greater
 16.1164 -                than current total if trimming has occurred.
 16.1165 -  fsmblks:   total bytes held in fastbin blocks 
 16.1166 -  uordblks:  current total allocated space (normal or mmapped)
 16.1167 -  fordblks:  total free space 
 16.1168 -  keepcost:  the maximum number of bytes that could ideally be released
 16.1169 -               back to system via malloc_trim. ("ideally" means that
 16.1170 -               it ignores page restrictions etc.)
 16.1171 -
 16.1172 -  Because these fields are ints, but internal bookkeeping may
 16.1173 -  be kept as longs, the reported values may wrap around zero and 
 16.1174 -  thus be inaccurate.
 16.1175 -*/
 16.1176 -#if __STD_C
 16.1177 -struct mallinfo public_mALLINFo(void);
 16.1178 -#else
 16.1179 -struct mallinfo public_mALLINFo();
 16.1180 -#endif
 16.1181 -
 16.1182 -/*
 16.1183 -  independent_calloc(size_t n_elements, size_t element_size, Void_t* chunks[]);
 16.1184 -
 16.1185 -  independent_calloc is similar to calloc, but instead of returning a
 16.1186 -  single cleared space, it returns an array of pointers to n_elements
 16.1187 -  independent elements that can hold contents of size elem_size, each
 16.1188 -  of which starts out cleared, and can be independently freed,
 16.1189 -  realloc'ed etc. The elements are guaranteed to be adjacently
 16.1190 -  allocated (this is not guaranteed to occur with multiple callocs or
 16.1191 -  mallocs), which may also improve cache locality in some
 16.1192 -  applications.
 16.1193 -
 16.1194 -  The "chunks" argument is optional (i.e., may be null, which is
 16.1195 -  probably the most typical usage). If it is null, the returned array
 16.1196 -  is itself dynamically allocated and should also be freed when it is
 16.1197 -  no longer needed. Otherwise, the chunks array must be of at least
 16.1198 -  n_elements in length. It is filled in with the pointers to the
 16.1199 -  chunks.
 16.1200 -
 16.1201 -  In either case, independent_calloc returns this pointer array, or
 16.1202 -  null if the allocation failed.  If n_elements is zero and "chunks"
 16.1203 -  is null, it returns a chunk representing an array with zero elements
 16.1204 -  (which should be freed if not wanted).
 16.1205 -
 16.1206 -  Each element must be individually freed when it is no longer
 16.1207 -  needed. If you'd like to instead be able to free all at once, you
 16.1208 -  should instead use regular calloc and assign pointers into this
 16.1209 -  space to represent elements.  (In this case though, you cannot
 16.1210 -  independently free elements.)
 16.1211 -  
 16.1212 -  independent_calloc simplifies and speeds up implementations of many
 16.1213 -  kinds of pools.  It may also be useful when constructing large data
 16.1214 -  structures that initially have a fixed number of fixed-sized nodes,
 16.1215 -  but the number is not known at compile time, and some of the nodes
 16.1216 -  may later need to be freed. For example:
 16.1217 -
 16.1218 -  struct Node { int item; struct Node* next; };
 16.1219 -  
 16.1220 -  struct Node* build_list() {
 16.1221 -    struct Node** pool;
 16.1222 -    int n = read_number_of_nodes_needed();
 16.1223 -    if (n <= 0) return 0;
 16.1224 -    pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
 16.1225 -    if (pool == 0) die(); 
 16.1226 -    // organize into a linked list... 
 16.1227 -    struct Node* first = pool[0];
 16.1228 -    for (i = 0; i < n-1; ++i) 
 16.1229 -      pool[i]->next = pool[i+1];
 16.1230 -    free(pool);     // Can now free the array (or not, if it is needed later)
 16.1231 -    return first;
 16.1232 -  }
 16.1233 -*/
 16.1234 -#if __STD_C
 16.1235 -Void_t** public_iCALLOc(size_t, size_t, Void_t**);
 16.1236 -#else
 16.1237 -Void_t** public_iCALLOc();
 16.1238 -#endif
 16.1239 -
 16.1240 -/*
 16.1241 -  independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]);
 16.1242 -
 16.1243 -  independent_comalloc allocates, all at once, a set of n_elements
 16.1244 -  chunks with sizes indicated in the "sizes" array.    It returns
 16.1245 -  an array of pointers to these elements, each of which can be
 16.1246 -  independently freed, realloc'ed etc. The elements are guaranteed to
 16.1247 -  be adjacently allocated (this is not guaranteed to occur with
 16.1248 -  multiple callocs or mallocs), which may also improve cache locality
 16.1249 -  in some applications.
 16.1250 -
 16.1251 -  The "chunks" argument is optional (i.e., may be null). If it is null
 16.1252 -  the returned array is itself dynamically allocated and should also
 16.1253 -  be freed when it is no longer needed. Otherwise, the chunks array
 16.1254 -  must be of at least n_elements in length. It is filled in with the
 16.1255 -  pointers to the chunks.
 16.1256 -
 16.1257 -  In either case, independent_comalloc returns this pointer array, or
 16.1258 -  null if the allocation failed.  If n_elements is zero and chunks is
 16.1259 -  null, it returns a chunk representing an array with zero elements
 16.1260 -  (which should be freed if not wanted).
 16.1261 -  
 16.1262 -  Each element must be individually freed when it is no longer
 16.1263 -  needed. If you'd like to instead be able to free all at once, you
 16.1264 -  should instead use a single regular malloc, and assign pointers at
 16.1265 -  particular offsets in the aggregate space. (In this case though, you 
 16.1266 -  cannot independently free elements.)
 16.1267 -
 16.1268 -  independent_comallac differs from independent_calloc in that each
 16.1269 -  element may have a different size, and also that it does not
 16.1270 -  automatically clear elements.
 16.1271 -
 16.1272 -  independent_comalloc can be used to speed up allocation in cases
 16.1273 -  where several structs or objects must always be allocated at the
 16.1274 -  same time.  For example:
 16.1275 -
 16.1276 -  struct Head { ... }
 16.1277 -  struct Foot { ... }
 16.1278 -
 16.1279 -  void send_message(char* msg) {
 16.1280 -    int msglen = strlen(msg);
 16.1281 -    size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
 16.1282 -    void* chunks[3];
 16.1283 -    if (independent_comalloc(3, sizes, chunks) == 0)
 16.1284 -      die();
 16.1285 -    struct Head* head = (struct Head*)(chunks[0]);
 16.1286 -    char*        body = (char*)(chunks[1]);
 16.1287 -    struct Foot* foot = (struct Foot*)(chunks[2]);
 16.1288 -    // ...
 16.1289 -  }
 16.1290 -
 16.1291 -  In general though, independent_comalloc is worth using only for
 16.1292 -  larger values of n_elements. For small values, you probably won't
 16.1293 -  detect enough difference from series of malloc calls to bother.
 16.1294 -
 16.1295 -  Overuse of independent_comalloc can increase overall memory usage,
 16.1296 -  since it cannot reuse existing noncontiguous small chunks that
 16.1297 -  might be available for some of the elements.
 16.1298 -*/
 16.1299 -#if __STD_C
 16.1300 -Void_t** public_iCOMALLOc(size_t, size_t*, Void_t**);
 16.1301 -#else
 16.1302 -Void_t** public_iCOMALLOc();
 16.1303 -#endif
 16.1304 -
 16.1305 -
 16.1306 -/*
 16.1307 -  pvalloc(size_t n);
 16.1308 -  Equivalent to valloc(minimum-page-that-holds(n)), that is,
 16.1309 -  round up n to nearest pagesize.
 16.1310 - */
 16.1311 -#if __STD_C
 16.1312 -Void_t*  public_pVALLOc(size_t);
 16.1313 -#else
 16.1314 -Void_t*  public_pVALLOc();
 16.1315 -#endif
 16.1316 -
 16.1317 -/*
 16.1318 -  cfree(Void_t* p);
 16.1319 -  Equivalent to free(p).
 16.1320 -
 16.1321 -  cfree is needed/defined on some systems that pair it with calloc,
 16.1322 -  for odd historical reasons (such as: cfree is used in example 
 16.1323 -  code in the first edition of K&R).
 16.1324 -*/
 16.1325 -#if __STD_C
 16.1326 -void     public_cFREe(Void_t*);
 16.1327 -#else
 16.1328 -void     public_cFREe();
 16.1329 -#endif
 16.1330 -
 16.1331 -/*
 16.1332 -  malloc_trim(size_t pad);
 16.1333 -
 16.1334 -  If possible, gives memory back to the system (via negative
 16.1335 -  arguments to sbrk) if there is unused memory at the `high' end of
 16.1336 -  the malloc pool. You can call this after freeing large blocks of
 16.1337 -  memory to potentially reduce the system-level memory requirements
 16.1338 -  of a program. However, it cannot guarantee to reduce memory. Under
 16.1339 -  some allocation patterns, some large free blocks of memory will be
 16.1340 -  locked between two used chunks, so they cannot be given back to
 16.1341 -  the system.
 16.1342 -  
 16.1343 -  The `pad' argument to malloc_trim represents the amount of free
 16.1344 -  trailing space to leave untrimmed. If this argument is zero,
 16.1345 -  only the minimum amount of memory to maintain internal data
 16.1346 -  structures will be left (one page or less). Non-zero arguments
 16.1347 -  can be supplied to maintain enough trailing space to service
 16.1348 -  future expected allocations without having to re-obtain memory
 16.1349 -  from the system.
 16.1350 -  
 16.1351 -  Malloc_trim returns 1 if it actually released any memory, else 0.
 16.1352 -  On systems that do not support "negative sbrks", it will always
 16.1353 -  rreturn 0.
 16.1354 -*/
 16.1355 -#if __STD_C
 16.1356 -int      public_mTRIm(size_t);
 16.1357 -#else
 16.1358 -int      public_mTRIm();
 16.1359 -#endif
 16.1360 -
 16.1361 -/*
 16.1362 -  malloc_usable_size(Void_t* p);
 16.1363 -
 16.1364 -  Returns the number of bytes you can actually use in
 16.1365 -  an allocated chunk, which may be more than you requested (although
 16.1366 -  often not) due to alignment and minimum size constraints.
 16.1367 -  You can use this many bytes without worrying about
 16.1368 -  overwriting other allocated objects. This is not a particularly great
 16.1369 -  programming practice. malloc_usable_size can be more useful in
 16.1370 -  debugging and assertions, for example:
 16.1371 -
 16.1372 -  p = malloc(n);
 16.1373 -  assert(malloc_usable_size(p) >= 256);
 16.1374 -
 16.1375 -*/
 16.1376 -#if __STD_C
 16.1377 -size_t   public_mUSABLe(Void_t*);
 16.1378 -#else
 16.1379 -size_t   public_mUSABLe();
 16.1380 -#endif
 16.1381 -
 16.1382 -/*
 16.1383 -  malloc_stats();
 16.1384 -  Prints on stderr the amount of space obtained from the system (both
 16.1385 -  via sbrk and mmap), the maximum amount (which may be more than
 16.1386 -  current if malloc_trim and/or munmap got called), and the current
 16.1387 -  number of bytes allocated via malloc (or realloc, etc) but not yet
 16.1388 -  freed. Note that this is the number of bytes allocated, not the
 16.1389 -  number requested. It will be larger than the number requested
 16.1390 -  because of alignment and bookkeeping overhead. Because it includes
 16.1391 -  alignment wastage as being in use, this figure may be greater than
 16.1392 -  zero even when no user-level chunks are allocated.
 16.1393 -
 16.1394 -  The reported current and maximum system memory can be inaccurate if
 16.1395 -  a program makes other calls to system memory allocation functions
 16.1396 -  (normally sbrk) outside of malloc.
 16.1397 -
 16.1398 -  malloc_stats prints only the most commonly interesting statistics.
 16.1399 -  More information can be obtained by calling mallinfo.
 16.1400 -
 16.1401 -*/
 16.1402 -#if __STD_C
 16.1403 -void     public_mSTATs();
 16.1404 -#else
 16.1405 -void     public_mSTATs();
 16.1406 -#endif
 16.1407 -
 16.1408 -/* mallopt tuning options */
 16.1409 -
 16.1410 -/*
 16.1411 -  M_MXFAST is the maximum request size used for "fastbins", special bins
 16.1412 -  that hold returned chunks without consolidating their spaces. This
 16.1413 -  enables future requests for chunks of the same size to be handled
 16.1414 -  very quickly, but can increase fragmentation, and thus increase the
 16.1415 -  overall memory footprint of a program.
 16.1416 -
 16.1417 -  This malloc manages fastbins very conservatively yet still
 16.1418 -  efficiently, so fragmentation is rarely a problem for values less
 16.1419 -  than or equal to the default.  The maximum supported value of MXFAST
 16.1420 -  is 80. You wouldn't want it any higher than this anyway.  Fastbins
 16.1421 -  are designed especially for use with many small structs, objects or
 16.1422 -  strings -- the default handles structs/objects/arrays with sizes up
 16.1423 -  to 16 4byte fields, or small strings representing words, tokens,
 16.1424 -  etc. Using fastbins for larger objects normally worsens
 16.1425 -  fragmentation without improving speed.
 16.1426 -
 16.1427 -  M_MXFAST is set in REQUEST size units. It is internally used in
 16.1428 -  chunksize units, which adds padding and alignment.  You can reduce
 16.1429 -  M_MXFAST to 0 to disable all use of fastbins.  This causes the malloc
 16.1430 -  algorithm to be a closer approximation of fifo-best-fit in all cases,
 16.1431 -  not just for larger requests, but will generally cause it to be
 16.1432 -  slower.
 16.1433 -*/
 16.1434 -
 16.1435 -
 16.1436 -/* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
 16.1437 -#ifndef M_MXFAST
 16.1438 -#define M_MXFAST            1    
 16.1439 -#endif
 16.1440 -
 16.1441 -#ifndef DEFAULT_MXFAST
 16.1442 -#define DEFAULT_MXFAST     64
 16.1443 -#endif
 16.1444 -
 16.1445 -
 16.1446 -/*
 16.1447 -  M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
 16.1448 -  to keep before releasing via malloc_trim in free().
 16.1449 -
 16.1450 -  Automatic trimming is mainly useful in long-lived programs.
 16.1451 -  Because trimming via sbrk can be slow on some systems, and can
 16.1452 -  sometimes be wasteful (in cases where programs immediately
 16.1453 -  afterward allocate more large chunks) the value should be high
 16.1454 -  enough so that your overall system performance would improve by
 16.1455 -  releasing this much memory.
 16.1456 -
 16.1457 -  The trim threshold and the mmap control parameters (see below)
 16.1458 -  can be traded off with one another. Trimming and mmapping are
 16.1459 -  two different ways of releasing unused memory back to the
 16.1460 -  system. Between these two, it is often possible to keep
 16.1461 -  system-level demands of a long-lived program down to a bare
 16.1462 -  minimum. For example, in one test suite of sessions measuring
 16.1463 -  the XF86 X server on Linux, using a trim threshold of 128K and a
 16.1464 -  mmap threshold of 192K led to near-minimal long term resource
 16.1465 -  consumption.
 16.1466 -
 16.1467 -  If you are using this malloc in a long-lived program, it should
 16.1468 -  pay to experiment with these values.  As a rough guide, you
 16.1469 -  might set to a value close to the average size of a process
 16.1470 -  (program) running on your system.  Releasing this much memory
 16.1471 -  would allow such a process to run in memory.  Generally, it's
 16.1472 -  worth it to tune for trimming rather tham memory mapping when a
 16.1473 -  program undergoes phases where several large chunks are
 16.1474 -  allocated and released in ways that can reuse each other's
 16.1475 -  storage, perhaps mixed with phases where there are no such
 16.1476 -  chunks at all.  And in well-behaved long-lived programs,
 16.1477 -  controlling release of large blocks via trimming versus mapping
 16.1478 -  is usually faster.
 16.1479 -
 16.1480 -  However, in most programs, these parameters serve mainly as
 16.1481 -  protection against the system-level effects of carrying around
 16.1482 -  massive amounts of unneeded memory. Since frequent calls to
 16.1483 -  sbrk, mmap, and munmap otherwise degrade performance, the default
 16.1484 -  parameters are set to relatively high values that serve only as
 16.1485 -  safeguards.
 16.1486 -
 16.1487 -  The trim value must be greater than page size to have any useful
 16.1488 -  effect.  To disable trimming completely, you can set to 
 16.1489 -  (unsigned long)(-1)
 16.1490 -
 16.1491 -  Trim settings interact with fastbin (MXFAST) settings: Unless
 16.1492 -  TRIM_FASTBINS is defined, automatic trimming never takes place upon
 16.1493 -  freeing a chunk with size less than or equal to MXFAST. Trimming is
 16.1494 -  instead delayed until subsequent freeing of larger chunks. However,
 16.1495 -  you can still force an attempted trim by calling malloc_trim.
 16.1496 -
 16.1497 -  Also, trimming is not generally possible in cases where
 16.1498 -  the main arena is obtained via mmap.
 16.1499 -
 16.1500 -  Note that the trick some people use of mallocing a huge space and
 16.1501 -  then freeing it at program startup, in an attempt to reserve system
 16.1502 -  memory, doesn't have the intended effect under automatic trimming,
 16.1503 -  since that memory will immediately be returned to the system.
 16.1504 -*/
 16.1505 -
 16.1506 -#define M_TRIM_THRESHOLD       -1
 16.1507 -
 16.1508 -#ifndef DEFAULT_TRIM_THRESHOLD
 16.1509 -#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
 16.1510 -#endif
 16.1511 -
 16.1512 -/*
 16.1513 -  M_TOP_PAD is the amount of extra `padding' space to allocate or
 16.1514 -  retain whenever sbrk is called. It is used in two ways internally:
 16.1515 -
 16.1516 -  * When sbrk is called to extend the top of the arena to satisfy
 16.1517 -  a new malloc request, this much padding is added to the sbrk
 16.1518 -  request.
 16.1519 -
 16.1520 -  * When malloc_trim is called automatically from free(),
 16.1521 -  it is used as the `pad' argument.
 16.1522 -
 16.1523 -  In both cases, the actual amount of padding is rounded
 16.1524 -  so that the end of the arena is always a system page boundary.
 16.1525 -
 16.1526 -  The main reason for using padding is to avoid calling sbrk so
 16.1527 -  often. Having even a small pad greatly reduces the likelihood
 16.1528 -  that nearly every malloc request during program start-up (or
 16.1529 -  after trimming) will invoke sbrk, which needlessly wastes
 16.1530 -  time.
 16.1531 -
 16.1532 -  Automatic rounding-up to page-size units is normally sufficient
 16.1533 -  to avoid measurable overhead, so the default is 0.  However, in
 16.1534 -  systems where sbrk is relatively slow, it can pay to increase
 16.1535 -  this value, at the expense of carrying around more memory than
 16.1536 -  the program needs.
 16.1537 -*/
 16.1538 -
 16.1539 -#define M_TOP_PAD              -2
 16.1540 -
 16.1541 -#ifndef DEFAULT_TOP_PAD
 16.1542 -#define DEFAULT_TOP_PAD        (0)
 16.1543 -#endif
 16.1544 -
 16.1545 -/*
 16.1546 -  M_MMAP_THRESHOLD is the request size threshold for using mmap()
 16.1547 -  to service a request. Requests of at least this size that cannot
 16.1548 -  be allocated using already-existing space will be serviced via mmap.
 16.1549 -  (If enough normal freed space already exists it is used instead.)
 16.1550 -
 16.1551 -  Using mmap segregates relatively large chunks of memory so that
 16.1552 -  they can be individually obtained and released from the host
 16.1553 -  system. A request serviced through mmap is never reused by any
 16.1554 -  other request (at least not directly; the system may just so
 16.1555 -  happen to remap successive requests to the same locations).
 16.1556 -
 16.1557 -  Segregating space in this way has the benefits that:
 16.1558 -
 16.1559 -   1. Mmapped space can ALWAYS be individually released back 
 16.1560 -      to the system, which helps keep the system level memory 
 16.1561 -      demands of a long-lived program low. 
 16.1562 -   2. Mapped memory can never become `locked' between
 16.1563 -      other chunks, as can happen with normally allocated chunks, which
 16.1564 -      means that even trimming via malloc_trim would not release them.
 16.1565 -   3. On some systems with "holes" in address spaces, mmap can obtain
 16.1566 -      memory that sbrk cannot.
 16.1567 -
 16.1568 -  However, it has the disadvantages that:
 16.1569 -
 16.1570 -   1. The space cannot be reclaimed, consolidated, and then
 16.1571 -      used to service later requests, as happens with normal chunks.
 16.1572 -   2. It can lead to more wastage because of mmap page alignment
 16.1573 -      requirements
 16.1574 -   3. It causes malloc performance to be more dependent on host
 16.1575 -      system memory management support routines which may vary in
 16.1576 -      implementation quality and may impose arbitrary
 16.1577 -      limitations. Generally, servicing a request via normal
 16.1578 -      malloc steps is faster than going through a system's mmap.
 16.1579 -
 16.1580 -  The advantages of mmap nearly always outweigh disadvantages for
 16.1581 -  "large" chunks, but the value of "large" varies across systems.  The
 16.1582 -  default is an empirically derived value that works well in most
 16.1583 -  systems.
 16.1584 -*/
 16.1585 -
 16.1586 -#define M_MMAP_THRESHOLD      -3
 16.1587 -
 16.1588 -#ifndef DEFAULT_MMAP_THRESHOLD
 16.1589 -#define DEFAULT_MMAP_THRESHOLD (256 * 1024)
 16.1590 -#endif
 16.1591 -
 16.1592 -/*
 16.1593 -  M_MMAP_MAX is the maximum number of requests to simultaneously
 16.1594 -  service using mmap. This parameter exists because
 16.1595 -. Some systems have a limited number of internal tables for
 16.1596 -  use by mmap, and using more than a few of them may degrade
 16.1597 -  performance.
 16.1598 -
 16.1599 -  The default is set to a value that serves only as a safeguard.
 16.1600 -  Setting to 0 disables use of mmap for servicing large requests.  If
 16.1601 -  HAVE_MMAP is not set, the default value is 0, and attempts to set it
 16.1602 -  to non-zero values in mallopt will fail.
 16.1603 -*/
 16.1604 -
 16.1605 -#define M_MMAP_MAX             -4
 16.1606 -
 16.1607 -#ifndef DEFAULT_MMAP_MAX
 16.1608 -#if HAVE_MMAP
 16.1609 -#define DEFAULT_MMAP_MAX       (65536)
 16.1610 -#else
 16.1611 -#define DEFAULT_MMAP_MAX       (0)
 16.1612 -#endif
 16.1613 -#endif
 16.1614 -
 16.1615 -#ifdef __cplusplus
 16.1616 -};  /* end of extern "C" */
 16.1617 -#endif
 16.1618 -
 16.1619 -
 16.1620 -/* RN XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
 16.1621 -#endif 
 16.1622 -
 16.1623 -/* 
 16.1624 -  ========================================================================
 16.1625 -  To make a fully customizable malloc.h header file, cut everything
 16.1626 -  above this line, put into file malloc.h, edit to suit, and #include it 
 16.1627 -  on the next line, as well as in programs that use this malloc.
 16.1628 -  ========================================================================
 16.1629 -*/
 16.1630 -
 16.1631 -/* #include "malloc.h" */
 16.1632 -
 16.1633 -/* --------------------- public wrappers ---------------------- */
 16.1634 -
 16.1635 -#ifdef USE_PUBLIC_MALLOC_WRAPPERS
 16.1636 -
 16.1637 -/* Declare all routines as internal */
 16.1638 -#if __STD_C
 16.1639 -static Void_t*  mALLOc(size_t);
 16.1640 -static void     fREe(Void_t*);
 16.1641 -static Void_t*  rEALLOc(Void_t*, size_t);
 16.1642 -static Void_t*  mEMALIGn(size_t, size_t);
 16.1643 -static Void_t*  vALLOc(size_t);
 16.1644 -static Void_t*  pVALLOc(size_t);
 16.1645 -static Void_t*  cALLOc(size_t, size_t);
 16.1646 -static Void_t** iCALLOc(size_t, size_t, Void_t**);
 16.1647 -static Void_t** iCOMALLOc(size_t, size_t*, Void_t**);
 16.1648 -static void     cFREe(Void_t*);
 16.1649 -static int      mTRIm(size_t);
 16.1650 -static size_t   mUSABLe(Void_t*);
 16.1651 -static void     mSTATs();
 16.1652 -static int      mALLOPt(int, int);
 16.1653 -static struct mallinfo mALLINFo(void);
 16.1654 -#else
 16.1655 -static Void_t*  mALLOc();
 16.1656 -static void     fREe();
 16.1657 -static Void_t*  rEALLOc();
 16.1658 -static Void_t*  mEMALIGn();
 16.1659 -static Void_t*  vALLOc();
 16.1660 -static Void_t*  pVALLOc();
 16.1661 -static Void_t*  cALLOc();
 16.1662 -static Void_t** iCALLOc();
 16.1663 -static Void_t** iCOMALLOc();
 16.1664 -static void     cFREe();
 16.1665 -static int      mTRIm();
 16.1666 -static size_t   mUSABLe();
 16.1667 -static void     mSTATs();
 16.1668 -static int      mALLOPt();
 16.1669 -static struct mallinfo mALLINFo();
 16.1670 -#endif
 16.1671 -
 16.1672 -/*
 16.1673 -  MALLOC_PREACTION and MALLOC_POSTACTION should be
 16.1674 -  defined to return 0 on success, and nonzero on failure.
 16.1675 -  The return value of MALLOC_POSTACTION is currently ignored
 16.1676 -  in wrapper functions since there is no reasonable default
 16.1677 -  action to take on failure.
 16.1678 -*/
 16.1679 -
 16.1680 -
 16.1681 -#ifdef USE_MALLOC_LOCK
 16.1682 -
 16.1683 -#ifdef WIN32
 16.1684 -
 16.1685 -static int mALLOC_MUTEx;
 16.1686 -#define MALLOC_PREACTION   slwait(&mALLOC_MUTEx)
 16.1687 -#define MALLOC_POSTACTION  slrelease(&mALLOC_MUTEx)
 16.1688 -
 16.1689 -#else
 16.1690 -
 16.1691 -#include <pthread.h>
 16.1692 -
 16.1693 -static pthread_mutex_t mALLOC_MUTEx = PTHREAD_MUTEX_INITIALIZER;
 16.1694 -
 16.1695 -#define MALLOC_PREACTION   pthread_mutex_lock(&mALLOC_MUTEx)
 16.1696 -#define MALLOC_POSTACTION  pthread_mutex_unlock(&mALLOC_MUTEx)
 16.1697 -
 16.1698 -#endif /* USE_MALLOC_LOCK */
 16.1699 -
 16.1700 -#else
 16.1701 -
 16.1702 -/* Substitute anything you like for these */
 16.1703 -
 16.1704 -#define MALLOC_PREACTION   (0)
 16.1705 -#define MALLOC_POSTACTION  (0)
 16.1706 -
 16.1707 -#endif
 16.1708 -
 16.1709 -Void_t* public_mALLOc(size_t bytes) {
 16.1710 -  Void_t* m;
 16.1711 -  if (MALLOC_PREACTION != 0) {
 16.1712 -    return 0;
 16.1713 -  }
 16.1714 -  m = mALLOc(bytes);
 16.1715 -  if (MALLOC_POSTACTION != 0) {
 16.1716 -  }
 16.1717 -  return m;
 16.1718 -}
 16.1719 -
 16.1720 -void public_fREe(Void_t* m) {
 16.1721 -  if (MALLOC_PREACTION != 0) {
 16.1722 -    return;
 16.1723 -  }
 16.1724 -  fREe(m);
 16.1725 -  if (MALLOC_POSTACTION != 0) {
 16.1726 -  }
 16.1727 -}
 16.1728 -
 16.1729 -Void_t* public_rEALLOc(Void_t* m, size_t bytes) {
 16.1730 -  if (MALLOC_PREACTION != 0) {
 16.1731 -    return 0;
 16.1732 -  }
 16.1733 -  m = rEALLOc(m, bytes);
 16.1734 -  if (MALLOC_POSTACTION != 0) {
 16.1735 -  }
 16.1736 -  return m;
 16.1737 -}
 16.1738 -
 16.1739 -Void_t* public_mEMALIGn(size_t alignment, size_t bytes) {
 16.1740 -  Void_t* m;
 16.1741 -  if (MALLOC_PREACTION != 0) {
 16.1742 -    return 0;
 16.1743 -  }
 16.1744 -  m = mEMALIGn(alignment, bytes);
 16.1745 -  if (MALLOC_POSTACTION != 0) {
 16.1746 -  }
 16.1747 -  return m;
 16.1748 -}
 16.1749 -
 16.1750 -Void_t* public_vALLOc(size_t bytes) {
 16.1751 -  Void_t* m;
 16.1752 -  if (MALLOC_PREACTION != 0) {
 16.1753 -    return 0;
 16.1754 -  }
 16.1755 -  m = vALLOc(bytes);
 16.1756 -  if (MALLOC_POSTACTION != 0) {
 16.1757 -  }
 16.1758 -  return m;
 16.1759 -}
 16.1760 -
 16.1761 -Void_t* public_pVALLOc(size_t bytes) {
 16.1762 -  Void_t* m;
 16.1763 -  if (MALLOC_PREACTION != 0) {
 16.1764 -    return 0;
 16.1765 -  }
 16.1766 -  m = pVALLOc(bytes);
 16.1767 -  if (MALLOC_POSTACTION != 0) {
 16.1768 -  }
 16.1769 -  return m;
 16.1770 -}
 16.1771 -
 16.1772 -Void_t* public_cALLOc(size_t n, size_t elem_size) {
 16.1773 -  Void_t* m;
 16.1774 -  if (MALLOC_PREACTION != 0) {
 16.1775 -    return 0;
 16.1776 -  }
 16.1777 -  m = cALLOc(n, elem_size);
 16.1778 -  if (MALLOC_POSTACTION != 0) {
 16.1779 -  }
 16.1780 -  return m;
 16.1781 -}
 16.1782 -
 16.1783 -
 16.1784 -Void_t** public_iCALLOc(size_t n, size_t elem_size, Void_t** chunks) {
 16.1785 -  Void_t** m;
 16.1786 -  if (MALLOC_PREACTION != 0) {
 16.1787 -    return 0;
 16.1788 -  }
 16.1789 -  m = iCALLOc(n, elem_size, chunks);
 16.1790 -  if (MALLOC_POSTACTION != 0) {
 16.1791 -  }
 16.1792 -  return m;
 16.1793 -}
 16.1794 -
 16.1795 -Void_t** public_iCOMALLOc(size_t n, size_t sizes[], Void_t** chunks) {
 16.1796 -  Void_t** m;
 16.1797 -  if (MALLOC_PREACTION != 0) {
 16.1798 -    return 0;
 16.1799 -  }
 16.1800 -  m = iCOMALLOc(n, sizes, chunks);
 16.1801 -  if (MALLOC_POSTACTION != 0) {
 16.1802 -  }
 16.1803 -  return m;
 16.1804 -}
 16.1805 -
 16.1806 -void public_cFREe(Void_t* m) {
 16.1807 -  if (MALLOC_PREACTION != 0) {
 16.1808 -    return;
 16.1809 -  }
 16.1810 -  cFREe(m);
 16.1811 -  if (MALLOC_POSTACTION != 0) {
 16.1812 -  }
 16.1813 -}
 16.1814 -
 16.1815 -int public_mTRIm(size_t s) {
 16.1816 -  int result;
 16.1817 -  if (MALLOC_PREACTION != 0) {
 16.1818 -    return 0;
 16.1819 -  }
 16.1820 -  result = mTRIm(s);
 16.1821 -  if (MALLOC_POSTACTION != 0) {
 16.1822 -  }
 16.1823 -  return result;
 16.1824 -}
 16.1825 -
 16.1826 -size_t public_mUSABLe(Void_t* m) {
 16.1827 -  size_t result;
 16.1828 -  if (MALLOC_PREACTION != 0) {
 16.1829 -    return 0;
 16.1830 -  }
 16.1831 -  result = mUSABLe(m);
 16.1832 -  if (MALLOC_POSTACTION != 0) {
 16.1833 -  }
 16.1834 -  return result;
 16.1835 -}
 16.1836 -
 16.1837 -void public_mSTATs() {
 16.1838 -  if (MALLOC_PREACTION != 0) {
 16.1839 -    return;
 16.1840 -  }
 16.1841 -  mSTATs();
 16.1842 -  if (MALLOC_POSTACTION != 0) {
 16.1843 -  }
 16.1844 -}
 16.1845 -
 16.1846 -struct mallinfo public_mALLINFo() {
 16.1847 -  struct mallinfo m;
 16.1848 -  if (MALLOC_PREACTION != 0) {
 16.1849 -    struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 16.1850 -    return nm;
 16.1851 -  }
 16.1852 -  m = mALLINFo();
 16.1853 -  if (MALLOC_POSTACTION != 0) {
 16.1854 -  }
 16.1855 -  return m;
 16.1856 -}
 16.1857 -
 16.1858 -int public_mALLOPt(int p, int v) {
 16.1859 -  int result;
 16.1860 -  if (MALLOC_PREACTION != 0) {
 16.1861 -    return 0;
 16.1862 -  }
 16.1863 -  result = mALLOPt(p, v);
 16.1864 -  if (MALLOC_POSTACTION != 0) {
 16.1865 -  }
 16.1866 -  return result;
 16.1867 -}
 16.1868 -
 16.1869 -#endif
 16.1870 -
 16.1871 -
 16.1872 -
 16.1873 -/* ------------- Optional versions of memcopy ---------------- */
 16.1874 -
 16.1875 -
 16.1876 -#if USE_MEMCPY
 16.1877 -
 16.1878 -/* 
 16.1879 -  Note: memcpy is ONLY invoked with non-overlapping regions,
 16.1880 -  so the (usually slower) memmove is not needed.
 16.1881 -*/
 16.1882 -
 16.1883 -#define MALLOC_COPY(dest, src, nbytes)  memcpy(dest, src, nbytes)
 16.1884 -#define MALLOC_ZERO(dest, nbytes)       memset(dest, 0,   nbytes)
 16.1885 -
 16.1886 -#else /* !USE_MEMCPY */
 16.1887 -
 16.1888 -/* Use Duff's device for good zeroing/copying performance. */
 16.1889 -
 16.1890 -#define MALLOC_ZERO(charp, nbytes)                                            \
 16.1891 -do {                                                                          \
 16.1892 -  INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp);                           \
 16.1893 -  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
 16.1894 -  long mcn;                                                                   \
 16.1895 -  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
 16.1896 -  switch (mctmp) {                                                            \
 16.1897 -    case 0: for(;;) { *mzp++ = 0;                                             \
 16.1898 -    case 7:           *mzp++ = 0;                                             \
 16.1899 -    case 6:           *mzp++ = 0;                                             \
 16.1900 -    case 5:           *mzp++ = 0;                                             \
 16.1901 -    case 4:           *mzp++ = 0;                                             \
 16.1902 -    case 3:           *mzp++ = 0;                                             \
 16.1903 -    case 2:           *mzp++ = 0;                                             \
 16.1904 -    case 1:           *mzp++ = 0; if(mcn <= 0) break; mcn--; }                \
 16.1905 -  }                                                                           \
 16.1906 -} while(0)
 16.1907 -
 16.1908 -#define MALLOC_COPY(dest,src,nbytes)                                          \
 16.1909 -do {                                                                          \
 16.1910 -  INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src;                            \
 16.1911 -  INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest;                           \
 16.1912 -  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
 16.1913 -  long mcn;                                                                   \
 16.1914 -  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
 16.1915 -  switch (mctmp) {                                                            \
 16.1916 -    case 0: for(;;) { *mcdst++ = *mcsrc++;                                    \
 16.1917 -    case 7:           *mcdst++ = *mcsrc++;                                    \
 16.1918 -    case 6:           *mcdst++ = *mcsrc++;                                    \
 16.1919 -    case 5:           *mcdst++ = *mcsrc++;                                    \
 16.1920 -    case 4:           *mcdst++ = *mcsrc++;                                    \
 16.1921 -    case 3:           *mcdst++ = *mcsrc++;                                    \
 16.1922 -    case 2:           *mcdst++ = *mcsrc++;                                    \
 16.1923 -    case 1:           *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; }       \
 16.1924 -  }                                                                           \
 16.1925 -} while(0)
 16.1926 -
 16.1927 -#endif
 16.1928 -
 16.1929 -/* ------------------ MMAP support ------------------  */
 16.1930 -
 16.1931 -
 16.1932 -#if HAVE_MMAP
 16.1933 -
 16.1934 -#ifndef LACKS_FCNTL_H
 16.1935 -#include <fcntl.h>
 16.1936 -#endif
 16.1937 -
 16.1938 -#ifndef LACKS_SYS_MMAN_H
 16.1939 -#include <sys/mman.h>
 16.1940 -#endif
 16.1941 -
 16.1942 -#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
 16.1943 -#define MAP_ANONYMOUS MAP_ANON
 16.1944 -#endif
 16.1945 -
 16.1946 -/* 
 16.1947 -   Nearly all versions of mmap support MAP_ANONYMOUS, 
 16.1948 -   so the following is unlikely to be needed, but is
 16.1949 -   supplied just in case.
 16.1950 -*/
 16.1951 -
 16.1952 -#ifndef MAP_ANONYMOUS
 16.1953 -
 16.1954 -static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
 16.1955 -
 16.1956 -#define MMAP(addr, size, prot, flags) ((dev_zero_fd < 0) ? \
 16.1957 - (dev_zero_fd = open("/dev/zero", O_RDWR), \
 16.1958 -  mmap((addr), (size), (prot), (flags), dev_zero_fd, 0)) : \
 16.1959 -   mmap((addr), (size), (prot), (flags), dev_zero_fd, 0))
 16.1960 -
 16.1961 -#else
 16.1962 -
 16.1963 -#define MMAP(addr, size, prot, flags) \
 16.1964 - (mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS, -1, 0))
 16.1965 -
 16.1966 -#endif
 16.1967 -
 16.1968 -
 16.1969 -#endif /* HAVE_MMAP */
 16.1970 -
 16.1971 -
 16.1972 -/*
 16.1973 -  -----------------------  Chunk representations -----------------------
 16.1974 -*/
 16.1975 -
 16.1976 -
 16.1977 -/*
 16.1978 -  This struct declaration is misleading (but accurate and necessary).
 16.1979 -  It declares a "view" into memory allowing access to necessary
 16.1980 -  fields at known offsets from a given base. See explanation below.
 16.1981 -*/
 16.1982 -
 16.1983 -struct malloc_chunk {
 16.1984 -
 16.1985 -  INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */
 16.1986 -  INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */
 16.1987 -
 16.1988 -  struct malloc_chunk* fd;         /* double links -- used only if free. */
 16.1989 -  struct malloc_chunk* bk;
 16.1990 -};
 16.1991 -
 16.1992 -
 16.1993 -typedef struct malloc_chunk* mchunkptr;
 16.1994 -
 16.1995 -/*
 16.1996 -   malloc_chunk details:
 16.1997 -
 16.1998 -    (The following includes lightly edited explanations by Colin Plumb.)
 16.1999 -
 16.2000 -    Chunks of memory are maintained using a `boundary tag' method as
 16.2001 -    described in e.g., Knuth or Standish.  (See the paper by Paul
 16.2002 -    Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
 16.2003 -    survey of such techniques.)  Sizes of free chunks are stored both
 16.2004 -    in the front of each chunk and at the end.  This makes
 16.2005 -    consolidating fragmented chunks into bigger chunks very fast.  The
 16.2006 -    size fields also hold bits representing whether chunks are free or
 16.2007 -    in use.
 16.2008 -
 16.2009 -    An allocated chunk looks like this:
 16.2010 -
 16.2011 -
 16.2012 -    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2013 -            |             Size of previous chunk, if allocated            | |
 16.2014 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2015 -            |             Size of chunk, in bytes                         |P|
 16.2016 -      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2017 -            |             User data starts here...                          .
 16.2018 -            .                                                               .
 16.2019 -            .             (malloc_usable_space() bytes)                     .
 16.2020 -            .                                                               |
 16.2021 -nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2022 -            |             Size of chunk                                     |
 16.2023 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2024 -
 16.2025 -
 16.2026 -    Where "chunk" is the front of the chunk for the purpose of most of
 16.2027 -    the malloc code, but "mem" is the pointer that is returned to the
 16.2028 -    user.  "Nextchunk" is the beginning of the next contiguous chunk.
 16.2029 -
 16.2030 -    Chunks always begin on even word boundries, so the mem portion
 16.2031 -    (which is returned to the user) is also on an even word boundary, and
 16.2032 -    thus at least double-word aligned.
 16.2033 -
 16.2034 -    Free chunks are stored in circular doubly-linked lists, and look like this:
 16.2035 -
 16.2036 -    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2037 -            |             Size of previous chunk                            |
 16.2038 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2039 -    `head:' |             Size of chunk, in bytes                         |P|
 16.2040 -      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2041 -            |             Forward pointer to next chunk in list             |
 16.2042 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2043 -            |             Back pointer to previous chunk in list            |
 16.2044 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2045 -            |             Unused space (may be 0 bytes long)                .
 16.2046 -            .                                                               .
 16.2047 -            .                                                               |
 16.2048 -nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2049 -    `foot:' |             Size of chunk, in bytes                           |
 16.2050 -            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 16.2051 -
 16.2052 -    The P (PREV_INUSE) bit, stored in the unused low-order bit of the
 16.2053 -    chunk size (which is always a multiple of two words), is an in-use
 16.2054 -    bit for the *previous* chunk.  If that bit is *clear*, then the
 16.2055 -    word before the current chunk size contains the previous chunk
 16.2056 -    size, and can be used to find the front of the previous chunk.
 16.2057 -    The very first chunk allocated always has this bit set,
 16.2058 -    preventing access to non-existent (or non-owned) memory. If
 16.2059 -    prev_inuse is set for any given chunk, then you CANNOT determine
 16.2060 -    the size of the previous chunk, and might even get a memory
 16.2061 -    addressing fault when trying to do so.
 16.2062 -
 16.2063 -    Note that the `foot' of the current chunk is actually represented
 16.2064 -    as the prev_size of the NEXT chunk. This makes it easier to
 16.2065 -    deal with alignments etc but can be very confusing when trying
 16.2066 -    to extend or adapt this code.
 16.2067 -
 16.2068 -    The two exceptions to all this are
 16.2069 -
 16.2070 -     1. The special chunk `top' doesn't bother using the
 16.2071 -        trailing size field since there is no next contiguous chunk
 16.2072 -        that would have to index off it. After initialization, `top'
 16.2073 -        is forced to always exist.  If it would become less than
 16.2074 -        MINSIZE bytes long, it is replenished.
 16.2075 -
 16.2076 -     2. Chunks allocated via mmap, which have the second-lowest-order
 16.2077 -        bit (IS_MMAPPED) set in their size fields.  Because they are
 16.2078 -        allocated one-by-one, each must contain its own trailing size field.
 16.2079 -
 16.2080 -*/
 16.2081 -
 16.2082 -/*
 16.2083 -  ---------- Size and alignment checks and conversions ----------
 16.2084 -*/
 16.2085 -
 16.2086 -/* conversion from malloc headers to user pointers, and back */
 16.2087 -
 16.2088 -#define chunk2mem(p)   ((Void_t*)((char*)(p) + 2*SIZE_SZ))
 16.2089 -#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
 16.2090 -
 16.2091 -/* The smallest possible chunk */
 16.2092 -#define MIN_CHUNK_SIZE        (sizeof(struct malloc_chunk))
 16.2093 -
 16.2094 -/* The smallest size we can malloc is an aligned minimal chunk */
 16.2095 -
 16.2096 -#define MINSIZE  \
 16.2097 -  (CHUNK_SIZE_T)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
 16.2098 -
 16.2099 -/* Check if m has acceptable alignment */
 16.2100 -
 16.2101 -#define aligned_OK(m)  (((PTR_UINT)((m)) & (MALLOC_ALIGN_MASK)) == 0)
 16.2102 -
 16.2103 -
 16.2104 -/* 
 16.2105 -   Check if a request is so large that it would wrap around zero when
 16.2106 -   padded and aligned. To simplify some other code, the bound is made
 16.2107 -   low enough so that adding MINSIZE will also not wrap around sero.
 16.2108 -*/
 16.2109 -
 16.2110 -#define REQUEST_OUT_OF_RANGE(req)                                 \
 16.2111 -  ((CHUNK_SIZE_T)(req) >=                                        \
 16.2112 -   (CHUNK_SIZE_T)(INTERNAL_SIZE_T)(-2 * MINSIZE))    
 16.2113 -
 16.2114 -/* pad request bytes into a usable size -- internal version */
 16.2115 -
 16.2116 -#define request2size(req)                                         \
 16.2117 -  (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
 16.2118 -   MINSIZE :                                                      \
 16.2119 -   ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
 16.2120 -
 16.2121 -/*  Same, except also perform argument check */
 16.2122 -
 16.2123 -#define checked_request2size(req, sz)                             \
 16.2124 -  if (REQUEST_OUT_OF_RANGE(req)) {                                \
 16.2125 -    MALLOC_FAILURE_ACTION;                                        \
 16.2126 -    return 0;                                                     \
 16.2127 -  }                                                               \
 16.2128 -  (sz) = request2size(req);                                              
 16.2129 -
 16.2130 -/*
 16.2131 -  --------------- Physical chunk operations ---------------
 16.2132 -*/
 16.2133 -
 16.2134 -
 16.2135 -/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
 16.2136 -#define PREV_INUSE 0x1
 16.2137 -
 16.2138 -/* extract inuse bit of previous chunk */
 16.2139 -#define prev_inuse(p)       ((p)->size & PREV_INUSE)
 16.2140 -
 16.2141 -
 16.2142 -/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
 16.2143 -#define IS_MMAPPED 0x2
 16.2144 -
 16.2145 -/* check for mmap()'ed chunk */
 16.2146 -#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
 16.2147 -
 16.2148 -/* 
 16.2149 -  Bits to mask off when extracting size 
 16.2150 -
 16.2151 -  Note: IS_MMAPPED is intentionally not masked off from size field in
 16.2152 -  macros for which mmapped chunks should never be seen. This should
 16.2153 -  cause helpful core dumps to occur if it is tried by accident by
 16.2154 -  people extending or adapting this malloc.
 16.2155 -*/
 16.2156 -#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
 16.2157 -
 16.2158 -/* Get size, ignoring use bits */
 16.2159 -#define chunksize(p)         ((p)->size & ~(SIZE_BITS))
 16.2160 -
 16.2161 -
 16.2162 -/* Ptr to next physical malloc_chunk. */
 16.2163 -#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
 16.2164 -
 16.2165 -/* Ptr to previous physical malloc_chunk */
 16.2166 -#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
 16.2167 -
 16.2168 -/* Treat space at ptr + offset as a chunk */
 16.2169 -#define chunk_at_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))
 16.2170 -
 16.2171 -/* extract p's inuse bit */
 16.2172 -#define inuse(p)\
 16.2173 -((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
 16.2174 -
 16.2175 -/* set/clear chunk as being inuse without otherwise disturbing */
 16.2176 -#define set_inuse(p)\
 16.2177 -((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
 16.2178 -
 16.2179 -#define clear_inuse(p)\
 16.2180 -((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
 16.2181 -
 16.2182 -
 16.2183 -/* check/set/clear inuse bits in known places */
 16.2184 -#define inuse_bit_at_offset(p, s)\
 16.2185 - (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
 16.2186 -
 16.2187 -#define set_inuse_bit_at_offset(p, s)\
 16.2188 - (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
 16.2189 -
 16.2190 -#define clear_inuse_bit_at_offset(p, s)\
 16.2191 - (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
 16.2192 -
 16.2193 -
 16.2194 -/* Set size at head, without disturbing its use bit */
 16.2195 -#define set_head_size(p, s)  ((p)->size = (((p)->size & PREV_INUSE) | (s)))
 16.2196 -
 16.2197 -/* Set size/use field */
 16.2198 -#define set_head(p, s)       ((p)->size = (s))
 16.2199 -
 16.2200 -/* Set size at footer (only when chunk is not in use) */
 16.2201 -#define set_foot(p, s)       (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
 16.2202 -
 16.2203 -
 16.2204 -/*
 16.2205 -  -------------------- Internal data structures --------------------
 16.2206 -
 16.2207 -   All internal state is held in an instance of malloc_state defined
 16.2208 -   below. There are no other static variables, except in two optional
 16.2209 -   cases: 
 16.2210 -   * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above. 
 16.2211 -   * If HAVE_MMAP is true, but mmap doesn't support
 16.2212 -     MAP_ANONYMOUS, a dummy file descriptor for mmap.
 16.2213 -
 16.2214 -   Beware of lots of tricks that minimize the total bookkeeping space
 16.2215 -   requirements. The result is a little over 1K bytes (for 4byte
 16.2216 -   pointers and size_t.)
 16.2217 -*/
 16.2218 -
 16.2219 -/*
 16.2220 -  Bins
 16.2221 -
 16.2222 -    An array of bin headers for free chunks. Each bin is doubly
 16.2223 -    linked.  The bins are approximately proportionally (log) spaced.
 16.2224 -    There are a lot of these bins (128). This may look excessive, but
 16.2225 -    works very well in practice.  Most bins hold sizes that are
 16.2226 -    unusual as malloc request sizes, but are more usual for fragments
 16.2227 -    and consolidated sets of chunks, which is what these bins hold, so
 16.2228 -    they can be found quickly.  All procedures maintain the invariant
 16.2229 -    that no consolidated chunk physically borders another one, so each
 16.2230 -    chunk in a list is known to be preceeded and followed by either
 16.2231 -    inuse chunks or the ends of memory.
 16.2232 -
 16.2233 -    Chunks in bins are kept in size order, with ties going to the
 16.2234 -    approximately least recently used chunk. Ordering isn't needed
 16.2235 -    for the small bins, which all contain the same-sized chunks, but
 16.2236 -    facilitates best-fit allocation for larger chunks. These lists
 16.2237 -    are just sequential. Keeping them in order almost never requires
 16.2238 -    enough traversal to warrant using fancier ordered data
 16.2239 -    structures.  
 16.2240 -
 16.2241 -    Chunks of the same size are linked with the most
 16.2242 -    recently freed at the front, and allocations are taken from the
 16.2243 -    back.  This results in LRU (FIFO) allocation order, which tends
 16.2244 -    to give each chunk an equal opportunity to be consolidated with
 16.2245 -    adjacent freed chunks, resulting in larger free chunks and less
 16.2246 -    fragmentation.
 16.2247 -
 16.2248 -    To simplify use in double-linked lists, each bin header acts
 16.2249 -    as a malloc_chunk. This avoids special-casing for headers.
 16.2250 -    But to conserve space and improve locality, we allocate
 16.2251 -    only the fd/bk pointers of bins, and then use repositioning tricks
 16.2252 -    to treat these as the fields of a malloc_chunk*.  
 16.2253 -*/
 16.2254 -
 16.2255 -typedef struct malloc_chunk* mbinptr;
 16.2256 -
 16.2257 -/* addressing -- note that bin_at(0) does not exist */
 16.2258 -#define bin_at(m, i) ((mbinptr)((char*)&((m)->bins[(i)<<1]) - (SIZE_SZ<<1)))
 16.2259 -
 16.2260 -/* analog of ++bin */
 16.2261 -#define next_bin(b)  ((mbinptr)((char*)(b) + (sizeof(mchunkptr)<<1)))
 16.2262 -
 16.2263 -/* Reminders about list directionality within bins */
 16.2264 -#define first(b)     ((b)->fd)
 16.2265 -#define last(b)      ((b)->bk)
 16.2266 -
 16.2267 -/* Take a chunk off a bin list */
 16.2268 -#define unlink(P, BK, FD) {                                            \
 16.2269 -  FD = P->fd;                                                          \
 16.2270 -  BK = P->bk;                                                          \
 16.2271 -  FD->bk = BK;                                                         \
 16.2272 -  BK->fd = FD;                                                         \
 16.2273 -}
 16.2274 -
 16.2275 -/*
 16.2276 -  Indexing
 16.2277 -
 16.2278 -    Bins for sizes < 512 bytes contain chunks of all the same size, spaced
 16.2279 -    8 bytes apart. Larger bins are approximately logarithmically spaced:
 16.2280 -
 16.2281 -    64 bins of size       8
 16.2282 -    32 bins of size      64
 16.2283 -    16 bins of size     512
 16.2284 -     8 bins of size    4096
 16.2285 -     4 bins of size   32768
 16.2286 -     2 bins of size  262144
 16.2287 -     1 bin  of size what's left
 16.2288 -
 16.2289 -    The bins top out around 1MB because we expect to service large
 16.2290 -    requests via mmap.
 16.2291 -*/
 16.2292 -
 16.2293 -#define NBINS              96
 16.2294 -#define NSMALLBINS         32
 16.2295 -#define SMALLBIN_WIDTH      8
 16.2296 -#define MIN_LARGE_SIZE    256
 16.2297 -
 16.2298 -#define in_smallbin_range(sz)  \
 16.2299 -  ((CHUNK_SIZE_T)(sz) < (CHUNK_SIZE_T)MIN_LARGE_SIZE)
 16.2300 -
 16.2301 -#define smallbin_index(sz)     (((unsigned)(sz)) >> 3)
 16.2302 -
 16.2303 -/*
 16.2304 -  Compute index for size. We expect this to be inlined when
 16.2305 -  compiled with optimization, else not, which works out well.
 16.2306 -*/
 16.2307 -static int largebin_index(unsigned int sz) {
 16.2308 -  unsigned int  x = sz >> SMALLBIN_WIDTH; 
 16.2309 -  unsigned int m;            /* bit position of highest set bit of m */
 16.2310 -
 16.2311 -  if (x >= 0x10000) return NBINS-1;
 16.2312 -
 16.2313 -  /* On intel, use BSRL instruction to find highest bit */
 16.2314 -#if defined(__GNUC__) && defined(i386)
 16.2315 -
 16.2316 -  __asm__("bsrl %1,%0\n\t"
 16.2317 -          : "=r" (m) 
 16.2318 -          : "g"  (x));
 16.2319 -
 16.2320 -#else
 16.2321 -  {
 16.2322 -    /*
 16.2323 -      Based on branch-free nlz algorithm in chapter 5 of Henry
 16.2324 -      S. Warren Jr's book "Hacker's Delight".
 16.2325 -    */
 16.2326 -
 16.2327 -    unsigned int n = ((x - 0x100) >> 16) & 8;
 16.2328 -    x <<= n; 
 16.2329 -    m = ((x - 0x1000) >> 16) & 4;
 16.2330 -    n += m; 
 16.2331 -    x <<= m; 
 16.2332 -    m = ((x - 0x4000) >> 16) & 2;
 16.2333 -    n += m; 
 16.2334 -    x = (x << m) >> 14;
 16.2335 -    m = 13 - n + (x & ~(x>>1));
 16.2336 -  }
 16.2337 -#endif
 16.2338 -
 16.2339 -  /* Use next 2 bits to create finer-granularity bins */
 16.2340 -  return NSMALLBINS + (m << 2) + ((sz >> (m + 6)) & 3);
 16.2341 -}
 16.2342 -
 16.2343 -#define bin_index(sz) \
 16.2344 - ((in_smallbin_range(sz)) ? smallbin_index(sz) : largebin_index(sz))
 16.2345 -
 16.2346 -/*
 16.2347 -  FIRST_SORTED_BIN_SIZE is the chunk size corresponding to the
 16.2348 -  first bin that is maintained in sorted order. This must
 16.2349 -  be the smallest size corresponding to a given bin.
 16.2350 -
 16.2351 -  Normally, this should be MIN_LARGE_SIZE. But you can weaken
 16.2352 -  best fit guarantees to sometimes speed up malloc by increasing value.
 16.2353 -  Doing this means that malloc may choose a chunk that is 
 16.2354 -  non-best-fitting by up to the width of the bin.
 16.2355 -
 16.2356 -  Some useful cutoff values:
 16.2357 -      512 - all bins sorted
 16.2358 -     2560 - leaves bins <=     64 bytes wide unsorted  
 16.2359 -    12288 - leaves bins <=    512 bytes wide unsorted
 16.2360 -    65536 - leaves bins <=   4096 bytes wide unsorted
 16.2361 -   262144 - leaves bins <=  32768 bytes wide unsorted
 16.2362 -       -1 - no bins sorted (not recommended!)
 16.2363 -*/
 16.2364 -
 16.2365 -#define FIRST_SORTED_BIN_SIZE MIN_LARGE_SIZE 
 16.2366 -/* #define FIRST_SORTED_BIN_SIZE 65536 */
 16.2367 -
 16.2368 -/*
 16.2369 -  Unsorted chunks
 16.2370 -
 16.2371 -    All remainders from chunk splits, as well as all returned chunks,
 16.2372 -    are first placed in the "unsorted" bin. They are then placed
 16.2373 -    in regular bins after malloc gives them ONE chance to be used before
 16.2374 -    binning. So, basically, the unsorted_chunks list acts as a queue,
 16.2375 -    with chunks being placed on it in free (and malloc_consolidate),
 16.2376 -    and taken off (to be either used or placed in bins) in malloc.
 16.2377 -*/
 16.2378 -
 16.2379 -/* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
 16.2380 -#define unsorted_chunks(M)          (bin_at(M, 1))
 16.2381 -
 16.2382 -/*
 16.2383 -  Top
 16.2384 -
 16.2385 -    The top-most available chunk (i.e., the one bordering the end of
 16.2386 -    available memory) is treated specially. It is never included in
 16.2387 -    any bin, is used only if no other chunk is available, and is
 16.2388 -    released back to the system if it is very large (see
 16.2389 -    M_TRIM_THRESHOLD).  Because top initially
 16.2390 -    points to its own bin with initial zero size, thus forcing
 16.2391 -    extension on the first malloc request, we avoid having any special
 16.2392 -    code in malloc to check whether it even exists yet. But we still
 16.2393 -    need to do so when getting memory from system, so we make
 16.2394 -    initial_top treat the bin as a legal but unusable chunk during the
 16.2395 -    interval between initialization and the first call to
 16.2396 -    sYSMALLOc. (This is somewhat delicate, since it relies on
 16.2397 -    the 2 preceding words to be zero during this interval as well.)
 16.2398 -*/
 16.2399 -
 16.2400 -/* Conveniently, the unsorted bin can be used as dummy top on first call */
 16.2401 -#define initial_top(M)              (unsorted_chunks(M))
 16.2402 -
 16.2403 -/*
 16.2404 -  Binmap
 16.2405 -
 16.2406 -    To help compensate for the large number of bins, a one-level index
 16.2407 -    structure is used for bin-by-bin searching.  `binmap' is a
 16.2408 -    bitvector recording whether bins are definitely empty so they can
 16.2409 -    be skipped over during during traversals.  The bits are NOT always
 16.2410 -    cleared as soon as bins are empty, but instead only
 16.2411 -    when they are noticed to be empty during traversal in malloc.
 16.2412 -*/
 16.2413 -
 16.2414 -/* Conservatively use 32 bits per map word, even if on 64bit system */
 16.2415 -#define BINMAPSHIFT      5
 16.2416 -#define BITSPERMAP       (1U << BINMAPSHIFT)
 16.2417 -#define BINMAPSIZE       (NBINS / BITSPERMAP)
 16.2418 -
 16.2419 -#define idx2block(i)     ((i) >> BINMAPSHIFT)
 16.2420 -#define idx2bit(i)       ((1U << ((i) & ((1U << BINMAPSHIFT)-1))))
 16.2421 -
 16.2422 -#define mark_bin(m,i)    ((m)->binmap[idx2block(i)] |=  idx2bit(i))
 16.2423 -#define unmark_bin(m,i)  ((m)->binmap[idx2block(i)] &= ~(idx2bit(i)))
 16.2424 -#define get_binmap(m,i)  ((m)->binmap[idx2block(i)] &   idx2bit(i))
 16.2425 -
 16.2426 -/*
 16.2427 -  Fastbins
 16.2428 -
 16.2429 -    An array of lists holding recently freed small chunks.  Fastbins
 16.2430 -    are not doubly linked.  It is faster to single-link them, and
 16.2431 -    since chunks are never removed from the middles of these lists,
 16.2432 -    double linking is not necessary. Also, unlike regular bins, they
 16.2433 -    are not even processed in FIFO order (they use faster LIFO) since
 16.2434 -    ordering doesn't much matter in the transient contexts in which
 16.2435 -    fastbins are normally used.
 16.2436 -
 16.2437 -    Chunks in fastbins keep their inuse bit set, so they cannot
 16.2438 -    be consolidated with other free chunks. malloc_consolidate
 16.2439 -    releases all chunks in fastbins and consolidates them with
 16.2440 -    other free chunks. 
 16.2441 -*/
 16.2442 -
 16.2443 -typedef struct malloc_chunk* mfastbinptr;
 16.2444 -
 16.2445 -/* offset 2 to use otherwise unindexable first 2 bins */
 16.2446 -#define fastbin_index(sz)        ((((unsigned int)(sz)) >> 3) - 2)
 16.2447 -
 16.2448 -/* The maximum fastbin request size we support */
 16.2449 -#define MAX_FAST_SIZE     80
 16.2450 -
 16.2451 -#define NFASTBINS  (fastbin_index(request2size(MAX_FAST_SIZE))+1)
 16.2452 -
 16.2453 -/*
 16.2454 -  FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
 16.2455 -  that triggers automatic consolidation of possibly-surrounding
 16.2456 -  fastbin chunks. This is a heuristic, so the exact value should not
 16.2457 -  matter too much. It is defined at half the default trim threshold as a
 16.2458 -  compromise heuristic to only attempt consolidation if it is likely
 16.2459 -  to lead to trimming. However, it is not dynamically tunable, since
 16.2460 -  consolidation reduces fragmentation surrounding loarge chunks even 
 16.2461 -  if trimming is not used.
 16.2462 -*/
 16.2463 -
 16.2464 -#define FASTBIN_CONSOLIDATION_THRESHOLD  \
 16.2465 -  ((unsigned long)(DEFAULT_TRIM_THRESHOLD) >> 1)
 16.2466 -
 16.2467 -/*
 16.2468 -  Since the lowest 2 bits in max_fast don't matter in size comparisons, 
 16.2469 -  they are used as flags.
 16.2470 -*/
 16.2471 -
 16.2472 -/*
 16.2473 -  ANYCHUNKS_BIT held in max_fast indicates that there may be any
 16.2474 -  freed chunks at all. It is set true when entering a chunk into any
 16.2475 -  bin.
 16.2476 -*/
 16.2477 -
 16.2478 -#define ANYCHUNKS_BIT        (1U)
 16.2479 -
 16.2480 -#define have_anychunks(M)     (((M)->max_fast &  ANYCHUNKS_BIT))
 16.2481 -#define set_anychunks(M)      ((M)->max_fast |=  ANYCHUNKS_BIT)
 16.2482 -#define clear_anychunks(M)    ((M)->max_fast &= ~ANYCHUNKS_BIT)
 16.2483 -
 16.2484 -/*
 16.2485 -  FASTCHUNKS_BIT held in max_fast indicates that there are probably
 16.2486 -  some fastbin chunks. It is set true on entering a chunk into any
 16.2487 -  fastbin, and cleared only in malloc_consolidate.
 16.2488 -*/
 16.2489 -
 16.2490 -#define FASTCHUNKS_BIT        (2U)
 16.2491 -
 16.2492 -#define have_fastchunks(M)   (((M)->max_fast &  FASTCHUNKS_BIT))
 16.2493 -#define set_fastchunks(M)    ((M)->max_fast |=  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
 16.2494 -#define clear_fastchunks(M)  ((M)->max_fast &= ~(FASTCHUNKS_BIT))
 16.2495 -
 16.2496 -/* 
 16.2497 -   Set value of max_fast. 
 16.2498 -   Use impossibly small value if 0.
 16.2499 -*/
 16.2500 -
 16.2501 -#define set_max_fast(M, s) \
 16.2502 -  (M)->max_fast = (((s) == 0)? SMALLBIN_WIDTH: request2size(s)) | \
 16.2503 -  ((M)->max_fast &  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
 16.2504 -
 16.2505 -#define get_max_fast(M) \
 16.2506 -  ((M)->max_fast & ~(FASTCHUNKS_BIT | ANYCHUNKS_BIT))
 16.2507 -
 16.2508 -
 16.2509 -/*
 16.2510 -  morecore_properties is a status word holding dynamically discovered
 16.2511 -  or controlled properties of the morecore function
 16.2512 -*/
 16.2513 -
 16.2514 -#define MORECORE_CONTIGUOUS_BIT  (1U)
 16.2515 -
 16.2516 -#define contiguous(M) \
 16.2517 -        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT))
 16.2518 -#define noncontiguous(M) \
 16.2519 -        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT) == 0)
 16.2520 -#define set_contiguous(M) \
 16.2521 -        ((M)->morecore_properties |=  MORECORE_CONTIGUOUS_BIT)
 16.2522 -#define set_noncontiguous(M) \
 16.2523 -        ((M)->morecore_properties &= ~MORECORE_CONTIGUOUS_BIT)
 16.2524 -
 16.2525 -
 16.2526 -/*
 16.2527 -   ----------- Internal state representation and initialization -----------
 16.2528 -*/
 16.2529 -
 16.2530 -struct malloc_state {
 16.2531 -
 16.2532 -  /* The maximum chunk size to be eligible for fastbin */
 16.2533 -  INTERNAL_SIZE_T  max_fast;   /* low 2 bits used as flags */
 16.2534 -
 16.2535 -  /* Fastbins */
 16.2536 -  mfastbinptr      fastbins[NFASTBINS];
 16.2537 -
 16.2538 -  /* Base of the topmost chunk -- not otherwise kept in a bin */
 16.2539 -  mchunkptr        top;
 16.2540 -
 16.2541 -  /* The remainder from the most recent split of a small request */
 16.2542 -  mchunkptr        last_remainder;
 16.2543 -
 16.2544 -  /* Normal bins packed as described above */
 16.2545 -  mchunkptr        bins[NBINS * 2];
 16.2546 -
 16.2547 -  /* Bitmap of bins. Trailing zero map handles cases of largest binned size */
 16.2548 -  unsigned int     binmap[BINMAPSIZE+1];
 16.2549 -
 16.2550 -  /* Tunable parameters */
 16.2551 -  CHUNK_SIZE_T     trim_threshold;
 16.2552 -  INTERNAL_SIZE_T  top_pad;
 16.2553 -  INTERNAL_SIZE_T  mmap_threshold;
 16.2554 -
 16.2555 -  /* Memory map support */
 16.2556 -  int              n_mmaps;
 16.2557 -  int              n_mmaps_max;
 16.2558 -  int              max_n_mmaps;
 16.2559 -
 16.2560 -  /* Cache malloc_getpagesize */
 16.2561 -  unsigned int     pagesize;    
 16.2562 -
 16.2563 -  /* Track properties of MORECORE */
 16.2564 -  unsigned int     morecore_properties;
 16.2565 -
 16.2566 -  /* Statistics */
 16.2567 -  INTERNAL_SIZE_T  mmapped_mem;
 16.2568 -  INTERNAL_SIZE_T  sbrked_mem;
 16.2569 -  INTERNAL_SIZE_T  max_sbrked_mem;
 16.2570 -  INTERNAL_SIZE_T  max_mmapped_mem;
 16.2571 -  INTERNAL_SIZE_T  max_total_mem;
 16.2572 -};
 16.2573 -
 16.2574 -typedef struct malloc_state *mstate;
 16.2575 -
 16.2576 -/* 
 16.2577 -   There is exactly one instance of this struct in this malloc.
 16.2578 -   If you are adapting this malloc in a way that does NOT use a static
 16.2579 -   malloc_state, you MUST explicitly zero-fill it before using. This
 16.2580 -   malloc relies on the property that malloc_state is initialized to
 16.2581 -   all zeroes (as is true of C statics).
 16.2582 -*/
 16.2583 -
 16.2584 -static struct malloc_state av_;  /* never directly referenced */
 16.2585 -
 16.2586 -/*
 16.2587 -   All uses of av_ are via get_malloc_state().
 16.2588 -   At most one "call" to get_malloc_state is made per invocation of
 16.2589 -   the public versions of malloc and free, but other routines
 16.2590 -   that in turn invoke malloc and/or free may call more then once. 
 16.2591 -   Also, it is called in check* routines if DEBUG is set.
 16.2592 -*/
 16.2593 -
 16.2594 -#define get_malloc_state() (&(av_))
 16.2595 -
 16.2596 -/*
 16.2597 -  Initialize a malloc_state struct.
 16.2598 -
 16.2599 -  This is called only from within malloc_consolidate, which needs
 16.2600 -  be called in the same contexts anyway.  It is never called directly
 16.2601 -  outside of malloc_consolidate because some optimizing compilers try
 16.2602 -  to inline it at all call points, which turns out not to be an
 16.2603 -  optimization at all. (Inlining it in malloc_consolidate is fine though.)
 16.2604 -*/
 16.2605 -
 16.2606 -#if __STD_C
 16.2607 -static void malloc_init_state(mstate av)
 16.2608 -#else
 16.2609 -static void malloc_init_state(av) mstate av;
 16.2610 -#endif
 16.2611 -{
 16.2612 -  int     i;
 16.2613 -  mbinptr bin;
 16.2614 -  
 16.2615 -  /* Establish circular links for normal bins */
 16.2616 -  for (i = 1; i < NBINS; ++i) { 
 16.2617 -    bin = bin_at(av,i);
 16.2618 -    bin->fd = bin->bk = bin;
 16.2619 -  }
 16.2620 -
 16.2621 -  av->top_pad        = DEFAULT_TOP_PAD;
 16.2622 -  av->n_mmaps_max    = DEFAULT_MMAP_MAX;
 16.2623 -  av->mmap_threshold = DEFAULT_MMAP_THRESHOLD;
 16.2624 -  av->trim_threshold = DEFAULT_TRIM_THRESHOLD;
 16.2625 -
 16.2626 -#if MORECORE_CONTIGUOUS
 16.2627 -  set_contiguous(av);
 16.2628 -#else
 16.2629 -  set_noncontiguous(av);
 16.2630 -#endif
 16.2631 -
 16.2632 -
 16.2633 -  set_max_fast(av, DEFAULT_MXFAST);
 16.2634 -
 16.2635 -  av->top            = initial_top(av);
 16.2636 -  av->pagesize       = malloc_getpagesize;
 16.2637 -}
 16.2638 -
 16.2639 -/* 
 16.2640 -   Other internal utilities operating on mstates
 16.2641 -*/
 16.2642 -
 16.2643 -static Void_t*  sYSMALLOc(INTERNAL_SIZE_T, mstate);
 16.2644 -#ifndef MORECORE_CANNOT_TRIM
 16.2645 -static int      sYSTRIm(size_t, mstate);
 16.2646 -#endif
 16.2647 -static void     malloc_consolidate(mstate);
 16.2648 -static Void_t** iALLOc(size_t, size_t*, int, Void_t**);
 16.2649 -
 16.2650 -/*
 16.2651 -  Debugging support
 16.2652 -
 16.2653 -  These routines make a number of assertions about the states
 16.2654 -  of data structures that should be true at all times. If any
 16.2655 -  are not true, it's very likely that a user program has somehow
 16.2656 -  trashed memory. (It's also possible that there is a coding error
 16.2657 -  in malloc. In which case, please report it!)
 16.2658 -*/
 16.2659 -
 16.2660 -#if ! DEBUG
 16.2661 -
 16.2662 -#define check_chunk(P)
 16.2663 -#define check_free_chunk(P)
 16.2664 -#define check_inuse_chunk(P)
 16.2665 -#define check_remalloced_chunk(P,N)
 16.2666 -#define check_malloced_chunk(P,N)
 16.2667 -#define check_malloc_state()
 16.2668 -
 16.2669 -#else
 16.2670 -#define check_chunk(P)              do_check_chunk(P)
 16.2671 -#define check_free_chunk(P)         do_check_free_chunk(P)
 16.2672 -#define check_inuse_chunk(P)        do_check_inuse_chunk(P)
 16.2673 -#define check_remalloced_chunk(P,N) do_check_remalloced_chunk(P,N)
 16.2674 -#define check_malloced_chunk(P,N)   do_check_malloced_chunk(P,N)
 16.2675 -#define check_malloc_state()        do_check_malloc_state()
 16.2676 -
 16.2677 -/*
 16.2678 -  Properties of all chunks
 16.2679 -*/
 16.2680 -
 16.2681 -#if __STD_C
 16.2682 -static void do_check_chunk(mchunkptr p)
 16.2683 -#else
 16.2684 -static void do_check_chunk(p) mchunkptr p;
 16.2685 -#endif
 16.2686 -{
 16.2687 -  mstate av = get_malloc_state();
 16.2688 -  CHUNK_SIZE_T  sz = chunksize(p);
 16.2689 -  /* min and max possible addresses assuming contiguous allocation */
 16.2690 -  char* max_address = (char*)(av->top) + chunksize(av->top);
 16.2691 -  char* min_address = max_address - av->sbrked_mem;
 16.2692 -
 16.2693 -  if (!chunk_is_mmapped(p)) {
 16.2694 -    
 16.2695 -    /* Has legal address ... */
 16.2696 -    if (p != av->top) {
 16.2697 -      if (contiguous(av)) {
 16.2698 -        assert(((char*)p) >= min_address);
 16.2699 -        assert(((char*)p + sz) <= ((char*)(av->top)));
 16.2700 -      }
 16.2701 -    }
 16.2702 -    else {
 16.2703 -      /* top size is always at least MINSIZE */
 16.2704 -      assert((CHUNK_SIZE_T)(sz) >= MINSIZE);
 16.2705 -      /* top predecessor always marked inuse */
 16.2706 -      assert(prev_inuse(p));
 16.2707 -    }
 16.2708 -      
 16.2709 -  }
 16.2710 -  else {
 16.2711 -#if HAVE_MMAP
 16.2712 -    /* address is outside main heap  */
 16.2713 -    if (contiguous(av) && av->top != initial_top(av)) {
 16.2714 -      assert(((char*)p) < min_address || ((char*)p) > max_address);
 16.2715 -    }
 16.2716 -    /* chunk is page-aligned */
 16.2717 -    assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
 16.2718 -    /* mem is aligned */
 16.2719 -    assert(aligned_OK(chunk2mem(p)));
 16.2720 -#else
 16.2721 -    /* force an appropriate assert violation if debug set */
 16.2722 -    assert(!chunk_is_mmapped(p));
 16.2723 -#endif
 16.2724 -  }
 16.2725 -}
 16.2726 -
 16.2727 -/*
 16.2728 -  Properties of free chunks
 16.2729 -*/
 16.2730 -
 16.2731 -#if __STD_C
 16.2732 -static void do_check_free_chunk(mchunkptr p)
 16.2733 -#else
 16.2734 -static void do_check_free_chunk(p) mchunkptr p;
 16.2735 -#endif
 16.2736 -{
 16.2737 -  mstate av = get_malloc_state();
 16.2738 -
 16.2739 -  INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
 16.2740 -  mchunkptr next = chunk_at_offset(p, sz);
 16.2741 -
 16.2742 -  do_check_chunk(p);
 16.2743 -
 16.2744 -  /* Chunk must claim to be free ... */
 16.2745 -  assert(!inuse(p));
 16.2746 -  assert (!chunk_is_mmapped(p));
 16.2747 -
 16.2748 -  /* Unless a special marker, must have OK fields */
 16.2749 -  if ((CHUNK_SIZE_T)(sz) >= MINSIZE)
 16.2750 -  {
 16.2751 -    assert((sz & MALLOC_ALIGN_MASK) == 0);
 16.2752 -    assert(aligned_OK(chunk2mem(p)));
 16.2753 -    /* ... matching footer field */
 16.2754 -    assert(next->prev_size == sz);
 16.2755 -    /* ... and is fully consolidated */
 16.2756 -    assert(prev_inuse(p));
 16.2757 -    assert (next == av->top || inuse(next));
 16.2758 -
 16.2759 -    /* ... and has minimally sane links */
 16.2760 -    assert(p->fd->bk == p);
 16.2761 -    assert(p->bk->fd == p);
 16.2762 -  }
 16.2763 -  else /* markers are always of size SIZE_SZ */
 16.2764 -    assert(sz == SIZE_SZ);
 16.2765 -}
 16.2766 -
 16.2767 -/*
 16.2768 -  Properties of inuse chunks
 16.2769 -*/
 16.2770 -
 16.2771 -#if __STD_C
 16.2772 -static void do_check_inuse_chunk(mchunkptr p)
 16.2773 -#else
 16.2774 -static void do_check_inuse_chunk(p) mchunkptr p;
 16.2775 -#endif
 16.2776 -{
 16.2777 -  mstate av = get_malloc_state();
 16.2778 -  mchunkptr next;
 16.2779 -  do_check_chunk(p);
 16.2780 -
 16.2781 -  if (chunk_is_mmapped(p))
 16.2782 -    return; /* mmapped chunks have no next/prev */
 16.2783 -
 16.2784 -  /* Check whether it claims to be in use ... */
 16.2785 -  assert(inuse(p));
 16.2786 -
 16.2787 -  next = next_chunk(p);
 16.2788 -
 16.2789 -  /* ... and is surrounded by OK chunks.
 16.2790 -    Since more things can be checked with free chunks than inuse ones,
 16.2791 -    if an inuse chunk borders them and debug is on, it's worth doing them.
 16.2792 -  */
 16.2793 -  if (!prev_inuse(p))  {
 16.2794 -    /* Note that we cannot even look at prev unless it is not inuse */
 16.2795 -    mchunkptr prv = prev_chunk(p);
 16.2796 -    assert(next_chunk(prv) == p);
 16.2797 -    do_check_free_chunk(prv);
 16.2798 -  }
 16.2799 -
 16.2800 -  if (next == av->top) {
 16.2801 -    assert(prev_inuse(next));
 16.2802 -    assert(chunksize(next) >= MINSIZE);
 16.2803 -  }
 16.2804 -  else if (!inuse(next))
 16.2805 -    do_check_free_chunk(next);
 16.2806 -}
 16.2807 -
 16.2808 -/*
 16.2809 -  Properties of chunks recycled from fastbins
 16.2810 -*/
 16.2811 -
 16.2812 -#if __STD_C
 16.2813 -static void do_check_remalloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
 16.2814 -#else
 16.2815 -static void do_check_remalloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
 16.2816 -#endif
 16.2817 -{
 16.2818 -  INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
 16.2819 -
 16.2820 -  do_check_inuse_chunk(p);
 16.2821 -
 16.2822 -  /* Legal size ... */
 16.2823 -  assert((sz & MALLOC_ALIGN_MASK) == 0);
 16.2824 -  assert((CHUNK_SIZE_T)(sz) >= MINSIZE);
 16.2825 -  /* ... and alignment */
 16.2826 -  assert(aligned_OK(chunk2mem(p)));
 16.2827 -  /* chunk is less than MINSIZE more than request */
 16.2828 -  assert((long)(sz) - (long)(s) >= 0);
 16.2829 -  assert((long)(sz) - (long)(s + MINSIZE) < 0);
 16.2830 -}
 16.2831 -
 16.2832 -/*
 16.2833 -  Properties of nonrecycled chunks at the point they are malloced
 16.2834 -*/
 16.2835 -
 16.2836 -#if __STD_C
 16.2837 -static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
 16.2838 -#else
 16.2839 -static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
 16.2840 -#endif
 16.2841 -{
 16.2842 -  /* same as recycled case ... */
 16.2843 -  do_check_remalloced_chunk(p, s);
 16.2844 -
 16.2845 -  /*
 16.2846 -    ... plus,  must obey implementation invariant that prev_inuse is
 16.2847 -    always true of any allocated chunk; i.e., that each allocated
 16.2848 -    chunk borders either a previously allocated and still in-use
 16.2849 -    chunk, or the base of its memory arena. This is ensured
 16.2850 -    by making all allocations from the the `lowest' part of any found
 16.2851 -    chunk.  This does not necessarily hold however for chunks
 16.2852 -    recycled via fastbins.
 16.2853 -  */
 16.2854 -
 16.2855 -  assert(prev_inuse(p));
 16.2856 -}
 16.2857 -
 16.2858 -
 16.2859 -/*
 16.2860 -  Properties of malloc_state.
 16.2861 -
 16.2862 -  This may be useful for debugging malloc, as well as detecting user
 16.2863 -  programmer errors that somehow write into malloc_state.
 16.2864 -
 16.2865 -  If you are extending or experimenting with this malloc, you can
 16.2866 -  probably figure out how to hack this routine to print out or
 16.2867 -  display chunk addresses, sizes, bins, and other instrumentation.
 16.2868 -*/
 16.2869 -
 16.2870 -static void do_check_malloc_state()
 16.2871 -{
 16.2872 -  mstate av = get_malloc_state();
 16.2873 -  int i;
 16.2874 -  mchunkptr p;
 16.2875 -  mchunkptr q;
 16.2876 -  mbinptr b;
 16.2877 -  unsigned int binbit;
 16.2878 -  int empty;
 16.2879 -  unsigned int idx;
 16.2880 -  INTERNAL_SIZE_T size;
 16.2881 -  CHUNK_SIZE_T  total = 0;
 16.2882 -  int max_fast_bin;
 16.2883 -
 16.2884 -  /* internal size_t must be no wider than pointer type */
 16.2885 -  assert(sizeof(INTERNAL_SIZE_T) <= sizeof(char*));
 16.2886 -
 16.2887 -  /* alignment is a power of 2 */
 16.2888 -  assert((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-1)) == 0);
 16.2889 -
 16.2890 -  /* cannot run remaining checks until fully initialized */
 16.2891 -  if (av->top == 0 || av->top == initial_top(av))
 16.2892 -    return;
 16.2893 -
 16.2894 -  /* pagesize is a power of 2 */
 16.2895 -  assert((av->pagesize & (av->pagesize-1)) == 0);
 16.2896 -
 16.2897 -  /* properties of fastbins */
 16.2898 -
 16.2899 -  /* max_fast is in allowed range */
 16.2900 -  assert(get_max_fast(av) <= request2size(MAX_FAST_SIZE));
 16.2901 -
 16.2902 -  max_fast_bin = fastbin_index(av->max_fast);
 16.2903 -
 16.2904 -  for (i = 0; i < NFASTBINS; ++i) {
 16.2905 -    p = av->fastbins[i];
 16.2906 -
 16.2907 -    /* all bins past max_fast are empty */
 16.2908 -    if (i > max_fast_bin)
 16.2909 -      assert(p == 0);
 16.2910 -
 16.2911 -    while (p != 0) {
 16.2912 -      /* each chunk claims to be inuse */
 16.2913 -      do_check_inuse_chunk(p);
 16.2914 -      total += chunksize(p);
 16.2915 -      /* chunk belongs in this bin */
 16.2916 -      assert(fastbin_index(chunksize(p)) == i);
 16.2917 -      p = p->fd;
 16.2918 -    }
 16.2919 -  }
 16.2920 -
 16.2921 -  if (total != 0)
 16.2922 -    assert(have_fastchunks(av));
 16.2923 -  else if (!have_fastchunks(av))
 16.2924 -    assert(total == 0);
 16.2925 -
 16.2926 -  /* check normal bins */
 16.2927 -  for (i = 1; i < NBINS; ++i) {
 16.2928 -    b = bin_at(av,i);
 16.2929 -
 16.2930 -    /* binmap is accurate (except for bin 1 == unsorted_chunks) */
 16.2931 -    if (i >= 2) {
 16.2932 -      binbit = get_binmap(av,i);
 16.2933 -      empty = last(b) == b;
 16.2934 -      if (!binbit)
 16.2935 -        assert(empty);
 16.2936 -      else if (!empty)
 16.2937 -        assert(binbit);
 16.2938 -    }
 16.2939 -
 16.2940 -    for (p = last(b); p != b; p = p->bk) {
 16.2941 -      /* each chunk claims to be free */
 16.2942 -      do_check_free_chunk(p);
 16.2943 -      size = chunksize(p);
 16.2944 -      total += size;
 16.2945 -      if (i >= 2) {
 16.2946 -        /* chunk belongs in bin */
 16.2947 -        idx = bin_index(size);
 16.2948 -        assert(idx == i);
 16.2949 -        /* lists are sorted */
 16.2950 -        if ((CHUNK_SIZE_T) size >= (CHUNK_SIZE_T)(FIRST_SORTED_BIN_SIZE)) {
 16.2951 -          assert(p->bk == b || 
 16.2952 -                 (CHUNK_SIZE_T)chunksize(p->bk) >= 
 16.2953 -                 (CHUNK_SIZE_T)chunksize(p));
 16.2954 -        }
 16.2955 -      }
 16.2956 -      /* chunk is followed by a legal chain of inuse chunks */
 16.2957 -      for (q = next_chunk(p);
 16.2958 -           (q != av->top && inuse(q) && 
 16.2959 -             (CHUNK_SIZE_T)(chunksize(q)) >= MINSIZE);
 16.2960 -           q = next_chunk(q))
 16.2961 -        do_check_inuse_chunk(q);
 16.2962 -    }
 16.2963 -  }
 16.2964 -
 16.2965 -  /* top chunk is OK */
 16.2966 -  check_chunk(av->top);
 16.2967 -
 16.2968 -  /* sanity checks for statistics */
 16.2969 -
 16.2970 -  assert(total <= (CHUNK_SIZE_T)(av->max_total_mem));
 16.2971 -  assert(av->n_mmaps >= 0);
 16.2972 -  assert(av->n_mmaps <= av->max_n_mmaps);
 16.2973 -
 16.2974 -  assert((CHUNK_SIZE_T)(av->sbrked_mem) <=
 16.2975 -         (CHUNK_SIZE_T)(av->max_sbrked_mem));
 16.2976 -
 16.2977 -  assert((CHUNK_SIZE_T)(av->mmapped_mem) <=
 16.2978 -         (CHUNK_SIZE_T)(av->max_mmapped_mem));
 16.2979 -
 16.2980 -  assert((CHUNK_SIZE_T)(av->max_total_mem) >=
 16.2981 -         (CHUNK_SIZE_T)(av->mmapped_mem) + (CHUNK_SIZE_T)(av->sbrked_mem));
 16.2982 -}
 16.2983 -#endif
 16.2984 -
 16.2985 -
 16.2986 -/* ----------- Routines dealing with system allocation -------------- */
 16.2987 -
 16.2988 -/*
 16.2989 -  sysmalloc handles malloc cases requiring more memory from the system.
 16.2990 -  On entry, it is assumed that av->top does not have enough
 16.2991 -  space to service request for nb bytes, thus requiring that av->top
 16.2992 -  be extended or replaced.
 16.2993 -*/
 16.2994 -
 16.2995 -#if __STD_C
 16.2996 -static Void_t* sYSMALLOc(INTERNAL_SIZE_T nb, mstate av)
 16.2997 -#else
 16.2998 -static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
 16.2999 -#endif
 16.3000 -{
 16.3001 -  mchunkptr       old_top;        /* incoming value of av->top */
 16.3002 -  INTERNAL_SIZE_T old_size;       /* its size */
 16.3003 -  char*           old_end;        /* its end address */
 16.3004 -
 16.3005 -  long            size;           /* arg to first MORECORE or mmap call */
 16.3006 -  char*           brk;            /* return value from MORECORE */
 16.3007 -
 16.3008 -  long            correction;     /* arg to 2nd MORECORE call */
 16.3009 -  char*           snd_brk;        /* 2nd return val */
 16.3010 -
 16.3011 -  INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */
 16.3012 -  INTERNAL_SIZE_T end_misalign;   /* partial page left at end of new space */
 16.3013 -  char*           aligned_brk;    /* aligned offset into brk */
 16.3014 -
 16.3015 -  mchunkptr       p;              /* the allocated/returned chunk */
 16.3016 -  mchunkptr       remainder;      /* remainder from allocation */
 16.3017 -  CHUNK_SIZE_T    remainder_size; /* its size */
 16.3018 -
 16.3019 -  CHUNK_SIZE_T    sum;            /* for updating stats */
 16.3020 -
 16.3021 -  size_t          pagemask  = av->pagesize - 1;
 16.3022 -
 16.3023 -  /*
 16.3024 -    If there is space available in fastbins, consolidate and retry
 16.3025 -    malloc from scratch rather than getting memory from system.  This
 16.3026 -    can occur only if nb is in smallbin range so we didn't consolidate
 16.3027 -    upon entry to malloc. It is much easier to handle this case here
 16.3028 -    than in malloc proper.
 16.3029 -  */
 16.3030 -
 16.3031 -  if (have_fastchunks(av)) {
 16.3032 -    assert(in_smallbin_range(nb));
 16.3033 -    malloc_consolidate(av);
 16.3034 -    return mALLOc(nb - MALLOC_ALIGN_MASK);
 16.3035 -  }
 16.3036 -
 16.3037 -
 16.3038 -#if HAVE_MMAP
 16.3039 -
 16.3040 -  /*
 16.3041 -    If have mmap, and the request size meets the mmap threshold, and
 16.3042 -    the system supports mmap, and there are few enough currently
 16.3043 -    allocated mmapped regions, try to directly map this request
 16.3044 -    rather than expanding top.
 16.3045 -  */
 16.3046 -
 16.3047 -  if ((CHUNK_SIZE_T)(nb) >= (CHUNK_SIZE_T)(av->mmap_threshold) &&
 16.3048 -      (av->n_mmaps < av->n_mmaps_max)) {
 16.3049 -
 16.3050 -    char* mm;             /* return value from mmap call*/
 16.3051 -
 16.3052 -    /*
 16.3053 -      Round up size to nearest page.  For mmapped chunks, the overhead
 16.3054 -      is one SIZE_SZ unit larger than for normal chunks, because there
 16.3055 -      is no following chunk whose prev_size field could be used.
 16.3056 -    */
 16.3057 -    size = (nb + SIZE_SZ + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
 16.3058 -
 16.3059 -    /* Don't try if size wraps around 0 */
 16.3060 -    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
 16.3061 -
 16.3062 -      mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
 16.3063 -      
 16.3064 -      if (mm != (char*)(MORECORE_FAILURE)) {
 16.3065 -        
 16.3066 -        /*
 16.3067 -          The offset to the start of the mmapped region is stored
 16.3068 -          in the prev_size field of the chunk. This allows us to adjust
 16.3069 -          returned start address to meet alignment requirements here 
 16.3070 -          and in memalign(), and still be able to compute proper
 16.3071 -          address argument for later munmap in free() and realloc().
 16.3072 -        */
 16.3073 -        
 16.3074 -        front_misalign = (INTERNAL_SIZE_T)chunk2mem(mm) & MALLOC_ALIGN_MASK;
 16.3075 -        if (front_misalign > 0) {
 16.3076 -          correction = MALLOC_ALIGNMENT - front_misalign;
 16.3077 -          p = (mchunkptr)(mm + correction);
 16.3078 -          p->prev_size = correction;
 16.3079 -          set_head(p, (size - correction) |IS_MMAPPED);
 16.3080 -        }
 16.3081 -        else {
 16.3082 -          p = (mchunkptr)mm;
 16.3083 -          p->prev_size = 0;
 16.3084 -          set_head(p, size|IS_MMAPPED);
 16.3085 -        }
 16.3086 -        
 16.3087 -        /* update statistics */
 16.3088 -        
 16.3089 -        if (++av->n_mmaps > av->max_n_mmaps) 
 16.3090 -          av->max_n_mmaps = av->n_mmaps;
 16.3091 -        
 16.3092 -        sum = av->mmapped_mem += size;
 16.3093 -        if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
 16.3094 -          av->max_mmapped_mem = sum;
 16.3095 -        sum += av->sbrked_mem;
 16.3096 -        if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
 16.3097 -          av->max_total_mem = sum;
 16.3098 -
 16.3099 -        check_chunk(p);
 16.3100 -        
 16.3101 -        return chunk2mem(p);
 16.3102 -      }
 16.3103 -    }
 16.3104 -  }
 16.3105 -#endif
 16.3106 -
 16.3107 -  /* Record incoming configuration of top */
 16.3108 -
 16.3109 -  old_top  = av->top;
 16.3110 -  old_size = chunksize(old_top);
 16.3111 -  old_end  = (char*)(chunk_at_offset(old_top, old_size));
 16.3112 -
 16.3113 -  brk = snd_brk = (char*)(MORECORE_FAILURE); 
 16.3114 -
 16.3115 -  /* 
 16.3116 -     If not the first time through, we require old_size to be
 16.3117 -     at least MINSIZE and to have prev_inuse set.
 16.3118 -  */
 16.3119 -
 16.3120 -  assert((old_top == initial_top(av) && old_size == 0) || 
 16.3121 -         ((CHUNK_SIZE_T) (old_size) >= MINSIZE &&
 16.3122 -          prev_inuse(old_top)));
 16.3123 -
 16.3124 -  /* Precondition: not enough current space to satisfy nb request */
 16.3125 -  assert((CHUNK_SIZE_T)(old_size) < (CHUNK_SIZE_T)(nb + MINSIZE));
 16.3126 -
 16.3127 -  /* Precondition: all fastbins are consolidated */
 16.3128 -  assert(!have_fastchunks(av));
 16.3129 -
 16.3130 -
 16.3131 -  /* Request enough space for nb + pad + overhead */
 16.3132 -
 16.3133 -  size = nb + av->top_pad + MINSIZE;
 16.3134 -
 16.3135 -  /*
 16.3136 -    If contiguous, we can subtract out existing space that we hope to
 16.3137 -    combine with new space. We add it back later only if
 16.3138 -    we don't actually get contiguous space.
 16.3139 -  */
 16.3140 -
 16.3141 -  if (contiguous(av))
 16.3142 -    size -= old_size;
 16.3143 -
 16.3144 -  /*
 16.3145 -    Round to a multiple of page size.
 16.3146 -    If MORECORE is not contiguous, this ensures that we only call it
 16.3147 -    with whole-page arguments.  And if MORECORE is contiguous and
 16.3148 -    this is not first time through, this preserves page-alignment of
 16.3149 -    previous calls. Otherwise, we correct to page-align below.
 16.3150 -  */
 16.3151 -
 16.3152 -  size = (size + pagemask) & ~pagemask;
 16.3153 -
 16.3154 -  /*
 16.3155 -    Don't try to call MORECORE if argument is so big as to appear
 16.3156 -    negative. Note that since mmap takes size_t arg, it may succeed
 16.3157 -    below even if we cannot call MORECORE.
 16.3158 -  */
 16.3159 -
 16.3160 -  if (size > 0) 
 16.3161 -    brk = (char*)(MORECORE(size));
 16.3162 -
 16.3163 -  /*
 16.3164 -    If have mmap, try using it as a backup when MORECORE fails or
 16.3165 -    cannot be used. This is worth doing on systems that have "holes" in
 16.3166 -    address space, so sbrk cannot extend to give contiguous space, but
 16.3167 -    space is available elsewhere.  Note that we ignore mmap max count
 16.3168 -    and threshold limits, since the space will not be used as a
 16.3169 -    segregated mmap region.
 16.3170 -  */
 16.3171 -
 16.3172 -#if HAVE_MMAP
 16.3173 -  if (brk == (char*)(MORECORE_FAILURE)) {
 16.3174 -
 16.3175 -    /* Cannot merge with old top, so add its size back in */
 16.3176 -    if (contiguous(av))
 16.3177 -      size = (size + old_size + pagemask) & ~pagemask;
 16.3178 -
 16.3179 -    /* If we are relying on mmap as backup, then use larger units */
 16.3180 -    if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(MMAP_AS_MORECORE_SIZE))
 16.3181 -      size = MMAP_AS_MORECORE_SIZE;
 16.3182 -
 16.3183 -    /* Don't try if size wraps around 0 */
 16.3184 -    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
 16.3185 -
 16.3186 -      brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
 16.3187 -      
 16.3188 -      if (brk != (char*)(MORECORE_FAILURE)) {
 16.3189 -        
 16.3190 -        /* We do not need, and cannot use, another sbrk call to find end */
 16.3191 -        snd_brk = brk + size;
 16.3192 -        
 16.3193 -        /* 
 16.3194 -           Record that we no longer have a contiguous sbrk region. 
 16.3195 -           After the first time mmap is used as backup, we do not
 16.3196 -           ever rely on contiguous space since this could incorrectly
 16.3197 -           bridge regions.
 16.3198 -        */
 16.3199 -        set_noncontiguous(av);
 16.3200 -      }
 16.3201 -    }
 16.3202 -  }
 16.3203 -#endif
 16.3204 -
 16.3205 -  if (brk != (char*)(MORECORE_FAILURE)) {
 16.3206 -    av->sbrked_mem += size;
 16.3207 -
 16.3208 -    /*
 16.3209 -      If MORECORE extends previous space, we can likewise extend top size.
 16.3210 -    */
 16.3211 -    
 16.3212 -    if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
 16.3213 -      set_head(old_top, (size + old_size) | PREV_INUSE);
 16.3214 -    }
 16.3215 -
 16.3216 -    /*
 16.3217 -      Otherwise, make adjustments:
 16.3218 -      
 16.3219 -      * If the first time through or noncontiguous, we need to call sbrk
 16.3220 -        just to find out where the end of memory lies.
 16.3221 -
 16.3222 -      * We need to ensure that all returned chunks from malloc will meet
 16.3223 -        MALLOC_ALIGNMENT
 16.3224 -
 16.3225 -      * If there was an intervening foreign sbrk, we need to adjust sbrk
 16.3226 -        request size to account for fact that we will not be able to
 16.3227 -        combine new space with existing space in old_top.
 16.3228 -
 16.3229 -      * Almost all systems internally allocate whole pages at a time, in
 16.3230 -        which case we might as well use the whole last page of request.
 16.3231 -        So we allocate enough more memory to hit a page boundary now,
 16.3232 -        which in turn causes future contiguous calls to page-align.
 16.3233 -    */
 16.3234 -    
 16.3235 -    else {
 16.3236 -      front_misalign = 0;
 16.3237 -      end_misalign = 0;
 16.3238 -      correction = 0;
 16.3239 -      aligned_brk = brk;
 16.3240 -
 16.3241 -      /*
 16.3242 -        If MORECORE returns an address lower than we have seen before,
 16.3243 -        we know it isn't really contiguous.  This and some subsequent
 16.3244 -        checks help cope with non-conforming MORECORE functions and
 16.3245 -        the presence of "foreign" calls to MORECORE from outside of
 16.3246 -        malloc or by other threads.  We cannot guarantee to detect
 16.3247 -        these in all cases, but cope with the ones we do detect.
 16.3248 -      */
 16.3249 -      if (contiguous(av) && old_size != 0 && brk < old_end) {
 16.3250 -        set_noncontiguous(av);
 16.3251 -      }
 16.3252 -      
 16.3253 -      /* handle contiguous cases */
 16.3254 -      if (contiguous(av)) { 
 16.3255 -
 16.3256 -        /* 
 16.3257 -           We can tolerate forward non-contiguities here (usually due
 16.3258 -           to foreign calls) but treat them as part of our space for
 16.3259 -           stats reporting.
 16.3260 -        */
 16.3261 -        if (old_size != 0) 
 16.3262 -          av->sbrked_mem += brk - old_end;
 16.3263 -        
 16.3264 -        /* Guarantee alignment of first new chunk made from this space */
 16.3265 -
 16.3266 -        front_misalign = (INTERNAL_SIZE_T)chunk2mem(brk) & MALLOC_ALIGN_MASK;
 16.3267 -        if (front_misalign > 0) {
 16.3268 -
 16.3269 -          /*
 16.3270 -            Skip over some bytes to arrive at an aligned position.
 16.3271 -            We don't need to specially mark these wasted front bytes.
 16.3272 -            They will never be accessed anyway because
 16.3273 -            prev_inuse of av->top (and any chunk created from its start)
 16.3274 -            is always true after initialization.
 16.3275 -          */
 16.3276 -
 16.3277 -          correction = MALLOC_ALIGNMENT - front_misalign;
 16.3278 -          aligned_brk += correction;
 16.3279 -        }
 16.3280 -        
 16.3281 -        /*
 16.3282 -          If this isn't adjacent to existing space, then we will not
 16.3283 -          be able to merge with old_top space, so must add to 2nd request.
 16.3284 -        */
 16.3285 -        
 16.3286 -        correction += old_size;
 16.3287 -        
 16.3288 -        /* Extend the end address to hit a page boundary */
 16.3289 -        end_misalign = (INTERNAL_SIZE_T)(brk + size + correction);
 16.3290 -        correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
 16.3291 -        
 16.3292 -        assert(correction >= 0);
 16.3293 -        snd_brk = (char*)(MORECORE(correction));
 16.3294 -        
 16.3295 -        if (snd_brk == (char*)(MORECORE_FAILURE)) {
 16.3296 -          /*
 16.3297 -            If can't allocate correction, try to at least find out current
 16.3298 -            brk.  It might be enough to proceed without failing.
 16.3299 -          */
 16.3300 -          correction = 0;
 16.3301 -          snd_brk = (char*)(MORECORE(0));
 16.3302 -        }
 16.3303 -        else if (snd_brk < brk) {
 16.3304 -          /*
 16.3305 -            If the second call gives noncontiguous space even though
 16.3306 -            it says it won't, the only course of action is to ignore
 16.3307 -            results of second call, and conservatively estimate where
 16.3308 -            the first call left us. Also set noncontiguous, so this
 16.3309 -            won't happen again, leaving at most one hole.
 16.3310 -            
 16.3311 -            Note that this check is intrinsically incomplete.  Because
 16.3312 -            MORECORE is allowed to give more space than we ask for,
 16.3313 -            there is no reliable way to detect a noncontiguity
 16.3314 -            producing a forward gap for the second call.
 16.3315 -          */
 16.3316 -          snd_brk = brk + size;
 16.3317 -          correction = 0;
 16.3318 -          set_noncontiguous(av);
 16.3319 -        }
 16.3320 -
 16.3321 -      }
 16.3322 -      
 16.3323 -      /* handle non-contiguous cases */
 16.3324 -      else { 
 16.3325 -        /* MORECORE/mmap must correctly align */
 16.3326 -        assert(aligned_OK(chunk2mem(brk)));
 16.3327 -        
 16.3328 -        /* Find out current end of memory */
 16.3329 -        if (snd_brk == (char*)(MORECORE_FAILURE)) {
 16.3330 -          snd_brk = (char*)(MORECORE(0));
 16.3331 -          av->sbrked_mem += snd_brk - brk - size;
 16.3332 -        }
 16.3333 -      }
 16.3334 -      
 16.3335 -      /* Adjust top based on results of second sbrk */
 16.3336 -      if (snd_brk != (char*)(MORECORE_FAILURE)) {
 16.3337 -        av->top = (mchunkptr)aligned_brk;
 16.3338 -        set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
 16.3339 -        av->sbrked_mem += correction;
 16.3340 -     
 16.3341 -        /*
 16.3342 -          If not the first time through, we either have a
 16.3343 -          gap due to foreign sbrk or a non-contiguous region.  Insert a
 16.3344 -          double fencepost at old_top to prevent consolidation with space
 16.3345 -          we don't own. These fenceposts are artificial chunks that are
 16.3346 -          marked as inuse and are in any case too small to use.  We need
 16.3347 -          two to make sizes and alignments work out.
 16.3348 -        */
 16.3349 -   
 16.3350 -        if (old_size != 0) {
 16.3351 -          /* 
 16.3352 -             Shrink old_top to insert fenceposts, keeping size a
 16.3353 -             multiple of MALLOC_ALIGNMENT. We know there is at least
 16.3354 -             enough space in old_top to do this.
 16.3355 -          */
 16.3356 -          old_size = (old_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
 16.3357 -          set_head(old_top, old_size | PREV_INUSE);
 16.3358 -          
 16.3359 -          /*
 16.3360 -            Note that the following assignments completely overwrite
 16.3361 -            old_top when old_size was previously MINSIZE.  This is
 16.3362 -            intentional. We need the fencepost, even if old_top otherwise gets
 16.3363 -            lost.
 16.3364 -          */
 16.3365 -          chunk_at_offset(old_top, old_size          )->size =
 16.3366 -            SIZE_SZ|PREV_INUSE;
 16.3367 -
 16.3368 -          chunk_at_offset(old_top, old_size + SIZE_SZ)->size =
 16.3369 -            SIZE_SZ|PREV_INUSE;
 16.3370 -
 16.3371 -          /* 
 16.3372 -             If possible, release the rest, suppressing trimming.
 16.3373 -          */
 16.3374 -          if (old_size >= MINSIZE) {
 16.3375 -            INTERNAL_SIZE_T tt = av->trim_threshold;
 16.3376 -            av->trim_threshold = (INTERNAL_SIZE_T)(-1);
 16.3377 -            fREe(chunk2mem(old_top));
 16.3378 -            av->trim_threshold = tt;
 16.3379 -          }
 16.3380 -        }
 16.3381 -      }
 16.3382 -    }
 16.3383 -    
 16.3384 -    /* Update statistics */
 16.3385 -    sum = av->sbrked_mem;
 16.3386 -    if (sum > (CHUNK_SIZE_T)(av->max_sbrked_mem))
 16.3387 -      av->max_sbrked_mem = sum;
 16.3388 -    
 16.3389 -    sum += av->mmapped_mem;
 16.3390 -    if (sum > (CHUNK_SIZE_T)(av->max_total_mem))
 16.3391 -      av->max_total_mem = sum;
 16.3392 -
 16.3393 -    check_malloc_state();
 16.3394 -    
 16.3395 -    /* finally, do the allocation */
 16.3396 -
 16.3397 -    p = av->top;
 16.3398 -    size = chunksize(p);
 16.3399 -    
 16.3400 -    /* check that one of the above allocation paths succeeded */
 16.3401 -    if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
 16.3402 -      remainder_size = size - nb;
 16.3403 -      remainder = chunk_at_offset(p, nb);
 16.3404 -      av->top = remainder;
 16.3405 -      set_head(p, nb | PREV_INUSE);
 16.3406 -      set_head(remainder, remainder_size | PREV_INUSE);
 16.3407 -      check_malloced_chunk(p, nb);
 16.3408 -      return chunk2mem(p);
 16.3409 -    }
 16.3410 -
 16.3411 -  }
 16.3412 -
 16.3413 -  /* catch all failure paths */
 16.3414 -  MALLOC_FAILURE_ACTION;
 16.3415 -  return 0;
 16.3416 -}
 16.3417 -
 16.3418 -
 16.3419 -
 16.3420 -
 16.3421 -#ifndef MORECORE_CANNOT_TRIM
 16.3422 -/*
 16.3423 -  sYSTRIm is an inverse of sorts to sYSMALLOc.  It gives memory back
 16.3424 -  to the system (via negative arguments to sbrk) if there is unused
 16.3425 -  memory at the `high' end of the malloc pool. It is called
 16.3426 -  automatically by free() when top space exceeds the trim
 16.3427 -  threshold. It is also called by the public malloc_trim routine.  It
 16.3428 -  returns 1 if it actually released any memory, else 0.
 16.3429 -*/
 16.3430 -
 16.3431 -#if __STD_C
 16.3432 -static int sYSTRIm(size_t pad, mstate av)
 16.3433 -#else
 16.3434 -static int sYSTRIm(pad, av) size_t pad; mstate av;
 16.3435 -#endif
 16.3436 -{
 16.3437 -  long  top_size;        /* Amount of top-most memory */
 16.3438 -  long  extra;           /* Amount to release */
 16.3439 -  long  released;        /* Amount actually released */
 16.3440 -  char* current_brk;     /* address returned by pre-check sbrk call */
 16.3441 -  char* new_brk;         /* address returned by post-check sbrk call */
 16.3442 -  size_t pagesz;
 16.3443 -
 16.3444 -  pagesz = av->pagesize;
 16.3445 -  top_size = chunksize(av->top);
 16.3446 -  
 16.3447 -  /* Release in pagesize units, keeping at least one page */
 16.3448 -  extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
 16.3449 -  
 16.3450 -  if (extra > 0) {
 16.3451 -    
 16.3452 -    /*
 16.3453 -      Only proceed if end of memory is where we last set it.
 16.3454 -      This avoids problems if there were foreign sbrk calls.
 16.3455 -    */
 16.3456 -    current_brk = (char*)(MORECORE(0));
 16.3457 -    if (current_brk == (char*)(av->top) + top_size) {
 16.3458 -      
 16.3459 -      /*
 16.3460 -        Attempt to release memory. We ignore MORECORE return value,
 16.3461 -        and instead call again to find out where new end of memory is.
 16.3462 -        This avoids problems if first call releases less than we asked,
 16.3463 -        of if failure somehow altered brk value. (We could still
 16.3464 -        encounter problems if it altered brk in some very bad way,
 16.3465 -        but the only thing we can do is adjust anyway, which will cause
 16.3466 -        some downstream failure.)
 16.3467 -      */
 16.3468 -      
 16.3469 -      MORECORE(-extra);
 16.3470 -      new_brk = (char*)(MORECORE(0));
 16.3471 -      
 16.3472 -      if (new_brk != (char*)MORECORE_FAILURE) {
 16.3473 -        released = (long)(current_brk - new_brk);
 16.3474 -        
 16.3475 -        if (released != 0) {
 16.3476 -          /* Success. Adjust top. */
 16.3477 -          av->sbrked_mem -= released;
 16.3478 -          set_head(av->top, (top_size - released) | PREV_INUSE);
 16.3479 -          check_malloc_state();
 16.3480 -          return 1;
 16.3481 -        }
 16.3482 -      }
 16.3483 -    }
 16.3484 -  }
 16.3485 -  return 0;
 16.3486 -}
 16.3487 -#endif
 16.3488 -
 16.3489 -/*
 16.3490 -  ------------------------------ malloc ------------------------------
 16.3491 -*/
 16.3492 -
 16.3493 -
 16.3494 -#if __STD_C
 16.3495 -Void_t* mALLOc(size_t bytes)
 16.3496 -#else
 16.3497 -  Void_t* mALLOc(bytes) size_t bytes;
 16.3498 -#endif
 16.3499 -{
 16.3500 -  mstate av = get_malloc_state();
 16.3501 -
 16.3502 -  INTERNAL_SIZE_T nb;               /* normalized request size */
 16.3503 -  unsigned int    idx;              /* associated bin index */
 16.3504 -  mbinptr         bin;              /* associated bin */
 16.3505 -  mfastbinptr*    fb;               /* associated fastbin */
 16.3506 -
 16.3507 -  mchunkptr       victim;           /* inspected/selected chunk */
 16.3508 -  INTERNAL_SIZE_T size;             /* its size */
 16.3509 -  int             victim_index;     /* its bin index */
 16.3510 -
 16.3511 -  mchunkptr       remainder;        /* remainder from a split */
 16.3512 -  CHUNK_SIZE_T    remainder_size;   /* its size */
 16.3513 -
 16.3514 -  unsigned int    block;            /* bit map traverser */
 16.3515 -  unsigned int    bit;              /* bit map traverser */
 16.3516 -  unsigned int    map;              /* current word of binmap */
 16.3517 -
 16.3518 -  mchunkptr       fwd;              /* misc temp for linking */
 16.3519 -  mchunkptr       bck;              /* misc temp for linking */
 16.3520 -
 16.3521 -  /*
 16.3522 -    Convert request size to internal form by adding SIZE_SZ bytes
 16.3523 -    overhead plus possibly more to obtain necessary alignment and/or
 16.3524 -    to obtain a size of at least MINSIZE, the smallest allocatable
 16.3525 -    size. Also, checked_request2size traps (returning 0) request sizes
 16.3526 -    that are so large that they wrap around zero when padded and
 16.3527 -    aligned.
 16.3528 -  */
 16.3529 -
 16.3530 -  checked_request2size(bytes, nb);
 16.3531 -
 16.3532 -  /*
 16.3533 -    Bypass search if no frees yet
 16.3534 -   */
 16.3535 -  if (!have_anychunks(av)) {
 16.3536 -    if (av->max_fast == 0) /* initialization check */
 16.3537 -      malloc_consolidate(av);
 16.3538 -    goto use_top;
 16.3539 -  }
 16.3540 -
 16.3541 -  /*
 16.3542 -    If the size qualifies as a fastbin, first check corresponding bin.
 16.3543 -  */
 16.3544 -
 16.3545 -  if ((CHUNK_SIZE_T)(nb) <= (CHUNK_SIZE_T)(av->max_fast)) { 
 16.3546 -    fb = &(av->fastbins[(fastbin_index(nb))]);
 16.3547 -    if ( (victim = *fb) != 0) {
 16.3548 -      *fb = victim->fd;
 16.3549 -      check_remalloced_chunk(victim, nb);
 16.3550 -      return chunk2mem(victim);
 16.3551 -    }
 16.3552 -  }
 16.3553 -
 16.3554 -  /*
 16.3555 -    If a small request, check regular bin.  Since these "smallbins"
 16.3556 -    hold one size each, no searching within bins is necessary.
 16.3557 -    (For a large request, we need to wait until unsorted chunks are
 16.3558 -    processed to find best fit. But for small ones, fits are exact
 16.3559 -    anyway, so we can check now, which is faster.)
 16.3560 -  */
 16.3561 -
 16.3562 -  if (in_smallbin_range(nb)) {
 16.3563 -    idx = smallbin_index(nb);
 16.3564 -    bin = bin_at(av,idx);
 16.3565 -
 16.3566 -    if ( (victim = last(bin)) != bin) {
 16.3567 -      bck = victim->bk;
 16.3568 -      set_inuse_bit_at_offset(victim, nb);
 16.3569 -      bin->bk = bck;
 16.3570 -      bck->fd = bin;
 16.3571 -      
 16.3572 -      check_malloced_chunk(victim, nb);
 16.3573 -      return chunk2mem(victim);
 16.3574 -    }
 16.3575 -  }
 16.3576 -
 16.3577 -  /* 
 16.3578 -     If this is a large request, consolidate fastbins before continuing.
 16.3579 -     While it might look excessive to kill all fastbins before
 16.3580 -     even seeing if there is space available, this avoids
 16.3581 -     fragmentation problems normally associated with fastbins.
 16.3582 -     Also, in practice, programs tend to have runs of either small or
 16.3583 -     large requests, but less often mixtures, so consolidation is not 
 16.3584 -     invoked all that often in most programs. And the programs that
 16.3585 -     it is called frequently in otherwise tend to fragment.
 16.3586 -  */
 16.3587 -
 16.3588 -  else {
 16.3589 -    idx = largebin_index(nb);
 16.3590 -    if (have_fastchunks(av)) 
 16.3591 -      malloc_consolidate(av);
 16.3592 -  }
 16.3593 -
 16.3594 -  /*
 16.3595 -    Process recently freed or remaindered chunks, taking one only if
 16.3596 -    it is exact fit, or, if this a small request, the chunk is remainder from
 16.3597 -    the most recent non-exact fit.  Place other traversed chunks in
 16.3598 -    bins.  Note that this step is the only place in any routine where
 16.3599 -    chunks are placed in bins.
 16.3600 -  */
 16.3601 -    
 16.3602 -  while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
 16.3603 -    bck = victim->bk;
 16.3604 -    size = chunksize(victim);
 16.3605 -    
 16.3606 -    /* 
 16.3607 -       If a small request, try to use last remainder if it is the
 16.3608 -       only chunk in unsorted bin.  This helps promote locality for
 16.3609 -       runs of consecutive small requests. This is the only
 16.3610 -       exception to best-fit, and applies only when there is
 16.3611 -       no exact fit for a small chunk.
 16.3612 -    */
 16.3613 -    
 16.3614 -    if (in_smallbin_range(nb) && 
 16.3615 -        bck == unsorted_chunks(av) &&
 16.3616 -        victim == av->last_remainder &&
 16.3617 -        (CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE)) {
 16.3618 -      
 16.3619 -      /* split and reattach remainder */
 16.3620 -      remainder_size = size - nb;
 16.3621 -      remainder = chunk_at_offset(victim, nb);
 16.3622 -      unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
 16.3623 -      av->last_remainder = remainder; 
 16.3624 -      remainder->bk = remainder->fd = unsorted_chunks(av);
 16.3625 -      
 16.3626 -      set_head(victim, nb | PREV_INUSE);
 16.3627 -      set_head(remainder, remainder_size | PREV_INUSE);
 16.3628 -      set_foot(remainder, remainder_size);
 16.3629 -      
 16.3630 -      check_malloced_chunk(victim, nb);
 16.3631 -      return chunk2mem(victim);
 16.3632 -    }
 16.3633 -    
 16.3634 -    /* remove from unsorted list */
 16.3635 -    unsorted_chunks(av)->bk = bck;
 16.3636 -    bck->fd = unsorted_chunks(av);
 16.3637 -    
 16.3638 -    /* Take now instead of binning if exact fit */
 16.3639 -    
 16.3640 -    if (size == nb) {
 16.3641 -      set_inuse_bit_at_offset(victim, size);
 16.3642 -      check_malloced_chunk(victim, nb);
 16.3643 -      return chunk2mem(victim);
 16.3644 -    }
 16.3645 -    
 16.3646 -    /* place chunk in bin */
 16.3647 -    
 16.3648 -    if (in_smallbin_range(size)) {
 16.3649 -      victim_index = smallbin_index(size);
 16.3650 -      bck = bin_at(av, victim_index);
 16.3651 -      fwd = bck->fd;
 16.3652 -    }
 16.3653 -    else {
 16.3654 -      victim_index = largebin_index(size);
 16.3655 -      bck = bin_at(av, victim_index);
 16.3656 -      fwd = bck->fd;
 16.3657 -      
 16.3658 -      if (fwd != bck) {
 16.3659 -        /* if smaller than smallest, place first */
 16.3660 -        if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(bck->bk->size)) {
 16.3661 -          fwd = bck;
 16.3662 -          bck = bck->bk;
 16.3663 -        }
 16.3664 -        else if ((CHUNK_SIZE_T)(size) >= 
 16.3665 -                 (CHUNK_SIZE_T)(FIRST_SORTED_BIN_SIZE)) {
 16.3666 -          
 16.3667 -          /* maintain large bins in sorted order */
 16.3668 -          size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
 16.3669 -          while ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(fwd->size)) 
 16.3670 -            fwd = fwd->fd;
 16.3671 -          bck = fwd->bk;
 16.3672 -        }
 16.3673 -      }
 16.3674 -    }
 16.3675 -      
 16.3676 -    mark_bin(av, victim_index);
 16.3677 -    victim->bk = bck;
 16.3678 -    victim->fd = fwd;
 16.3679 -    fwd->bk = victim;
 16.3680 -    bck->fd = victim;
 16.3681 -  }
 16.3682 -  
 16.3683 -  /*
 16.3684 -    If a large request, scan through the chunks of current bin to
 16.3685 -    find one that fits.  (This will be the smallest that fits unless
 16.3686 -    FIRST_SORTED_BIN_SIZE has been changed from default.)  This is
 16.3687 -    the only step where an unbounded number of chunks might be
 16.3688 -    scanned without doing anything useful with them. However the
 16.3689 -    lists tend to be short.
 16.3690 -  */
 16.3691 -  
 16.3692 -  if (!in_smallbin_range(nb)) {
 16.3693 -    bin = bin_at(av, idx);
 16.3694 -    
 16.3695 -    for (victim = last(bin); victim != bin; victim = victim->bk) {
 16.3696 -      size = chunksize(victim);
 16.3697 -      
 16.3698 -      if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb)) {
 16.3699 -        remainder_size = size - nb;
 16.3700 -        unlink(victim, bck, fwd);
 16.3701 -        
 16.3702 -        /* Exhaust */
 16.3703 -        if (remainder_size < MINSIZE)  {
 16.3704 -          set_inuse_bit_at_offset(victim, size);
 16.3705 -          check_malloced_chunk(victim, nb);
 16.3706 -          return chunk2mem(victim);
 16.3707 -        }
 16.3708 -        /* Split */
 16.3709 -        else {
 16.3710 -          remainder = chunk_at_offset(victim, nb);
 16.3711 -          unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
 16.3712 -          remainder->bk = remainder->fd = unsorted_chunks(av);
 16.3713 -          set_head(victim, nb | PREV_INUSE);
 16.3714 -          set_head(remainder, remainder_size | PREV_INUSE);
 16.3715 -          set_foot(remainder, remainder_size);
 16.3716 -          check_malloced_chunk(victim, nb);
 16.3717 -          return chunk2mem(victim);
 16.3718 -        } 
 16.3719 -      }
 16.3720 -    }    
 16.3721 -  }
 16.3722 -
 16.3723 -  /*
 16.3724 -    Search for a chunk by scanning bins, starting with next largest
 16.3725 -    bin. This search is strictly by best-fit; i.e., the smallest
 16.3726 -    (with ties going to approximately the least recently used) chunk
 16.3727 -    that fits is selected.
 16.3728 -    
 16.3729 -    The bitmap avoids needing to check that most blocks are nonempty.
 16.3730 -  */
 16.3731 -    
 16.3732 -  ++idx;
 16.3733 -  bin = bin_at(av,idx);
 16.3734 -  block = idx2block(idx);
 16.3735 -  map = av->binmap[block];
 16.3736 -  bit = idx2bit(idx);
 16.3737 -  
 16.3738 -  for (;;) {
 16.3739 -    
 16.3740 -    /* Skip rest of block if there are no more set bits in this block.  */
 16.3741 -    if (bit > map || bit == 0) {
 16.3742 -      do {
 16.3743 -        if (++block >= BINMAPSIZE)  /* out of bins */
 16.3744 -          goto use_top;
 16.3745 -      } while ( (map = av->binmap[block]) == 0);
 16.3746 -      
 16.3747 -      bin = bin_at(av, (block << BINMAPSHIFT));
 16.3748 -      bit = 1;
 16.3749 -    }
 16.3750 -    
 16.3751 -    /* Advance to bin with set bit. There must be one. */
 16.3752 -    while ((bit & map) == 0) {
 16.3753 -      bin = next_bin(bin);
 16.3754 -      bit <<= 1;
 16.3755 -      assert(bit != 0);
 16.3756 -    }
 16.3757 -    
 16.3758 -    /* Inspect the bin. It is likely to be non-empty */
 16.3759 -    victim = last(bin);
 16.3760 -    
 16.3761 -    /*  If a false alarm (empty bin), clear the bit. */
 16.3762 -    if (victim == bin) {
 16.3763 -      av->binmap[block] = map &= ~bit; /* Write through */
 16.3764 -      bin = next_bin(bin);
 16.3765 -      bit <<= 1;
 16.3766 -    }
 16.3767 -    
 16.3768 -    else {
 16.3769 -      size = chunksize(victim);
 16.3770 -      
 16.3771 -      /*  We know the first chunk in this bin is big enough to use. */
 16.3772 -      assert((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb));
 16.3773 -      
 16.3774 -      remainder_size = size - nb;
 16.3775 -      
 16.3776 -      /* unlink */
 16.3777 -      bck = victim->bk;
 16.3778 -      bin->bk = bck;
 16.3779 -      bck->fd = bin;
 16.3780 -      
 16.3781 -      /* Exhaust */
 16.3782 -      if (remainder_size < MINSIZE) {
 16.3783 -        set_inuse_bit_at_offset(victim, size);
 16.3784 -        check_malloced_chunk(victim, nb);
 16.3785 -        return chunk2mem(victim);
 16.3786 -      }
 16.3787 -      
 16.3788 -      /* Split */
 16.3789 -      else {
 16.3790 -        remainder = chunk_at_offset(victim, nb);
 16.3791 -        
 16.3792 -        unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
 16.3793 -        remainder->bk = remainder->fd = unsorted_chunks(av);
 16.3794 -        /* advertise as last remainder */
 16.3795 -        if (in_smallbin_range(nb)) 
 16.3796 -          av->last_remainder = remainder; 
 16.3797 -        
 16.3798 -        set_head(victim, nb | PREV_INUSE);
 16.3799 -        set_head(remainder, remainder_size | PREV_INUSE);
 16.3800 -        set_foot(remainder, remainder_size);
 16.3801 -        check_malloced_chunk(victim, nb);
 16.3802 -        return chunk2mem(victim);
 16.3803 -      }
 16.3804 -    }
 16.3805 -  }
 16.3806 -
 16.3807 -  use_top:    
 16.3808 -  /*
 16.3809 -    If large enough, split off the chunk bordering the end of memory
 16.3810 -    (held in av->top). Note that this is in accord with the best-fit
 16.3811 -    search rule.  In effect, av->top is treated as larger (and thus
 16.3812 -    less well fitting) than any other available chunk since it can
 16.3813 -    be extended to be as large as necessary (up to system
 16.3814 -    limitations).
 16.3815 -    
 16.3816 -    We require that av->top always exists (i.e., has size >=
 16.3817 -    MINSIZE) after initialization, so if it would otherwise be
 16.3818 -    exhuasted by current request, it is replenished. (The main
 16.3819 -    reason for ensuring it exists is that we may need MINSIZE space
 16.3820 -    to put in fenceposts in sysmalloc.)
 16.3821 -  */
 16.3822 -  
 16.3823 -  victim = av->top;
 16.3824 -  size = chunksize(victim);
 16.3825 -  
 16.3826 -  if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
 16.3827 -    remainder_size = size - nb;
 16.3828 -    remainder = chunk_at_offset(victim, nb);
 16.3829 -    av->top = remainder;
 16.3830 -    set_head(victim, nb | PREV_INUSE);
 16.3831 -    set_head(remainder, remainder_size | PREV_INUSE);
 16.3832 -    
 16.3833 -    check_malloced_chunk(victim, nb);
 16.3834 -    return chunk2mem(victim);
 16.3835 -  }
 16.3836 -  
 16.3837 -  /* 
 16.3838 -     If no space in top, relay to handle system-dependent cases 
 16.3839 -  */
 16.3840 -  return sYSMALLOc(nb, av);    
 16.3841 -}
 16.3842 -
 16.3843 -/*
 16.3844 -  ------------------------------ free ------------------------------
 16.3845 -*/
 16.3846 -
 16.3847 -#if __STD_C
 16.3848 -void fREe(Void_t* mem)
 16.3849 -#else
 16.3850 -void fREe(mem) Void_t* mem;
 16.3851 -#endif
 16.3852 -{
 16.3853 -  mstate av = get_malloc_state();
 16.3854 -
 16.3855 -  mchunkptr       p;           /* chunk corresponding to mem */
 16.3856 -  INTERNAL_SIZE_T size;        /* its size */
 16.3857 -  mfastbinptr*    fb;          /* associated fastbin */
 16.3858 -  mchunkptr       nextchunk;   /* next contiguous chunk */
 16.3859 -  INTERNAL_SIZE_T nextsize;    /* its size */
 16.3860 -  int             nextinuse;   /* true if nextchunk is used */
 16.3861 -  INTERNAL_SIZE_T prevsize;    /* size of previous contiguous chunk */
 16.3862 -  mchunkptr       bck;         /* misc temp for linking */
 16.3863 -  mchunkptr       fwd;         /* misc temp for linking */
 16.3864 -
 16.3865 -  /* free(0) has no effect */
 16.3866 -  if (mem != 0) {
 16.3867 -    p = mem2chunk(mem);
 16.3868 -    size = chunksize(p);
 16.3869 -
 16.3870 -    check_inuse_chunk(p);
 16.3871 -
 16.3872 -    /*
 16.3873 -      If eligible, place chunk on a fastbin so it can be found
 16.3874 -      and used quickly in malloc.
 16.3875 -    */
 16.3876 -
 16.3877 -    if ((CHUNK_SIZE_T)(size) <= (CHUNK_SIZE_T)(av->max_fast)
 16.3878 -
 16.3879 -#if TRIM_FASTBINS
 16.3880 -        /* 
 16.3881 -           If TRIM_FASTBINS set, don't place chunks
 16.3882 -           bordering top into fastbins
 16.3883 -        */
 16.3884 -        && (chunk_at_offset(p, size) != av->top)
 16.3885 -#endif
 16.3886 -        ) {
 16.3887 -
 16.3888 -      set_fastchunks(av);
 16.3889 -      fb = &(av->fastbins[fastbin_index(size)]);
 16.3890 -      p->fd = *fb;
 16.3891 -      *fb = p;
 16.3892 -    }
 16.3893 -
 16.3894 -    /*
 16.3895 -       Consolidate other non-mmapped chunks as they arrive.
 16.3896 -    */
 16.3897 -
 16.3898 -    else if (!chunk_is_mmapped(p)) {
 16.3899 -      set_anychunks(av);
 16.3900 -
 16.3901 -      nextchunk = chunk_at_offset(p, size);
 16.3902 -      nextsize = chunksize(nextchunk);
 16.3903 -
 16.3904 -      /* consolidate backward */
 16.3905 -      if (!prev_inuse(p)) {
 16.3906 -        prevsize = p->prev_size;
 16.3907 -        size += prevsize;
 16.3908 -        p = chunk_at_offset(p, -((long) prevsize));
 16.3909 -        unlink(p, bck, fwd);
 16.3910 -      }
 16.3911 -
 16.3912 -      if (nextchunk != av->top) {
 16.3913 -        /* get and clear inuse bit */
 16.3914 -        nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
 16.3915 -        set_head(nextchunk, nextsize);
 16.3916 -
 16.3917 -        /* consolidate forward */
 16.3918 -        if (!nextinuse) {
 16.3919 -          unlink(nextchunk, bck, fwd);
 16.3920 -          size += nextsize;
 16.3921 -        }
 16.3922 -
 16.3923 -        /*
 16.3924 -          Place the chunk in unsorted chunk list. Chunks are
 16.3925 -          not placed into regular bins until after they have
 16.3926 -          been given one chance to be used in malloc.
 16.3927 -        */
 16.3928 -
 16.3929 -        bck = unsorted_chunks(av);
 16.3930 -        fwd = bck->fd;
 16.3931 -        p->bk = bck;
 16.3932 -        p->fd = fwd;
 16.3933 -        bck->fd = p;
 16.3934 -        fwd->bk = p;
 16.3935 -
 16.3936 -        set_head(p, size | PREV_INUSE);
 16.3937 -        set_foot(p, size);
 16.3938 -        
 16.3939 -        check_free_chunk(p);
 16.3940 -      }
 16.3941 -
 16.3942 -      /*
 16.3943 -         If the chunk borders the current high end of memory,
 16.3944 -         consolidate into top
 16.3945 -      */
 16.3946 -
 16.3947 -      else {
 16.3948 -        size += nextsize;
 16.3949 -        set_head(p, size | PREV_INUSE);
 16.3950 -        av->top = p;
 16.3951 -        check_chunk(p);
 16.3952 -      }
 16.3953 -
 16.3954 -      /*
 16.3955 -        If freeing a large space, consolidate possibly-surrounding
 16.3956 -        chunks. Then, if the total unused topmost memory exceeds trim
 16.3957 -        threshold, ask malloc_trim to reduce top.
 16.3958 -
 16.3959 -        Unless max_fast is 0, we don't know if there are fastbins
 16.3960 -        bordering top, so we cannot tell for sure whether threshold
 16.3961 -        has been reached unless fastbins are consolidated.  But we
 16.3962 -        don't want to consolidate on each free.  As a compromise,
 16.3963 -        consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
 16.3964 -        is reached.
 16.3965 -      */
 16.3966 -
 16.3967 -      if ((CHUNK_SIZE_T)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) { 
 16.3968 -        if (have_fastchunks(av)) 
 16.3969 -          malloc_consolidate(av);
 16.3970 -
 16.3971 -#ifndef MORECORE_CANNOT_TRIM        
 16.3972 -        if ((CHUNK_SIZE_T)(chunksize(av->top)) >= 
 16.3973 -            (CHUNK_SIZE_T)(av->trim_threshold))
 16.3974 -          sYSTRIm(av->top_pad, av);
 16.3975 -#endif
 16.3976 -      }
 16.3977 -
 16.3978 -    }
 16.3979 -    /*
 16.3980 -      If the chunk was allocated via mmap, release via munmap()
 16.3981 -      Note that if HAVE_MMAP is false but chunk_is_mmapped is
 16.3982 -      true, then user must have overwritten memory. There's nothing
 16.3983 -      we can do to catch this error unless DEBUG is set, in which case
 16.3984 -      check_inuse_chunk (above) will have triggered error.
 16.3985 -    */
 16.3986 -
 16.3987 -    else {
 16.3988 -#if HAVE_MMAP
 16.3989 -      int ret;
 16.3990 -      INTERNAL_SIZE_T offset = p->prev_size;
 16.3991 -      av->n_mmaps--;
 16.3992 -      av->mmapped_mem -= (size + offset);
 16.3993 -      ret = munmap((char*)p - offset, size + offset);
 16.3994 -      /* munmap returns non-zero on failure */
 16.3995 -      assert(ret == 0);
 16.3996 -#endif
 16.3997 -    }
 16.3998 -  }
 16.3999 -}
 16.4000 -
 16.4001 -/*
 16.4002 -  ------------------------- malloc_consolidate -------------------------
 16.4003 -
 16.4004 -  malloc_consolidate is a specialized version of free() that tears
 16.4005 -  down chunks held in fastbins.  Free itself cannot be used for this
 16.4006 -  purpose since, among other things, it might place chunks back onto
 16.4007 -  fastbins.  So, instead, we need to use a minor variant of the same
 16.4008 -  code.
 16.4009 -  
 16.4010 -  Also, because this routine needs to be called the first time through
 16.4011 -  malloc anyway, it turns out to be the perfect place to trigger
 16.4012 -  initialization code.
 16.4013 -*/
 16.4014 -
 16.4015 -#if __STD_C
 16.4016 -static void malloc_consolidate(mstate av)
 16.4017 -#else
 16.4018 -static void malloc_consolidate(av) mstate av;
 16.4019 -#endif
 16.4020 -{
 16.4021 -  mfastbinptr*    fb;                 /* current fastbin being consolidated */
 16.4022 -  mfastbinptr*    maxfb;              /* last fastbin (for loop control) */
 16.4023 -  mchunkptr       p;                  /* current chunk being consolidated */
 16.4024 -  mchunkptr       nextp;              /* next chunk to consolidate */
 16.4025 -  mchunkptr       unsorted_bin;       /* bin header */
 16.4026 -  mchunkptr       first_unsorted;     /* chunk to link to */
 16.4027 -
 16.4028 -  /* These have same use as in free() */
 16.4029 -  mchunkptr       nextchunk;
 16.4030 -  INTERNAL_SIZE_T size;
 16.4031 -  INTERNAL_SIZE_T nextsize;
 16.4032 -  INTERNAL_SIZE_T prevsize;
 16.4033 -  int             nextinuse;
 16.4034 -  mchunkptr       bck;
 16.4035 -  mchunkptr       fwd;
 16.4036 -
 16.4037 -  /*
 16.4038 -    If max_fast is 0, we know that av hasn't
 16.4039 -    yet been initialized, in which case do so below
 16.4040 -  */
 16.4041 -
 16.4042 -  if (av->max_fast != 0) {
 16.4043 -    clear_fastchunks(av);
 16.4044 -
 16.4045 -    unsorted_bin = unsorted_chunks(av);
 16.4046 -
 16.4047 -    /*
 16.4048 -      Remove each chunk from fast bin and consolidate it, placing it
 16.4049 -      then in unsorted bin. Among other reasons for doing this,
 16.4050 -      placing in unsorted bin avoids needing to calculate actual bins
 16.4051 -      until malloc is sure that chunks aren't immediately going to be
 16.4052 -      reused anyway.
 16.4053 -    */
 16.4054 -    
 16.4055 -    maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
 16.4056 -    fb = &(av->fastbins[0]);
 16.4057 -    do {
 16.4058 -      if ( (p = *fb) != 0) {
 16.4059 -        *fb = 0;
 16.4060 -        
 16.4061 -        do {
 16.4062 -          check_inuse_chunk(p);
 16.4063 -          nextp = p->fd;
 16.4064 -          
 16.4065 -          /* Slightly streamlined version of consolidation code in free() */
 16.4066 -          size = p->size & ~PREV_INUSE;
 16.4067 -          nextchunk = chunk_at_offset(p, size);
 16.4068 -          nextsize = chunksize(nextchunk);
 16.4069 -          
 16.4070 -          if (!prev_inuse(p)) {
 16.4071 -            prevsize = p->prev_size;
 16.4072 -            size += prevsize;
 16.4073 -            p = chunk_at_offset(p, -((long) prevsize));
 16.4074 -            unlink(p, bck, fwd);
 16.4075 -          }
 16.4076 -          
 16.4077 -          if (nextchunk != av->top) {
 16.4078 -            nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
 16.4079 -            set_head(nextchunk, nextsize);
 16.4080 -            
 16.4081 -            if (!nextinuse) {
 16.4082 -              size += nextsize;
 16.4083 -              unlink(nextchunk, bck, fwd);
 16.4084 -            }
 16.4085 -            
 16.4086 -            first_unsorted = unsorted_bin->fd;
 16.4087 -            unsorted_bin->fd = p;
 16.4088 -            first_unsorted->bk = p;
 16.4089 -            
 16.4090 -            set_head(p, size | PREV_INUSE);
 16.4091 -            p->bk = unsorted_bin;
 16.4092 -            p->fd = first_unsorted;
 16.4093 -            set_foot(p, size);
 16.4094 -          }
 16.4095 -          
 16.4096 -          else {
 16.4097 -            size += nextsize;
 16.4098 -            set_head(p, size | PREV_INUSE);
 16.4099 -            av->top = p;
 16.4100 -          }
 16.4101 -          
 16.4102 -        } while ( (p = nextp) != 0);
 16.4103 -        
 16.4104 -      }
 16.4105 -    } while (fb++ != maxfb);
 16.4106 -  }
 16.4107 -  else {
 16.4108 -    malloc_init_state(av);
 16.4109 -    check_malloc_state();
 16.4110 -  }
 16.4111 -}
 16.4112 -
 16.4113 -/*
 16.4114 -  ------------------------------ realloc ------------------------------
 16.4115 -*/
 16.4116 -
 16.4117 -
 16.4118 -#if __STD_C
 16.4119 -Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
 16.4120 -#else
 16.4121 -Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
 16.4122 -#endif
 16.4123 -{
 16.4124 -  mstate av = get_malloc_state();
 16.4125 -
 16.4126 -  INTERNAL_SIZE_T  nb;              /* padded request size */
 16.4127 -
 16.4128 -  mchunkptr        oldp;            /* chunk corresponding to oldmem */
 16.4129 -  INTERNAL_SIZE_T  oldsize;         /* its size */
 16.4130 -
 16.4131 -  mchunkptr        newp;            /* chunk to return */
 16.4132 -  INTERNAL_SIZE_T  newsize;         /* its size */
 16.4133 -  Void_t*          newmem;          /* corresponding user mem */
 16.4134 -
 16.4135 -  mchunkptr        next;            /* next contiguous chunk after oldp */
 16.4136 -
 16.4137 -  mchunkptr        remainder;       /* extra space at end of newp */
 16.4138 -  CHUNK_SIZE_T     remainder_size;  /* its size */
 16.4139 -
 16.4140 -  mchunkptr        bck;             /* misc temp for linking */
 16.4141 -  mchunkptr        fwd;             /* misc temp for linking */
 16.4142 -
 16.4143 -  CHUNK_SIZE_T     copysize;        /* bytes to copy */
 16.4144 -  unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
 16.4145 -  INTERNAL_SIZE_T* s;               /* copy source */ 
 16.4146 -  INTERNAL_SIZE_T* d;               /* copy destination */
 16.4147 -
 16.4148 -
 16.4149 -#ifdef REALLOC_ZERO_BYTES_FREES
 16.4150 -  if (bytes == 0) {
 16.4151 -    fREe(oldmem);
 16.4152 -    return 0;
 16.4153 -  }
 16.4154 -#endif
 16.4155 -
 16.4156 -  /* realloc of null is supposed to be same as malloc */
 16.4157 -  if (oldmem == 0) return mALLOc(bytes);
 16.4158 -
 16.4159 -  checked_request2size(bytes, nb);
 16.4160 -
 16.4161 -  oldp    = mem2chunk(oldmem);
 16.4162 -  oldsize = chunksize(oldp);
 16.4163 -
 16.4164 -  check_inuse_chunk(oldp);
 16.4165 -
 16.4166 -  if (!chunk_is_mmapped(oldp)) {
 16.4167 -
 16.4168 -    if ((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb)) {
 16.4169 -      /* already big enough; split below */
 16.4170 -      newp = oldp;
 16.4171 -      newsize = oldsize;
 16.4172 -    }
 16.4173 -
 16.4174 -    else {
 16.4175 -      next = chunk_at_offset(oldp, oldsize);
 16.4176 -
 16.4177 -      /* Try to expand forward into top */
 16.4178 -      if (next == av->top &&
 16.4179 -          (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
 16.4180 -          (CHUNK_SIZE_T)(nb + MINSIZE)) {
 16.4181 -        set_head_size(oldp, nb);
 16.4182 -        av->top = chunk_at_offset(oldp, nb);
 16.4183 -        set_head(av->top, (newsize - nb) | PREV_INUSE);
 16.4184 -        return chunk2mem(oldp);
 16.4185 -      }
 16.4186 -      
 16.4187 -      /* Try to expand forward into next chunk;  split off remainder below */
 16.4188 -      else if (next != av->top && 
 16.4189 -               !inuse(next) &&
 16.4190 -               (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
 16.4191 -               (CHUNK_SIZE_T)(nb)) {
 16.4192 -        newp = oldp;
 16.4193 -        unlink(next, bck, fwd);
 16.4194 -      }
 16.4195 -
 16.4196 -      /* allocate, copy, free */
 16.4197 -      else {
 16.4198 -        newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
 16.4199 -        if (newmem == 0)
 16.4200 -          return 0; /* propagate failure */
 16.4201 -      
 16.4202 -        newp = mem2chunk(newmem);
 16.4203 -        newsize = chunksize(newp);
 16.4204 -        
 16.4205 -        /*
 16.4206 -          Avoid copy if newp is next chunk after oldp.
 16.4207 -        */
 16.4208 -        if (newp == next) {
 16.4209 -          newsize += oldsize;
 16.4210 -          newp = oldp;
 16.4211 -        }
 16.4212 -        else {
 16.4213 -          /*
 16.4214 -            Unroll copy of <= 36 bytes (72 if 8byte sizes)
 16.4215 -            We know that contents have an odd number of
 16.4216 -            INTERNAL_SIZE_T-sized words; minimally 3.
 16.4217 -          */
 16.4218 -          
 16.4219 -          copysize = oldsize - SIZE_SZ;
 16.4220 -          s = (INTERNAL_SIZE_T*)(oldmem);
 16.4221 -          d = (INTERNAL_SIZE_T*)(newmem);
 16.4222 -          ncopies = copysize / sizeof(INTERNAL_SIZE_T);
 16.4223 -          assert(ncopies >= 3);
 16.4224 -          
 16.4225 -          if (ncopies > 9)
 16.4226 -            MALLOC_COPY(d, s, copysize);
 16.4227 -          
 16.4228 -          else {
 16.4229 -            *(d+0) = *(s+0);
 16.4230 -            *(d+1) = *(s+1);
 16.4231 -            *(d+2) = *(s+2);
 16.4232 -            if (ncopies > 4) {
 16.4233 -              *(d+3) = *(s+3);
 16.4234 -              *(d+4) = *(s+4);
 16.4235 -              if (ncopies > 6) {
 16.4236 -                *(d+5) = *(s+5);
 16.4237 -                *(d+6) = *(s+6);
 16.4238 -                if (ncopies > 8) {
 16.4239 -                  *(d+7) = *(s+7);
 16.4240 -                  *(d+8) = *(s+8);
 16.4241 -                }
 16.4242 -              }
 16.4243 -            }
 16.4244 -          }
 16.4245 -          
 16.4246 -          fREe(oldmem);
 16.4247 -          check_inuse_chunk(newp);
 16.4248 -          return chunk2mem(newp);
 16.4249 -        }
 16.4250 -      }
 16.4251 -    }
 16.4252 -
 16.4253 -    /* If possible, free extra space in old or extended chunk */
 16.4254 -
 16.4255 -    assert((CHUNK_SIZE_T)(newsize) >= (CHUNK_SIZE_T)(nb));
 16.4256 -
 16.4257 -    remainder_size = newsize - nb;
 16.4258 -
 16.4259 -    if (remainder_size < MINSIZE) { /* not enough extra to split off */
 16.4260 -      set_head_size(newp, newsize);
 16.4261 -      set_inuse_bit_at_offset(newp, newsize);
 16.4262 -    }
 16.4263 -    else { /* split remainder */
 16.4264 -      remainder = chunk_at_offset(newp, nb);
 16.4265 -      set_head_size(newp, nb);
 16.4266 -      set_head(remainder, remainder_size | PREV_INUSE);
 16.4267 -      /* Mark remainder as inuse so free() won't complain */
 16.4268 -      set_inuse_bit_at_offset(remainder, remainder_size);
 16.4269 -      fREe(chunk2mem(remainder)); 
 16.4270 -    }
 16.4271 -
 16.4272 -    check_inuse_chunk(newp);
 16.4273 -    return chunk2mem(newp);
 16.4274 -  }
 16.4275 -
 16.4276 -  /*
 16.4277 -    Handle mmap cases
 16.4278 -  */
 16.4279 -
 16.4280 -  else {
 16.4281 -#if HAVE_MMAP
 16.4282 -
 16.4283 -#if HAVE_MREMAP
 16.4284 -    INTERNAL_SIZE_T offset = oldp->prev_size;
 16.4285 -    size_t pagemask = av->pagesize - 1;
 16.4286 -    char *cp;
 16.4287 -    CHUNK_SIZE_T  sum;
 16.4288 -    
 16.4289 -    /* Note the extra SIZE_SZ overhead */
 16.4290 -    newsize = (nb + offset + SIZE_SZ + pagemask) & ~pagemask;
 16.4291 -
 16.4292 -    /* don't need to remap if still within same page */
 16.4293 -    if (oldsize == newsize - offset) 
 16.4294 -      return oldmem;
 16.4295 -
 16.4296 -    cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
 16.4297 -    
 16.4298 -    if (cp != (char*)MORECORE_FAILURE) {
 16.4299 -
 16.4300 -      newp = (mchunkptr)(cp + offset);
 16.4301 -      set_head(newp, (newsize - offset)|IS_MMAPPED);
 16.4302 -      
 16.4303 -      assert(aligned_OK(chunk2mem(newp)));
 16.4304 -      assert((newp->prev_size == offset));
 16.4305 -      
 16.4306 -      /* update statistics */
 16.4307 -      sum = av->mmapped_mem += newsize - oldsize;
 16.4308 -      if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
 16.4309 -        av->max_mmapped_mem = sum;
 16.4310 -      sum += av->sbrked_mem;
 16.4311 -      if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
 16.4312 -        av->max_total_mem = sum;
 16.4313 -      
 16.4314 -      return chunk2mem(newp);
 16.4315 -    }
 16.4316 -#endif
 16.4317 -
 16.4318 -    /* Note the extra SIZE_SZ overhead. */
 16.4319 -    if ((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb + SIZE_SZ)) 
 16.4320 -      newmem = oldmem; /* do nothing */
 16.4321 -    else {
 16.4322 -      /* Must alloc, copy, free. */
 16.4323 -      newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
 16.4324 -      if (newmem != 0) {
 16.4325 -        MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
 16.4326 -        fREe(oldmem);
 16.4327 -      }
 16.4328 -    }
 16.4329 -    return newmem;
 16.4330 -
 16.4331 -#else 
 16.4332 -    /* If !HAVE_MMAP, but chunk_is_mmapped, user must have overwritten mem */
 16.4333 -    check_malloc_state();
 16.4334 -    MALLOC_FAILURE_ACTION;
 16.4335 -    return 0;
 16.4336 -#endif
 16.4337 -  }
 16.4338 -}
 16.4339 -
 16.4340 -/*
 16.4341 -  ------------------------------ memalign ------------------------------
 16.4342 -*/
 16.4343 -
 16.4344 -#if __STD_C
 16.4345 -Void_t* mEMALIGn(size_t alignment, size_t bytes)
 16.4346 -#else
 16.4347 -Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
 16.4348 -#endif
 16.4349 -{
 16.4350 -  INTERNAL_SIZE_T nb;             /* padded  request size */
 16.4351 -  char*           m;              /* memory returned by malloc call */
 16.4352 -  mchunkptr       p;              /* corresponding chunk */
 16.4353 -  char*           brk;            /* alignment point within p */
 16.4354 -  mchunkptr       newp;           /* chunk to return */
 16.4355 -  INTERNAL_SIZE_T newsize;        /* its size */
 16.4356 -  INTERNAL_SIZE_T leadsize;       /* leading space before alignment point */
 16.4357 -  mchunkptr       remainder;      /* spare room at end to split off */
 16.4358 -  CHUNK_SIZE_T    remainder_size; /* its size */
 16.4359 -  INTERNAL_SIZE_T size;
 16.4360 -
 16.4361 -  /* If need less alignment than we give anyway, just relay to malloc */
 16.4362 -
 16.4363 -  if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
 16.4364 -
 16.4365 -  /* Otherwise, ensure that it is at least a minimum chunk size */
 16.4366 -
 16.4367 -  if (alignment <  MINSIZE) alignment = MINSIZE;
 16.4368 -
 16.4369 -  /* Make sure alignment is power of 2 (in case MINSIZE is not).  */
 16.4370 -  if ((alignment & (alignment - 1)) != 0) {
 16.4371 -    size_t a = MALLOC_ALIGNMENT * 2;
 16.4372 -    while ((CHUNK_SIZE_T)a < (CHUNK_SIZE_T)alignment) a <<= 1;
 16.4373 -    alignment = a;
 16.4374 -  }
 16.4375 -
 16.4376 -  checked_request2size(bytes, nb);
 16.4377 -
 16.4378 -  /*
 16.4379 -    Strategy: find a spot within that chunk that meets the alignment
 16.4380 -    request, and then possibly free the leading and trailing space.
 16.4381 -  */
 16.4382 -
 16.4383 -
 16.4384 -  /* Call malloc with worst case padding to hit alignment. */
 16.4385 -
 16.4386 -  m  = (char*)(mALLOc(nb + alignment + MINSIZE));
 16.4387 -
 16.4388 -  if (m == 0) return 0; /* propagate failure */
 16.4389 -
 16.4390 -  p = mem2chunk(m);
 16.4391 -
 16.4392 -  if ((((PTR_UINT)(m)) % alignment) != 0) { /* misaligned */
 16.4393 -
 16.4394 -    /*
 16.4395 -      Find an aligned spot inside chunk.  Since we need to give back
 16.4396 -      leading space in a chunk of at least MINSIZE, if the first
 16.4397 -      calculation places us at a spot with less than MINSIZE leader,
 16.4398 -      we can move to the next aligned spot -- we've allocated enough
 16.4399 -      total room so that this is always possible.
 16.4400 -    */
 16.4401 -
 16.4402 -    brk = (char*)mem2chunk((PTR_UINT)(((PTR_UINT)(m + alignment - 1)) &
 16.4403 -                           -((signed long) alignment)));
 16.4404 -    if ((CHUNK_SIZE_T)(brk - (char*)(p)) < MINSIZE)
 16.4405 -      brk += alignment;
 16.4406 -
 16.4407 -    newp = (mchunkptr)brk;
 16.4408 -    leadsize = brk - (char*)(p);
 16.4409 -    newsize = chunksize(p) - leadsize;
 16.4410 -
 16.4411 -    /* For mmapped chunks, just adjust offset */
 16.4412 -    if (chunk_is_mmapped(p)) {
 16.4413 -      newp->prev_size = p->prev_size + leadsize;
 16.4414 -      set_head(newp, newsize|IS_MMAPPED);
 16.4415 -      return chunk2mem(newp);
 16.4416 -    }
 16.4417 -
 16.4418 -    /* Otherwise, give back leader, use the rest */
 16.4419 -    set_head(newp, newsize | PREV_INUSE);
 16.4420 -    set_inuse_bit_at_offset(newp, newsize);
 16.4421 -    set_head_size(p, leadsize);
 16.4422 -    fREe(chunk2mem(p));
 16.4423 -    p = newp;
 16.4424 -
 16.4425 -    assert (newsize >= nb &&
 16.4426 -            (((PTR_UINT)(chunk2mem(p))) % alignment) == 0);
 16.4427 -  }
 16.4428 -
 16.4429 -  /* Also give back spare room at the end */
 16.4430 -  if (!chunk_is_mmapped(p)) {
 16.4431 -    size = chunksize(p);
 16.4432 -    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE)) {
 16.4433 -      remainder_size = size - nb;
 16.4434 -      remainder = chunk_at_offset(p, nb);
 16.4435 -      set_head(remainder, remainder_size | PREV_INUSE);
 16.4436 -      set_head_size(p, nb);
 16.4437 -      fREe(chunk2mem(remainder));
 16.4438 -    }
 16.4439 -  }
 16.4440 -
 16.4441 -  check_inuse_chunk(p);
 16.4442 -  return chunk2mem(p);
 16.4443 -}
 16.4444 -
 16.4445 -/*
 16.4446 -  ------------------------------ calloc ------------------------------
 16.4447 -*/
 16.4448 -
 16.4449 -#if __STD_C
 16.4450 -Void_t* cALLOc(size_t n_elements, size_t elem_size)
 16.4451 -#else
 16.4452 -Void_t* cALLOc(n_elements, elem_size) size_t n_elements; size_t elem_size;
 16.4453 -#endif
 16.4454 -{
 16.4455 -  mchunkptr p;
 16.4456 -  CHUNK_SIZE_T  clearsize;
 16.4457 -  CHUNK_SIZE_T  nclears;
 16.4458 -  INTERNAL_SIZE_T* d;
 16.4459 -
 16.4460 -  Void_t* mem = mALLOc(n_elements * elem_size);
 16.4461 -
 16.4462 -  if (mem != 0) {
 16.4463 -    p = mem2chunk(mem);
 16.4464 -
 16.4465 -    if (!chunk_is_mmapped(p))
 16.4466 -    {  
 16.4467 -      /*
 16.4468 -        Unroll clear of <= 36 bytes (72 if 8byte sizes)
 16.4469 -        We know that contents have an odd number of
 16.4470 -        INTERNAL_SIZE_T-sized words; minimally 3.
 16.4471 -      */
 16.4472 -
 16.4473 -      d = (INTERNAL_SIZE_T*)mem;
 16.4474 -      clearsize = chunksize(p) - SIZE_SZ;
 16.4475 -      nclears = clearsize / sizeof(INTERNAL_SIZE_T);
 16.4476 -      assert(nclears >= 3);
 16.4477 -
 16.4478 -      if (nclears > 9)
 16.4479 -        MALLOC_ZERO(d, clearsize);
 16.4480 -
 16.4481 -      else {
 16.4482 -        *(d+0) = 0;
 16.4483 -        *(d+1) = 0;
 16.4484 -        *(d+2) = 0;
 16.4485 -        if (nclears > 4) {
 16.4486 -          *(d+3) = 0;
 16.4487 -          *(d+4) = 0;
 16.4488 -          if (nclears > 6) {
 16.4489 -            *(d+5) = 0;
 16.4490 -            *(d+6) = 0;
 16.4491 -            if (nclears > 8) {
 16.4492 -              *(d+7) = 0;
 16.4493 -              *(d+8) = 0;
 16.4494 -            }
 16.4495 -          }
 16.4496 -        }
 16.4497 -      }
 16.4498 -    }
 16.4499 -#if ! MMAP_CLEARS
 16.4500 -    else
 16.4501 -    {
 16.4502 -      d = (INTERNAL_SIZE_T*)mem;
 16.4503 -      /*
 16.4504 -        Note the additional SIZE_SZ
 16.4505 -      */
 16.4506 -      clearsize = chunksize(p) - 2*SIZE_SZ;
 16.4507 -      MALLOC_ZERO(d, clearsize);
 16.4508 -    }
 16.4509 -#endif
 16.4510 -  }
 16.4511 -  return mem;
 16.4512 -}
 16.4513 -
 16.4514 -/*
 16.4515 -  ------------------------------ cfree ------------------------------
 16.4516 -*/
 16.4517 -
 16.4518 -#if __STD_C
 16.4519 -void cFREe(Void_t *mem)
 16.4520 -#else
 16.4521 -void cFREe(mem) Void_t *mem;
 16.4522 -#endif
 16.4523 -{
 16.4524 -  fREe(mem);
 16.4525 -}
 16.4526 -
 16.4527 -/*
 16.4528 -  ------------------------- independent_calloc -------------------------
 16.4529 -*/
 16.4530 -
 16.4531 -#if __STD_C
 16.4532 -Void_t** iCALLOc(size_t n_elements, size_t elem_size, Void_t* chunks[])
 16.4533 -#else
 16.4534 -Void_t** iCALLOc(n_elements, elem_size, chunks) size_t n_elements; size_t elem_size; Void_t* chunks[];
 16.4535 -#endif
 16.4536 -{
 16.4537 -  size_t sz = elem_size; /* serves as 1-element array */
 16.4538 -  /* opts arg of 3 means all elements are same size, and should be cleared */
 16.4539 -  return iALLOc(n_elements, &sz, 3, chunks);
 16.4540 -}
 16.4541 -
 16.4542 -/*
 16.4543 -  ------------------------- independent_comalloc -------------------------
 16.4544 -*/
 16.4545 -
 16.4546 -#if __STD_C
 16.4547 -Void_t** iCOMALLOc(size_t n_elements, size_t sizes[], Void_t* chunks[])
 16.4548 -#else
 16.4549 -Void_t** iCOMALLOc(n_elements, sizes, chunks) size_t n_elements; size_t sizes[]; Void_t* chunks[];
 16.4550 -#endif
 16.4551 -{
 16.4552 -  return iALLOc(n_elements, sizes, 0, chunks);
 16.4553 -}
 16.4554 -
 16.4555 -
 16.4556 -/*
 16.4557 -  ------------------------------ ialloc ------------------------------
 16.4558 -  ialloc provides common support for independent_X routines, handling all of
 16.4559 -  the combinations that can result.
 16.4560 -
 16.4561 -  The opts arg has:
 16.4562 -    bit 0 set if all elements are same size (using sizes[0])
 16.4563 -    bit 1 set if elements should be zeroed
 16.4564 -*/
 16.4565 -
 16.4566 -
 16.4567 -#if __STD_C
 16.4568 -static Void_t** iALLOc(size_t n_elements, 
 16.4569 -                       size_t* sizes,  
 16.4570 -                       int opts,
 16.4571 -                       Void_t* chunks[])
 16.4572 -#else
 16.4573 -static Void_t** iALLOc(n_elements, sizes, opts, chunks) size_t n_elements; size_t* sizes; int opts; Void_t* chunks[];
 16.4574 -#endif
 16.4575 -{
 16.4576 -  mstate av = get_malloc_state();
 16.4577 -  INTERNAL_SIZE_T element_size;   /* chunksize of each element, if all same */
 16.4578 -  INTERNAL_SIZE_T contents_size;  /* total size of elements */
 16.4579 -  INTERNAL_SIZE_T array_size;     /* request size of pointer array */
 16.4580 -  Void_t*         mem;            /* malloced aggregate space */
 16.4581 -  mchunkptr       p;              /* corresponding chunk */
 16.4582 -  INTERNAL_SIZE_T remainder_size; /* remaining bytes while splitting */
 16.4583 -  Void_t**        marray;         /* either "chunks" or malloced ptr array */
 16.4584 -  mchunkptr       array_chunk;    /* chunk for malloced ptr array */
 16.4585 -  int             mmx;            /* to disable mmap */
 16.4586 -  INTERNAL_SIZE_T size;           
 16.4587 -  size_t          i;
 16.4588 -
 16.4589 -  /* Ensure initialization */
 16.4590 -  if (av->max_fast == 0) malloc_consolidate(av);
 16.4591 -
 16.4592 -  /* compute array length, if needed */
 16.4593 -  if (chunks != 0) {
 16.4594 -    if (n_elements == 0)
 16.4595 -      return chunks; /* nothing to do */
 16.4596 -    marray = chunks;
 16.4597 -    array_size = 0;
 16.4598 -  }
 16.4599 -  else {
 16.4600 -    /* if empty req, must still return chunk representing empty array */
 16.4601 -    if (n_elements == 0) 
 16.4602 -      return (Void_t**) mALLOc(0);
 16.4603 -    marray = 0;
 16.4604 -    array_size = request2size(n_elements * (sizeof(Void_t*)));
 16.4605 -  }
 16.4606 -
 16.4607 -  /* compute total element size */
 16.4608 -  if (opts & 0x1) { /* all-same-size */
 16.4609 -    element_size = request2size(*sizes);
 16.4610 -    contents_size = n_elements * element_size;
 16.4611 -  }
 16.4612 -  else { /* add up all the sizes */
 16.4613 -    element_size = 0;
 16.4614 -    contents_size = 0;
 16.4615 -    for (i = 0; i != n_elements; ++i) 
 16.4616 -      contents_size += request2size(sizes[i]);     
 16.4617 -  }
 16.4618 -
 16.4619 -  /* subtract out alignment bytes from total to minimize overallocation */
 16.4620 -  size = contents_size + array_size - MALLOC_ALIGN_MASK;
 16.4621 -  
 16.4622 -  /* 
 16.4623 -     Allocate the aggregate chunk.
 16.4624 -     But first disable mmap so malloc won't use it, since
 16.4625 -     we would not be able to later free/realloc space internal
 16.4626 -     to a segregated mmap region.
 16.4627 - */
 16.4628 -  mmx = av->n_mmaps_max;   /* disable mmap */
 16.4629 -  av->n_mmaps_max = 0;
 16.4630 -  mem = mALLOc(size);
 16.4631 -  av->n_mmaps_max = mmx;   /* reset mmap */
 16.4632 -  if (mem == 0) 
 16.4633 -    return 0;
 16.4634 -
 16.4635 -  p = mem2chunk(mem);
 16.4636 -  assert(!chunk_is_mmapped(p)); 
 16.4637 -  remainder_size = chunksize(p);
 16.4638 -
 16.4639 -  if (opts & 0x2) {       /* optionally clear the elements */
 16.4640 -    MALLOC_ZERO(mem, remainder_size - SIZE_SZ - array_size);
 16.4641 -  }
 16.4642 -
 16.4643 -  /* If not provided, allocate the pointer array as final part of chunk */
 16.4644 -  if (marray == 0) {
 16.4645 -    array_chunk = chunk_at_offset(p, contents_size);
 16.4646 -    marray = (Void_t**) (chunk2mem(array_chunk));
 16.4647 -    set_head(array_chunk, (remainder_size - contents_size) | PREV_INUSE);
 16.4648 -    remainder_size = contents_size;
 16.4649 -  }
 16.4650 -
 16.4651 -  /* split out elements */
 16.4652 -  for (i = 0; ; ++i) {
 16.4653 -    marray[i] = chunk2mem(p);
 16.4654 -    if (i != n_elements-1) {
 16.4655 -      if (element_size != 0) 
 16.4656 -        size = element_size;
 16.4657 -      else
 16.4658 -        size = request2size(sizes[i]);          
 16.4659 -      remainder_size -= size;
 16.4660 -      set_head(p, size | PREV_INUSE);
 16.4661 -      p = chunk_at_offset(p, size);
 16.4662 -    }
 16.4663 -    else { /* the final element absorbs any overallocation slop */
 16.4664 -      set_head(p, remainder_size | PREV_INUSE);
 16.4665 -      break;
 16.4666 -    }
 16.4667 -  }
 16.4668 -
 16.4669 -#if DEBUG
 16.4670 -  if (marray != chunks) {
 16.4671 -    /* final element must have exactly exhausted chunk */
 16.4672 -    if (element_size != 0) 
 16.4673 -      assert(remainder_size == element_size);
 16.4674 -    else
 16.4675 -      assert(remainder_size == request2size(sizes[i]));
 16.4676 -    check_inuse_chunk(mem2chunk(marray));
 16.4677 -  }
 16.4678 -
 16.4679 -  for (i = 0; i != n_elements; ++i)
 16.4680 -    check_inuse_chunk(mem2chunk(marray[i]));
 16.4681 -#endif
 16.4682 -
 16.4683 -  return marray;
 16.4684 -}
 16.4685 -
 16.4686 -
 16.4687 -/*
 16.4688 -  ------------------------------ valloc ------------------------------
 16.4689 -*/
 16.4690 -
 16.4691 -#if __STD_C
 16.4692 -Void_t* vALLOc(size_t bytes)
 16.4693 -#else
 16.4694 -Void_t* vALLOc(bytes) size_t bytes;
 16.4695 -#endif
 16.4696 -{
 16.4697 -  /* Ensure initialization */
 16.4698 -  mstate av = get_malloc_state();
 16.4699 -  if (av->max_fast == 0) malloc_consolidate(av);
 16.4700 -  return mEMALIGn(av->pagesize, bytes);
 16.4701 -}
 16.4702 -
 16.4703 -/*
 16.4704 -  ------------------------------ pvalloc ------------------------------
 16.4705 -*/
 16.4706 -
 16.4707 -
 16.4708 -#if __STD_C
 16.4709 -Void_t* pVALLOc(size_t bytes)
 16.4710 -#else
 16.4711 -Void_t* pVALLOc(bytes) size_t bytes;
 16.4712 -#endif
 16.4713 -{
 16.4714 -  mstate av = get_malloc_state();
 16.4715 -  size_t pagesz;
 16.4716 -
 16.4717 -  /* Ensure initialization */
 16.4718 -  if (av->max_fast == 0) malloc_consolidate(av);
 16.4719 -  pagesz = av->pagesize;
 16.4720 -  return mEMALIGn(pagesz, (bytes + pagesz - 1) & ~(pagesz - 1));
 16.4721 -}
 16.4722 -   
 16.4723 -
 16.4724 -/*
 16.4725 -  ------------------------------ malloc_trim ------------------------------
 16.4726 -*/
 16.4727 -
 16.4728 -#if __STD_C
 16.4729 -int mTRIm(size_t pad)
 16.4730 -#else
 16.4731 -int mTRIm(pad) size_t pad;
 16.4732 -#endif
 16.4733 -{
 16.4734 -  mstate av = get_malloc_state();
 16.4735 -  /* Ensure initialization/consolidation */
 16.4736 -  malloc_consolidate(av);
 16.4737 -
 16.4738 -#ifndef MORECORE_CANNOT_TRIM        
 16.4739 -  return sYSTRIm(pad, av);
 16.4740 -#else
 16.4741 -  return 0;
 16.4742 -#endif
 16.4743 -}
 16.4744 -
 16.4745 -
 16.4746 -/*
 16.4747 -  ------------------------- malloc_usable_size -------------------------
 16.4748 -*/
 16.4749 -
 16.4750 -#if __STD_C
 16.4751 -size_t mUSABLe(Void_t* mem)
 16.4752 -#else
 16.4753 -size_t mUSABLe(mem) Void_t* mem;
 16.4754 -#endif
 16.4755 -{
 16.4756 -  mchunkptr p;
 16.4757 -  if (mem != 0) {
 16.4758 -    p = mem2chunk(mem);
 16.4759 -    if (chunk_is_mmapped(p))
 16.4760 -      return chunksize(p) - 2*SIZE_SZ;
 16.4761 -    else if (inuse(p))
 16.4762 -      return chunksize(p) - SIZE_SZ;
 16.4763 -  }
 16.4764 -  return 0;
 16.4765 -}
 16.4766 -
 16.4767 -/*
 16.4768 -  ------------------------------ mallinfo ------------------------------
 16.4769 -*/
 16.4770 -
 16.4771 -struct mallinfo mALLINFo()
 16.4772 -{
 16.4773 -  mstate av = get_malloc_state();
 16.4774 -  struct mallinfo mi;
 16.4775 -  int i;
 16.4776 -  mbinptr b;
 16.4777 -  mchunkptr p;
 16.4778 -  INTERNAL_SIZE_T avail;
 16.4779 -  INTERNAL_SIZE_T fastavail;
 16.4780 -  int nblocks;
 16.4781 -  int nfastblocks;
 16.4782 -
 16.4783 -  /* Ensure initialization */
 16.4784 -  if (av->top == 0)  malloc_consolidate(av);
 16.4785 -
 16.4786 -  check_malloc_state();
 16.4787 -
 16.4788 -  /* Account for top */
 16.4789 -  avail = chunksize(av->top);
 16.4790 -  nblocks = 1;  /* top always exists */
 16.4791 -
 16.4792 -  /* traverse fastbins */
 16.4793 -  nfastblocks = 0;
 16.4794 -  fastavail = 0;
 16.4795 -
 16.4796 -  for (i = 0; i < NFASTBINS; ++i) {
 16.4797 -    for (p = av->fastbins[i]; p != 0; p = p->fd) {
 16.4798 -      ++nfastblocks;
 16.4799 -      fastavail += chunksize(p);
 16.4800 -    }
 16.4801 -  }
 16.4802 -
 16.4803 -  avail += fastavail;
 16.4804 -
 16.4805 -  /* traverse regular bins */
 16.4806 -  for (i = 1; i < NBINS; ++i) {
 16.4807 -    b = bin_at(av, i);
 16.4808 -    for (p = last(b); p != b; p = p->bk) {
 16.4809 -      ++nblocks;
 16.4810 -      avail += chunksize(p);
 16.4811 -    }
 16.4812 -  }
 16.4813 -
 16.4814 -  mi.smblks = nfastblocks;
 16.4815 -  mi.ordblks = nblocks;
 16.4816 -  mi.fordblks = avail;
 16.4817 -  mi.uordblks = av->sbrked_mem - avail;
 16.4818 -  mi.arena = av->sbrked_mem;
 16.4819 -  mi.hblks = av->n_mmaps;
 16.4820 -  mi.hblkhd = av->mmapped_mem;
 16.4821 -  mi.fsmblks = fastavail;
 16.4822 -  mi.keepcost = chunksize(av->top);
 16.4823 -  mi.usmblks = av->max_total_mem;
 16.4824 -  return mi;
 16.4825 -}
 16.4826 -
 16.4827 -/*
 16.4828 -  ------------------------------ malloc_stats ------------------------------
 16.4829 -*/
 16.4830 -
 16.4831 -void mSTATs()
 16.4832 -{
 16.4833 -  struct mallinfo mi = mALLINFo();
 16.4834 -
 16.4835 -#ifdef WIN32
 16.4836 -  {
 16.4837 -    CHUNK_SIZE_T  free, reserved, committed;
 16.4838 -    vminfo (&free, &reserved, &committed);
 16.4839 -    fprintf(stderr, "free bytes       = %10lu\n", 
 16.4840 -            free);
 16.4841 -    fprintf(stderr, "reserved bytes   = %10lu\n", 
 16.4842 -            reserved);
 16.4843 -    fprintf(stderr, "committed bytes  = %10lu\n", 
 16.4844 -            committed);
 16.4845 -  }
 16.4846 -#endif
 16.4847 -
 16.4848 -/* RN XXX  */
 16.4849 -  printf("max system bytes = %10lu\n",
 16.4850 -          (CHUNK_SIZE_T)(mi.usmblks));
 16.4851 -  printf("system bytes     = %10lu\n",
 16.4852 -          (CHUNK_SIZE_T)(mi.arena + mi.hblkhd));
 16.4853 -  printf("in use bytes     = %10lu\n",
 16.4854 -          (CHUNK_SIZE_T)(mi.uordblks + mi.hblkhd));
 16.4855 -
 16.4856 -#ifdef WIN32 
 16.4857 -  {
 16.4858 -    CHUNK_SIZE_T  kernel, user;
 16.4859 -    if (cpuinfo (TRUE, &kernel, &user)) {
 16.4860 -      fprintf(stderr, "kernel ms        = %10lu\n", 
 16.4861 -              kernel);
 16.4862 -      fprintf(stderr, "user ms          = %10lu\n", 
 16.4863 -              user);
 16.4864 -    }
 16.4865 -  }
 16.4866 -#endif
 16.4867 -}
 16.4868 -
 16.4869 -
 16.4870 -/*
 16.4871 -  ------------------------------ mallopt ------------------------------
 16.4872 -*/
 16.4873 -
 16.4874 -#if __STD_C
 16.4875 -int mALLOPt(int param_number, int value)
 16.4876 -#else
 16.4877 -int mALLOPt(param_number, value) int param_number; int value;
 16.4878 -#endif
 16.4879 -{
 16.4880 -  mstate av = get_malloc_state();
 16.4881 -  /* Ensure initialization/consolidation */
 16.4882 -  malloc_consolidate(av);
 16.4883 -
 16.4884 -  switch(param_number) {
 16.4885 -  case M_MXFAST:
 16.4886 -    if (value >= 0 && value <= MAX_FAST_SIZE) {
 16.4887 -      set_max_fast(av, value);
 16.4888 -      return 1;
 16.4889 -    }
 16.4890 -    else
 16.4891 -      return 0;
 16.4892 -
 16.4893 -  case M_TRIM_THRESHOLD:
 16.4894 -    av->trim_threshold = value;
 16.4895 -    return 1;
 16.4896 -
 16.4897 -  case M_TOP_PAD:
 16.4898 -    av->top_pad = value;
 16.4899 -    return 1;
 16.4900 -
 16.4901 -  case M_MMAP_THRESHOLD:
 16.4902 -    av->mmap_threshold = value;
 16.4903 -    return 1;
 16.4904 -
 16.4905 -  case M_MMAP_MAX:
 16.4906 -#if !HAVE_MMAP
 16.4907 -    if (value != 0)
 16.4908 -      return 0;
 16.4909 -#endif
 16.4910 -    av->n_mmaps_max = value;
 16.4911 -    return 1;
 16.4912 -
 16.4913 -  default:
 16.4914 -    return 0;
 16.4915 -  }
 16.4916 -}
 16.4917 -
 16.4918 -
 16.4919 -/* 
 16.4920 -  -------------------- Alternative MORECORE functions --------------------
 16.4921 -*/
 16.4922 -
 16.4923 -
 16.4924 -/*
 16.4925 -  General Requirements for MORECORE.
 16.4926 -
 16.4927 -  The MORECORE function must have the following properties:
 16.4928 -
 16.4929 -  If MORECORE_CONTIGUOUS is false:
 16.4930 -
 16.4931 -    * MORECORE must allocate in multiples of pagesize. It will
 16.4932 -      only be called with arguments that are multiples of pagesize.
 16.4933 -
 16.4934 -    * MORECORE(0) must return an address that is at least 
 16.4935 -      MALLOC_ALIGNMENT aligned. (Page-aligning always suffices.)
 16.4936 -
 16.4937 -  else (i.e. If MORECORE_CONTIGUOUS is true):
 16.4938 -
 16.4939 -    * Consecutive calls to MORECORE with positive arguments
 16.4940 -      return increasing addresses, indicating that space has been
 16.4941 -      contiguously extended. 
 16.4942 -
 16.4943 -    * MORECORE need not allocate in multiples of pagesize.
 16.4944 -      Calls to MORECORE need not have args of multiples of pagesize.
 16.4945 -
 16.4946 -    * MORECORE need not page-align.
 16.4947 -
 16.4948 -  In either case:
 16.4949 -
 16.4950 -    * MORECORE may allocate more memory than requested. (Or even less,
 16.4951 -      but this will generally result in a malloc failure.)
 16.4952 -
 16.4953 -    * MORECORE must not allocate memory when given argument zero, but
 16.4954 -      instead return one past the end address of memory from previous
 16.4955 -      nonzero call. This malloc does NOT call MORECORE(0)
 16.4956 -      until at least one call with positive arguments is made, so
 16.4957 -      the initial value returned is not important.
 16.4958 -
 16.4959 -    * Even though consecutive calls to MORECORE need not return contiguous
 16.4960 -      addresses, it must be OK for malloc'ed chunks to span multiple
 16.4961 -      regions in those cases where they do happen to be contiguous.
 16.4962 -
 16.4963 -    * MORECORE need not handle negative arguments -- it may instead
 16.4964 -      just return MORECORE_FAILURE when given negative arguments.
 16.4965 -      Negative arguments are always multiples of pagesize. MORECORE
 16.4966 -      must not misinterpret negative args as large positive unsigned
 16.4967 -      args. You can suppress all such calls from even occurring by defining
 16.4968 -      MORECORE_CANNOT_TRIM,
 16.4969 -
 16.4970 -  There is some variation across systems about the type of the
 16.4971 -  argument to sbrk/MORECORE. If size_t is unsigned, then it cannot
 16.4972 -  actually be size_t, because sbrk supports negative args, so it is
 16.4973 -  normally the signed type of the same width as size_t (sometimes
 16.4974 -  declared as "intptr_t", and sometimes "ptrdiff_t").  It doesn't much
 16.4975 -  matter though. Internally, we use "long" as arguments, which should
 16.4976 -  work across all reasonable possibilities.
 16.4977 -
 16.4978 -  Additionally, if MORECORE ever returns failure for a positive
 16.4979 -  request, and HAVE_MMAP is true, then mmap is used as a noncontiguous
 16.4980 -  system allocator. This is a useful backup strategy for systems with
 16.4981 -  holes in address spaces -- in this case sbrk cannot contiguously
 16.4982 -  expand the heap, but mmap may be able to map noncontiguous space.
 16.4983 -
 16.4984 -  If you'd like mmap to ALWAYS be used, you can define MORECORE to be
 16.4985 -  a function that always returns MORECORE_FAILURE.
 16.4986 -
 16.4987 -  Malloc only has limited ability to detect failures of MORECORE
 16.4988 -  to supply contiguous space when it says it can. In particular,
 16.4989 -  multithreaded programs that do not use locks may result in
 16.4990 -  rece conditions across calls to MORECORE that result in gaps
 16.4991 -  that cannot be detected as such, and subsequent corruption.
 16.4992 -
 16.4993 -  If you are using this malloc with something other than sbrk (or its
 16.4994 -  emulation) to supply memory regions, you probably want to set
 16.4995 -  MORECORE_CONTIGUOUS as false.  As an example, here is a custom
 16.4996 -  allocator kindly contributed for pre-OSX macOS.  It uses virtually
 16.4997 -  but not necessarily physically contiguous non-paged memory (locked
 16.4998 -  in, present and won't get swapped out).  You can use it by
 16.4999 -  uncommenting this section, adding some #includes, and setting up the
 16.5000 -  appropriate defines above:
 16.5001 -
 16.5002 -      #define MORECORE osMoreCore
 16.5003 -      #define MORECORE_CONTIGUOUS 0
 16.5004 -
 16.5005 -  There is also a shutdown routine that should somehow be called for
 16.5006 -  cleanup upon program exit.
 16.5007 -
 16.5008 -  #define MAX_POOL_ENTRIES 100
 16.5009 -  #define MINIMUM_MORECORE_SIZE  (64 * 1024)
 16.5010 -  static int next_os_pool;
 16.5011 -  void *our_os_pools[MAX_POOL_ENTRIES];
 16.5012 -
 16.5013 -  void *osMoreCore(int size)
 16.5014 -  {
 16.5015 -    void *ptr = 0;
 16.5016 -    static void *sbrk_top = 0;
 16.5017 -
 16.5018 -    if (size > 0)
 16.5019 -    {
 16.5020 -      if (size < MINIMUM_MORECORE_SIZE)
 16.5021 -         size = MINIMUM_MORECORE_SIZE;
 16.5022 -      if (CurrentExecutionLevel() == kTaskLevel)
 16.5023 -         ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0);
 16.5024 -      if (ptr == 0)
 16.5025 -      {
 16.5026 -        return (void *) MORECORE_FAILURE;
 16.5027 -      }
 16.5028 -      // save ptrs so they can be freed during cleanup
 16.5029 -      our_os_pools[next_os_pool] = ptr;
 16.5030 -      next_os_pool++;
 16.5031 -      ptr = (void *) ((((CHUNK_SIZE_T) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK);
 16.5032 -      sbrk_top = (char *) ptr + size;
 16.5033 -      return ptr;
 16.5034 -    }
 16.5035 -    else if (size < 0)
 16.5036 -    {
 16.5037 -      // we don't currently support shrink behavior
 16.5038 -      return (void *) MORECORE_FAILURE;
 16.5039 -    }
 16.5040 -    else
 16.5041 -    {
 16.5042 -      return sbrk_top;
 16.5043 -    }
 16.5044 -  }
 16.5045 -
 16.5046 -  // cleanup any allocated memory pools
 16.5047 -  // called as last thing before shutting down driver
 16.5048 -
 16.5049 -  void osCleanupMem(void)
 16.5050 -  {
 16.5051 -    void **ptr;
 16.5052 -
 16.5053 -    for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++)
 16.5054 -      if (*ptr)
 16.5055 -      {
 16.5056 -         PoolDeallocate(*ptr);
 16.5057 -         *ptr = 0;
 16.5058 -      }
 16.5059 -  }
 16.5060 -
 16.5061 -*/
 16.5062 -
 16.5063 -
 16.5064 -/* 
 16.5065 -  -------------------------------------------------------------- 
 16.5066 -
 16.5067 -  Emulation of sbrk for win32. 
 16.5068 -  Donated by J. Walter <Walter@GeNeSys-e.de>.
 16.5069 -  For additional information about this code, and malloc on Win32, see 
 16.5070 -     http://www.genesys-e.de/jwalter/
 16.5071 -*/
 16.5072 -
 16.5073 -
 16.5074 -#ifdef WIN32
 16.5075 -
 16.5076 -#ifdef _DEBUG
 16.5077 -/* #define TRACE */
 16.5078 -#endif
 16.5079 -
 16.5080 -/* Support for USE_MALLOC_LOCK */
 16.5081 -#ifdef USE_MALLOC_LOCK
 16.5082 -
 16.5083 -/* Wait for spin lock */
 16.5084 -static int slwait (int *sl) {
 16.5085 -    while (InterlockedCompareExchange ((void **) sl, (void *) 1, (void *) 0) != 0) 
 16.5086 -	    Sleep (0);
 16.5087 -    return 0;
 16.5088 -}
 16.5089 -
 16.5090 -/* Release spin lock */
 16.5091 -static int slrelease (int *sl) {
 16.5092 -    InterlockedExchange (sl, 0);
 16.5093 -    return 0;
 16.5094 -}
 16.5095 -
 16.5096 -#ifdef NEEDED
 16.5097 -/* Spin lock for emulation code */
 16.5098 -static int g_sl;
 16.5099 -#endif
 16.5100 -
 16.5101 -#endif /* USE_MALLOC_LOCK */
 16.5102 -
 16.5103 -/* getpagesize for windows */
 16.5104 -static long getpagesize (void) {
 16.5105 -    static long g_pagesize = 0;
 16.5106 -    if (! g_pagesize) {
 16.5107 -        SYSTEM_INFO system_info;
 16.5108 -        GetSystemInfo (&system_info);
 16.5109 -        g_pagesize = system_info.dwPageSize;
 16.5110 -    }
 16.5111 -    return g_pagesize;
 16.5112 -}
 16.5113 -static long getregionsize (void) {
 16.5114 -    static long g_regionsize = 0;
 16.5115 -    if (! g_regionsize) {
 16.5116 -        SYSTEM_INFO system_info;
 16.5117 -        GetSystemInfo (&system_info);
 16.5118 -        g_regionsize = system_info.dwAllocationGranularity;
 16.5119 -    }
 16.5120 -    return g_regionsize;
 16.5121 -}
 16.5122 -
 16.5123 -/* A region list entry */
 16.5124 -typedef struct _region_list_entry {
 16.5125 -    void *top_allocated;
 16.5126 -    void *top_committed;
 16.5127 -    void *top_reserved;
 16.5128 -    long reserve_size;
 16.5129 -    struct _region_list_entry *previous;
 16.5130 -} region_list_entry;
 16.5131 -
 16.5132 -/* Allocate and link a region entry in the region list */
 16.5133 -static int region_list_append (region_list_entry **last, void *base_reserved, long reserve_size) {
 16.5134 -    region_list_entry *next = HeapAlloc (GetProcessHeap (), 0, sizeof (region_list_entry));
 16.5135 -    if (! next)
 16.5136 -        return FALSE;
 16.5137 -    next->top_allocated = (char *) base_reserved;
 16.5138 -    next->top_committed = (char *) base_reserved;
 16.5139 -    next->top_reserved = (char *) base_reserved + reserve_size;
 16.5140 -    next->reserve_size = reserve_size;
 16.5141 -    next->previous = *last;
 16.5142 -    *last = next;
 16.5143 -    return TRUE;
 16.5144 -}
 16.5145 -/* Free and unlink the last region entry from the region list */
 16.5146 -static int region_list_remove (region_list_entry **last) {
 16.5147 -    region_list_entry *previous = (*last)->previous;
 16.5148 -    if (! HeapFree (GetProcessHeap (), sizeof (region_list_entry), *last))
 16.5149 -        return FALSE;
 16.5150 -    *last = previous;
 16.5151 -    return TRUE;
 16.5152 -}
 16.5153 -
 16.5154 -#define CEIL(size,to)	(((size)+(to)-1)&~((to)-1))
 16.5155 -#define FLOOR(size,to)	((size)&~((to)-1))
 16.5156 -
 16.5157 -#define SBRK_SCALE  0
 16.5158 -/* #define SBRK_SCALE  1 */
 16.5159 -/* #define SBRK_SCALE  2 */
 16.5160 -/* #define SBRK_SCALE  4  */
 16.5161 -
 16.5162 -/* sbrk for windows */
 16.5163 -static void *sbrk (long size) {
 16.5164 -    static long g_pagesize, g_my_pagesize;
 16.5165 -    static long g_regionsize, g_my_regionsize;
 16.5166 -    static region_list_entry *g_last;
 16.5167 -    void *result = (void *) MORECORE_FAILURE;
 16.5168 -#ifdef TRACE
 16.5169 -    printf ("sbrk %d\n", size);
 16.5170 -#endif
 16.5171 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5172 -    /* Wait for spin lock */
 16.5173 -    slwait (&g_sl);
 16.5174 -#endif
 16.5175 -    /* First time initialization */
 16.5176 -    if (! g_pagesize) {
 16.5177 -        g_pagesize = getpagesize ();
 16.5178 -        g_my_pagesize = g_pagesize << SBRK_SCALE;
 16.5179 -    }
 16.5180 -    if (! g_regionsize) {
 16.5181 -        g_regionsize = getregionsize ();
 16.5182 -        g_my_regionsize = g_regionsize << SBRK_SCALE;
 16.5183 -    }
 16.5184 -    if (! g_last) {
 16.5185 -        if (! region_list_append (&g_last, 0, 0)) 
 16.5186 -           goto sbrk_exit;
 16.5187 -    }
 16.5188 -    /* Assert invariants */
 16.5189 -    assert (g_last);
 16.5190 -    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_allocated &&
 16.5191 -            g_last->top_allocated <= g_last->top_committed);
 16.5192 -    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_committed &&
 16.5193 -            g_last->top_committed <= g_last->top_reserved &&
 16.5194 -            (unsigned) g_last->top_committed % g_pagesize == 0);
 16.5195 -    assert ((unsigned) g_last->top_reserved % g_regionsize == 0);
 16.5196 -    assert ((unsigned) g_last->reserve_size % g_regionsize == 0);
 16.5197 -    /* Allocation requested? */
 16.5198 -    if (size >= 0) {
 16.5199 -        /* Allocation size is the requested size */
 16.5200 -        long allocate_size = size;
 16.5201 -        /* Compute the size to commit */
 16.5202 -        long to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
 16.5203 -        /* Do we reach the commit limit? */
 16.5204 -        if (to_commit > 0) {
 16.5205 -            /* Round size to commit */
 16.5206 -            long commit_size = CEIL (to_commit, g_my_pagesize);
 16.5207 -            /* Compute the size to reserve */
 16.5208 -            long to_reserve = (char *) g_last->top_committed + commit_size - (char *) g_last->top_reserved;
 16.5209 -            /* Do we reach the reserve limit? */
 16.5210 -            if (to_reserve > 0) {
 16.5211 -                /* Compute the remaining size to commit in the current region */
 16.5212 -                long remaining_commit_size = (char *) g_last->top_reserved - (char *) g_last->top_committed;
 16.5213 -                if (remaining_commit_size > 0) {
 16.5214 -                    /* Assert preconditions */
 16.5215 -                    assert ((unsigned) g_last->top_committed % g_pagesize == 0);
 16.5216 -                    assert (0 < remaining_commit_size && remaining_commit_size % g_pagesize == 0); {
 16.5217 -                        /* Commit this */
 16.5218 -                        void *base_committed = VirtualAlloc (g_last->top_committed, remaining_commit_size,
 16.5219 -							                                 MEM_COMMIT, PAGE_READWRITE);
 16.5220 -                        /* Check returned pointer for consistency */
 16.5221 -                        if (base_committed != g_last->top_committed)
 16.5222 -                            goto sbrk_exit;
 16.5223 -                        /* Assert postconditions */
 16.5224 -                        assert ((unsigned) base_committed % g_pagesize == 0);
 16.5225 -#ifdef TRACE
 16.5226 -                        printf ("Commit %p %d\n", base_committed, remaining_commit_size);
 16.5227 -#endif
 16.5228 -                        /* Adjust the regions commit top */
 16.5229 -                        g_last->top_committed = (char *) base_committed + remaining_commit_size;
 16.5230 -                    }
 16.5231 -                } {
 16.5232 -                    /* Now we are going to search and reserve. */
 16.5233 -                    int contiguous = -1;
 16.5234 -                    int found = FALSE;
 16.5235 -                    MEMORY_BASIC_INFORMATION memory_info;
 16.5236 -                    void *base_reserved;
 16.5237 -                    long reserve_size;
 16.5238 -                    do {
 16.5239 -                        /* Assume contiguous memory */
 16.5240 -                        contiguous = TRUE;
 16.5241 -                        /* Round size to reserve */
 16.5242 -                        reserve_size = CEIL (to_reserve, g_my_regionsize);
 16.5243 -                        /* Start with the current region's top */
 16.5244 -                        memory_info.BaseAddress = g_last->top_reserved;
 16.5245 -                        /* Assert preconditions */
 16.5246 -                        assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
 16.5247 -                        assert (0 < reserve_size && reserve_size % g_regionsize == 0);
 16.5248 -                        while (VirtualQuery (memory_info.BaseAddress, &memory_info, sizeof (memory_info))) {
 16.5249 -                            /* Assert postconditions */
 16.5250 -                            assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
 16.5251 -#ifdef TRACE
 16.5252 -                            printf ("Query %p %d %s\n", memory_info.BaseAddress, memory_info.RegionSize, 
 16.5253 -                                    memory_info.State == MEM_FREE ? "FREE": 
 16.5254 -                                    (memory_info.State == MEM_RESERVE ? "RESERVED":
 16.5255 -                                     (memory_info.State == MEM_COMMIT ? "COMMITTED": "?")));
 16.5256 -#endif
 16.5257 -                            /* Region is free, well aligned and big enough: we are done */
 16.5258 -                            if (memory_info.State == MEM_FREE &&
 16.5259 -                                (unsigned) memory_info.BaseAddress % g_regionsize == 0 &&
 16.5260 -                                memory_info.RegionSize >= (unsigned) reserve_size) {
 16.5261 -                                found = TRUE;
 16.5262 -                                break;
 16.5263 -                            }
 16.5264 -                            /* From now on we can't get contiguous memory! */
 16.5265 -                            contiguous = FALSE;
 16.5266 -                            /* Recompute size to reserve */
 16.5267 -                            reserve_size = CEIL (allocate_size, g_my_regionsize);
 16.5268 -                            memory_info.BaseAddress = (char *) memory_info.BaseAddress + memory_info.RegionSize;
 16.5269 -                            /* Assert preconditions */
 16.5270 -                            assert ((unsigned) memory_info.BaseAddress % g_pagesize == 0);
 16.5271 -                            assert (0 < reserve_size && reserve_size % g_regionsize == 0);
 16.5272 -                        }
 16.5273 -                        /* Search failed? */
 16.5274 -                        if (! found) 
 16.5275 -                            goto sbrk_exit;
 16.5276 -                        /* Assert preconditions */
 16.5277 -                        assert ((unsigned) memory_info.BaseAddress % g_regionsize == 0);
 16.5278 -                        assert (0 < reserve_size && reserve_size % g_regionsize == 0);
 16.5279 -                        /* Try to reserve this */
 16.5280 -                        base_reserved = VirtualAlloc (memory_info.BaseAddress, reserve_size, 
 16.5281 -					                                  MEM_RESERVE, PAGE_NOACCESS);
 16.5282 -                        if (! base_reserved) {
 16.5283 -                            int rc = GetLastError ();
 16.5284 -                            if (rc != ERROR_INVALID_ADDRESS) 
 16.5285 -                                goto sbrk_exit;
 16.5286 -                        }
 16.5287 -                        /* A null pointer signals (hopefully) a race condition with another thread. */
 16.5288 -                        /* In this case, we try again. */
 16.5289 -                    } while (! base_reserved);
 16.5290 -                    /* Check returned pointer for consistency */
 16.5291 -                    if (memory_info.BaseAddress && base_reserved != memory_info.BaseAddress)
 16.5292 -                        goto sbrk_exit;
 16.5293 -                    /* Assert postconditions */
 16.5294 -                    assert ((unsigned) base_reserved % g_regionsize == 0);
 16.5295 -#ifdef TRACE
 16.5296 -                    printf ("Reserve %p %d\n", base_reserved, reserve_size);
 16.5297 -#endif
 16.5298 -                    /* Did we get contiguous memory? */
 16.5299 -                    if (contiguous) {
 16.5300 -                        long start_size = (char *) g_last->top_committed - (char *) g_last->top_allocated;
 16.5301 -                        /* Adjust allocation size */
 16.5302 -                        allocate_size -= start_size;
 16.5303 -                        /* Adjust the regions allocation top */
 16.5304 -                        g_last->top_allocated = g_last->top_committed;
 16.5305 -                        /* Recompute the size to commit */
 16.5306 -                        to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
 16.5307 -                        /* Round size to commit */
 16.5308 -                        commit_size = CEIL (to_commit, g_my_pagesize);
 16.5309 -                    } 
 16.5310 -                    /* Append the new region to the list */
 16.5311 -                    if (! region_list_append (&g_last, base_reserved, reserve_size))
 16.5312 -                        goto sbrk_exit;
 16.5313 -                    /* Didn't we get contiguous memory? */
 16.5314 -                    if (! contiguous) {
 16.5315 -                        /* Recompute the size to commit */
 16.5316 -                        to_commit = (char *) g_last->top_allocated + allocate_size - (char *) g_last->top_committed;
 16.5317 -                        /* Round size to commit */
 16.5318 -                        commit_size = CEIL (to_commit, g_my_pagesize);
 16.5319 -                    }
 16.5320 -                }
 16.5321 -            } 
 16.5322 -            /* Assert preconditions */
 16.5323 -            assert ((unsigned) g_last->top_committed % g_pagesize == 0);
 16.5324 -            assert (0 < commit_size && commit_size % g_pagesize == 0); {
 16.5325 -                /* Commit this */
 16.5326 -                void *base_committed = VirtualAlloc (g_last->top_committed, commit_size, 
 16.5327 -				    			                     MEM_COMMIT, PAGE_READWRITE);
 16.5328 -                /* Check returned pointer for consistency */
 16.5329 -                if (base_committed != g_last->top_committed)
 16.5330 -                    goto sbrk_exit;
 16.5331 -                /* Assert postconditions */
 16.5332 -                assert ((unsigned) base_committed % g_pagesize == 0);
 16.5333 -#ifdef TRACE
 16.5334 -                printf ("Commit %p %d\n", base_committed, commit_size);
 16.5335 -#endif
 16.5336 -                /* Adjust the regions commit top */
 16.5337 -                g_last->top_committed = (char *) base_committed + commit_size;
 16.5338 -            }
 16.5339 -        } 
 16.5340 -        /* Adjust the regions allocation top */
 16.5341 -        g_last->top_allocated = (char *) g_last->top_allocated + allocate_size;
 16.5342 -        result = (char *) g_last->top_allocated - size;
 16.5343 -    /* Deallocation requested? */
 16.5344 -    } else if (size < 0) {
 16.5345 -        long deallocate_size = - size;
 16.5346 -        /* As long as we have a region to release */
 16.5347 -        while ((char *) g_last->top_allocated - deallocate_size < (char *) g_last->top_reserved - g_last->reserve_size) {
 16.5348 -            /* Get the size to release */
 16.5349 -            long release_size = g_last->reserve_size;
 16.5350 -            /* Get the base address */
 16.5351 -            void *base_reserved = (char *) g_last->top_reserved - release_size;
 16.5352 -            /* Assert preconditions */
 16.5353 -            assert ((unsigned) base_reserved % g_regionsize == 0); 
 16.5354 -            assert (0 < release_size && release_size % g_regionsize == 0); {
 16.5355 -                /* Release this */
 16.5356 -                int rc = VirtualFree (base_reserved, 0, 
 16.5357 -                                      MEM_RELEASE);
 16.5358 -                /* Check returned code for consistency */
 16.5359 -                if (! rc)
 16.5360 -                    goto sbrk_exit;
 16.5361 -#ifdef TRACE
 16.5362 -                printf ("Release %p %d\n", base_reserved, release_size);
 16.5363 -#endif
 16.5364 -            }
 16.5365 -            /* Adjust deallocation size */
 16.5366 -            deallocate_size -= (char *) g_last->top_allocated - (char *) base_reserved;
 16.5367 -            /* Remove the old region from the list */
 16.5368 -            if (! region_list_remove (&g_last))
 16.5369 -                goto sbrk_exit;
 16.5370 -        } {
 16.5371 -            /* Compute the size to decommit */
 16.5372 -            long to_decommit = (char *) g_last->top_committed - ((char *) g_last->top_allocated - deallocate_size);
 16.5373 -            if (to_decommit >= g_my_pagesize) {
 16.5374 -                /* Compute the size to decommit */
 16.5375 -                long decommit_size = FLOOR (to_decommit, g_my_pagesize);
 16.5376 -                /*  Compute the base address */
 16.5377 -                void *base_committed = (char *) g_last->top_committed - decommit_size;
 16.5378 -                /* Assert preconditions */
 16.5379 -                assert ((unsigned) base_committed % g_pagesize == 0);
 16.5380 -                assert (0 < decommit_size && decommit_size % g_pagesize == 0); {
 16.5381 -                    /* Decommit this */
 16.5382 -                    int rc = VirtualFree ((char *) base_committed, decommit_size, 
 16.5383 -                                          MEM_DECOMMIT);
 16.5384 -                    /* Check returned code for consistency */
 16.5385 -                    if (! rc)
 16.5386 -                        goto sbrk_exit;
 16.5387 -#ifdef TRACE
 16.5388 -                    printf ("Decommit %p %d\n", base_committed, decommit_size);
 16.5389 -#endif
 16.5390 -                }
 16.5391 -                /* Adjust deallocation size and regions commit and allocate top */
 16.5392 -                deallocate_size -= (char *) g_last->top_allocated - (char *) base_committed;
 16.5393 -                g_last->top_committed = base_committed;
 16.5394 -                g_last->top_allocated = base_committed;
 16.5395 -            }
 16.5396 -        }
 16.5397 -        /* Adjust regions allocate top */
 16.5398 -        g_last->top_allocated = (char *) g_last->top_allocated - deallocate_size;
 16.5399 -        /* Check for underflow */
 16.5400 -        if ((char *) g_last->top_reserved - g_last->reserve_size > (char *) g_last->top_allocated ||
 16.5401 -            g_last->top_allocated > g_last->top_committed) {
 16.5402 -            /* Adjust regions allocate top */
 16.5403 -            g_last->top_allocated = (char *) g_last->top_reserved - g_last->reserve_size;
 16.5404 -            goto sbrk_exit;
 16.5405 -        }
 16.5406 -        result = g_last->top_allocated;
 16.5407 -    }
 16.5408 -    /* Assert invariants */
 16.5409 -    assert (g_last);
 16.5410 -    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_allocated &&
 16.5411 -            g_last->top_allocated <= g_last->top_committed);
 16.5412 -    assert ((char *) g_last->top_reserved - g_last->reserve_size <= (char *) g_last->top_committed &&
 16.5413 -            g_last->top_committed <= g_last->top_reserved &&
 16.5414 -            (unsigned) g_last->top_committed % g_pagesize == 0);
 16.5415 -    assert ((unsigned) g_last->top_reserved % g_regionsize == 0);
 16.5416 -    assert ((unsigned) g_last->reserve_size % g_regionsize == 0);
 16.5417 -
 16.5418 -sbrk_exit:
 16.5419 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5420 -    /* Release spin lock */
 16.5421 -    slrelease (&g_sl);
 16.5422 -#endif
 16.5423 -    return result;
 16.5424 -}
 16.5425 -
 16.5426 -/* mmap for windows */
 16.5427 -static void *mmap (void *ptr, long size, long prot, long type, long handle, long arg) {
 16.5428 -    static long g_pagesize;
 16.5429 -    static long g_regionsize;
 16.5430 -#ifdef TRACE
 16.5431 -    printf ("mmap %d\n", size);
 16.5432 -#endif
 16.5433 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5434 -    /* Wait for spin lock */
 16.5435 -    slwait (&g_sl);
 16.5436 -#endif
 16.5437 -    /* First time initialization */
 16.5438 -    if (! g_pagesize) 
 16.5439 -        g_pagesize = getpagesize ();
 16.5440 -    if (! g_regionsize) 
 16.5441 -        g_regionsize = getregionsize ();
 16.5442 -    /* Assert preconditions */
 16.5443 -    assert ((unsigned) ptr % g_regionsize == 0);
 16.5444 -    assert (size % g_pagesize == 0);
 16.5445 -    /* Allocate this */
 16.5446 -    ptr = VirtualAlloc (ptr, size,
 16.5447 -					    MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
 16.5448 -    if (! ptr) {
 16.5449 -        ptr = (void *) MORECORE_FAILURE;
 16.5450 -        goto mmap_exit;
 16.5451 -    }
 16.5452 -    /* Assert postconditions */
 16.5453 -    assert ((unsigned) ptr % g_regionsize == 0);
 16.5454 -#ifdef TRACE
 16.5455 -    printf ("Commit %p %d\n", ptr, size);
 16.5456 -#endif
 16.5457 -mmap_exit:
 16.5458 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5459 -    /* Release spin lock */
 16.5460 -    slrelease (&g_sl);
 16.5461 -#endif
 16.5462 -    return ptr;
 16.5463 -}
 16.5464 -
 16.5465 -/* munmap for windows */
 16.5466 -static long munmap (void *ptr, long size) {
 16.5467 -    static long g_pagesize;
 16.5468 -    static long g_regionsize;
 16.5469 -    int rc = MUNMAP_FAILURE;
 16.5470 -#ifdef TRACE
 16.5471 -    printf ("munmap %p %d\n", ptr, size);
 16.5472 -#endif
 16.5473 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5474 -    /* Wait for spin lock */
 16.5475 -    slwait (&g_sl);
 16.5476 -#endif
 16.5477 -    /* First time initialization */
 16.5478 -    if (! g_pagesize) 
 16.5479 -        g_pagesize = getpagesize ();
 16.5480 -    if (! g_regionsize) 
 16.5481 -        g_regionsize = getregionsize ();
 16.5482 -    /* Assert preconditions */
 16.5483 -    assert ((unsigned) ptr % g_regionsize == 0);
 16.5484 -    assert (size % g_pagesize == 0);
 16.5485 -    /* Free this */
 16.5486 -    if (! VirtualFree (ptr, 0, 
 16.5487 -                       MEM_RELEASE))
 16.5488 -        goto munmap_exit;
 16.5489 -    rc = 0;
 16.5490 -#ifdef TRACE
 16.5491 -    printf ("Release %p %d\n", ptr, size);
 16.5492 -#endif
 16.5493 -munmap_exit:
 16.5494 -#if defined (USE_MALLOC_LOCK) && defined (NEEDED)
 16.5495 -    /* Release spin lock */
 16.5496 -    slrelease (&g_sl);
 16.5497 -#endif
 16.5498 -    return rc;
 16.5499 -}
 16.5500 -
 16.5501 -static void vminfo (CHUNK_SIZE_T  *free, CHUNK_SIZE_T  *reserved, CHUNK_SIZE_T  *committed) {
 16.5502 -    MEMORY_BASIC_INFORMATION memory_info;
 16.5503 -    memory_info.BaseAddress = 0;
 16.5504 -    *free = *reserved = *committed = 0;
 16.5505 -    while (VirtualQuery (memory_info.BaseAddress, &memory_info, sizeof (memory_info))) {
 16.5506 -        switch (memory_info.State) {
 16.5507 -        case MEM_FREE:
 16.5508 -            *free += memory_info.RegionSize;
 16.5509 -            break;
 16.5510 -        case MEM_RESERVE:
 16.5511 -            *reserved += memory_info.RegionSize;
 16.5512 -            break;
 16.5513 -        case MEM_COMMIT:
 16.5514 -            *committed += memory_info.RegionSize;
 16.5515 -            break;
 16.5516 -        }
 16.5517 -        memory_info.BaseAddress = (char *) memory_info.BaseAddress + memory_info.RegionSize;
 16.5518 -    }
 16.5519 -}
 16.5520 -
 16.5521 -static int cpuinfo (int whole, CHUNK_SIZE_T  *kernel, CHUNK_SIZE_T  *user) {
 16.5522 -    if (whole) {
 16.5523 -        __int64 creation64, exit64, kernel64, user64;
 16.5524 -        int rc = GetProcessTimes (GetCurrentProcess (), 
 16.5525 -                                  (FILETIME *) &creation64,  
 16.5526 -                                  (FILETIME *) &exit64, 
 16.5527 -                                  (FILETIME *) &kernel64, 
 16.5528 -                                  (FILETIME *) &user64);
 16.5529 -        if (! rc) {
 16.5530 -            *kernel = 0;
 16.5531 -            *user = 0;
 16.5532 -            return FALSE;
 16.5533 -        } 
 16.5534 -        *kernel = (CHUNK_SIZE_T) (kernel64 / 10000);
 16.5535 -        *user = (CHUNK_SIZE_T) (user64 / 10000);
 16.5536 -        return TRUE;
 16.5537 -    } else {
 16.5538 -        __int64 creation64, exit64, kernel64, user64;
 16.5539 -        int rc = GetThreadTimes (GetCurrentThread (), 
 16.5540 -                                 (FILETIME *) &creation64,  
 16.5541 -                                 (FILETIME *) &exit64, 
 16.5542 -                                 (FILETIME *) &kernel64, 
 16.5543 -                                 (FILETIME *) &user64);
 16.5544 -        if (! rc) {
 16.5545 -            *kernel = 0;
 16.5546 -            *user = 0;
 16.5547 -            return FALSE;
 16.5548 -        } 
 16.5549 -        *kernel = (CHUNK_SIZE_T) (kernel64 / 10000);
 16.5550 -        *user = (CHUNK_SIZE_T) (user64 / 10000);
 16.5551 -        return TRUE;
 16.5552 -    }
 16.5553 -}
 16.5554 -
 16.5555 -#endif /* WIN32 */
 16.5556 -
 16.5557 -/* ------------------------------------------------------------
 16.5558 -History:
 16.5559 -    V2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
 16.5560 -      * Fix malloc_state bitmap array misdeclaration
 16.5561 -
 16.5562 -    V2.7.1 Thu Jul 25 10:58:03 2002  Doug Lea  (dl at gee)
 16.5563 -      * Allow tuning of FIRST_SORTED_BIN_SIZE
 16.5564 -      * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte.
 16.5565 -      * Better detection and support for non-contiguousness of MORECORE. 
 16.5566 -        Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger
 16.5567 -      * Bypass most of malloc if no frees. Thanks To Emery Berger.
 16.5568 -      * Fix freeing of old top non-contiguous chunk im sysmalloc.
 16.5569 -      * Raised default trim and map thresholds to 256K.
 16.5570 -      * Fix mmap-related #defines. Thanks to Lubos Lunak.
 16.5571 -      * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield.
 16.5572 -      * Branch-free bin calculation
 16.5573 -      * Default trim and mmap thresholds now 256K.
 16.5574 -
 16.5575 -    V2.7.0 Sun Mar 11 14:14:06 2001  Doug Lea  (dl at gee)
 16.5576 -      * Introduce independent_comalloc and independent_calloc.
 16.5577 -        Thanks to Michael Pachos for motivation and help.
 16.5578 -      * Make optional .h file available
 16.5579 -      * Allow > 2GB requests on 32bit systems.
 16.5580 -      * new WIN32 sbrk, mmap, munmap, lock code from <Walter@GeNeSys-e.de>.
 16.5581 -        Thanks also to Andreas Mueller <a.mueller at paradatec.de>,
 16.5582 -        and Anonymous.
 16.5583 -      * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for 
 16.5584 -        helping test this.)
 16.5585 -      * memalign: check alignment arg
 16.5586 -      * realloc: don't try to shift chunks backwards, since this
 16.5587 -        leads to  more fragmentation in some programs and doesn't
 16.5588 -        seem to help in any others.
 16.5589 -      * Collect all cases in malloc requiring system memory into sYSMALLOc
 16.5590 -      * Use mmap as backup to sbrk
 16.5591 -      * Place all internal state in malloc_state
 16.5592 -      * Introduce fastbins (although similar to 2.5.1)
 16.5593 -      * Many minor tunings and cosmetic improvements
 16.5594 -      * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK 
 16.5595 -      * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS
 16.5596 -        Thanks to Tony E. Bennett <tbennett@nvidia.com> and others.
 16.5597 -      * Include errno.h to support default failure action.
 16.5598 -
 16.5599 -    V2.6.6 Sun Dec  5 07:42:19 1999  Doug Lea  (dl at gee)
 16.5600 -      * return null for negative arguments
 16.5601 -      * Added Several WIN32 cleanups from Martin C. Fong <mcfong at yahoo.com>
 16.5602 -         * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
 16.5603 -          (e.g. WIN32 platforms)
 16.5604 -         * Cleanup header file inclusion for WIN32 platforms
 16.5605 -         * Cleanup code to avoid Microsoft Visual C++ compiler complaints
 16.5606 -         * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
 16.5607 -           memory allocation routines
 16.5608 -         * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
 16.5609 -         * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
 16.5610 -           usage of 'assert' in non-WIN32 code
 16.5611 -         * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
 16.5612 -           avoid infinite loop
 16.5613 -      * Always call 'fREe()' rather than 'free()'
 16.5614 -
 16.5615 -    V2.6.5 Wed Jun 17 15:57:31 1998  Doug Lea  (dl at gee)
 16.5616 -      * Fixed ordering problem with boundary-stamping
 16.5617 -
 16.5618 -    V2.6.3 Sun May 19 08:17:58 1996  Doug Lea  (dl at gee)
 16.5619 -      * Added pvalloc, as recommended by H.J. Liu
 16.5620 -      * Added 64bit pointer support mainly from Wolfram Gloger
 16.5621 -      * Added anonymously donated WIN32 sbrk emulation
 16.5622 -      * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
 16.5623 -      * malloc_extend_top: fix mask error that caused wastage after
 16.5624 -        foreign sbrks
 16.5625 -      * Add linux mremap support code from HJ Liu
 16.5626 -
 16.5627 -    V2.6.2 Tue Dec  5 06:52:55 1995  Doug Lea  (dl at gee)
 16.5628 -      * Integrated most documentation with the code.
 16.5629 -      * Add support for mmap, with help from
 16.5630 -        Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
 16.5631 -      * Use last_remainder in more cases.
 16.5632 -      * Pack bins using idea from  colin@nyx10.cs.du.edu
 16.5633 -      * Use ordered bins instead of best-fit threshhold
 16.5634 -      * Eliminate block-local decls to simplify tracing and debugging.
 16.5635 -      * Support another case of realloc via move into top
 16.5636 -      * Fix error occuring when initial sbrk_base not word-aligned.
 16.5637 -      * Rely on page size for units instead of SBRK_UNIT to
 16.5638 -        avoid surprises about sbrk alignment conventions.
 16.5639 -      * Add mallinfo, mallopt. Thanks to Raymond Nijssen
 16.5640 -        (raymond@es.ele.tue.nl) for the suggestion.
 16.5641 -      * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
 16.5642 -      * More precautions for cases where other routines call sbrk,
 16.5643 -        courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
 16.5644 -      * Added macros etc., allowing use in linux libc from
 16.5645 -        H.J. Lu (hjl@gnu.ai.mit.edu)
 16.5646 -      * Inverted this history list
 16.5647 -
 16.5648 -    V2.6.1 Sat Dec  2 14:10:57 1995  Doug Lea  (dl at gee)
 16.5649 -      * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
 16.5650 -      * Removed all preallocation code since under current scheme
 16.5651 -        the work required to undo bad preallocations exceeds
 16.5652 -        the work saved in good cases for most test programs.
 16.5653 -      * No longer use return list or unconsolidated bins since
 16.5654 -        no scheme using them consistently outperforms those that don't
 16.5655 -        given above changes.
 16.5656 -      * Use best fit for very large chunks to prevent some worst-cases.
 16.5657 -      * Added some support for debugging
 16.5658 -
 16.5659 -    V2.6.0 Sat Nov  4 07:05:23 1995  Doug Lea  (dl at gee)
 16.5660 -      * Removed footers when chunks are in use. Thanks to
 16.5661 -        Paul Wilson (wilson@cs.texas.edu) for the suggestion.
 16.5662 -
 16.5663 -    V2.5.4 Wed Nov  1 07:54:51 1995  Doug Lea  (dl at gee)
 16.5664 -      * Added malloc_trim, with help from Wolfram Gloger
 16.5665 -        (wmglo@Dent.MED.Uni-Muenchen.DE).
 16.5666 -
 16.5667 -    V2.5.3 Tue Apr 26 10:16:01 1994  Doug Lea  (dl at g)
 16.5668 -
 16.5669 -    V2.5.2 Tue Apr  5 16:20:40 1994  Doug Lea  (dl at g)
 16.5670 -      * realloc: try to expand in both directions
 16.5671 -      * malloc: swap order of clean-bin strategy;
 16.5672 -      * realloc: only conditionally expand backwards
 16.5673 -      * Try not to scavenge used bins
 16.5674 -      * Use bin counts as a guide to preallocation
 16.5675 -      * Occasionally bin return list chunks in first scan
 16.5676 -      * Add a few optimizations from colin@nyx10.cs.du.edu
 16.5677 -
 16.5678 -    V2.5.1 Sat Aug 14 15:40:43 1993  Doug Lea  (dl at g)
 16.5679 -      * faster bin computation & slightly different binning
 16.5680 -      * merged all consolidations to one part of malloc proper
 16.5681 -         (eliminating old malloc_find_space & malloc_clean_bin)
 16.5682 -      * Scan 2 returns chunks (not just 1)
 16.5683 -      * Propagate failure in realloc if malloc returns 0
 16.5684 -      * Add stuff to allow compilation on non-ANSI compilers
 16.5685 -          from kpv@research.att.com
 16.5686 -
 16.5687 -    V2.5 Sat Aug  7 07:41:59 1993  Doug Lea  (dl at g.oswego.edu)
 16.5688 -      * removed potential for odd address access in prev_chunk
 16.5689 -      * removed dependency on getpagesize.h
 16.5690 -      * misc cosmetics and a bit more internal documentation
 16.5691 -      * anticosmetics: mangled names in macros to evade debugger strangeness
 16.5692 -      * tested on sparc, hp-700, dec-mips, rs6000
 16.5693 -          with gcc & native cc (hp, dec only) allowing
 16.5694 -          Detlefs & Zorn comparison study (in SIGPLAN Notices.)
 16.5695 -
 16.5696 -    Trial version Fri Aug 28 13:14:29 1992  Doug Lea  (dl at g.oswego.edu)
 16.5697 -      * Based loosely on libg++-1.2X malloc. (It retains some of the overall
 16.5698 -         structure of old version,  but most details differ.)
 16.5699 -
 16.5700 -*/
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/extras/mini-os/lib/xmalloc.c	Fri Aug 26 13:47:16 2005 -0700
    17.3 @@ -0,0 +1,219 @@
    17.4 +/* 
    17.5 + ****************************************************************************
    17.6 + * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
    17.7 + ****************************************************************************
    17.8 + *
    17.9 + *        File: xmaloc.c
   17.10 + *      Author: Grzegorz Milos (gm281@cam.ac.uk)
   17.11 + *     Changes: 
   17.12 + *              
   17.13 + *        Date: Aug 2005
   17.14 + * 
   17.15 + * Environment: Xen Minimal OS
   17.16 + * Description: simple memory allocator
   17.17 + *
   17.18 + ****************************************************************************
   17.19 + * Simple allocator for Mini-os.  If larger than a page, simply use the
   17.20 + * page-order allocator.
   17.21 + *
   17.22 + * Copy of the allocator for Xen by Rusty Russell:
   17.23 + * Copyright (C) 2005 Rusty Russell IBM Corporation
   17.24 + *
   17.25 + * This program is free software; you can redistribute it and/or modify
   17.26 + * it under the terms of the GNU General Public License as published by
   17.27 + * the Free Software Foundation; either version 2 of the License, or
   17.28 + * (at your option) any later version.
   17.29 + *
   17.30 + * This program is distributed in the hope that it will be useful,
   17.31 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   17.32 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17.33 + * GNU General Public License for more details.
   17.34 + *
   17.35 + * You should have received a copy of the GNU General Public License
   17.36 + * along with this program; if not, write to the Free Software
   17.37 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17.38 + */
   17.39 +
   17.40 +#include <os.h>
   17.41 +#include <mm.h>
   17.42 +#include <types.h>
   17.43 +#include <lib.h>
   17.44 +#include <list.h>
   17.45 +
   17.46 +static LIST_HEAD(freelist);
   17.47 +/* static spinlock_t freelist_lock = SPIN_LOCK_UNLOCKED; */
   17.48 +
   17.49 +struct xmalloc_hdr
   17.50 +{
   17.51 +    /* Total including this hdr. */
   17.52 +    size_t size;
   17.53 +    struct list_head freelist;
   17.54 +} __cacheline_aligned;
   17.55 +
   17.56 +static void maybe_split(struct xmalloc_hdr *hdr, size_t size, size_t block)
   17.57 +{
   17.58 +    struct xmalloc_hdr *extra;
   17.59 +    size_t leftover = block - size;
   17.60 +
   17.61 +    /* If enough is left to make a block, put it on free list. */
   17.62 +    if ( leftover >= (2 * sizeof(struct xmalloc_hdr)) )
   17.63 +    {
   17.64 +        extra = (struct xmalloc_hdr *)((unsigned long)hdr + size);
   17.65 +        extra->size = leftover;
   17.66 +        list_add(&extra->freelist, &freelist);
   17.67 +    }
   17.68 +    else
   17.69 +    {
   17.70 +        size = block;
   17.71 +    }
   17.72 +
   17.73 +    hdr->size = size;
   17.74 +    /* Debugging aid. */
   17.75 +    hdr->freelist.next = hdr->freelist.prev = NULL;
   17.76 +}
   17.77 +
   17.78 +static void *xmalloc_new_page(size_t size)
   17.79 +{
   17.80 +    struct xmalloc_hdr *hdr;
   17.81 +    /* unsigned long flags; */
   17.82 +
   17.83 +    hdr = (struct xmalloc_hdr *)alloc_page();
   17.84 +    if ( hdr == NULL )
   17.85 +        return NULL;
   17.86 +
   17.87 +    /* spin_lock_irqsave(&freelist_lock, flags); */
   17.88 +    maybe_split(hdr, size, PAGE_SIZE);
   17.89 +    /* spin_unlock_irqrestore(&freelist_lock, flags); */
   17.90 +
   17.91 +    return hdr+1;
   17.92 +}
   17.93 +
   17.94 +/* Big object?  Just use the page allocator. */
   17.95 +static void *xmalloc_whole_pages(size_t size)
   17.96 +{
   17.97 +    struct xmalloc_hdr *hdr;
   17.98 +    unsigned int pageorder = get_order(size);
   17.99 +
  17.100 +    hdr = (struct xmalloc_hdr *)alloc_pages(pageorder);
  17.101 +    if ( hdr == NULL )
  17.102 +        return NULL;
  17.103 +
  17.104 +    hdr->size = (1 << (pageorder + PAGE_SHIFT));
  17.105 +    /* Debugging aid. */
  17.106 +    hdr->freelist.next = hdr->freelist.prev = NULL;
  17.107 +
  17.108 +    return hdr+1;
  17.109 +}
  17.110 +
  17.111 +/* Return size, increased to alignment with align. */
  17.112 +static inline size_t align_up(size_t size, size_t align)
  17.113 +{
  17.114 +    return (size + align - 1) & ~(align - 1);
  17.115 +}
  17.116 +
  17.117 +void *_xmalloc(size_t size, size_t align)
  17.118 +{
  17.119 +    struct xmalloc_hdr *i;
  17.120 +    /* unsigned long flags; */
  17.121 +
  17.122 +    /* Add room for header, pad to align next header. */
  17.123 +    size += sizeof(struct xmalloc_hdr);
  17.124 +    size = align_up(size, __alignof__(struct xmalloc_hdr));
  17.125 +
  17.126 +    /* For big allocs, give them whole pages. */
  17.127 +    if ( size >= PAGE_SIZE )
  17.128 +        return xmalloc_whole_pages(size);
  17.129 +
  17.130 +    /* Search free list. */
  17.131 +    /* spin_lock_irqsave(&freelist_lock, flags); */
  17.132 +    list_for_each_entry( i, &freelist, freelist )
  17.133 +    {
  17.134 +        if ( i->size < size )
  17.135 +            continue;
  17.136 +        list_del(&i->freelist);
  17.137 +        maybe_split(i, size, i->size);
  17.138 +        /* spin_unlock_irqrestore(&freelist_lock, flags); */
  17.139 +        return i+1;
  17.140 +    }
  17.141 +    /* spin_unlock_irqrestore(&freelist_lock, flags); */
  17.142 +
  17.143 +    /* Alloc a new page and return from that. */
  17.144 +    return xmalloc_new_page(size);
  17.145 +}
  17.146 +
  17.147 +void xfree(const void *p)
  17.148 +{
  17.149 +    /* unsigned long flags; */
  17.150 +    struct xmalloc_hdr *i, *tmp, *hdr;
  17.151 +
  17.152 +    if ( p == NULL )
  17.153 +        return;
  17.154 +
  17.155 +    hdr = (struct xmalloc_hdr *)p - 1;
  17.156 +
  17.157 +    /* We know hdr will be on same page. */
  17.158 +    if(((long)p & PAGE_MASK) != ((long)hdr & PAGE_MASK))
  17.159 +    {
  17.160 +        printk("Header should be on the same page\n");
  17.161 +        *(int*)0=0;
  17.162 +    }
  17.163 +
  17.164 +    /* Not previously freed. */
  17.165 +    if(hdr->freelist.next || hdr->freelist.prev)
  17.166 +    {
  17.167 +        printk("Should not be previously freed\n");
  17.168 +        *(int*)0=0;
  17.169 +    }
  17.170 +
  17.171 +    /* Big allocs free directly. */
  17.172 +    if ( hdr->size >= PAGE_SIZE )
  17.173 +    {
  17.174 +        free_pages(hdr, get_order(hdr->size));
  17.175 +        return;
  17.176 +    }
  17.177 +
  17.178 +    /* Merge with other free block, or put in list. */
  17.179 +    /* spin_lock_irqsave(&freelist_lock, flags); */
  17.180 +    list_for_each_entry_safe( i, tmp, &freelist, freelist )
  17.181 +    {
  17.182 +        unsigned long _i   = (unsigned long)i;
  17.183 +        unsigned long _hdr = (unsigned long)hdr;
  17.184 +
  17.185 +        /* Do not merge across page boundaries. */
  17.186 +        if ( ((_i ^ _hdr) & PAGE_MASK) != 0 )
  17.187 +            continue;
  17.188 +
  17.189 +        /* We follow this block?  Swallow it. */
  17.190 +        if ( (_i + i->size) == _hdr )
  17.191 +        {
  17.192 +            list_del(&i->freelist);
  17.193 +            i->size += hdr->size;
  17.194 +            hdr = i;
  17.195 +        }
  17.196 +
  17.197 +        /* We precede this block? Swallow it. */
  17.198 +        if ( (_hdr + hdr->size) == _i )
  17.199 +        {
  17.200 +            list_del(&i->freelist);
  17.201 +            hdr->size += i->size;
  17.202 +        }
  17.203 +    }
  17.204 +
  17.205 +    /* Did we merge an entire page? */
  17.206 +    if ( hdr->size == PAGE_SIZE )
  17.207 +    {
  17.208 +        if((((unsigned long)hdr) & (PAGE_SIZE-1)) != 0)
  17.209 +        {
  17.210 +            printk("Bug\n");
  17.211 +            *(int*)0=0;
  17.212 +        }
  17.213 +        free_pages(hdr, 0);
  17.214 +    }
  17.215 +    else
  17.216 +    {
  17.217 +        list_add(&hdr->freelist, &freelist);
  17.218 +    }
  17.219 +
  17.220 +    /* spin_unlock_irqrestore(&freelist_lock, flags); */
  17.221 +}
  17.222 +
    18.1 --- a/extras/mini-os/mm.c	Thu Aug 25 13:52:38 2005 -0700
    18.2 +++ b/extras/mini-os/mm.c	Fri Aug 26 13:47:16 2005 -0700
    18.3 @@ -1,6 +1,7 @@
    18.4 -/* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
    18.5 +/* 
    18.6   ****************************************************************************
    18.7   * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
    18.8 + * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
    18.9   ****************************************************************************
   18.10   *
   18.11   *        File: mm.c
   18.12 @@ -14,8 +15,6 @@
   18.13   *              contains buddy page allocator from Xen.
   18.14   *
   18.15   ****************************************************************************
   18.16 - * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
   18.17 - ****************************************************************************
   18.18   * Permission is hereby granted, free of charge, to any person obtaining a copy
   18.19   * of this software and associated documentation files (the "Software"), to
   18.20   * deal in the Software without restriction, including without limitation the
   18.21 @@ -40,7 +39,7 @@
   18.22  #include <mm.h>
   18.23  #include <types.h>
   18.24  #include <lib.h>
   18.25 -
   18.26 +#include <xmalloc.h>
   18.27  
   18.28  #ifdef MM_DEBUG
   18.29  #define DEBUG(_f, _a...) \
   18.30 @@ -505,6 +504,6 @@ void init_mm(void)
   18.31             (u_long)to_virt(PFN_PHYS(max_pfn)), PFN_PHYS(max_pfn));
   18.32      init_page_allocator(PFN_PHYS(start_pfn), PFN_PHYS(max_pfn));   
   18.33  #endif
   18.34 -
   18.35 +    
   18.36      printk("MM: done\n");
   18.37  }
   135.1 --- a/linux-2.6-xen-sparse/arch/xen/i386/kernel/smpboot.c	Thu Aug 25 13:52:38 2005 -0700
   135.2 +++ b/linux-2.6-xen-sparse/arch/xen/i386/kernel/smpboot.c	Fri Aug 26 13:47:16 2005 -0700
   135.3 @@ -853,7 +853,7 @@ static int __init do_boot_cpu(int apicid
   135.4  	atomic_set(&init_deasserted, 0);
   135.5  
   135.6  #if 1
   135.7 -	cpu_gdt_descr[cpu].address = __get_free_page(GFP_KERNEL);
   135.8 +	cpu_gdt_descr[cpu].address = __get_free_page(GFP_KERNEL|__GFP_ZERO);
   135.9  	BUG_ON(cpu_gdt_descr[0].size > PAGE_SIZE);
  135.10  	cpu_gdt_descr[cpu].size = cpu_gdt_descr[0].size;
  135.11  	printk("GDT: copying %d bytes from %lx to %lx\n",
   179.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/kernel/ldt.c	Thu Aug 25 13:52:38 2005 -0700
   179.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/kernel/ldt.c	Fri Aug 26 13:47:16 2005 -0700
   179.3 @@ -105,14 +105,19 @@ int init_new_context(struct task_struct 
   179.4  	struct mm_struct * old_mm;
   179.5  	int retval = 0;
   179.6  
   179.7 +	memset(&mm->context, 0, sizeof(mm->context));
   179.8  	init_MUTEX(&mm->context.sem);
   179.9 -	mm->context.size = 0;
  179.10  	old_mm = current->mm;
  179.11  	if (old_mm && old_mm->context.size > 0) {
  179.12  		down(&old_mm->context.sem);
  179.13  		retval = copy_ldt(&mm->context, &old_mm->context);
  179.14  		up(&old_mm->context.sem);
  179.15  	}
  179.16 +	if (retval == 0) {
  179.17 +		spin_lock(&mm_unpinned_lock);
  179.18 +		list_add(&mm->context.unpinned, &mm_unpinned);
  179.19 +		spin_unlock(&mm_unpinned_lock);
  179.20 +	}
  179.21  	return retval;
  179.22  }
  179.23  
  179.24 @@ -134,6 +139,11 @@ void destroy_context(struct mm_struct *m
  179.25  			kfree(mm->context.ldt);
  179.26  		mm->context.size = 0;
  179.27  	}
  179.28 +	if (!mm->context.pinned) {
  179.29 +		spin_lock(&mm_unpinned_lock);
  179.30 +		list_del(&mm->context.unpinned);
  179.31 +		spin_unlock(&mm_unpinned_lock);
  179.32 +	}
  179.33  }
  179.34  
  179.35  static int read_ldt(void __user * ptr, unsigned long bytecount)
   187.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/kernel/smpboot.c	Thu Aug 25 13:52:38 2005 -0700
   187.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/kernel/smpboot.c	Fri Aug 26 13:47:16 2005 -0700
   187.3 @@ -739,7 +739,7 @@ static int __cpuinit do_boot_cpu(int cpu
   187.4  	atomic_set(&init_deasserted, 0);
   187.5  
   187.6  #ifdef CONFIG_XEN
   187.7 -	cpu_gdt_descr[cpu].address = __get_free_page(GFP_KERNEL);
   187.8 +	cpu_gdt_descr[cpu].address = __get_free_page(GFP_KERNEL|__GFP_ZERO);
   187.9  	BUG_ON(cpu_gdt_descr[0].size > PAGE_SIZE);
  187.10  	cpu_gdt_descr[cpu].size = cpu_gdt_descr[0].size;
  187.11  	memcpy((void *)cpu_gdt_descr[cpu].address,
   194.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/mm/init.c	Thu Aug 25 13:52:38 2005 -0700
   194.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/mm/init.c	Fri Aug 26 13:47:16 2005 -0700
   194.3 @@ -712,6 +712,7 @@ void __init paging_init(void)
   194.4          HYPERVISOR_shared_info = (shared_info_t *)fix_to_virt(FIX_SHARED_INFO);
   194.5  
   194.6          memset(empty_zero_page, 0, sizeof(empty_zero_page));
   194.7 +	init_mm.context.pinned = 1;
   194.8  
   194.9  #ifdef CONFIG_XEN_PHYSDEV_ACCESS
  194.10  	{
   195.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/mm/pageattr.c	Thu Aug 25 13:52:38 2005 -0700
   195.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/mm/pageattr.c	Fri Aug 26 13:47:16 2005 -0700
   195.3 @@ -12,19 +12,145 @@
   195.4  #include <asm/uaccess.h>
   195.5  #include <asm/processor.h>
   195.6  #include <asm/tlbflush.h>
   195.7 +#include <asm/io.h>
   195.8 +
   195.9 +#ifdef CONFIG_XEN
  195.10  #include <asm/pgalloc.h>
  195.11 -#include <asm/io.h>
  195.12 +#include <asm/mmu_context.h>
  195.13 +
  195.14 +LIST_HEAD(mm_unpinned);
  195.15 +DEFINE_SPINLOCK(mm_unpinned_lock);
  195.16 +
  195.17 +static inline void mm_walk_set_prot(void *pt, pgprot_t flags)
  195.18 +{
  195.19 +	struct page *page = virt_to_page(pt);
  195.20 +	unsigned long pfn = page_to_pfn(page);
  195.21 +
  195.22 +	BUG_ON(HYPERVISOR_update_va_mapping(
  195.23 +		       (unsigned long)__va(pfn << PAGE_SHIFT),
  195.24 +		       pfn_pte(pfn, flags), 0));
  195.25 +}
  195.26 +
  195.27 +static void mm_walk(struct mm_struct *mm, pgprot_t flags)
  195.28 +{
  195.29 +	pgd_t       *pgd;
  195.30 +	pud_t       *pud;
  195.31 +	pmd_t       *pmd;
  195.32 +	pte_t       *pte;
  195.33 +	int          g,u,m;
  195.34 +
  195.35 +	pgd = mm->pgd;
  195.36 +	for (g = 0; g <= USER_PTRS_PER_PGD; g++, pgd++) {
  195.37 +		if (pgd_none(*pgd))
  195.38 +			continue;
  195.39 +		pud = pud_offset(pgd, 0);
  195.40 +		if (PTRS_PER_PUD > 1) /* not folded */ 
  195.41 +			mm_walk_set_prot(pud,flags);
  195.42 +		for (u = 0; u < PTRS_PER_PUD; u++, pud++) {
  195.43 +			if (pud_none(*pud))
  195.44 +				continue;
  195.45 +			pmd = pmd_offset(pud, 0);
  195.46 +			if (PTRS_PER_PMD > 1) /* not folded */ 
  195.47 +				mm_walk_set_prot(pmd,flags);
  195.48 +			for (m = 0; m < PTRS_PER_PMD; m++, pmd++) {
  195.49 +				if (pmd_none(*pmd))
  195.50 +					continue;
  195.51 +				pte = pte_offset_kernel(pmd,0);
  195.52 +				mm_walk_set_prot(pte,flags);
  195.53 +			}
  195.54 +		}
  195.55 +	}
  195.56 +}
  195.57 +
  195.58 +void mm_pin(struct mm_struct *mm)
  195.59 +{
  195.60 +	spin_lock(&mm->page_table_lock);
  195.61 +
  195.62 +	mm_walk(mm, PAGE_KERNEL_RO);
  195.63 +	BUG_ON(HYPERVISOR_update_va_mapping(
  195.64 +		       (unsigned long)mm->pgd,
  195.65 +		       pfn_pte(virt_to_phys(mm->pgd)>>PAGE_SHIFT, PAGE_KERNEL_RO),
  195.66 +		       UVMF_TLB_FLUSH));
  195.67 +	BUG_ON(HYPERVISOR_update_va_mapping(
  195.68 +		       (unsigned long)__user_pgd(mm->pgd),
  195.69 +		       pfn_pte(virt_to_phys(__user_pgd(mm->pgd))>>PAGE_SHIFT, PAGE_KERNEL_RO),
  195.70 +		       UVMF_TLB_FLUSH));
  195.71 +	xen_pgd_pin(__pa(mm->pgd)); /* kernel */
  195.72 +	xen_pgd_pin(__pa(__user_pgd(mm->pgd))); /* user */
  195.73 +	mm->context.pinned = 1;
  195.74 +	spin_lock(&mm_unpinned_lock);
  195.75 +	list_del(&mm->context.unpinned);
  195.76 +	spin_unlock(&mm_unpinned_lock);
  195.77 +
  195.78 +	spin_unlock(&mm->page_table_lock);
  195.79 +}
  195.80 +
  195.81 +void mm_unpin(struct mm_struct *mm)
  195.82 +{
  195.83 +	spin_lock(&mm->page_table_lock);
  195.84 +
  195.85 +	xen_pgd_unpin(__pa(mm->pgd));
  195.86 +	xen_pgd_unpin(__pa(__user_pgd(mm->pgd)));
  195.87 +	BUG_ON(HYPERVISOR_update_va_mapping(
  195.88 +		       (unsigned long)mm->pgd,
  195.89 +		       pfn_pte(virt_to_phys(mm->pgd)>>PAGE_SHIFT, PAGE_KERNEL), 0));
  195.90 +	BUG_ON(HYPERVISOR_update_va_mapping(
  195.91 +		       (unsigned long)__user_pgd(mm->pgd),
  195.92 +		       pfn_pte(virt_to_phys(__user_pgd(mm->pgd))>>PAGE_SHIFT, PAGE_KERNEL), 0));
  195.93 +	mm_walk(mm, PAGE_KERNEL);
  195.94 +	xen_tlb_flush();
  195.95 +	mm->context.pinned = 0;
  195.96 +	spin_lock(&mm_unpinned_lock);
  195.97 +	list_add(&mm->context.unpinned, &mm_unpinned);
  195.98 +	spin_unlock(&mm_unpinned_lock);
  195.99 +
 195.100 +	spin_unlock(&mm->page_table_lock);
 195.101 +}
 195.102 +
 195.103 +void mm_pin_all(void)
 195.104 +{
 195.105 +	while (!list_empty(&mm_unpinned))	
 195.106 +		mm_pin(list_entry(mm_unpinned.next, struct mm_struct,
 195.107 +				  context.unpinned));
 195.108 +}
 195.109 +
 195.110 +void _arch_exit_mmap(struct mm_struct *mm)
 195.111 +{
 195.112 +    struct task_struct *tsk = current;
 195.113 +
 195.114 +    task_lock(tsk);
 195.115 +
 195.116 +    /*
 195.117 +     * We aggressively remove defunct pgd from cr3. We execute unmap_vmas()
 195.118 +     * *much* faster this way, as no tlb flushes means bigger wrpt batches.
 195.119 +     */
 195.120 +    if ( tsk->active_mm == mm )
 195.121 +    {
 195.122 +        tsk->active_mm = &init_mm;
 195.123 +        atomic_inc(&init_mm.mm_count);
 195.124 +
 195.125 +        switch_mm(mm, &init_mm, tsk);
 195.126 +
 195.127 +        atomic_dec(&mm->mm_count);
 195.128 +        BUG_ON(atomic_read(&mm->mm_count) == 0);
 195.129 +    }
 195.130 +
 195.131 +    task_unlock(tsk);
 195.132 +
 195.133 +    if ( mm->context.pinned && (atomic_read(&mm->mm_count) == 1) )
 195.134 +        mm_unpin(mm);
 195.135 +}
 195.136  
 195.137  void pte_free(struct page *pte)
 195.138  {
 195.139 -        pte_t *ptep;
 195.140 -
 195.141 -        ptep = pfn_to_kaddr(page_to_pfn(pte));
 195.142 +	unsigned long va = (unsigned long)__va(page_to_pfn(pte)<<PAGE_SHIFT);
 195.143  
 195.144 -        xen_pte_unpin(__pa(ptep));
 195.145 -        make_page_writable(ptep);
 195.146 -	__free_page(pte); 
 195.147 +	if (!pte_write(*virt_to_ptep(va)))
 195.148 +		BUG_ON(HYPERVISOR_update_va_mapping(
 195.149 +			va, pfn_pte(page_to_pfn(pte), PAGE_KERNEL), 0));
 195.150 +	__free_page(pte);
 195.151  }
 195.152 +#endif	/* CONFIG_XEN */
 195.153  
 195.154  static inline pte_t *lookup_address(unsigned long address) 
 195.155  { 
 195.156 @@ -78,7 +204,7 @@ static void flush_kernel_map(void *addre
 195.157  	} else
 195.158  		asm volatile("wbinvd":::"memory"); 
 195.159  	if (address)
 195.160 -                __flush_tlb_one((unsigned long) address);
 195.161 +		__flush_tlb_one(address);
 195.162  	else
 195.163  		__flush_tlb_all();
 195.164  }
 195.165 @@ -166,14 +292,17 @@ static int
 195.166  		BUG();
 195.167  
 195.168  	/* on x86-64 the direct mapping set at boot is not using 4k pages */
 195.169 -// 	BUG_ON(PageReserved(kpte_page));
 195.170  	/*
 195.171  	 * ..., but the XEN guest kernels (currently) do:
 195.172  	 * If the pte was reserved, it means it was created at boot
 195.173  	 * time (not via split_large_page) and in turn we must not
 195.174  	 * replace it with a large page.
 195.175  	 */
 195.176 -	if (!PageReserved(kpte_page)) {
 195.177 +#ifndef CONFIG_XEN
 195.178 + 	BUG_ON(PageReserved(kpte_page));
 195.179 +#else
 195.180 +	if (!PageReserved(kpte_page))
 195.181 +#endif
 195.182  		switch (page_count(kpte_page)) {
 195.183  		case 1:
 195.184  			save_page(address, kpte_page); 		     
 195.185 @@ -182,7 +311,6 @@ static int
 195.186  		case 0:
 195.187  			BUG(); /* memleak and failed 2M page regeneration */
 195.188  	 	}
 195.189 -	}
 195.190  	return 0;
 195.191  } 
 195.192  
   208.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/interface.c	Thu Aug 25 13:52:38 2005 -0700
   208.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/interface.c	Fri Aug 26 13:47:16 2005 -0700
   208.3 @@ -124,6 +124,7 @@ static void free_blkif(void *arg)
   208.4      if (blkif->blk_ring.sring) {
   208.5  	unmap_frontend_page(blkif);
   208.6  	vfree(blkif->blk_ring.sring);
   208.7 +	blkif->blk_ring.sring = NULL;
   208.8      }
   208.9  
  208.10      kmem_cache_free(blkif_cachep, blkif);
   213.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Thu Aug 25 13:52:38 2005 -0700
   213.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Fri Aug 26 13:47:16 2005 -0700
   213.3 @@ -1258,6 +1258,7 @@ static int blkfront_probe(struct xenbus_
   213.4  	err = talk_to_backend(dev, info);
   213.5  	if (err) {
   213.6  		kfree(info);
   213.7 +		dev->data = NULL;
   213.8  		return err;
   213.9  	}
  213.10  
   234.1 --- a/linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c	Thu Aug 25 13:52:38 2005 -0700
   234.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c	Fri Aug 26 13:47:16 2005 -0700
   234.3 @@ -939,7 +939,6 @@ static int create_netdev(int handle, str
   234.4  
   234.5  static int destroy_netdev(struct net_device *netdev)
   234.6  {
   234.7 -	struct net_private *np = NULL;
   234.8  
   234.9  #ifdef CONFIG_PROC_FS
  234.10  	xennet_proc_delif(netdev);
  234.11 @@ -947,11 +946,6 @@ static int destroy_netdev(struct net_dev
  234.12  
  234.13          unregister_netdev(netdev);
  234.14  
  234.15 -	np = netdev_priv(netdev);
  234.16 -	list_del(&np->list);
  234.17 -
  234.18 -	kfree(netdev);
  234.19 -
  234.20  	return 0;
  234.21  }
  234.22  
  234.23 @@ -1244,12 +1238,17 @@ static int netfront_probe(struct xenbus_
  234.24  	}
  234.25  
  234.26  	info = netdev_priv(netdev);
  234.27 +	dev->data = info;
  234.28 +
  234.29  	err = talk_to_backend(dev, info);
  234.30  	if (err) {
  234.31  		destroy_netdev(netdev);
  234.32 +		kfree(netdev);
  234.33 +		dev->data = NULL;
  234.34  		return err;
  234.35  	}
  234.36  
  234.37 +
  234.38  	/* Call once in case entries already there. */
  234.39  	watch_for_status(&info->watch, info->watch.node);
  234.40  
   236.1 --- a/linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c	Thu Aug 25 13:52:38 2005 -0700
   236.2 +++ b/linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c	Fri Aug 26 13:47:16 2005 -0700
   236.3 @@ -63,16 +63,19 @@ static int privcmd_ioctl(struct inode *i
   236.4              "popl %%edi; popl %%esi; popl %%edx; popl %%ecx; popl %%ebx"
   236.5              : "=a" (ret) : "0" (&hypercall) : "memory" );
   236.6  #elif defined (__x86_64__)
   236.7 -	__asm__ __volatile__ (
   236.8 -	    "movq   %5,%%r10; movq %6,%%r8;" TRAP_INSTR
   236.9 -	    : "=a" (ret)
  236.10 -	    : "a" ((unsigned long)hypercall.op), 
  236.11 -	      "D" ((unsigned long)hypercall.arg[0]), 
  236.12 -	      "S" ((unsigned long)hypercall.arg[1]),
  236.13 -	      "d" ((unsigned long)hypercall.arg[2]), 
  236.14 -	      "g" ((unsigned long)hypercall.arg[3]),
  236.15 -	      "g" ((unsigned long)hypercall.arg[4])
  236.16 -	    : "r11","rcx","r8","r10","memory");
  236.17 +        {
  236.18 +            long ign1, ign2, ign3;
  236.19 +            __asm__ __volatile__ (
  236.20 +                "movq   %5,%%r10; movq %6,%%r8;" TRAP_INSTR
  236.21 +                : "=a" (ret), "=D" (ign1), "=S" (ign2), "=d" (ign3)
  236.22 +                : "0" ((unsigned long)hypercall.op), 
  236.23 +                "1" ((unsigned long)hypercall.arg[0]), 
  236.24 +                "2" ((unsigned long)hypercall.arg[1]),
  236.25 +                "3" ((unsigned long)hypercall.arg[2]), 
  236.26 +                "g" ((unsigned long)hypercall.arg[3]),
  236.27 +                "g" ((unsigned long)hypercall.arg[4])
  236.28 +                : "r11","rcx","r8","r10","memory");
  236.29 +        }
  236.30  #endif
  236.31      }
  236.32      break;
   256.1 --- a/linux-2.6-xen-sparse/include/asm-xen/asm-i386/hypercall.h	Thu Aug 25 13:52:38 2005 -0700
   256.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-i386/hypercall.h	Fri Aug 26 13:47:16 2005 -0700
   256.3 @@ -29,551 +29,362 @@
   256.4  
   256.5  #ifndef __HYPERCALL_H__
   256.6  #define __HYPERCALL_H__
   256.7 +
   256.8  #include <asm-xen/xen-public/xen.h>
   256.9  
  256.10 -/*
  256.11 - * Assembler stubs for hyper-calls.
  256.12 - */
  256.13 +#define _hypercall0(type, name)			\
  256.14 +({						\
  256.15 +	long __res;				\
  256.16 +	asm volatile (				\
  256.17 +		TRAP_INSTR			\
  256.18 +		: "=a" (__res)			\
  256.19 +		: "0" (__HYPERVISOR_##name)	\
  256.20 +		: "memory" );			\
  256.21 +	(type)__res;				\
  256.22 +})
  256.23 +
  256.24 +#define _hypercall1(type, name, a1)				\
  256.25 +({								\
  256.26 +	long __res, __ign1;					\
  256.27 +	asm volatile (						\
  256.28 +		TRAP_INSTR					\
  256.29 +		: "=a" (__res), "=b" (__ign1)			\
  256.30 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1))	\
  256.31 +		: "memory" );					\
  256.32 +	(type)__res;						\
  256.33 +})
  256.34 +
  256.35 +#define _hypercall2(type, name, a1, a2)				\
  256.36 +({								\
  256.37 +	long __res, __ign1, __ign2;				\
  256.38 +	asm volatile (						\
  256.39 +		TRAP_INSTR					\
  256.40 +		: "=a" (__res), "=b" (__ign1), "=c" (__ign2)	\
  256.41 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  256.42 +		"2" ((long)(a2))				\
  256.43 +		: "memory" );					\
  256.44 +	(type)__res;						\
  256.45 +})
  256.46 +
  256.47 +#define _hypercall3(type, name, a1, a2, a3)			\
  256.48 +({								\
  256.49 +	long __res, __ign1, __ign2, __ign3;			\
  256.50 +	asm volatile (						\
  256.51 +		TRAP_INSTR					\
  256.52 +		: "=a" (__res), "=b" (__ign1), "=c" (__ign2), 	\
  256.53 +		"=d" (__ign3)					\
  256.54 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  256.55 +		"2" ((long)(a2)), "3" ((long)(a3))		\
  256.56 +		: "memory" );					\
  256.57 +	(type)__res;						\
  256.58 +})
  256.59 +
  256.60 +#define _hypercall4(type, name, a1, a2, a3, a4)			\
  256.61 +({								\
  256.62 +	long __res, __ign1, __ign2, __ign3, __ign4;		\
  256.63 +	asm volatile (						\
  256.64 +		TRAP_INSTR					\
  256.65 +		: "=a" (__res), "=b" (__ign1), "=c" (__ign2),	\
  256.66 +		"=d" (__ign3), "=S" (__ign4)			\
  256.67 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  256.68 +		"2" ((long)(a2)), "3" ((long)(a3)),		\
  256.69 +		"4" ((long)(a4))				\
  256.70 +		: "memory" );					\
  256.71 +	(type)__res;						\
  256.72 +})
  256.73 +
  256.74 +#define _hypercall5(type, name, a1, a2, a3, a4, a5)		\
  256.75 +({								\
  256.76 +	long __res, __ign1, __ign2, __ign3, __ign4, __ign5;	\
  256.77 +	asm volatile (						\
  256.78 +		TRAP_INSTR					\
  256.79 +		: "=a" (__res), "=b" (__ign1), "=c" (__ign2),	\
  256.80 +		"=d" (__ign3), "=S" (__ign4), "=D" (__ign5)	\
  256.81 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  256.82 +		"2" ((long)(a2)), "3" ((long)(a3)),		\
  256.83 +		"4" ((long)(a4)), "5" ((long)(a5))		\
  256.84 +		: "memory" );					\
  256.85 +	(type)__res;						\
  256.86 +})
  256.87  
  256.88  static inline int
  256.89  HYPERVISOR_set_trap_table(
  256.90 -    trap_info_t *table)
  256.91 +	trap_info_t *table)
  256.92  {
  256.93 -    int ret;
  256.94 -    unsigned long ignore;
  256.95 -
  256.96 -    __asm__ __volatile__ (
  256.97 -        TRAP_INSTR
  256.98 -        : "=a" (ret), "=b" (ignore)
  256.99 -	: "0" (__HYPERVISOR_set_trap_table), "1" (table)
 256.100 -	: "memory" );
 256.101 -
 256.102 -    return ret;
 256.103 +	return _hypercall1(int, set_trap_table, table);
 256.104  }
 256.105  
 256.106  static inline int
 256.107  HYPERVISOR_mmu_update(
 256.108 -    mmu_update_t *req, int count, int *success_count, domid_t domid)
 256.109 +	mmu_update_t *req, int count, int *success_count, domid_t domid)
 256.110  {
 256.111 -    int ret;
 256.112 -    unsigned long ign1, ign2, ign3, ign4;
 256.113 -
 256.114 -    __asm__ __volatile__ (
 256.115 -        TRAP_INSTR
 256.116 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4)
 256.117 -	: "0" (__HYPERVISOR_mmu_update), "1" (req), "2" (count),
 256.118 -        "3" (success_count), "4" (domid)
 256.119 -	: "memory" );
 256.120 -
 256.121 -    return ret;
 256.122 +	return _hypercall4(int, mmu_update, req, count, success_count, domid);
 256.123  }
 256.124  
 256.125  static inline int
 256.126  HYPERVISOR_mmuext_op(
 256.127 -    struct mmuext_op *op, int count, int *success_count, domid_t domid)
 256.128 +	struct mmuext_op *op, int count, int *success_count, domid_t domid)
 256.129  {
 256.130 -    int ret;
 256.131 -    unsigned long ign1, ign2, ign3, ign4;
 256.132 -
 256.133 -    __asm__ __volatile__ (
 256.134 -        TRAP_INSTR
 256.135 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4)
 256.136 -	: "0" (__HYPERVISOR_mmuext_op), "1" (op), "2" (count),
 256.137 -        "3" (success_count), "4" (domid)
 256.138 -	: "memory" );
 256.139 -
 256.140 -    return ret;
 256.141 +	return _hypercall4(int, mmuext_op, op, count, success_count, domid);
 256.142  }
 256.143  
 256.144  static inline int
 256.145  HYPERVISOR_set_gdt(
 256.146 -    unsigned long *frame_list, int entries)
 256.147 +	unsigned long *frame_list, int entries)
 256.148  {
 256.149 -    int ret;
 256.150 -    unsigned long ign1, ign2;
 256.151 -
 256.152 -    __asm__ __volatile__ (
 256.153 -        TRAP_INSTR
 256.154 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.155 -	: "0" (__HYPERVISOR_set_gdt), "1" (frame_list), "2" (entries)
 256.156 -	: "memory" );
 256.157 -
 256.158 -
 256.159 -    return ret;
 256.160 +	return _hypercall2(int, set_gdt, frame_list, entries);
 256.161  }
 256.162  
 256.163  static inline int
 256.164  HYPERVISOR_stack_switch(
 256.165 -    unsigned long ss, unsigned long esp)
 256.166 +	unsigned long ss, unsigned long esp)
 256.167  {
 256.168 -    int ret;
 256.169 -    unsigned long ign1, ign2;
 256.170 -
 256.171 -    __asm__ __volatile__ (
 256.172 -        TRAP_INSTR
 256.173 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.174 -	: "0" (__HYPERVISOR_stack_switch), "1" (ss), "2" (esp)
 256.175 -	: "memory" );
 256.176 -
 256.177 -    return ret;
 256.178 +	return _hypercall2(int, stack_switch, ss, esp);
 256.179  }
 256.180  
 256.181  static inline int
 256.182  HYPERVISOR_set_callbacks(
 256.183 -    unsigned long event_selector, unsigned long event_address,
 256.184 -    unsigned long failsafe_selector, unsigned long failsafe_address)
 256.185 +	unsigned long event_selector, unsigned long event_address,
 256.186 +	unsigned long failsafe_selector, unsigned long failsafe_address)
 256.187  {
 256.188 -    int ret;
 256.189 -    unsigned long ign1, ign2, ign3, ign4;
 256.190 -
 256.191 -    __asm__ __volatile__ (
 256.192 -        TRAP_INSTR
 256.193 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4)
 256.194 -	: "0" (__HYPERVISOR_set_callbacks), "1" (event_selector),
 256.195 -	  "2" (event_address), "3" (failsafe_selector), "4" (failsafe_address)
 256.196 -	: "memory" );
 256.197 -
 256.198 -    return ret;
 256.199 +	return _hypercall4(int, set_callbacks,
 256.200 +			   event_selector, event_address,
 256.201 +			   failsafe_selector, failsafe_address);
 256.202  }
 256.203  
 256.204  static inline int
 256.205  HYPERVISOR_fpu_taskswitch(
 256.206 -    int set)
 256.207 +	int set)
 256.208  {
 256.209 -    int ret;
 256.210 -    unsigned long ign;
 256.211 -
 256.212 -    __asm__ __volatile__ (
 256.213 -        TRAP_INSTR
 256.214 -        : "=a" (ret), "=b" (ign)
 256.215 -        : "0" (__HYPERVISOR_fpu_taskswitch), "1" (set)
 256.216 -        : "memory" );
 256.217 -
 256.218 -    return ret;
 256.219 +	return _hypercall1(int, fpu_taskswitch, set);
 256.220  }
 256.221  
 256.222  static inline int
 256.223  HYPERVISOR_yield(
 256.224 -    void)
 256.225 +	void)
 256.226  {
 256.227 -    int ret;
 256.228 -    unsigned long ign;
 256.229 -
 256.230 -    __asm__ __volatile__ (
 256.231 -        TRAP_INSTR
 256.232 -        : "=a" (ret), "=b" (ign)
 256.233 -	: "0" (__HYPERVISOR_sched_op), "1" (SCHEDOP_yield)
 256.234 -	: "memory", "ecx" );
 256.235 -
 256.236 -    return ret;
 256.237 +	return _hypercall2(int, sched_op, SCHEDOP_yield, 0);
 256.238  }
 256.239  
 256.240  static inline int
 256.241  HYPERVISOR_block(
 256.242 -    void)
 256.243 +	void)
 256.244  {
 256.245 -    int ret;
 256.246 -    unsigned long ign1;
 256.247 -    __asm__ __volatile__ (
 256.248 -        TRAP_INSTR
 256.249 -        : "=a" (ret), "=b" (ign1)
 256.250 -	: "0" (__HYPERVISOR_sched_op), "1" (SCHEDOP_block)
 256.251 -	: "memory", "ecx" );
 256.252 -
 256.253 -    return ret;
 256.254 +	return _hypercall2(int, sched_op, SCHEDOP_block, 0);
 256.255  }
 256.256  
 256.257  static inline int
 256.258  HYPERVISOR_shutdown(
 256.259 -    void)
 256.260 +	void)
 256.261  {
 256.262 -    int ret;
 256.263 -    unsigned long ign1;
 256.264 -    __asm__ __volatile__ (
 256.265 -        TRAP_INSTR
 256.266 -        : "=a" (ret), "=b" (ign1)
 256.267 -	: "0" (__HYPERVISOR_sched_op),
 256.268 -	  "1" (SCHEDOP_shutdown | (SHUTDOWN_poweroff << SCHEDOP_reasonshift))
 256.269 -        : "memory", "ecx" );
 256.270 -
 256.271 -    return ret;
 256.272 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 256.273 +			   (SHUTDOWN_poweroff << SCHEDOP_reasonshift), 0);
 256.274  }
 256.275  
 256.276  static inline int
 256.277  HYPERVISOR_reboot(
 256.278 -    void)
 256.279 +	void)
 256.280  {
 256.281 -    int ret;
 256.282 -    unsigned long ign1;
 256.283 -    __asm__ __volatile__ (
 256.284 -        TRAP_INSTR
 256.285 -        : "=a" (ret), "=b" (ign1)
 256.286 -	: "0" (__HYPERVISOR_sched_op),
 256.287 -	  "1" (SCHEDOP_shutdown | (SHUTDOWN_reboot << SCHEDOP_reasonshift))
 256.288 -        : "memory", "ecx" );
 256.289 -
 256.290 -    return ret;
 256.291 -}
 256.292 -
 256.293 -static inline int
 256.294 -HYPERVISOR_suspend(
 256.295 -    unsigned long srec)
 256.296 -{
 256.297 -    int ret;
 256.298 -    unsigned long ign1, ign2;
 256.299 -
 256.300 -    /* NB. On suspend, control software expects a suspend record in %esi. */
 256.301 -    __asm__ __volatile__ (
 256.302 -        TRAP_INSTR
 256.303 -        : "=a" (ret), "=b" (ign1), "=S" (ign2)
 256.304 -	: "0" (__HYPERVISOR_sched_op),
 256.305 -        "b" (SCHEDOP_shutdown | (SHUTDOWN_suspend << SCHEDOP_reasonshift)), 
 256.306 -        "S" (srec) : "memory", "ecx");
 256.307 -
 256.308 -    return ret;
 256.309 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 256.310 +			   (SHUTDOWN_reboot << SCHEDOP_reasonshift), 0);
 256.311  }
 256.312  
 256.313  static inline int
 256.314  HYPERVISOR_crash(
 256.315 -    void)
 256.316 +	void)
 256.317  {
 256.318 -    int ret;
 256.319 -    unsigned long ign1;
 256.320 -    __asm__ __volatile__ (
 256.321 -        TRAP_INSTR
 256.322 -        : "=a" (ret), "=b" (ign1)
 256.323 -	: "0" (__HYPERVISOR_sched_op),
 256.324 -	  "1" (SCHEDOP_shutdown | (SHUTDOWN_crash << SCHEDOP_reasonshift))
 256.325 -        : "memory", "ecx" );
 256.326 -
 256.327 -    return ret;
 256.328 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 256.329 +			   (SHUTDOWN_crash << SCHEDOP_reasonshift), 0);
 256.330  }
 256.331  
 256.332  static inline long
 256.333  HYPERVISOR_set_timer_op(
 256.334 -    u64 timeout)
 256.335 +	u64 timeout)
 256.336  {
 256.337 -    int ret;
 256.338 -    unsigned long timeout_hi = (unsigned long)(timeout>>32);
 256.339 -    unsigned long timeout_lo = (unsigned long)timeout;
 256.340 -    unsigned long ign1, ign2;
 256.341 -
 256.342 -    __asm__ __volatile__ (
 256.343 -        TRAP_INSTR
 256.344 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.345 -	: "0" (__HYPERVISOR_set_timer_op), "b" (timeout_lo), "c" (timeout_hi)
 256.346 -	: "memory");
 256.347 -
 256.348 -    return ret;
 256.349 +	unsigned long timeout_hi = (unsigned long)(timeout>>32);
 256.350 +	unsigned long timeout_lo = (unsigned long)timeout;
 256.351 +	return _hypercall2(long, set_timer_op, timeout_lo, timeout_hi);
 256.352  }
 256.353  
 256.354  static inline int
 256.355  HYPERVISOR_dom0_op(
 256.356 -    dom0_op_t *dom0_op)
 256.357 +	dom0_op_t *dom0_op)
 256.358  {
 256.359 -    int ret;
 256.360 -    unsigned long ign1;
 256.361 -
 256.362 -    dom0_op->interface_version = DOM0_INTERFACE_VERSION;
 256.363 -    __asm__ __volatile__ (
 256.364 -        TRAP_INSTR
 256.365 -        : "=a" (ret), "=b" (ign1)
 256.366 -	: "0" (__HYPERVISOR_dom0_op), "1" (dom0_op)
 256.367 -	: "memory");
 256.368 -
 256.369 -    return ret;
 256.370 +	dom0_op->interface_version = DOM0_INTERFACE_VERSION;
 256.371 +	return _hypercall1(int, dom0_op, dom0_op);
 256.372  }
 256.373  
 256.374  static inline int
 256.375  HYPERVISOR_set_debugreg(
 256.376 -    int reg, unsigned long value)
 256.377 +	int reg, unsigned long value)
 256.378  {
 256.379 -    int ret;
 256.380 -    unsigned long ign1, ign2;
 256.381 -    __asm__ __volatile__ (
 256.382 -        TRAP_INSTR
 256.383 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.384 -	: "0" (__HYPERVISOR_set_debugreg), "1" (reg), "2" (value)
 256.385 -	: "memory" );
 256.386 -
 256.387 -    return ret;
 256.388 +	return _hypercall2(int, set_debugreg, reg, value);
 256.389  }
 256.390  
 256.391  static inline unsigned long
 256.392  HYPERVISOR_get_debugreg(
 256.393 -    int reg)
 256.394 +	int reg)
 256.395  {
 256.396 -    unsigned long ret;
 256.397 -    unsigned long ign;
 256.398 -    __asm__ __volatile__ (
 256.399 -        TRAP_INSTR
 256.400 -        : "=a" (ret), "=b" (ign)
 256.401 -	: "0" (__HYPERVISOR_get_debugreg), "1" (reg)
 256.402 -	: "memory" );
 256.403 -
 256.404 -    return ret;
 256.405 +	return _hypercall1(unsigned long, get_debugreg, reg);
 256.406  }
 256.407  
 256.408  static inline int
 256.409  HYPERVISOR_update_descriptor(
 256.410 -    u64 ma, u64 desc)
 256.411 +	u64 ma, u64 desc)
 256.412  {
 256.413 -    int ret;
 256.414 -    unsigned long ign1, ign2, ign3, ign4;
 256.415 -
 256.416 -    __asm__ __volatile__ (
 256.417 -        TRAP_INSTR
 256.418 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4)
 256.419 -	: "0" (__HYPERVISOR_update_descriptor),
 256.420 -	  "1" ((unsigned long)ma), "2" ((unsigned long)(ma>>32)),
 256.421 -	  "3" ((unsigned long)desc), "4" ((unsigned long)(desc>>32))
 256.422 -	: "memory" );
 256.423 -
 256.424 -    return ret;
 256.425 +	return _hypercall4(int, update_descriptor, ma, ma>>32, desc, desc>>32);
 256.426  }
 256.427  
 256.428  static inline int
 256.429  HYPERVISOR_dom_mem_op(
 256.430 -    unsigned int op, unsigned long *extent_list,
 256.431 -    unsigned long nr_extents, unsigned int extent_order)
 256.432 +	unsigned int op, unsigned long *extent_list,
 256.433 +	unsigned long nr_extents, unsigned int extent_order)
 256.434  {
 256.435 -    int ret;
 256.436 -    unsigned long ign1, ign2, ign3, ign4, ign5;
 256.437 -
 256.438 -    __asm__ __volatile__ (
 256.439 -        TRAP_INSTR
 256.440 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4),
 256.441 -	  "=D" (ign5)
 256.442 -	: "0" (__HYPERVISOR_dom_mem_op), "1" (op), "2" (extent_list),
 256.443 -	  "3" (nr_extents), "4" (extent_order), "5" (DOMID_SELF)
 256.444 -        : "memory" );
 256.445 -
 256.446 -    return ret;
 256.447 +	return _hypercall5(int, dom_mem_op, op, extent_list,
 256.448 +			   nr_extents, extent_order, DOMID_SELF);
 256.449  }
 256.450  
 256.451  static inline int
 256.452  HYPERVISOR_multicall(
 256.453 -    void *call_list, int nr_calls)
 256.454 +	void *call_list, int nr_calls)
 256.455  {
 256.456 -    int ret;
 256.457 -    unsigned long ign1, ign2;
 256.458 -
 256.459 -    __asm__ __volatile__ (
 256.460 -        TRAP_INSTR
 256.461 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.462 -	: "0" (__HYPERVISOR_multicall), "1" (call_list), "2" (nr_calls)
 256.463 -	: "memory" );
 256.464 -
 256.465 -    return ret;
 256.466 +	return _hypercall2(int, multicall, call_list, nr_calls);
 256.467  }
 256.468  
 256.469  static inline int
 256.470  HYPERVISOR_update_va_mapping(
 256.471 -    unsigned long va, pte_t new_val, unsigned long flags)
 256.472 +	unsigned long va, pte_t new_val, unsigned long flags)
 256.473  {
 256.474 -    int ret;
 256.475 -    unsigned long ign1, ign2, ign3, ign4;
 256.476 -
 256.477 -    __asm__ __volatile__ (
 256.478 -        TRAP_INSTR
 256.479 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3), "=S" (ign4)
 256.480 -	: "0" (__HYPERVISOR_update_va_mapping), 
 256.481 -          "1" (va), "2" ((new_val).pte_low),
 256.482 +	unsigned long pte_hi = 0;
 256.483  #ifdef CONFIG_X86_PAE
 256.484 -	  "3" ((new_val).pte_high),
 256.485 -#else
 256.486 -	  "3" (0),
 256.487 +	pte_hi = new_val.pte_high;
 256.488  #endif
 256.489 -	  "4" (flags)
 256.490 -	: "memory" );
 256.491 -
 256.492 -    return ret;
 256.493 +	return _hypercall4(int, update_va_mapping, va,
 256.494 +			   new_val.pte_low, pte_hi, flags);
 256.495  }
 256.496  
 256.497  static inline int
 256.498  HYPERVISOR_event_channel_op(
 256.499 -    void *op)
 256.500 +	void *op)
 256.501  {
 256.502 -    int ret;
 256.503 -    unsigned long ignore;
 256.504 -    __asm__ __volatile__ (
 256.505 -        TRAP_INSTR
 256.506 -        : "=a" (ret), "=b" (ignore)
 256.507 -	: "0" (__HYPERVISOR_event_channel_op), "1" (op)
 256.508 -	: "memory" );
 256.509 -
 256.510 -    return ret;
 256.511 +	return _hypercall1(int, event_channel_op, op);
 256.512  }
 256.513  
 256.514  static inline int
 256.515  HYPERVISOR_xen_version(
 256.516 -    int cmd)
 256.517 +	int cmd)
 256.518  {
 256.519 -    int ret;
 256.520 -    unsigned long ignore;
 256.521 -
 256.522 -    __asm__ __volatile__ (
 256.523 -        TRAP_INSTR
 256.524 -        : "=a" (ret), "=b" (ignore)
 256.525 -	: "0" (__HYPERVISOR_xen_version), "1" (cmd)
 256.526 -	: "memory" );
 256.527 -
 256.528 -    return ret;
 256.529 +	return _hypercall1(int, xen_version, cmd);
 256.530  }
 256.531  
 256.532  static inline int
 256.533  HYPERVISOR_console_io(
 256.534 -    int cmd, int count, char *str)
 256.535 +	int cmd, int count, char *str)
 256.536  {
 256.537 -    int ret;
 256.538 -    unsigned long ign1, ign2, ign3;
 256.539 -    __asm__ __volatile__ (
 256.540 -        TRAP_INSTR
 256.541 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3)
 256.542 -	: "0" (__HYPERVISOR_console_io), "1" (cmd), "2" (count), "3" (str)
 256.543 -	: "memory" );
 256.544 -
 256.545 -    return ret;
 256.546 +	return _hypercall3(int, console_io, cmd, count, str);
 256.547  }
 256.548  
 256.549  static inline int
 256.550  HYPERVISOR_physdev_op(
 256.551 -    void *physdev_op)
 256.552 +	void *physdev_op)
 256.553  {
 256.554 -    int ret;
 256.555 -    unsigned long ign;
 256.556 -
 256.557 -    __asm__ __volatile__ (
 256.558 -        TRAP_INSTR
 256.559 -        : "=a" (ret), "=b" (ign)
 256.560 -	: "0" (__HYPERVISOR_physdev_op), "1" (physdev_op)
 256.561 -	: "memory" );
 256.562 -
 256.563 -    return ret;
 256.564 +	return _hypercall1(int, physdev_op, physdev_op);
 256.565  }
 256.566  
 256.567  static inline int
 256.568  HYPERVISOR_grant_table_op(
 256.569 -    unsigned int cmd, void *uop, unsigned int count)
 256.570 +	unsigned int cmd, void *uop, unsigned int count)
 256.571  {
 256.572 -    int ret;
 256.573 -    unsigned long ign1, ign2, ign3;
 256.574 -
 256.575 -    __asm__ __volatile__ (
 256.576 -        TRAP_INSTR
 256.577 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3)
 256.578 -	: "0" (__HYPERVISOR_grant_table_op), "1" (cmd), "2" (uop), "3" (count)
 256.579 -	: "memory" );
 256.580 -
 256.581 -    return ret;
 256.582 +	return _hypercall3(int, grant_table_op, cmd, uop, count);
 256.583  }
 256.584  
 256.585  static inline int
 256.586  HYPERVISOR_update_va_mapping_otherdomain(
 256.587 -    unsigned long va, pte_t new_val, unsigned long flags, domid_t domid)
 256.588 +	unsigned long va, pte_t new_val, unsigned long flags, domid_t domid)
 256.589  {
 256.590 -    int ret;
 256.591 -    unsigned long ign1, ign2, ign3, ign4, ign5;
 256.592 -
 256.593 -    __asm__ __volatile__ (
 256.594 -        TRAP_INSTR
 256.595 -        : "=a" (ret), "=b" (ign1), "=c" (ign2), "=d" (ign3),
 256.596 -	  "=S" (ign4), "=D" (ign5)
 256.597 -	: "0" (__HYPERVISOR_update_va_mapping_otherdomain),
 256.598 -          "1" (va), "2" ((new_val).pte_low),
 256.599 +	unsigned long pte_hi = 0;
 256.600  #ifdef CONFIG_X86_PAE
 256.601 -	  "3" ((new_val).pte_high),
 256.602 -#else
 256.603 -	  "3" (0),
 256.604 +	pte_hi = new_val.pte_high;
 256.605  #endif
 256.606 -	  "4" (flags), "5" (domid) :
 256.607 -        "memory" );
 256.608 -    
 256.609 -    return ret;
 256.610 +	return _hypercall5(int, update_va_mapping_otherdomain, va,
 256.611 +			   new_val.pte_low, pte_hi, flags, domid);
 256.612  }
 256.613  
 256.614  static inline int
 256.615  HYPERVISOR_vm_assist(
 256.616 -    unsigned int cmd, unsigned int type)
 256.617 +	unsigned int cmd, unsigned int type)
 256.618  {
 256.619 -    int ret;
 256.620 -    unsigned long ign1, ign2;
 256.621 -
 256.622 -    __asm__ __volatile__ (
 256.623 -        TRAP_INSTR
 256.624 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.625 -	: "0" (__HYPERVISOR_vm_assist), "1" (cmd), "2" (type)
 256.626 -	: "memory" );
 256.627 -
 256.628 -    return ret;
 256.629 +	return _hypercall2(int, vm_assist, cmd, type);
 256.630  }
 256.631  
 256.632  static inline int
 256.633  HYPERVISOR_boot_vcpu(
 256.634 -    unsigned long vcpu, vcpu_guest_context_t *ctxt)
 256.635 +	unsigned long vcpu, vcpu_guest_context_t *ctxt)
 256.636  {
 256.637 -    int ret;
 256.638 -    unsigned long ign1, ign2;
 256.639 +	return _hypercall2(int, boot_vcpu, vcpu, ctxt);
 256.640 +}
 256.641 +
 256.642 +static inline int
 256.643 +HYPERVISOR_vcpu_up(
 256.644 +	int vcpu)
 256.645 +{
 256.646 +	return _hypercall2(int, sched_op, SCHEDOP_vcpu_up |
 256.647 +			   (vcpu << SCHEDOP_vcpushift), 0);
 256.648 +}
 256.649  
 256.650 -    __asm__ __volatile__ (
 256.651 -        TRAP_INSTR
 256.652 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.653 -	: "0" (__HYPERVISOR_boot_vcpu), "1" (vcpu), "2" (ctxt)
 256.654 -	: "memory");
 256.655 +static inline int
 256.656 +HYPERVISOR_vcpu_pickle(
 256.657 +	int vcpu, vcpu_guest_context_t *ctxt)
 256.658 +{
 256.659 +	return _hypercall2(int, sched_op, SCHEDOP_vcpu_pickle |
 256.660 +			   (vcpu << SCHEDOP_vcpushift), ctxt);
 256.661 +}
 256.662  
 256.663 -    return ret;
 256.664 +static inline int
 256.665 +HYPERVISOR_suspend(
 256.666 +	unsigned long srec)
 256.667 +{
 256.668 +	int ret;
 256.669 +	unsigned long ign1, ign2;
 256.670 +
 256.671 +	/* On suspend, control software expects a suspend record in %esi. */
 256.672 +	__asm__ __volatile__ (
 256.673 +		TRAP_INSTR
 256.674 +		: "=a" (ret), "=b" (ign1), "=S" (ign2)
 256.675 +		: "0" (__HYPERVISOR_sched_op),
 256.676 +		"1" (SCHEDOP_shutdown | (SHUTDOWN_suspend <<
 256.677 +					 SCHEDOP_reasonshift)), 
 256.678 +		"2" (srec) : "memory", "ecx");
 256.679 +
 256.680 +	return ret;
 256.681  }
 256.682  
 256.683  static inline int
 256.684  HYPERVISOR_vcpu_down(
 256.685 -    int vcpu)
 256.686 -{
 256.687 -    int ret;
 256.688 -    unsigned long ign1;
 256.689 -    /* Yes, I really do want to clobber edx here: when we resume a
 256.690 -       vcpu after unpickling a multi-processor domain, it returns
 256.691 -       here, but clobbers all of the call clobbered registers. */
 256.692 -    __asm__ __volatile__ (
 256.693 -        TRAP_INSTR
 256.694 -        : "=a" (ret), "=b" (ign1)
 256.695 -	: "0" (__HYPERVISOR_sched_op),
 256.696 -	  "1" (SCHEDOP_vcpu_down | (vcpu << SCHEDOP_vcpushift))
 256.697 -        : "memory", "ecx", "edx" );
 256.698 -
 256.699 -    return ret;
 256.700 -}
 256.701 -
 256.702 -static inline int
 256.703 -HYPERVISOR_vcpu_up(
 256.704 -    int vcpu)
 256.705 +	int vcpu)
 256.706  {
 256.707 -    int ret;
 256.708 -    unsigned long ign1;
 256.709 -    __asm__ __volatile__ (
 256.710 -        TRAP_INSTR
 256.711 -        : "=a" (ret), "=b" (ign1)
 256.712 -	: "0" (__HYPERVISOR_sched_op),
 256.713 -	  "1" (SCHEDOP_vcpu_up | (vcpu << SCHEDOP_vcpushift))
 256.714 -        : "memory", "ecx" );
 256.715 -
 256.716 -    return ret;
 256.717 -}
 256.718 -
 256.719 -static inline int
 256.720 -HYPERVISOR_vcpu_pickle(
 256.721 -    int vcpu, vcpu_guest_context_t *ctxt)
 256.722 -{
 256.723 -    int ret;
 256.724 -    unsigned long ign1, ign2;
 256.725 -    __asm__ __volatile__ (
 256.726 -        TRAP_INSTR
 256.727 -        : "=a" (ret), "=b" (ign1), "=c" (ign2)
 256.728 -	: "0" (__HYPERVISOR_sched_op),
 256.729 -	  "1" (SCHEDOP_vcpu_pickle | (vcpu << SCHEDOP_vcpushift)),
 256.730 -	  "2" (ctxt)
 256.731 -        : "memory" );
 256.732 -
 256.733 -    return ret;
 256.734 +	int ret;
 256.735 +	unsigned long ign1;
 256.736 +	/* Yes, I really do want to clobber edx here: when we resume a
 256.737 +	   vcpu after unpickling a multi-processor domain, it returns
 256.738 +	   here, but clobbers all of the call clobbered registers. */
 256.739 +	__asm__ __volatile__ (
 256.740 +		TRAP_INSTR
 256.741 +		: "=a" (ret), "=b" (ign1)
 256.742 +		: "0" (__HYPERVISOR_sched_op),
 256.743 +		"1" (SCHEDOP_vcpu_down | (vcpu << SCHEDOP_vcpushift))
 256.744 +		: "memory", "ecx", "edx" );
 256.745 +	return ret;
 256.746  }
 256.747  
 256.748  #endif /* __HYPERCALL_H__ */
 256.749 +
 256.750 +/*
 256.751 + * Local variables:
 256.752 + *  c-file-style: "linux"
 256.753 + *  indent-tabs-mode: t
 256.754 + *  c-indent-level: 8
 256.755 + *  c-basic-offset: 8
 256.756 + *  tab-width: 8
 256.757 + * End:
 256.758 + */
   290.1 --- a/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/hypercall.h	Thu Aug 25 13:52:38 2005 -0700
   290.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/hypercall.h	Fri Aug 26 13:47:16 2005 -0700
   290.3 @@ -5,6 +5,10 @@
   290.4   * 
   290.5   * Copyright (c) 2002-2004, K A Fraser
   290.6   * 
   290.7 + * 64-bit updates:
   290.8 + *   Benjamin Liu <benjamin.liu@intel.com>
   290.9 + *   Jun Nakajima <jun.nakajima@intel.com>
  290.10 + * 
  290.11   * This file may be distributed separately from the Linux kernel, or
  290.12   * incorporated into other software packages, subject to the following license:
  290.13   * 
  290.14 @@ -26,497 +30,331 @@
  290.15   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  290.16   * IN THE SOFTWARE.
  290.17   */
  290.18 -/*
  290.19 - * Benjamin Liu <benjamin.liu@intel.com>
  290.20 - * Jun Nakajima <jun.nakajima@intel.com>
  290.21 - *   Ported to x86-64.
  290.22 - * 
  290.23 - */
  290.24  
  290.25  #ifndef __HYPERCALL_H__
  290.26  #define __HYPERCALL_H__
  290.27 +
  290.28  #include <asm-xen/xen-public/xen.h>
  290.29  
  290.30  #define __syscall_clobber "r11","rcx","memory"
  290.31  
  290.32 -/*
  290.33 - * Assembler stubs for hyper-calls.
  290.34 - */
  290.35 +#define _hypercall0(type, name)			\
  290.36 +({						\
  290.37 +	long __res;				\
  290.38 +	asm volatile (				\
  290.39 +		TRAP_INSTR			\
  290.40 +		: "=a" (__res)			\
  290.41 +		: "0" (__HYPERVISOR_##name)	\
  290.42 +		: __syscall_clobber );		\
  290.43 +	(type)__res;				\
  290.44 +})
  290.45 +
  290.46 +#define _hypercall1(type, name, a1)				\
  290.47 +({								\
  290.48 +	long __res, __ign1;					\
  290.49 +	asm volatile (						\
  290.50 +		TRAP_INSTR					\
  290.51 +		: "=a" (__res), "=D" (__ign1)			\
  290.52 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1))	\
  290.53 +		: __syscall_clobber );				\
  290.54 +	(type)__res;						\
  290.55 +})
  290.56 +
  290.57 +#define _hypercall2(type, name, a1, a2)				\
  290.58 +({								\
  290.59 +	long __res, __ign1, __ign2;				\
  290.60 +	asm volatile (						\
  290.61 +		TRAP_INSTR					\
  290.62 +		: "=a" (__res), "=D" (__ign1), "=S" (__ign2)	\
  290.63 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  290.64 +		"2" ((long)(a2))				\
  290.65 +		: __syscall_clobber );				\
  290.66 +	(type)__res;						\
  290.67 +})
  290.68 +
  290.69 +#define _hypercall3(type, name, a1, a2, a3)			\
  290.70 +({								\
  290.71 +	long __res, __ign1, __ign2, __ign3;			\
  290.72 +	asm volatile (						\
  290.73 +		TRAP_INSTR					\
  290.74 +		: "=a" (__res), "=D" (__ign1), "=S" (__ign2), 	\
  290.75 +		"=d" (__ign3)					\
  290.76 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  290.77 +		"2" ((long)(a2)), "3" ((long)(a3))		\
  290.78 +		: __syscall_clobber );				\
  290.79 +	(type)__res;						\
  290.80 +})
  290.81 +
  290.82 +#define _hypercall4(type, name, a1, a2, a3, a4)			\
  290.83 +({								\
  290.84 +	long __res, __ign1, __ign2, __ign3;			\
  290.85 +	asm volatile (						\
  290.86 +		"movq %8,%%r10; " TRAP_INSTR			\
  290.87 +		: "=a" (__res), "=D" (__ign1), "=S" (__ign2),	\
  290.88 +		"=d" (__ign3)					\
  290.89 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
  290.90 +		"2" ((long)(a2)), "3" ((long)(a3)),		\
  290.91 +		"g" ((long)(a4))				\
  290.92 +		: __syscall_clobber, "r10" );			\
  290.93 +	(type)__res;						\
  290.94 +})
  290.95 +
  290.96 +#define _hypercall5(type, name, a1, a2, a3, a4, a5)		\
  290.97 +({								\
  290.98 +	long __res, __ign1, __ign2, __ign3;			\
  290.99 +	asm volatile (						\
 290.100 +		"movq %8,%%r10; movq %9,%%r8; " TRAP_INSTR	\
 290.101 +		: "=a" (__res), "=D" (__ign1), "=S" (__ign2),	\
 290.102 +		"=d" (__ign3)					\
 290.103 +		: "0" (__HYPERVISOR_##name), "1" ((long)(a1)),	\
 290.104 +		"2" ((long)(a2)), "3" ((long)(a3)),		\
 290.105 +		"g" ((long)(a4)), "g" ((long)(a5))		\
 290.106 +		: __syscall_clobber, "r10", "r8" );		\
 290.107 +	(type)__res;						\
 290.108 +})
 290.109 +
 290.110  static inline int
 290.111  HYPERVISOR_set_trap_table(
 290.112 -    trap_info_t *table)
 290.113 +	trap_info_t *table)
 290.114  {
 290.115 -    int ret;
 290.116 -
 290.117 -    __asm__ __volatile__ (
 290.118 -        TRAP_INSTR
 290.119 -        : "=a" (ret)
 290.120 -	: "0" ((unsigned long)__HYPERVISOR_set_trap_table), "D" (table)
 290.121 -	: __syscall_clobber );
 290.122 -
 290.123 -    return ret;
 290.124 +	return _hypercall1(int, set_trap_table, table);
 290.125  }
 290.126  
 290.127  static inline int
 290.128  HYPERVISOR_mmu_update(
 290.129 -    mmu_update_t *req, int count, int *success_count, domid_t domid)
 290.130 +	mmu_update_t *req, int count, int *success_count, domid_t domid)
 290.131  {
 290.132 -    int ret;
 290.133 -
 290.134 -    __asm__ __volatile__ (
 290.135 -        "movq %5, %%r10;" TRAP_INSTR
 290.136 -        : "=a" (ret)
 290.137 -	: "0" ((unsigned long)__HYPERVISOR_mmu_update), "D" (req), "S" ((long)count),
 290.138 -	  "d" (success_count), "g" ((unsigned long)domid)
 290.139 -	: __syscall_clobber, "r10" );
 290.140 -
 290.141 -    return ret;
 290.142 +	return _hypercall4(int, mmu_update, req, count, success_count, domid);
 290.143  }
 290.144  
 290.145  static inline int
 290.146  HYPERVISOR_mmuext_op(
 290.147 -    struct mmuext_op *op, int count, int *success_count, domid_t domid)
 290.148 +	struct mmuext_op *op, int count, int *success_count, domid_t domid)
 290.149  {
 290.150 -    int ret;
 290.151 -
 290.152 -    __asm__ __volatile__ (
 290.153 -        "movq %5, %%r10;" TRAP_INSTR
 290.154 -        : "=a" (ret)
 290.155 -        : "0" (__HYPERVISOR_mmuext_op), "D" (op), "S" ((long)count), 
 290.156 -          "d" (success_count), "g" ((unsigned long)domid)
 290.157 -        : __syscall_clobber, "r10" );
 290.158 -
 290.159 -    return ret;
 290.160 +	return _hypercall4(int, mmuext_op, op, count, success_count, domid);
 290.161  }
 290.162  
 290.163  static inline int
 290.164  HYPERVISOR_set_gdt(
 290.165 -    unsigned long *frame_list, int entries)
 290.166 +	unsigned long *frame_list, int entries)
 290.167  {
 290.168 -    int ret;
 290.169 +	return _hypercall2(int, set_gdt, frame_list, entries);
 290.170 +}
 290.171  
 290.172 -    __asm__ __volatile__ (
 290.173 -        TRAP_INSTR
 290.174 -        : "=a" (ret)
 290.175 -	: "0" ((unsigned long)__HYPERVISOR_set_gdt), "D" (frame_list), "S" ((long)entries)
 290.176 -	: __syscall_clobber );
 290.177 -
 290.178 -
 290.179 -    return ret;
 290.180 -}
 290.181  static inline int
 290.182  HYPERVISOR_stack_switch(
 290.183 -    unsigned long ss, unsigned long esp)
 290.184 +	unsigned long ss, unsigned long esp)
 290.185  {
 290.186 -    int ret;
 290.187 -
 290.188 -    __asm__ __volatile__ (
 290.189 -        TRAP_INSTR
 290.190 -        : "=a" (ret)
 290.191 -	: "0" ((unsigned long)__HYPERVISOR_stack_switch), "D" (ss), "S" (esp)
 290.192 -	: __syscall_clobber );
 290.193 -
 290.194 -    return ret;
 290.195 +	return _hypercall2(int, stack_switch, ss, esp);
 290.196  }
 290.197  
 290.198  static inline int
 290.199  HYPERVISOR_set_callbacks(
 290.200 -    unsigned long event_address, unsigned long failsafe_address, 
 290.201 -    unsigned long syscall_address)
 290.202 +	unsigned long event_address, unsigned long failsafe_address, 
 290.203 +	unsigned long syscall_address)
 290.204  {
 290.205 -    int ret;
 290.206 -
 290.207 -    __asm__ __volatile__ (
 290.208 -        TRAP_INSTR
 290.209 -        : "=a" (ret)
 290.210 -	: "0" ((unsigned long)__HYPERVISOR_set_callbacks), "D" (event_address),
 290.211 -	  "S" (failsafe_address), "d" (syscall_address)
 290.212 -	: __syscall_clobber );
 290.213 -
 290.214 -    return ret;
 290.215 +	return _hypercall3(int, set_callbacks,
 290.216 +			   event_address, failsafe_address, syscall_address);
 290.217  }
 290.218  
 290.219  static inline int
 290.220  HYPERVISOR_fpu_taskswitch(
 290.221 -    int set)
 290.222 +	int set)
 290.223  {
 290.224 -    int ret;
 290.225 -    __asm__ __volatile__ (
 290.226 -        TRAP_INSTR
 290.227 -        : "=a" (ret) : "0" ((unsigned long)__HYPERVISOR_fpu_taskswitch),
 290.228 -          "D" ((unsigned long) set) : __syscall_clobber );
 290.229 -
 290.230 -    return ret;
 290.231 +	return _hypercall1(int, fpu_taskswitch, set);
 290.232  }
 290.233  
 290.234  static inline int
 290.235  HYPERVISOR_yield(
 290.236 -    void)
 290.237 +	void)
 290.238  {
 290.239 -    int ret;
 290.240 -
 290.241 -    __asm__ __volatile__ (
 290.242 -        TRAP_INSTR
 290.243 -        : "=a" (ret)
 290.244 -	: "0" ((unsigned long)__HYPERVISOR_sched_op), "D" ((unsigned long)SCHEDOP_yield)
 290.245 -	: __syscall_clobber );
 290.246 -
 290.247 -    return ret;
 290.248 +	return _hypercall2(int, sched_op, SCHEDOP_yield, 0);
 290.249  }
 290.250  
 290.251  static inline int
 290.252  HYPERVISOR_block(
 290.253 -    void)
 290.254 +	void)
 290.255  {
 290.256 -    int ret;
 290.257 -    __asm__ __volatile__ (
 290.258 -        TRAP_INSTR
 290.259 -        : "=a" (ret)
 290.260 -	: "0" ((unsigned long)__HYPERVISOR_sched_op), "D" ((unsigned long)SCHEDOP_block)
 290.261 -	: __syscall_clobber );
 290.262 -
 290.263 -    return ret;
 290.264 +	return _hypercall2(int, sched_op, SCHEDOP_block, 0);
 290.265  }
 290.266  
 290.267  static inline int
 290.268  HYPERVISOR_shutdown(
 290.269 -    void)
 290.270 +	void)
 290.271  {
 290.272 -    int ret;
 290.273 -    __asm__ __volatile__ (
 290.274 -        TRAP_INSTR
 290.275 -        : "=a" (ret)
 290.276 -	: "0" ((unsigned long)__HYPERVISOR_sched_op),
 290.277 -	  "D" ((unsigned long)(SCHEDOP_shutdown | (SHUTDOWN_poweroff << SCHEDOP_reasonshift)))
 290.278 -	: __syscall_clobber );
 290.279 -
 290.280 -    return ret;
 290.281 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 290.282 +			   (SHUTDOWN_poweroff << SCHEDOP_reasonshift), 0);
 290.283  }
 290.284  
 290.285  static inline int
 290.286  HYPERVISOR_reboot(
 290.287 -    void)
 290.288 +	void)
 290.289  {
 290.290 -    int ret;
 290.291 -
 290.292 -    __asm__ __volatile__ (
 290.293 -        TRAP_INSTR
 290.294 -        : "=a" (ret)
 290.295 -	: "0" ((unsigned long)__HYPERVISOR_sched_op),
 290.296 -	  "D" ((unsigned long)(SCHEDOP_shutdown | (SHUTDOWN_reboot << SCHEDOP_reasonshift)))
 290.297 -	: __syscall_clobber );
 290.298 -
 290.299 -    return ret;
 290.300 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 290.301 +			   (SHUTDOWN_reboot << SCHEDOP_reasonshift), 0);
 290.302  }
 290.303  
 290.304 -static inline int
 290.305 -HYPERVISOR_suspend(
 290.306 -    unsigned long srec)
 290.307 -{
 290.308 -    int ret;
 290.309 -
 290.310 -    /* NB. On suspend, control software expects a suspend record in %esi. */
 290.311 -    __asm__ __volatile__ (
 290.312 -        TRAP_INSTR
 290.313 -        : "=a" (ret)
 290.314 -	: "0" ((unsigned long)__HYPERVISOR_sched_op),
 290.315 -        "D" ((unsigned long)(SCHEDOP_shutdown | (SHUTDOWN_suspend << SCHEDOP_reasonshift))), 
 290.316 -        "S" (srec)
 290.317 -	: __syscall_clobber );
 290.318 -
 290.319 -    return ret;
 290.320 -}
 290.321 -
 290.322 -/*
 290.323 - * We can have the timeout value in a single argument for the hypercall, but
 290.324 - * that will break the common code. 
 290.325 - */
 290.326  static inline long
 290.327  HYPERVISOR_set_timer_op(
 290.328 -    u64 timeout)
 290.329 +	u64 timeout)
 290.330  {
 290.331 -    int ret;
 290.332 -
 290.333 -    __asm__ __volatile__ (
 290.334 -        TRAP_INSTR
 290.335 -        : "=a" (ret)
 290.336 -	: "0" ((unsigned long)__HYPERVISOR_set_timer_op),
 290.337 -	  "D" (timeout)
 290.338 -	: __syscall_clobber );
 290.339 -
 290.340 -    return ret;
 290.341 +	return _hypercall1(long, set_timer_op, timeout);
 290.342  }
 290.343  
 290.344  static inline int
 290.345  HYPERVISOR_dom0_op(
 290.346 -    dom0_op_t *dom0_op)
 290.347 +	dom0_op_t *dom0_op)
 290.348  {
 290.349 -    int ret;
 290.350 -
 290.351 -    dom0_op->interface_version = DOM0_INTERFACE_VERSION;
 290.352 -    __asm__ __volatile__ (
 290.353 -        TRAP_INSTR
 290.354 -        : "=a" (ret)
 290.355 -	: "0" ((unsigned long)__HYPERVISOR_dom0_op), "D" (dom0_op)
 290.356 -	: __syscall_clobber );
 290.357 -
 290.358 -    return ret;
 290.359 +	dom0_op->interface_version = DOM0_INTERFACE_VERSION;
 290.360 +	return _hypercall1(int, dom0_op, dom0_op);
 290.361  }
 290.362  
 290.363  static inline int
 290.364  HYPERVISOR_set_debugreg(
 290.365 -    int reg, unsigned long value)
 290.366 +	int reg, unsigned long value)
 290.367  {
 290.368 -    int ret;
 290.369 -
 290.370 -    __asm__ __volatile__ (
 290.371 -        TRAP_INSTR
 290.372 -        : "=a" (ret)
 290.373 -	: "0" ((unsigned long)__HYPERVISOR_set_debugreg), "D" ((unsigned long)reg), "S" (value)
 290.374 -	: __syscall_clobber );
 290.375 -
 290.376 -    return ret;
 290.377 +	return _hypercall2(int, set_debugreg, reg, value);
 290.378  }
 290.379  
 290.380  static inline unsigned long
 290.381  HYPERVISOR_get_debugreg(
 290.382 -    int reg)
 290.383 +	int reg)
 290.384  {
 290.385 -    unsigned long ret;
 290.386 -
 290.387 -    __asm__ __volatile__ (
 290.388 -        TRAP_INSTR
 290.389 -        : "=a" (ret)
 290.390 -	: "0" ((unsigned long)__HYPERVISOR_get_debugreg), "D" ((unsigned long)reg)
 290.391 -	: __syscall_clobber );
 290.392 -
 290.393 -    return ret;
 290.394 +	return _hypercall1(unsigned long, get_debugreg, reg);
 290.395  }
 290.396  
 290.397  static inline int
 290.398  HYPERVISOR_update_descriptor(
 290.399 -    unsigned long ma, unsigned long word)
 290.400 +	unsigned long ma, unsigned long word)
 290.401  {
 290.402 -    int ret;
 290.403 -
 290.404 -    __asm__ __volatile__ (
 290.405 -        TRAP_INSTR
 290.406 -        : "=a" (ret)
 290.407 -	: "0" ((unsigned long)__HYPERVISOR_update_descriptor), "D" (ma),
 290.408 -	  "S" (word)
 290.409 -	: __syscall_clobber );
 290.410 -
 290.411 -    return ret;
 290.412 +	return _hypercall2(int, update_descriptor, ma, word);
 290.413  }
 290.414  
 290.415  static inline int
 290.416  HYPERVISOR_dom_mem_op(
 290.417 -    unsigned int op, unsigned long *extent_list,
 290.418 -    unsigned long nr_extents, unsigned int extent_order)
 290.419 +	unsigned int op, unsigned long *extent_list,
 290.420 +	unsigned long nr_extents, unsigned int extent_order)
 290.421  {
 290.422 -    int ret;
 290.423 -
 290.424 -    __asm__ __volatile__ (
 290.425 -        "movq %5,%%r10; movq %6,%%r8;" TRAP_INSTR
 290.426 -        : "=a" (ret)
 290.427 -	: "0" ((unsigned long)__HYPERVISOR_dom_mem_op), "D" ((unsigned long)op), "S" (extent_list),
 290.428 -	  "d" (nr_extents), "g" ((unsigned long) extent_order), "g" ((unsigned long) DOMID_SELF)
 290.429 -	: __syscall_clobber,"r8","r10");
 290.430 -
 290.431 -    return ret;
 290.432 +	return _hypercall5(int, dom_mem_op, op, extent_list,
 290.433 +			   nr_extents, extent_order, DOMID_SELF);
 290.434  }
 290.435  
 290.436  static inline int
 290.437  HYPERVISOR_multicall(
 290.438 -    void *call_list, int nr_calls)
 290.439 +	void *call_list, int nr_calls)
 290.440  {
 290.441 -    int ret;
 290.442 -
 290.443 -    __asm__ __volatile__ (
 290.444 -        TRAP_INSTR
 290.445 -        : "=a" (ret)
 290.446 -	: "0" ((unsigned long)__HYPERVISOR_multicall), "D" (call_list), "S" ((unsigned long)nr_calls)
 290.447 -	: __syscall_clobber);
 290.448 -
 290.449 -    return ret;
 290.450 +	return _hypercall2(int, multicall, call_list, nr_calls);
 290.451  }
 290.452  
 290.453  static inline int
 290.454  HYPERVISOR_update_va_mapping(
 290.455 -    unsigned long page_nr, pte_t new_val, unsigned long flags)
 290.456 +	unsigned long va, pte_t new_val, unsigned long flags)
 290.457  {
 290.458 -    int ret;
 290.459 -
 290.460 -    __asm__ __volatile__ (
 290.461 -        TRAP_INSTR
 290.462 -        : "=a" (ret)
 290.463 -	: "0" ((unsigned long)__HYPERVISOR_update_va_mapping), 
 290.464 -          "D" (page_nr), "S" (new_val.pte), "d" (flags)
 290.465 -	: __syscall_clobber);
 290.466 -
 290.467 -    return ret;
 290.468 +	return _hypercall3(int, update_va_mapping, va, new_val.pte, flags);
 290.469  }
 290.470  
 290.471  static inline int
 290.472  HYPERVISOR_event_channel_op(
 290.473 -    void *op)
 290.474 +	void *op)
 290.475  {
 290.476 -    int ret;
 290.477 -    __asm__ __volatile__ (
 290.478 -        TRAP_INSTR
 290.479 -        : "=a" (ret)
 290.480 -	: "0" ((unsigned long)__HYPERVISOR_event_channel_op), "D" (op)
 290.481 -	: __syscall_clobber);
 290.482 -
 290.483 -    return ret;
 290.484 +	return _hypercall1(int, event_channel_op, op);
 290.485  }
 290.486  
 290.487  static inline int
 290.488  HYPERVISOR_xen_version(
 290.489 -    int cmd)
 290.490 +	int cmd)
 290.491  {
 290.492 -    int ret;
 290.493 -
 290.494 -    __asm__ __volatile__ (
 290.495 -        TRAP_INSTR
 290.496 -        : "=a" (ret)
 290.497 -	: "0" ((unsigned long)__HYPERVISOR_xen_version), "D" ((unsigned long)cmd)
 290.498 -	: __syscall_clobber);
 290.499 -
 290.500 -    return ret;
 290.501 +	return _hypercall1(int, xen_version, cmd);
 290.502  }
 290.503  
 290.504  static inline int
 290.505  HYPERVISOR_console_io(
 290.506 -    int cmd, int count, char *str)
 290.507 +	int cmd, int count, char *str)
 290.508  {
 290.509 -    int ret;
 290.510 -    __asm__ __volatile__ (
 290.511 -        TRAP_INSTR
 290.512 -        : "=a" (ret)
 290.513 -	: "0" ((unsigned long)__HYPERVISOR_console_io), "D" ((unsigned long)cmd), "S" ((unsigned long)count), "d" (str)
 290.514 -	: __syscall_clobber);
 290.515 -
 290.516 -    return ret;
 290.517 +	return _hypercall3(int, console_io, cmd, count, str);
 290.518  }
 290.519  
 290.520  static inline int
 290.521  HYPERVISOR_physdev_op(
 290.522 -    void *physdev_op)
 290.523 +	void *physdev_op)
 290.524  {
 290.525 -    int ret;
 290.526 -
 290.527 -    __asm__ __volatile__ (
 290.528 -        TRAP_INSTR
 290.529 -        : "=a" (ret)
 290.530 -	: "0" ((unsigned long)__HYPERVISOR_physdev_op), "D" (physdev_op)
 290.531 -	: __syscall_clobber);
 290.532 -
 290.533 -    return ret;
 290.534 +	return _hypercall1(int, physdev_op, physdev_op);
 290.535  }
 290.536  
 290.537  static inline int
 290.538  HYPERVISOR_grant_table_op(
 290.539 -    unsigned int cmd, void *uop, unsigned int count)
 290.540 +	unsigned int cmd, void *uop, unsigned int count)
 290.541  {
 290.542 -    int ret;
 290.543 -
 290.544 -    __asm__ __volatile__ (
 290.545 -        TRAP_INSTR
 290.546 -        : "=a" (ret)
 290.547 -	: "0" ((unsigned long)__HYPERVISOR_grant_table_op), "D" ((unsigned long)cmd), "S" ((unsigned long)uop), "d" (count)
 290.548 -	: __syscall_clobber);
 290.549 -
 290.550 -    return ret;
 290.551 +	return _hypercall3(int, grant_table_op, cmd, uop, count);
 290.552  }
 290.553  
 290.554  static inline int
 290.555  HYPERVISOR_update_va_mapping_otherdomain(
 290.556 -    unsigned long page_nr, pte_t new_val, unsigned long flags, domid_t domid)
 290.557 +	unsigned long va, pte_t new_val, unsigned long flags, domid_t domid)
 290.558  {
 290.559 -    int ret;
 290.560 -
 290.561 -    __asm__ __volatile__ (
 290.562 -        "movq %5, %%r10;" TRAP_INSTR
 290.563 -        : "=a" (ret)
 290.564 -	: "0" ((unsigned long)__HYPERVISOR_update_va_mapping_otherdomain),
 290.565 -          "D" (page_nr), "S" (new_val.pte), "d" (flags), "g" ((unsigned long)domid)
 290.566 -	: __syscall_clobber,"r10");
 290.567 -    
 290.568 -    return ret;
 290.569 +	return _hypercall4(int, update_va_mapping_otherdomain, va,
 290.570 +			   new_val.pte, flags, domid);
 290.571  }
 290.572  
 290.573  static inline int
 290.574  HYPERVISOR_vm_assist(
 290.575 -    unsigned int cmd, unsigned int type)
 290.576 +	unsigned int cmd, unsigned int type)
 290.577  {
 290.578 -    int ret;
 290.579 +	return _hypercall2(int, vm_assist, cmd, type);
 290.580 +}
 290.581 +
 290.582 +static inline int
 290.583 +HYPERVISOR_boot_vcpu(
 290.584 +	unsigned long vcpu, vcpu_guest_context_t *ctxt)
 290.585 +{
 290.586 +	return _hypercall2(int, boot_vcpu, vcpu, ctxt);
 290.587 +}
 290.588  
 290.589 -    __asm__ __volatile__ (
 290.590 -        TRAP_INSTR
 290.591 -        : "=a" (ret)
 290.592 -	: "0" ((unsigned long)__HYPERVISOR_vm_assist), "D" ((unsigned long)cmd), "S" ((unsigned long)type)
 290.593 -	: __syscall_clobber);
 290.594 +static inline int
 290.595 +HYPERVISOR_vcpu_up(
 290.596 +	int vcpu)
 290.597 +{
 290.598 +	return _hypercall2(int, sched_op, SCHEDOP_vcpu_up |
 290.599 +			   (vcpu << SCHEDOP_vcpushift), 0);
 290.600 +}
 290.601  
 290.602 -    return ret;
 290.603 +static inline int
 290.604 +HYPERVISOR_vcpu_pickle(
 290.605 +	int vcpu, vcpu_guest_context_t *ctxt)
 290.606 +{
 290.607 +	return _hypercall2(int, sched_op, SCHEDOP_vcpu_pickle |
 290.608 +			   (vcpu << SCHEDOP_vcpushift), ctxt);
 290.609  }
 290.610  
 290.611  static inline int
 290.612  HYPERVISOR_switch_to_user(void)
 290.613  {
 290.614 -    int ret;
 290.615 -    __asm__ __volatile__ (
 290.616 -        TRAP_INSTR
 290.617 -        : "=a" (ret) : "0" ((unsigned long)__HYPERVISOR_switch_to_user) : __syscall_clobber );
 290.618 -
 290.619 -    return ret;
 290.620 -}
 290.621 -
 290.622 -static inline int
 290.623 -HYPERVISOR_boot_vcpu(
 290.624 -    unsigned long vcpu, vcpu_guest_context_t *ctxt)
 290.625 -{
 290.626 -    int ret;
 290.627 -
 290.628 -    __asm__ __volatile__ (
 290.629 -        TRAP_INSTR
 290.630 -        : "=a" (ret)
 290.631 -	: "0" (__HYPERVISOR_boot_vcpu), "D" (vcpu), "S" (ctxt)
 290.632 -	: __syscall_clobber);
 290.633 -
 290.634 -    return ret;
 290.635 +	return _hypercall0(int, switch_to_user);
 290.636  }
 290.637  
 290.638  static inline int
 290.639  HYPERVISOR_set_segment_base(
 290.640 -    int reg, unsigned long value)
 290.641 +	int reg, unsigned long value)
 290.642  {
 290.643 -    int ret;
 290.644 -
 290.645 -    __asm__ __volatile__ (
 290.646 -        TRAP_INSTR
 290.647 -        : "=a" (ret)
 290.648 -	: "0" ((unsigned long)__HYPERVISOR_set_segment_base), "D" ((unsigned long)reg), "S" (value)
 290.649 -	: __syscall_clobber );
 290.650 -
 290.651 -    return ret;
 290.652 +	return _hypercall2(int, set_segment_base, reg, value);
 290.653  }
 290.654  
 290.655  static inline int
 290.656 -HYPERVISOR_vcpu_pickle(
 290.657 -    int vcpu, vcpu_guest_context_t *ctxt)
 290.658 +HYPERVISOR_suspend(
 290.659 +	unsigned long srec)
 290.660  {
 290.661 -    int ret;
 290.662 -
 290.663 -    __asm__ __volatile__ (
 290.664 -        TRAP_INSTR
 290.665 -        : "=a" (ret)
 290.666 -	: "0" ((unsigned long)__HYPERVISOR_sched_op),
 290.667 -	"D" ((unsigned long)SCHEDOP_vcpu_pickle | (vcpu << SCHEDOP_vcpushift)),
 290.668 -	"S" ((unsigned long)ctxt)
 290.669 -	: __syscall_clobber );
 290.670 -
 290.671 -    return ret;
 290.672 +	return _hypercall2(int, sched_op, SCHEDOP_shutdown |
 290.673 +			   (SHUTDOWN_suspend << SCHEDOP_reasonshift), srec);
 290.674  }
 290.675  
 290.676  #endif /* __HYPERCALL_H__ */
 290.677 +
 290.678 +/*
 290.679 + * Local variables:
 290.680 + *  c-file-style: "linux"
 290.681 + *  indent-tabs-mode: t
 290.682 + *  c-indent-level: 8
 290.683 + *  c-basic-offset: 8
 290.684 + *  tab-width: 8
 290.685 + * End:
 290.686 + */
   299.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   299.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mmu.h	Fri Aug 26 13:47:16 2005 -0700
   299.3 @@ -0,0 +1,33 @@
   299.4 +#ifndef __x86_64_MMU_H
   299.5 +#define __x86_64_MMU_H
   299.6 +
   299.7 +#include <linux/spinlock.h>
   299.8 +#include <asm/semaphore.h>
   299.9 +
  299.10 +/*
  299.11 + * The x86_64 doesn't have a mmu context, but
  299.12 + * we put the segment information here.
  299.13 + *
  299.14 + * cpu_vm_mask is used to optimize ldt flushing.
  299.15 + */
  299.16 +typedef struct { 
  299.17 +	void *ldt;
  299.18 +	rwlock_t ldtlock; 
  299.19 +	int size;
  299.20 +	struct semaphore sem; 
  299.21 +#ifdef CONFIG_XEN
  299.22 +	unsigned pinned:1;
  299.23 +	struct list_head unpinned;
  299.24 +#endif
  299.25 +} mm_context_t;
  299.26 +
  299.27 +#ifdef CONFIG_XEN
  299.28 +extern struct list_head mm_unpinned;
  299.29 +extern spinlock_t mm_unpinned_lock;
  299.30 +
  299.31 +/* mm/memory.c:exit_mmap hook */
  299.32 +extern void _arch_exit_mmap(struct mm_struct *mm);
  299.33 +#define arch_exit_mmap(_mm) _arch_exit_mmap(_mm)
  299.34 +#endif
  299.35 +
  299.36 +#endif
   300.1 --- a/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mmu_context.h	Thu Aug 25 13:52:38 2005 -0700
   300.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/mmu_context.h	Fri Aug 26 13:47:16 2005 -0700
   300.3 @@ -58,6 +58,9 @@ static inline void __prepare_arch_switch
   300.4  	}
   300.5  }
   300.6  
   300.7 +extern void mm_pin(struct mm_struct *mm);
   300.8 +extern void mm_unpin(struct mm_struct *mm);
   300.9 +void mm_pin_all(void);
  300.10  
  300.11  static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 
  300.12  			     struct task_struct *tsk)
  300.13 @@ -66,6 +69,9 @@ static inline void switch_mm(struct mm_s
  300.14  	struct mmuext_op _op[3], *op = _op;
  300.15  
  300.16  	if (likely(prev != next)) {
  300.17 +		if (!next->context.pinned)
  300.18 +			mm_pin(next);
  300.19 +
  300.20  		/* stop flush ipis for the previous mm */
  300.21  		clear_bit(cpu, &prev->cpu_vm_mask);
  300.22  #if 0  /* XEN: no lazy tlb */
   304.1 --- a/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pgalloc.h	Thu Aug 25 13:52:38 2005 -0700
   304.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pgalloc.h	Fri Aug 26 13:47:16 2005 -0700
   304.3 @@ -21,12 +21,27 @@ static inline void pmd_populate_kernel(s
   304.4  
   304.5  static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte)
   304.6  {
   304.7 -	set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT)));
   304.8 +	if (unlikely((mm)->context.pinned)) {
   304.9 +		BUG_ON(HYPERVISOR_update_va_mapping(
  304.10 +			       (unsigned long)__va(page_to_pfn(pte) << PAGE_SHIFT),
  304.11 +			       pfn_pte(page_to_pfn(pte), PAGE_KERNEL_RO), 0));
  304.12 +		set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT)));
  304.13 +	} else {
  304.14 +		*(pmd) = __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT));
  304.15 +	}
  304.16  }
  304.17  
  304.18  static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
  304.19  {
  304.20 -	set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd)));
  304.21 +	if (unlikely((mm)->context.pinned)) {
  304.22 +		BUG_ON(HYPERVISOR_update_va_mapping(
  304.23 +			       (unsigned long)pmd,
  304.24 +			       pfn_pte(virt_to_phys(pmd)>>PAGE_SHIFT, 
  304.25 +				       PAGE_KERNEL_RO), 0));
  304.26 +		set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd)));
  304.27 +	} else {
  304.28 +		*(pud) =  __pud(_PAGE_TABLE | __pa(pmd));
  304.29 +	}
  304.30  }
  304.31  
  304.32  /*
  304.33 @@ -35,53 +50,54 @@ static inline void pud_populate(struct m
  304.34   */
  304.35  static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud)
  304.36  {
  304.37 -        set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud)));
  304.38 -        set_pgd(__user_pgd(pgd), __pgd(_PAGE_TABLE | __pa(pud)));
  304.39 -}
  304.40 -
  304.41 -extern __inline__ pmd_t *get_pmd(void)
  304.42 -{
  304.43 -        pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
  304.44 -        if (!pmd)
  304.45 -		return NULL;
  304.46 -        make_page_readonly(pmd);
  304.47 -        xen_pmd_pin(__pa(pmd));
  304.48 -	return pmd;
  304.49 +	if (unlikely((mm)->context.pinned)) {
  304.50 +		BUG_ON(HYPERVISOR_update_va_mapping(
  304.51 +			       (unsigned long)pud,
  304.52 +			       pfn_pte(virt_to_phys(pud)>>PAGE_SHIFT, 
  304.53 +				       PAGE_KERNEL_RO), 0));
  304.54 +		set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud)));
  304.55 +		set_pgd(__user_pgd(pgd), __pgd(_PAGE_TABLE | __pa(pud)));
  304.56 +	} else {
  304.57 +		*(pgd) =  __pgd(_PAGE_TABLE | __pa(pud));
  304.58 +		*(__user_pgd(pgd)) = *(pgd);
  304.59 +	}
  304.60  }
  304.61  
  304.62  extern __inline__ void pmd_free(pmd_t *pmd)
  304.63  {
  304.64 -	BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
  304.65 -        xen_pmd_unpin(__pa(pmd));
  304.66 -        make_page_writable(pmd);
  304.67 +	pte_t *ptep = virt_to_ptep(pmd);
  304.68 +
  304.69 +	if (!pte_write(*ptep)) {
  304.70 +		BUG_ON(HYPERVISOR_update_va_mapping(
  304.71 +			(unsigned long)pmd,
  304.72 +			pfn_pte(virt_to_phys(pmd)>>PAGE_SHIFT, PAGE_KERNEL),
  304.73 +			0));
  304.74 +	}
  304.75  	free_page((unsigned long)pmd);
  304.76  }
  304.77  
  304.78  static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
  304.79  {
  304.80          pmd_t *pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
  304.81 -        if (!pmd)
  304.82 -		return NULL;
  304.83 -        make_page_readonly(pmd);
  304.84 -        xen_pmd_pin(__pa(pmd)); 
  304.85          return pmd;
  304.86  }
  304.87  
  304.88  static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
  304.89  {
  304.90          pud_t *pud = (pud_t *) get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
  304.91 -        if (!pud)
  304.92 -		return NULL;
  304.93 -        make_page_readonly(pud);
  304.94 -        xen_pud_pin(__pa(pud)); 
  304.95          return pud;
  304.96  }
  304.97  
  304.98  static inline void pud_free(pud_t *pud)
  304.99  {
 304.100 -	BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
 304.101 -        xen_pud_unpin(__pa(pud));
 304.102 -        make_page_writable(pud);
 304.103 +	pte_t *ptep = virt_to_ptep(pud);
 304.104 +
 304.105 +	if (!pte_write(*ptep)) {
 304.106 +		BUG_ON(HYPERVISOR_update_va_mapping(
 304.107 +			(unsigned long)pud,
 304.108 +			pfn_pte(virt_to_phys(pud)>>PAGE_SHIFT, PAGE_KERNEL),
 304.109 +			0));
 304.110 +	}
 304.111  	free_page((unsigned long)pud);
 304.112  }
 304.113  
 304.114 @@ -107,10 +123,6 @@ static inline pgd_t *pgd_alloc(struct mm
 304.115  	       (PTRS_PER_PGD - boundary) * sizeof(pgd_t));
 304.116  
 304.117  	memset(__user_pgd(pgd), 0, PAGE_SIZE); /* clean up user pgd */
 304.118 -        make_pages_readonly(pgd, 2);
 304.119 -
 304.120 -        xen_pgd_pin(__pa(pgd)); /* kernel */
 304.121 -        xen_pgd_pin(__pa(__user_pgd(pgd))); /* user */
 304.122          /*
 304.123           * Set level3_user_pgt for vsyscall area
 304.124           */
 304.125 @@ -121,31 +133,45 @@ static inline pgd_t *pgd_alloc(struct mm
 304.126  
 304.127  static inline void pgd_free(pgd_t *pgd)
 304.128  {
 304.129 -	BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
 304.130 -        xen_pgd_unpin(__pa(pgd));
 304.131 -        xen_pgd_unpin(__pa(__user_pgd(pgd)));
 304.132 -        make_pages_writable(pgd, 2);
 304.133 +	pte_t *ptep = virt_to_ptep(pgd);
 304.134 +
 304.135 +	if (!pte_write(*ptep)) {
 304.136 +		xen_pgd_unpin(__pa(pgd));
 304.137 +		BUG_ON(HYPERVISOR_update_va_mapping(
 304.138 +			       (unsigned long)pgd,
 304.139 +			       pfn_pte(virt_to_phys(pgd)>>PAGE_SHIFT, PAGE_KERNEL),
 304.140 +			       0));
 304.141 +	}
 304.142 +
 304.143 +	ptep = virt_to_ptep(__user_pgd(pgd));
 304.144 +
 304.145 +	if (!pte_write(*ptep)) {
 304.146 +		xen_pgd_unpin(__pa(__user_pgd(pgd)));
 304.147 +		BUG_ON(HYPERVISOR_update_va_mapping(
 304.148 +			       (unsigned long)__user_pgd(pgd),
 304.149 +			       pfn_pte(virt_to_phys(__user_pgd(pgd))>>PAGE_SHIFT, 
 304.150 +				       PAGE_KERNEL),
 304.151 +			       0));
 304.152 +	}
 304.153 +
 304.154  	free_pages((unsigned long)pgd, 1);
 304.155  }
 304.156  
 304.157  static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
 304.158  {
 304.159          pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
 304.160 -        if (!pte)
 304.161 -		return NULL;
 304.162 -        make_page_readonly(pte);
 304.163 -        xen_pte_pin(__pa(pte));
 304.164 +        if (pte)
 304.165 +		make_page_readonly(pte);
 304.166 +
 304.167  	return pte;
 304.168  }
 304.169  
 304.170  static inline struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
 304.171  {
 304.172 -	pte_t *pte = (void *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
 304.173 -	if (!pte)
 304.174 -		return NULL;
 304.175 -        make_page_readonly(pte);
 304.176 -        xen_pte_pin(__pa(pte));
 304.177 -	return virt_to_page((unsigned long)pte);
 304.178 +	struct page *pte;
 304.179 +
 304.180 +	pte = alloc_pages(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO, 0);
 304.181 +	return pte;
 304.182  }
 304.183  
 304.184  /* Should really implement gc for free page table pages. This could be
   312.1 --- a/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/tlbflush.h	Thu Aug 25 13:52:38 2005 -0700
   312.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/tlbflush.h	Fri Aug 26 13:47:16 2005 -0700
   312.3 @@ -18,7 +18,7 @@ extern unsigned long pgkern_mask;
   312.4  
   312.5  #define __flush_tlb_all() __flush_tlb_global()
   312.6  
   312.7 -#define __flush_tlb_one(addr)	xen_invlpg(addr)
   312.8 +#define __flush_tlb_one(addr)	xen_invlpg((unsigned long)addr)
   312.9  
  312.10  
  312.11  /*
   464.1 --- a/tools/python/xen/util/Brctl.py	Thu Aug 25 13:52:38 2005 -0700
   464.2 +++ b/tools/python/xen/util/Brctl.py	Fri Aug 26 13:47:16 2005 -0700
   464.3 @@ -76,6 +76,7 @@ def bridge_set(bridge, hello=None, fd=No
   464.4  def bridge_del(bridge):
   464.5      """Delete a bridge.
   464.6      """
   464.7 +    cmd(CMD_IFCONFIG, '%s down' % bridge)
   464.8      cmd(CMD_BRCTL, 'delbr %s' % bridge)
   464.9  
  464.10  def routes():
   491.1 --- a/tools/python/xen/xend/XendVnet.py	Thu Aug 25 13:52:38 2005 -0700
   491.2 +++ b/tools/python/xen/xend/XendVnet.py	Fri Aug 26 13:47:16 2005 -0700
   491.3 @@ -22,7 +22,7 @@ from xen.util import Brctl
   491.4  from xen.xend import sxp
   491.5  from xen.xend.XendError import XendError
   491.6  from xen.xend.XendLogging import log
   491.7 -from xen.xend.xenstore import XenNode, DBMap
   491.8 +from xen.xend.xenstore import XenNode, DBMap, DBVar
   491.9  
  491.10  def vnet_cmd(cmd):
  491.11      out = None
  491.12 @@ -38,17 +38,40 @@ def vnet_cmd(cmd):
  491.13  class XendVnetInfo:
  491.14      
  491.15      vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
  491.16 +
  491.17 +    __exports__ = [
  491.18 +        DBVar('id',     ty='str'),
  491.19 +        DBVar('dbid',   ty='str'),
  491.20 +        DBVar('config', ty='sxpr'),
  491.21 +       ]
  491.22      
  491.23 -    def __init__(self, config):
  491.24 -        self.config = config
  491.25 -        self.id = sxp.child_value(config, 'id')
  491.26 -        self.id = str(self.id)
  491.27 +    def __init__(self, db, config=None):
  491.28 +        if config:
  491.29 +            self.id = sxp.child_value(config, 'id')
  491.30 +            self.id = str(self.id)
  491.31 +            self.dbid = self.id.replace(':', '-')
  491.32 +            self.db = db.addChild(self.dbid)
  491.33 +            self.config = config
  491.34 +        else:
  491.35 +            self.db = db
  491.36 +            self.importFromDB()
  491.37 +            config = self.config
  491.38 +            
  491.39          self.bridge = sxp.child_value(config, 'bridge')
  491.40          if not self.bridge:
  491.41              self.bridge = "vnet%s" % self.id
  491.42          self.vnetif = sxp.child_value(config, 'vnetif')
  491.43          if not self.vnetif:
  491.44 -            self.vnetif = "vnetif%s" % self.id
  491.45 +            self.vnetif = "vnif%s" % self.id
  491.46 +
  491.47 +    def saveToDB(self, save=False, sync=False):
  491.48 +        self.db.saveDB(save=save, sync=sync)
  491.49 +
  491.50 +    def exportToDB(self, save=False, sync=False):
  491.51 +        self.db.exportToDB(self, fields=self.__exports__, save=save, sync=sync)
  491.52 +
  491.53 +    def importFromDB(self):
  491.54 +        self.db.importFromDB(self, fields=self.__exports__)
  491.55  
  491.56      def sxpr(self):
  491.57          return self.config
  491.58 @@ -64,7 +87,9 @@ class XendVnetInfo:
  491.59          log.info("Deleting vnet %s", self.id)
  491.60          Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
  491.61          Brctl.bridge_del(self.bridge)
  491.62 -        return vnet_cmd(['vnet.del', self.id])
  491.63 +        val = vnet_cmd(['vnet.del', self.id])
  491.64 +        self.db.delete()
  491.65 +        return val
  491.66  
  491.67      def vifctl(self, op, vif, vmac):
  491.68          try:
  491.69 @@ -82,16 +107,18 @@ class XendVnet:
  491.70      def __init__(self):
  491.71          # Table of vnet info indexed by vnet id.
  491.72          self.vnet = {}
  491.73 -        self.dbmap = DBMap(db=XenNode(self.dbpath))
  491.74 -        self.dbmap.readDB()
  491.75 -        for vnetdb in self.dbmap.values():
  491.76 -            config = vnetdb.config
  491.77 -            info = XendVnetInfo(config)
  491.78 -            self.vnet[info.id] = info
  491.79 +        self.db = DBMap(db=XenNode(self.dbpath))
  491.80 +        self.db.readDB()
  491.81 +        for vnetdb in self.db.values():
  491.82              try:
  491.83 +                info = XendVnetInfo(vnetdb)
  491.84 +                self.vnet[info.id] = info
  491.85                  info.configure()
  491.86              except XendError, ex:
  491.87                  log.warning("Failed to configure vnet %s: %s", str(info.id), str(ex))
  491.88 +            except Exception, ex:
  491.89 +                log.exception("Vnet error")
  491.90 +                vnetdb.delete()
  491.91  
  491.92      def vnet_of_bridge(self, bridge):
  491.93          """Get the vnet for a bridge (if any).
  491.94 @@ -128,9 +155,9 @@ class XendVnet:
  491.95  
  491.96          @param config: config
  491.97          """
  491.98 -        info = XendVnetInfo(config)
  491.99 +        info = XendVnetInfo(self.db, config=config)
 491.100          self.vnet[info.id] = info
 491.101 -        self.dbmap["%s/config" % info.id] = info.sxpr()
 491.102 +        info.saveToDB()
 491.103          info.configure()
 491.104  
 491.105      def vnet_delete(self, id):
 491.106 @@ -141,7 +168,6 @@ class XendVnet:
 491.107          info = self.vnet_get(id)
 491.108          if info:
 491.109              del self.vnet[id]
 491.110 -            self.dbmap.delete(id)
 491.111              info.delete()
 491.112  
 491.113  def instance():
   502.1 --- a/tools/python/xen/xend/server/SrvVnetDir.py	Thu Aug 25 13:52:38 2005 -0700
   502.2 +++ b/tools/python/xen/xend/server/SrvVnetDir.py	Fri Aug 26 13:47:16 2005 -0700
   502.3 @@ -19,6 +19,7 @@ from xen.xend import sxp
   502.4  from xen.xend.Args import FormFn
   502.5  from xen.xend import PrettyPrint
   502.6  from xen.xend import XendVnet
   502.7 +from xen.xend.XendError import XendError
   502.8  
   502.9  from xen.web.SrvDir import SrvDir
  502.10  
   522.1 --- a/tools/python/xen/xm/main.py	Thu Aug 25 13:52:38 2005 -0700
   522.2 +++ b/tools/python/xen/xm/main.py	Fri Aug 26 13:47:16 2005 -0700
   522.3 @@ -105,6 +105,11 @@ xm full list of subcommands:
   522.4          Limit the transmission rate of a virtual network interface
   522.5      network-list    <DomId>        List virtual network interfaces for a domain
   522.6  
   522.7 +  Vnet commands:
   522.8 +    vnet-list   [-l|--long]    list vnets
   522.9 +    vnet-create <config>       create a vnet from a config file
  522.10 +    vnet-delete <vnetid>       delete a vnet
  522.11 +
  522.12  For a short list of subcommands run 'xm help'
  522.13  For more help on xm see the xm(1) man page
  522.14  For more help on xm create, see the xmdomain.cfg(5) man page"""
  522.15 @@ -547,6 +552,47 @@ def xm_block_destroy(args):
  522.16      from xen.xend.XendClient import server
  522.17      server.xend_domain_device_destroy(dom, 'vbd', dev)
  522.18  
  522.19 +def xm_vnet_list(args):
  522.20 +    from xen.xend.XendClient import server
  522.21 +    try:
  522.22 +        (options, params) = getopt(args, 'l', ['long'])
  522.23 +    except GetoptError, opterr:
  522.24 +        err(opterr)
  522.25 +        sys.exit(1)
  522.26 +    
  522.27 +    use_long = 0
  522.28 +    for (k, v) in options:
  522.29 +        if k in ['-l', '--long']:
  522.30 +            use_long = 1
  522.31 +            
  522.32 +    if params:
  522.33 +        use_long = 1
  522.34 +        vnets = params
  522.35 +    else:
  522.36 +        vnets = server.xend_vnets()
  522.37 +    
  522.38 +    for vnet in vnets:
  522.39 +        try:
  522.40 +            if use_long:
  522.41 +                info = server.xend_vnet(vnet)
  522.42 +                PrettyPrint.prettyprint(info)
  522.43 +            else:
  522.44 +                print vnet
  522.45 +        except Exception, ex:
  522.46 +            print vnet, ex
  522.47 +
  522.48 +def xm_vnet_create(args):
  522.49 +    arg_check(args, 1, "vnet-create")
  522.50 +    conf = args[0]
  522.51 +    from xen.xend.XendClient import server
  522.52 +    server.xend_vnet_create(conf)
  522.53 +
  522.54 +def xm_vnet_delete(args):
  522.55 +    arg_check(args, 1, "vnet-delete")
  522.56 +    vnet = args[0]
  522.57 +    from xen.xend.XendClient import server
  522.58 +    server.xend_vnet_delete(vnet)
  522.59 +
  522.60  commands = {
  522.61      # console commands
  522.62      "console": xm_console,
  522.63 @@ -592,7 +638,11 @@ commands = {
  522.64      "block-refresh": xm_block_refresh,
  522.65      # network
  522.66      "network-limit": xm_network_limit,
  522.67 -    "network-list": xm_network_list
  522.68 +    "network-list": xm_network_list,
  522.69 +    # vnet
  522.70 +    "vnet-list": xm_vnet_list,
  522.71 +    "vnet-create": xm_vnet_create,
  522.72 +    "vnet-delete": xm_vnet_delete,
  522.73      }
  522.74  
  522.75  aliases = {
   563.1 --- a/tools/vnet/00INSTALL	Thu Aug 25 13:52:38 2005 -0700
   563.2 +++ b/tools/vnet/00INSTALL	Fri Aug 26 13:47:16 2005 -0700
   563.3 @@ -1,14 +1,34 @@
   563.4  
   563.5 -To compile and install run "make install"; if it fails or you need to reinstall 
   563.6 -run "make clean" first or the build will fail, at least that is what I have 
   563.7 -found under 2.6.10.
   563.8 +make
   563.9 +        - compile in local dirs. The module is in vnet-module/vnet_module.ko.
  563.10 +
  563.11 +make dist
  563.12 +        - compile and install into $(XEN_ROOT)/dist/install,
  563.13 +        - where XEN_ROOT is the root of the xen tree.
  563.14 +
  563.15 +make install
  563.16 +        - compile and install into system.
  563.17  
  563.18 -Other important items:
  563.19 +The xen0 kernel must have been compiled before building the vnet module.
  563.20 +The vnet module installs to
  563.21 + /lib/modules/<kernel version>-xen0/kernel/xen/vnet_module.ko
  563.22 +
  563.23 +The vnet module should be loaded before starting xend, or
  563.24 +xend will fail to create any persistent vnets it has in its configuration.
  563.25 +The script network-vnet is a modified version of the xen network script
  563.26 +that loads the module if it's not already loaded.
  563.27 +
  563.28 +The module uses kernel crypto functions, and these need to be
  563.29 +enabled in the xen0 kernel config. They should be on by default -
  563.30 +if they're not you will get compile or insmod errors (see below).
  563.31 +
  563.32 +Kernel config options:
  563.33 +
  563.34  1)	You will need to have your xen0 kernel compiled with HMAC_SUPPORT 
  563.35  	2.6.x = (MAIN MENU: Cryptographic Options -> HMAC Support)
  563.36  	BEFORE running "make install".
  563.37  
  563.38 -2)	You will want at least some of the other alogorithms listed under
  563.39 +2)	You will want at least some of the other algorithms listed under
  563.40  	"Cryptographic Options" for the kernel compiled as modules.
  563.41  
  563.42  3)	You will want the networking IPsec/VLAN options compiled in as modules
  563.43 @@ -23,9 +43,5 @@ 3)	You will want the networking IPsec/VL
  563.44  	
  563.45  					802.1Q VLAN Support
  563.46  
  563.47 -4)	The module (vnet_module) will not properly load from the command line
  563.48 -	with a "modprobe vnet_module".  Use network-vnet to properly configure
  563.49 -	your system and load the module for you.
  563.50 -
  563.51  Please refer to the additional documentation found in tools/vnet/doc for
  563.52  proper syntax and config file parameters.
   564.1 --- a/tools/vnet/INSTALL	Thu Aug 25 13:52:38 2005 -0700
   564.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
   564.3 @@ -1,31 +0,0 @@
   564.4 -To compile and install run "make install"; if it fails or you need to reinstall 
   564.5 -run "make clean" first or the build will fail, at least that is what I have 
   564.6 -found under 2.6.10.
   564.7 -
   564.8 -Other important items:
   564.9 -1)	You will need to have your xen0 kernel compiled with HMAC_SUPPORT 
  564.10 -	2.6.x = (MAIN MENU: Cryptographic Options -> HMAC Support)
  564.11 -	BEFORE running "make install".
  564.12 -
  564.13 -2)	You will want at least some of the other alogorithms listed under
  564.14 -	"Cryptographic Options" for the kernel compiled as modules.
  564.15 -
  564.16 -3)	You will want the networking IPsec/VLAN options compiled in as modules
  564.17 -	2.6.x = (MAIN MENU: Device Drivers -> Networking Support -> 
  564.18 -				Networking Options ->
  564.19 -					IP: AH transformation
  564.20 -					IP: ESP transformation
  564.21 -					IP: IPComp transformation 
  564.22 -					IP: tunnel transformation
  564.23 -
  564.24 -					IPsec user configuration interface
  564.25 -	
  564.26 -					802.1Q VLAN Support
  564.27 -
  564.28 -4)	The module (vnet_module) will not properly load from the command line
  564.29 -	with a "modprobe vnet_module".  Use network-vnet to properly configure
  564.30 -	your system and load the module for you.
  564.31 -
  564.32 -Please refer to the additional documentation found in tools/vnet/doc for
  564.33 -proper syntax and config file parameters.
  564.34 -
   565.1 --- a/tools/vnet/Makefile	Thu Aug 25 13:52:38 2005 -0700
   565.2 +++ b/tools/vnet/Makefile	Fri Aug 26 13:47:16 2005 -0700
   565.3 @@ -1,19 +1,22 @@
   565.4 +# -*- mode: Makefile; -*-
   565.5  
   565.6 -export LINUX_SERIES ?=2.6
   565.7 +ifndef VNET_ROOT
   565.8 +export VNET_ROOT = $(shell pwd)
   565.9 +include $(VNET_ROOT)/Make.env
  565.10 +endif
  565.11  
  565.12 -# Root path to install in.
  565.13 -# Set to '/' to install relative to filesystem root.
  565.14 -export prefix?=$(shell cd ../../dist/install && pwd)
  565.15 +.PHONY: all compile install dist clean pristine
  565.16 +.PHONY: gc-all gc-install gc-clean
  565.17  
  565.18 -.PHONY: all compile
  565.19 -.PHONY: gc-install gc-clean gc-prstine
  565.20 -.PHONY: libxutil vnetd vnet-module install dist clean pristine
  565.21 +SUBDIRS:=
  565.22 +SUBDIRS+= examples
  565.23 +SUBDIRS+= gc
  565.24 +SUBDIRS+= libxutil
  565.25 +SUBDIRS+= vnetd
  565.26 +SUBDIRS+= vnet-module
  565.27  
  565.28  all: compile
  565.29  
  565.30 -compile: libxutil vnetd vnet-module
  565.31 -#compile: vnet-module
  565.32 -
  565.33  gc.tar.gz:
  565.34  	wget http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/$@
  565.35  
  565.36 @@ -21,36 +24,39 @@ gc: gc.tar.gz
  565.37  	tar xfz gc.tar.gz
  565.38  	ln -sf gc?.? gc
  565.39  
  565.40 -gc-install: gc 
  565.41 -	(cd gc && make test && ./configure --prefix=`pwd`/install)
  565.42 +$(GC_LIB_A): gc
  565.43 +	(cd gc && ./configure --prefix=$(GC_DIR) )
  565.44  	make -C gc
  565.45 -	make -C gc install
  565.46 +	DESTDIR="" make -C gc install
  565.47 +
  565.48 +gc-all: $(GC_LIB_A)
  565.49 +
  565.50 +gc-install:
  565.51  
  565.52  gc-clean:
  565.53 -	-$(MAKE) -C gc clean
  565.54 +	-@$(RM) -r gc?.? gc
  565.55  
  565.56 -gc-pristine:
  565.57 -	-rm -rf gc?.? gc
  565.58 +submak = $(MAKE) -C $(patsubst %-$(1),%,$(@)) $(1)
  565.59 +subtgt = $(patsubst %,%-$(1),$(SUBDIRS))
  565.60  
  565.61 -libxutil:
  565.62 -	$(MAKE) -C libxutil
  565.63 +%-all:
  565.64 +	$(call submak,all)
  565.65  
  565.66 -vnetd: gc-install
  565.67 -	$(MAKE) -C vnetd
  565.68 -
  565.69 -vnet-module:
  565.70 -	$(MAKE) -C vnet-module
  565.71 +%-clean:
  565.72 +	-$(call submak,clean)
  565.73  
  565.74 -install: compile
  565.75 -	$(MAKE) -C libxutil install
  565.76 -	$(MAKE) -C vnetd install
  565.77 -	$(MAKE) -C vnet-module install
  565.78 -	$(MAKE) -C examples install
  565.79 +%-install:
  565.80 +	$(call submak,install)
  565.81 +
  565.82 +compile: $(call subtgt,all)
  565.83 +
  565.84 +install: DESTDIR=
  565.85 +install: dist
  565.86  
  565.87 -clean:
  565.88 -	-$(MAKE) -C libxutil clean
  565.89 -	-$(MAKE) -C vnetd clean
  565.90 -	-$(MAKE) -C vnet-module clean
  565.91 -	-rm -rf gc?.? gc
  565.92 +dist: compile $(call subtgt,install)
  565.93  
  565.94 -pristine: clean gc-pristine
  565.95 +clean: $(call subtgt,clean)
  565.96 +	-@$(RM) -r build
  565.97 +
  565.98 +pristine: clean
  565.99 +	-@$(RM) gc.tar.gz
   566.1 --- a/tools/vnet/doc/vnet-module.txt	Thu Aug 25 13:52:38 2005 -0700
   566.2 +++ b/tools/vnet/doc/vnet-module.txt	Fri Aug 26 13:47:16 2005 -0700
   566.3 @@ -1,21 +1,34 @@
   566.4  Vnet Module Command Interface
   566.5  Mike Wray <mike.wray@hp.com>
   566.6 -2004/09/17
   566.7 +2005/08/25
   566.8  
   566.9  When insmod the vnet-module creates /proc/vnet/policy which
  566.10  can be used to control the module by writing commands into it.
  566.11  The return code from the command should be returned by close.
  566.12 +Xend uses these commands to implement its vnet interface.
  566.13  
  566.14  The commands are:
  566.15  
  566.16 -(vnet.add (id <id>) [(security { none | auth | conf } )] )
  566.17 +(vnet.add (id <id>) [(vnetif <ifname>)] [(security { none | auth | conf } )] )
  566.18  
  566.19  Create the vnet with id <id> and the given security level (default none).
  566.20 +Vnet ids are 128-bit and can be specified as 8 fields of 1 to 4 hex digits
  566.21 +separated by colons. A vnet id with no colons is treated as one with the first
  566.22 +7 fields zero. Examples:
  566.23 +
  566.24 +1500 - equivalent to 0:0:0:0:0:0:0:1500
  566.25 +aaff:0:0:0:0:0:77:88
  566.26 +
  566.27  Security levels:
  566.28  - none: no security
  566.29  - auth: message authentication (IPSEC hmac)
  566.30  - conf: message confidentiality (IPSEC hmac and encryption)
  566.31  
  566.32 +The <ifname> is the name of the network device created for the vnet.
  566.33 +If not given it defaults to vnif<N>, where <N> is the hex for the
  566.34 +8-th field in the id. Note that network device names can have a
  566.35 +maximum of 14 characters.
  566.36 +
  566.37  (vnet.del (id <id>))
  566.38  
  566.39  Delete the vnet with id <id>.
  566.40 @@ -31,12 +44,18 @@ on vnet <vnetid>.
  566.41  Remove the vif with MAC address <macaddr> from the vnet with id <vnetid>.
  566.42  The vnet module will stop responding to VARP for the vif.
  566.43  
  566.44 +(vif.print)
  566.45 +
  566.46 +Print the known vnets, vifs and varp cache on the console.
  566.47 +
  566.48  Examples:
  566.49  
  566.50  To create vnet 10 with no security:
  566.51  
  566.52  echo '(vnet.add (id 10))' > /proc/vnet/policy
  566.53  
  566.54 +This creates a device vnif0010.
  566.55 +
  566.56  To create vnet 11 with message authentication:
  566.57  
  566.58  echo '(vnet.add (id 11) (security auth))' > /proc/vnet/policy
   567.1 --- a/tools/vnet/doc/vnet-xend.txt	Thu Aug 25 13:52:38 2005 -0700
   567.2 +++ b/tools/vnet/doc/vnet-xend.txt	Fri Aug 26 13:47:16 2005 -0700
   567.3 @@ -3,11 +3,13 @@ Vnets: Virtual Networks for Virtual Mach
   567.4  
   567.5  Mike Wray <mike.wray@hp.com>
   567.6  
   567.7 +2005/08/25
   567.8 +
   567.9  0) Introduction
  567.10  ---------------
  567.11  
  567.12  Vnets provide virtual private LANs for virtual machines.
  567.13 -This is done using bridging and tunneling. A virtual interface
  567.14 +This is done using bridging and multipoint tunneling. A virtual interface
  567.15  on a vnet can only see other interfaces on the same vnet - it cannot
  567.16  see the real network, and the real network cannot see it either.
  567.17  
  567.18 @@ -32,13 +34,16 @@ Configure the network script:
  567.19  
  567.20  Restart xend.
  567.21  
  567.22 +Alternatively insert the vnet module using vnet-insert,
  567.23 +preferably before xend starts.
  567.24 +
  567.25  2) Creating vnets
  567.26  -----------------
  567.27  
  567.28  Xend already implements commands to add/remove vnets and
  567.29  bridge to them. To add a vnet use
  567.30  
  567.31 -xm call vnet_add <vnet config file>
  567.32 +xm vnet-create <vnet config file>
  567.33  
  567.34  For example, if vnet97.sxp contains:
  567.35  
  567.36 @@ -46,7 +51,7 @@ For example, if vnet97.sxp contains:
  567.37  
  567.38  do
  567.39  
  567.40 -xm call vnet_add vnet97.sxp
  567.41 +xm vnet-create vnet97.sxp
  567.42  
  567.43  This will define a vnet with id 97 and no security. The bridge for the
  567.44  vnet is called vnet97 and the virtual interface for it is vnetif97.
  567.45 @@ -64,31 +69,35 @@ In sxp:
  567.46  Once configured, vnets are persistent in the xend database.
  567.47  To remove a vnet use
  567.48  
  567.49 -xm call vnet_delete <vnet id>
  567.50 +xm vnet-delete <vnet id>
  567.51  
  567.52  To list vnets use
  567.53  
  567.54 -xm call vnets
  567.55 +xm vnet-list
  567.56  
  567.57  To get information on a vnet id use
  567.58  
  567.59 -xm call vnet <vnet id>
  567.60 +xm vnet-list <vnet id>
  567.61  
  567.62  3) Troubleshooting
  567.63  ------------------
  567.64  
  567.65  The vnet module should appear in 'lsmod'.
  567.66 -If a vnet has been configured it should appear in the output of 'xm call vnets'.
  567.67 +If a vnet has been configured it should appear in the output of 'xm vnet-list'.
  567.68  Its bridge and interface should appear in 'ifconfig'.
  567.69  It should also show in 'brctl show', with its attached interfaces.
  567.70  
  567.71 -You can 'see into' a vnet from dom0 if you put an IP address on the bridge.
  567.72 +You can 'see into' a vnet from dom0 if you put an IP address on the bridge
  567.73 +and configure its MAC address as a vif.
  567.74  For example, if you have vnet97 with a vm with ip addr 10.0.0.12 on it,
  567.75 -then
  567.76 +and <mac> is the MAC address of vnet97 (use ifconfig), then
  567.77  
  567.78 +echo '(vif.add (vnet 97) (vmac <mac>))' >/proc/vnet/policy
  567.79  ifconfig vnet97 10.0.0.20 up
  567.80  
  567.81  should let you ping 10.0.0.12 via the vnet97 bridge.
  567.82 +This works even if the vm with vif 10.0.0.12 is on another
  567.83 +machine (it only works locally if you don't use vif.add).
  567.84  
  567.85  4) Examples
  567.86  -----------
  567.87 @@ -104,11 +113,11 @@ Here's the full config for a vm on vnet 
  567.88    (linux
  567.89     (kernel /boot/vmlinuz-2.6-xenU)
  567.90     (ip 10.0.0.12:1.2.3.4::::eth0:off)
  567.91 -   (root /dev/hda1)
  567.92 +   (root /dev/sda1)
  567.93     (args 'rw fastboot 4')
  567.94    )
  567.95   )
  567.96 - (device (vbd (uname phy:hda2) (dev hda1) (mode w)))
  567.97 + (device (vbd (uname phy:hda2) (dev sda1) (mode w)))
  567.98   (device (vif (mac aa:00:00:11:00:12) (bridge vnet97)))
  567.99  )
 567.100  
 567.101 @@ -123,11 +132,11 @@ If you run another vm on the same vnet:
 567.102    (linux
 567.103     (kernel /boot/vmlinuz-2.6-xenU)
 567.104     (ip 10.0.0.11:1.2.3.4::::eth0:off)
 567.105 -   (root /dev/hda1)
 567.106 +   (root /dev/sda1)
 567.107     (args 'rw fastboot 4')
 567.108    )
 567.109   )
 567.110 - (device (vbd (uname phy:hda3) (dev hda1) (mode w)))
 567.111 + (device (vbd (uname phy:hda3) (dev sda1) (mode w)))
 567.112   (device (vif (mac aa:00:00:11:00:11) (bridge vnet97)))
 567.113  )
 567.114  
   568.1 --- a/tools/vnet/examples/Makefile	Thu Aug 25 13:52:38 2005 -0700
   568.2 +++ b/tools/vnet/examples/Makefile	Fri Aug 26 13:47:16 2005 -0700
   568.3 @@ -3,10 +3,13 @@
   568.4  
   568.5  XEN_SCRIPT_DIR:=/etc/xen/scripts
   568.6  
   568.7 +.PHONY: all install clean
   568.8 +
   568.9  all:
  568.10  
  568.11  install:
  568.12  	install -m 0755 -d $(DESTDIR)$(XEN_SCRIPT_DIR)
  568.13  	install -m 0554 network-vnet $(DESTDIR)$(XEN_SCRIPT_DIR)
  568.14 +	install -m 0554 vnet-insert $(DESTDIR)$(XEN_SCRIPT_DIR)
  568.15  
  568.16  clean:
  568.17 \ No newline at end of file
   569.1 --- a/tools/vnet/examples/network-vnet	Thu Aug 25 13:52:38 2005 -0700
   569.2 +++ b/tools/vnet/examples/network-vnet	Fri Aug 26 13:47:16 2005 -0700
   569.3 @@ -1,218 +1,10 @@
   569.4  #!/bin/sh
   569.5 -#============================================================================
   569.6 -# Default Xen network start/stop script.
   569.7 -# Xend calls a network script when it starts.
   569.8 -# The script name to use is defined in /etc/xen/xend-config.sxp
   569.9 -# in the network-script field.
  569.10 -#
  569.11 -# This script creates a bridge (default xen-br0), adds a device
  569.12 -# (default eth0) to it, copies the IP addresses from the device
  569.13 -# to the bridge and adjusts the routes accordingly.
  569.14 -#
  569.15 -# If all goes well, this should ensure that networking stays up.
  569.16 -# However, some configurations are upset by this, espe