anchor.diff:    (lock (bt:make-lock)))
   
   (defun open-mapping (path &key size)
  +  "Opens the file named by PATH and maps it into memory. 
  +
  +If SIZE is provided, the file is first extended to be SIZE bytes. SIZE must be provided when 
  +creating the file."
  +  ;; use regular CL functions to create the file and check its length
  +  (with-open-file (f path 
  +		     :direction :io 
  +		     :if-exists :overwrite 
  +		     :if-does-not-exist :create
  +		     :element-type '(unsigned-byte 8))
  +    (let ((length (file-length f)))
  +      (cond
  +	((zerop length)
  +	 (unless size (error "Must provide a size when creating mapping file"))
  +	 (file-position f size)
  +	 (write-byte 0 f))
  +	((and size (> size length))
  +	 (file-position f size)
  +	 (write-byte 0 f))
  +	((not size) 
  +	 (setf size length)))))
  +  ;; the file is now created and the correct size 
     (let ((fd (with-foreign-string (s path)
  -	      (%open s 0 0))))
  +	      (%open s 
  +		     64 ;; o_creat|o_rdwr
  +		     438)))) ;; rw|rw|rw
       (when (< fd 0)
         (error "failed to open"))
  -    (cond
  -      (size 
  -       (when (< size (get-file-size fd))
  -	 (write-zero-at (mapping-fd mapping) size)))
  -      ((and (not size) 
  -	    (zerop (get-file-size fd)))
  -       (%close fd)
  -       (error "Must provide a size when creating file"))
  -      (t 
  -       (setf size (get-file-size fd))))
  -    (let ((ptr (%mmap (null-pointer) size 0 0 fd 0)))
  +    (let ((ptr (%mmap (null-pointer) 
  +		      size 
  +		      3 ;; prot_read | prot_write
  +		      1 ;; map_shared
  +		      fd 
  +		      0)))
         (when (null-pointer-p ptr)
   	(%close fd)
   	(error "failed to mmap"))
  
positive.code:     (setf (mem-ref b :uint8) 0)
      (%write fd b 1)))
  
  (defcfun (%flock "flock"))
  
  ;; need to work out the size of each field
  (defcstruct stat-info
    (dev :uint32)
    (inode :uint32)
    (mode :uint32)
    (nlink :uint32)
    (uid :uint32)
    (gid :uint32)
    (dev :uint32)
    (size size-t)
    (blksize size-t)
    (blkcnt size-t)
    (atime size-t)
    (mtime size-t)
    (ctime size-t))
  
  (defcfun (%fstat "fstat")
      :int32
    (fd :int32)
    (stat :pointer))
  
  (defun get-file-size (fd)
    (with-foreign-object (stat '(:struct stat-info))
      (%fstat fd stat)
      (foreign-slot-value stat '(:struct stat-info) 'size)))
  
  (defstruct mapping 
    fd 
  
negative.code:     :pointer
    (addr :pointer)
    (length size-t)
    (prot :int32) ;; prot_read == 1, prot_write == 2
    (flags :int32) ;; map_shared == 1
    (fd :int32)
    (offset size-t))
  
  