github.com/goplus/llgo@v0.8.3/xtool/clang/parser/pages.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package parser
    18  
    19  // -----------------------------------------------------------------------------
    20  
    21  const pageSize = 1024 * 1024
    22  
    23  type PagedWriter struct {
    24  	pages []*[pageSize]byte
    25  	last  *[pageSize]byte
    26  	off   int
    27  }
    28  
    29  func NewPagedWriter() *PagedWriter {
    30  	return &PagedWriter{last: new([pageSize]byte)}
    31  }
    32  
    33  func (p *PagedWriter) Write(buf []byte) (written int, err error) {
    34  	for {
    35  		n := copy(p.last[p.off:], buf[written:])
    36  		written += n
    37  		if written >= len(buf) {
    38  			p.off += n
    39  			return
    40  		}
    41  		p.pages = append(p.pages, p.last)
    42  		p.last, p.off = new([pageSize]byte), 0
    43  	}
    44  }
    45  
    46  func (p *PagedWriter) Len() int {
    47  	return len(p.pages)*pageSize + p.off
    48  }
    49  
    50  func (p *PagedWriter) Bytes() []byte {
    51  	out, n := make([]byte, p.Len()), 0
    52  	for _, page := range p.pages {
    53  		n += copy(out[n:], page[:])
    54  	}
    55  	copy(out[n:], p.last[:p.off])
    56  	return out
    57  }
    58  
    59  /*
    60  func (p *PagedWriter) ToReader() *PagedReader {
    61  	return &PagedReader{src: p, curr: p.getPage(0)}
    62  }
    63  
    64  func (p *PagedWriter) getPage(ipage int) []byte {
    65  	if ipage == len(p.pages) { // last page
    66  		return p.last[:p.off]
    67  	}
    68  	return p.pages[ipage][:]
    69  }
    70  
    71  // -----------------------------------------------------------------------------
    72  
    73  type PagedReader struct {
    74  	src   *PagedWriter
    75  	curr  []byte
    76  	off   int
    77  	ipage int
    78  }
    79  
    80  func (p *PagedReader) WriteTo(w io.Writer) (written int64, err error) {
    81  	n, err := w.Write(p.curr[p.off:])
    82  	written = int64(n)
    83  	if err != nil {
    84  		return
    85  	}
    86  	src, ipage := p.src, p.ipage
    87  	for {
    88  		if ipage == len(src.pages) { // last page
    89  			p.ipage, p.off = ipage, len(p.curr)
    90  			return
    91  		}
    92  		ipage++
    93  		page := src.getPage(ipage)
    94  		n, err = w.Write(page)
    95  		written += int64(n)
    96  		if err != nil {
    97  			p.ipage, p.curr, p.off = ipage, page, n
    98  			return
    99  		}
   100  	}
   101  }
   102  
   103  func (p *PagedReader) Read(buf []byte) (nread int, err error) {
   104  	for {
   105  		n := copy(buf[nread:], p.curr[p.off:])
   106  		nread += n
   107  		p.off += n
   108  		if nread >= len(buf) {
   109  			return
   110  		}
   111  		src := p.src
   112  		if p.ipage == len(src.pages) { // last page
   113  			err = io.EOF
   114  			return
   115  		}
   116  		p.ipage++
   117  		p.curr, p.off = src.getPage(p.ipage), 0
   118  	}
   119  }
   120  */
   121  
   122  // -----------------------------------------------------------------------------