Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2014 Google, Inc. |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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|. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 29 | buffer_t* buffer_new(size_t size); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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 |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 34 | // caller must release the returned buffer with |buffer_free|. Note that |
Myles Watson | 9ca0709 | 2016-11-28 16:41:53 -0800 | [diff] [blame] | 35 | // releasing the returned buffer does not release |buf|. |buf| must not be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 36 | buffer_t* buffer_new_ref(const buffer_t* buf); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 42 | buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 43 | |
| 44 | // Frees a buffer object. |buf| may be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 45 | void buffer_free(buffer_t* buf); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 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. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 52 | void* buffer_ptr(const buffer_t* buf); |
Sharvil Nanavati | 1878c42 | 2014-12-15 01:37:59 -0800 | [diff] [blame] | 53 | |
| 54 | // Returns the length of the writeable memory region referred to by |buf|. |
| 55 | // |buf| must not be NULL. |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 56 | size_t buffer_length(const buffer_t* buf); |