anchor.diff:  (defcfun (%open "open")
       :int32
     (path :string)
  -  (flags :int32)
  -  (mode :int32))
  +  (flags :int32) ;; 64 == o_creat|o_rdwr
  +  (mode :int32)) ;; 438 == rw|rw|rw
   
   (defcfun (%close "close")
       :int32
  
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:   (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 
  		     64 ;; o_creat|o_rdwr
  		     438)))) ;; rw|rw|rw
      (when (< fd 0)
        (error "failed to open"))
      (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"))
  