buffer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #pragma once
  19. #include <stdbool.h>
  20. #include <stddef.h>
  21. typedef struct buffer_t buffer_t;
  22. // Returns a new buffer of |size| bytes. Returns NULL if a buffer could not be
  23. // allocated. |size| must be non-zero. The caller must release this buffer with
  24. // |buffer_free|.
  25. buffer_t* buffer_new(size_t size);
  26. // Creates a new reference to the buffer |buf|. A reference is indistinguishable
  27. // from the original: writes to the original will be reflected in the reference
  28. // and vice versa. In other words, this function creates an alias to |buf|. The
  29. // caller must release the returned buffer with |buffer_free|. Note that
  30. // releasing the returned buffer does not release |buf|. |buf| must not be NULL.
  31. buffer_t* buffer_new_ref(const buffer_t* buf);
  32. // Creates a new reference to the last |slice_size| bytes of |buf|. See
  33. // |buffer_new_ref| for a description of references. |slice_size| must be
  34. // greater than 0 and may be at most |buffer_length|
  35. // (0 < slice_size <= buffer_length). |buf| must not be NULL.
  36. buffer_t* buffer_new_slice(const buffer_t* buf, size_t slice_size);
  37. // Frees a buffer object. |buf| may be NULL.
  38. void buffer_free(buffer_t* buf);
  39. // Returns a pointer to a writeable memory region for |buf|. All references
  40. // and slices that share overlapping bytes will also be written to when
  41. // writing to the returned pointer. The caller may safely write up to
  42. // |buffer_length| consecutive bytes starting at the address returned by
  43. // this function. |buf| must not be NULL.
  44. void* buffer_ptr(const buffer_t* buf);
  45. // Returns the length of the writeable memory region referred to by |buf|.
  46. // |buf| must not be NULL.
  47. size_t buffer_length(const buffer_t* buf);