Home | History | Annotate | Download | only in include
      1 /******************************************************************************
      2  *
      3  *  Copyright 2014 Google, Inc.
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 #pragma once
     20 
     21 #include <stdbool.h>
     22 #include <stddef.h>
     23 
     24 typedef struct buffer_t buffer_t;
     25 
     26 // Returns a new buffer of |size| bytes. Returns NULL if a buffer could not be
     27 // allocated. |size| must be non-zero. The caller must release this buffer with
     28 // |buffer_free|.
     29 buffer_t* buffer_new(size_t size);
     30 
     31 // Creates a new reference to the buffer |buf|. A reference is indistinguishable
     32 // from the original: writes to the original will be reflected in the reference
     33 // and vice versa. In other words, this function creates an alias to |buf|. The
     34 // caller must release the returned buffer with |buffer_free|. Note that
     35 // releasing the returned buffer does not release |buf|. |buf| must not be NULL.
     36 buffer_t* buffer_new_ref(const buffer_t* buf);
     37 
     38 // Creates a new reference to the last |slice_size| bytes of |buf|. See
     39 // |buffer_new_ref| for a description of references. |slice_size| must be
     40 // greater than 0 and may be at most |buffer_length|
     41 // (0 < slice_size <= buffer_length). |buf| must not be NULL.
     42 buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size);
     43 
     44 // Frees a buffer object. |buf| may be NULL.
     45 void buffer_free(buffer_t* buf);
     46 
     47 // Returns a pointer to a writeable memory region for |buf|. All references
     48 // and slices that share overlapping bytes will also be written to when
     49 // writing to the returned pointer. The caller may safely write up to
     50 // |buffer_length| consecutive bytes starting at the address returned by
     51 // this function. |buf| must not be NULL.
     52 void* buffer_ptr(const buffer_t* buf);
     53 
     54 // Returns the length of the writeable memory region referred to by |buf|.
     55 // |buf| must not be NULL.
     56 size_t buffer_length(const buffer_t* buf);
     57