mallocRequest :: OpCode -> Key -> Word8 -> (Ptr Request -> IO ())
              -> Int -> (Ptr Request -> IO ()) -> Word32 -> CAS -> IO (Ptr Request)
mallocRequest (OpCode o) key elen epoke vlen vpoke opaque (CAS cas) = do
    let tlen = S.length key + fromIntegral elen + vlen
    p <- mallocBytes (24 + fromIntegral tlen)
    pokeWord8             p     0x80
    pokeWord8    (plusPtr p  1) o
    pokeWord16be (plusPtr p  2) (fromIntegral $ S.length key)
    pokeWord8    (plusPtr p  4) elen
    pokeWord8    (plusPtr p  5) 0x00
    pokeWord16be (plusPtr p  6) 0x00
    pokeWord32be (plusPtr p  8) (fromIntegral tlen)
    pokeWord32be (plusPtr p 12) opaque
    pokeWord64be (plusPtr p 16) cas
    epoke (plusPtr p 24)
    pokeByteString (plusPtr p $ 24 + fromIntegral elen) key
    vpoke (plusPtr p $ 24 + fromIntegral elen + S.length key)
    return p

mallocRequest :: OpCode -> Key -> Word8 -> (Ptr Request -> IO ())
              -> Int -> (Ptr Request -> IO ()) -> Word32 -> CAS -> IO (Ptr Request)
mallocRequest (OpCode o) key elen epoke vlen vpoke opaque (CAS cas) = do
    let tlen = S.length key + fromIntegral elen + vlen
    p <- mallocBytes (24 + fromIntegral tlen)
    pokeWord8             p     0x80
    pokeWord8    (plusPtr p  1) o
    pokeWord16be (plusPtr p  2) (fromIntegral $ S.length key)
    pokeWord8    (plusPtr p  4) elen
    pokeWord8    (plusPtr p  5) 0x00
    pokeWord16be (plusPtr p  6) 0x00
    pokeWord32be (plusPtr p  8) (fromIntegral tlen)
    pokeWord32be (plusPtr p 12) opaque
    pokeWord64be (plusPtr p 16) cas
    epoke (plusPtr p 24)
    pokeByteString (plusPtr p $ 24 + fromIntegral elen) key
    vpoke (plusPtr p $ 24 + fromIntegral elen + S.length key)
    return p

mallocRequest (OpCode o) key elen epoke vlen vpoke opaque (CAS cas) = do
    let tlen = S.length key + fromIntegral elen + vlen
    p <- mallocBytes (24 + fromIntegral tlen)
    pokeWord8             p     0x80
    pokeWord8    (plusPtr p  1) o
    pokeWord16be (plusPtr p  2) (fromIntegral $ S.length key)
    pokeWord8    (plusPtr p  4) elen
    pokeWord8    (plusPtr p  5) 0x00
    pokeWord16be (plusPtr p  6) 0x00
    pokeWord32be (plusPtr p  8) (fromIntegral tlen)
    pokeWord32be (plusPtr p 12) opaque
    pokeWord64be (plusPtr p 16) cas
    epoke (plusPtr p 24)
    pokeByteString (plusPtr p $ 24 + fromIntegral elen) key
    vpoke (plusPtr p $ 24 + fromIntegral elen + S.length key)
    return p
