blob: a7b3b57f76734de7ef1ae3d16677ca8529123bf9 [file] [log] [blame]
Sharvil Nanavati1878c422014-12-15 01:37:59 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Sharvil Nanavati1878c422014-12-15 01:37:59 -08004 *
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
24typedef 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 Watsonb55040c2016-10-19 13:15:34 -070029buffer_t* buffer_new(size_t size);
Sharvil Nanavati1878c422014-12-15 01:37:59 -080030
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 Watsonb55040c2016-10-19 13:15:34 -070034// caller must release the returned buffer with |buffer_free|. Note that
Myles Watson9ca07092016-11-28 16:41:53 -080035// releasing the returned buffer does not release |buf|. |buf| must not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070036buffer_t* buffer_new_ref(const buffer_t* buf);
Sharvil Nanavati1878c422014-12-15 01:37:59 -080037
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 Watsonb55040c2016-10-19 13:15:34 -070042buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size);
Sharvil Nanavati1878c422014-12-15 01:37:59 -080043
44// Frees a buffer object. |buf| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070045void buffer_free(buffer_t* buf);
Sharvil Nanavati1878c422014-12-15 01:37:59 -080046
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 Watsonb55040c2016-10-19 13:15:34 -070052void* buffer_ptr(const buffer_t* buf);
Sharvil Nanavati1878c422014-12-15 01:37:59 -080053
54// Returns the length of the writeable memory region referred to by |buf|.
55// |buf| must not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070056size_t buffer_length(const buffer_t* buf);