F RANZ I NC. An In-Depth Look at Simple Streams By Duane Rettig October, 2002.

Slides:



Advertisements
Similar presentations
Chapter 13 Application Framework. Outline Definition & anatomy Fulfilling the framework contract Building frameworks Examples.
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
04/14/2008CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Arrays (1) You create an array in LISP by using the function (make- array ). All elements are initially set to nil. To create a 1-dimensional array of.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Programming Language Paradigms: summary. Object-oriented programming Objects are the fundamental building blocks of a program. Interaction is structured.
VBA Modules, Functions, Variables, and Constants
Using Classes Lisp also supports the Object-Oriented paradigm. The process of defining classes in Lisp is similar to how you you define structures. The.
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Examining the Code [Reading assignment: Chapter 6, pp ]
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Functional Programming
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Def D OC An extensible, dynamic document creation system By Rahul Jain
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Error Handling Common Lisp has the throw/catch statements so that you can simulate exception handling as seen in languages like Java But CL goes far beyond.
I/O Systems I/O Hardware Application I/O Interface
PRACTICAL COMMON LISP Peter Seibel 1.
F RANZ I NC. Optimizing and Debugging Programs in Allegro CL By Duane Rettig April, 2007.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
The Java Programming Language
1 CLOS: Common Lisp Object System Basic principles of CLOS Classes, Instances and Slots Inheritance Generic functions Methods Multi-methods Comparison.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Silberschatz, Galvin and Gagne  Operating System Concepts Six Step Process to Perform DMA Transfer.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
14 October 2002GGF6 / CGS-WG1 Working with CIM Ellen Stokes
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
PRACTICAL COMMON LISP Peter Seibel 1.
FILE SYSTEM IMPLEMENTATION 1. 2 File-System Structure File structure Logical storage unit Collection of related information File system resides on secondary.
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.
1 An SDL Tutorial Two primary elements: –Structure –Identifies the various components of the system, and the communication paths among them. –Components:
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 15 - C++ As A "Better C"
Examples (D. Schmidt et al)
APPENDIX a WRITING SUBROUTINES IN C
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Object Writing in files
Allegro CL Certification Program
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7 Text Input/Output Objectives
CLOS CSC 358/
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Operating System Concepts
CS703 - Advanced Operating Systems
Imperative Data Structures
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Peter Seibel Practical Common Lisp Peter Seibel
Common Lisp II.
Module 12: I/O Systems I/O hardwared Application I/O Interface
Presentation transcript:

F RANZ I NC. An In-Depth Look at Simple Streams By Duane Rettig October, 2002

F RANZ I NC. What is a Lisp Stream? Lisp stream external device internal device

F RANZ I NC. An In-Depth Look at Simple Streams Simple Streams Design Goals What is a Simple Stream? Simple-stream Concepts –Opening, closing, blocking, reading, writing Character Strategies Common Windows Rewrite Further changes in next version (references distributed throughout presentation)

F RANZ I NC. Design Goals CL compliance Bivalence Thin Strategy layer that is uniform Extensibility via specializations/mixins and encapsulations Minimal decision points in critical strategy code Minimal calls to generic-functions External-format ready for 8 and 16 bit lisps

F RANZ I NC. Gray vs. Simple Stream Object functionality buffer API call Strategy Gray Stream Simple Stream Device interface

F RANZ I NC. Dual-channel stream Two octet buffers External-format character translation Ext. format Device methods Out buffer API In buffer External device

F RANZ I NC. Si ngle-channel stream One octet buffer External-format character translation Ext. format Device methods buffer API direction Internal or External device

F RANZ I NC. String stream Zero, one, or two string buffers No external-format translations Plugable character/buffer strategies Device methods Out buffer API In buffer Internal device

F RANZ I NC. Opening a Stream Creates or reuses a stream object Makes or retains a connection to a device Initializes character strategies Marks the stream as open

F RANZ I NC. Open open make-instance (gf)... shared-initialize (gf) shared-initialize :after [Method] device-open [Method]

F RANZ I NC. (Open): Simple encapsulation (setq bun (open "sesame")) (setq big-mac (make-instance 'all-beef :input-handle bun...)) bunbig-mac all-beef sesame

F RANZ I NC. (Open): shared-initialize (defmethod shared-initialize :after ((stream simple-stream) slots &rest initargs) (declare (ignore slots)) (unless (device-open stream initargs) (device-close stream t)) stream)

F RANZ I NC. (Open): device-open At device-open time: –All instance slots have been initialized –connection has been made or will be made by device-open When device-open returns: –If non-nil, the stream is ready for appropriate actions –If nil, then the open has failed.

F RANZ I NC. (Open): device-open device-open must: –Ensure a connection before returning –Ensure buffers are in place –Initialize/reinitialize pointers –Return nil for failure, or non-nil after success device-open must not: –Assume that the stream was closed –close the stream

F RANZ I NC. (Open): subclass example (defclass file-with-header (file-simple-stream) ((header :initform nil :accessor file-header-info))) (defmethod device-open ((stream file-with-header) options) (declare (ignore options)) (let ((success (call-next-method))) (when success (setf (file-header-info stream) (read-file-header stream)) t)))

F RANZ I NC. (Open): device-open :before methods In future releases, for string-simple-streams: –A catch-all primary method returns true for an open stream. –Strategy installation functions are directional and do not override. install-string-character-strategy deprecated, replaced by install-string-{input,output}-character-strategy –All :before methods fire according to CPL, and may shadow default actions.

F RANZ I NC. (Open): device-open: string-input (future) (defmethod device-open :before ((stream string-input-simple-stream) options) (with-stream-class (string-input-simple-stream stream) (let ((string (getf options :string))) (when (and string (null (sm buffer stream))) (let ((start (getf options :start)) (end (or (getf options :end) (length string)))) (setf (sm buffer stream) string (sm buffpos stream) start (sm buffer-ptr stream) end))))) (install-string-input-character-strategy stream) (add-stream-instance-flags stream :string :input :simple))

F RANZ I NC. (Open): device-open: string-output (future) (defmethod device-open :before ((stream string-output-simple-stream) options) (with-stream-class (string-output-simple-stream stream) (unless (sm out-buffer stream) (let ((string (getf options :string))) (if string (setf (sm out-buffer stream) string (sm max-out-pos stream) (length string)) (let ((buflen (max (device-buffer-length stream) 16))) (setf (sm out-buffer stream) (make-string buflen) (sm max-out-pos stream) buflen))))) (unless (sm control-out stream) (setf (sm control-out stream) *std-control-out-table*))) (install-string-output-character-strategy stream) (add-stream-instance-flags stream :string :output :simple))

F RANZ I NC. Closing a Stream Flushes output if any. Breaks connection to device. Secures against accidental future operations. Does not change-class. (defmethod close ((stream simple-stream) &key abort) (device-close stream abort))

F RANZ I NC. Close (defmethod device-close :around ((stream simple-stream) abort) (let (res) (when (pseudo::open-stream-p stream) (unwind-protect (progn (when (output-stream-p stream) (ignore-errors (if abort (clear-output stream) (force-output stream)))) (setq res (call-next-method))) (without-interrupts (pseudo::unset-open-flags stream) (setf (stream-input-handle stream) nil (stream-output-handle stream) nil)) (setf (stream-external-format stream) (find-external-format :void)) res))))

F RANZ I NC. (Close): device-close device-close should: –flush all data (unless aborting) –disconnect handles of any encapsulated streams –call lower-levels to close as necessary device-close should not: –operate on a closed stream –close an encapsulated stream

F RANZ I NC. Blocking Issues with listen –read direction only –covers character availability and not blocking –assumes character as the basic data unit stream-listen (carried over from Gray streams) read-no-hang-p and write-no-hang-p

F RANZ I NC. Blocking styles Non-blocking Blocking B/NB (blocking, then non-blocking) Character: One element Octet: One element

F RANZ I NC. Reading and Writing Mostly symmetrical Device methods obey B/NB discipline

F RANZ I NC. Basic read strategy (block read (when (>= buffpos buffer-ptr) (let ((res (device-read stream nil 0 buffpos blocking))) (when (< res 0) (pseudo::do-error-handling)) (when (= res 0) (if blocking (pseudo::do-eof-handling) (return-from read nil))) (setq buffer-ptr res buffpos 0))) (prog1 (aref buffer buffpos) (incf buffpos)))))

F RANZ I NC. Basic write strategy (block write (when (>= out-pos max-out-pos) (when (> out-pos 0) (erroring-device-write-whole stream nil 0 out-pos t)) (setq out-pos 0)) (setf (aref buffer out-pos) value) (incf out-pos))

F RANZ I NC. Implementing force-output, finish-output Both are implemented using device-write blocking argument determines whether force (nil) or finish (t) device-write buffer argument is :flush (future)

F RANZ I NC. Reading and Writing Sequences read-sequence and write-sequence –are width sensitive and by-element –require blocking semantics –do not return a count read-vector and write-vector –are octet-based –employ B/NB semantics –return a count, 0, or error code

F RANZ I NC. Deprecated generic function: device-extend History and relationship to device-read and device-write –structural differences –purity of reference Problems –one generic-function for two directions –inconsistent interface

F RANZ I NC. Replacement of device-extend Actions :input and :input-check become blocking argument to device-read Actions :output and :output-check become blocking argument to device-write Extra actions come through via device-read and device-write buffer argument

F RANZ I NC. Record orientation Via device-finish-record gf –dual-channel input –string output

F RANZ I NC. Other generic-functions device-buffer-length device-clear-input device-clear-output device-file-length device-file-position

F RANZ I NC. Strategies What is a strategy’s purpose? –To satisfy the high-level requirements of a specified behavior in a uniform manner. Octet strategies are non-programmable. –read-byte, write-byte are cast in concrete. Character strategies are programmable...

F RANZ I NC. Character strategies Programmable, replaceable. External-formats use template functions for dual-channel and single-channel streams. String-streams use simple strategy functions. (future): Subclassed strategy functions can shadow more general ones. Occupy “joint” slots in the stream.

F RANZ I NC. Example strategy set for *terminal-io* cl-user(5): :i #x711e057a = #<terminal-simple-stream [initial terminal io] fd #x711e057a> 0 Class > # 1 j-unread-char -> # 2 j-write-chars -> # 3 j-write-char -> # 4 j-read-chars -> # 5 j-read-char --> # 6 j-listen -----> # src-position-table -> The symbol nil cl-user(6):

F RANZ I NC. Example strategy set for string-output cl-user(9): :i #x719cb5e2 = #<string-output-simple-stream "" pos #x719cb5e2> 0 Class > # 1 j-unread-char -> The symbol nil 2 j-write-chars -> # 3 j-write-char -> # 4 j-read-chars -> The symbol nil 5 j-read-char --> The symbol nil 6 j-listen -----> The symbol nil out-buffer ---> A simple-string (4096) that starts "xxxxxxxxxxxxxxxx” cl-user(10):

F RANZ I NC. Example strategy set for string-input cl-user(14): :i #x719e74aa = #<string-input-simple-stream "abc" pos #x719e74aa> 0 Class > # 1 j-unread-char -> # 2 j-write-chars -> The symbol nil 3 j-write-char -> The symbol nil 4 j-read-chars -> # 5 j-read-char --> # 6 j-listen -----> # src-position-table -> The symbol nil cl-user(15):

F RANZ I NC. j-read-char Args: stream eof-error-p eof-value blocking Implements read-char and read-char-no-hang functionality directly (after argument resolution) blocking argument is nil/true dichotomy

F RANZ I NC. j-write-char Args: character stream Implements write-char directly (after argument resolution) Writing nil as the character argument will flush an external-format’s output state.

F RANZ I NC. j-read-chars Args: stream string search start end blocking Implements read-sequence, read-line blocking argument is nil, :bnb, or true

F RANZ I NC. j-write-chars Args: stream string start end Implements write-sequence, write-string Always blocks (for now)

F RANZ I NC. j-listen Args: stream Implements stream-listen directly Must have a complete character or an error condition in order to return true.

F RANZ I NC. j-unread-char Args: stream relaxed May need to unread multiple characters as part of a composing or encapsulating character. relaxed allows non-error unreads beyond the first unread.

F RANZ I NC. Taking strategies beyond ANS Other strategies can be created for operations that don’t fit into CL standard functionalities. Example: one-buffer ring fifo queue

F RANZ I NC. Common Windows low-level rewrite Source code comparison between Gray and simple streams Demo