array.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <stdint.h>
  22. typedef struct array_t array_t;
  23. // Returns a new array object that stores elements of size |element_size|. The
  24. // returned object must be freed with |array_free|. |element_size| must be
  25. // greater than 0. Returns NULL on failure.
  26. array_t* array_new(size_t element_size);
  27. // Frees an array that was allocated with |array_new|. |array| may be NULL.
  28. void array_free(array_t* array);
  29. // Returns a pointer to the first stored element in |array|. |array| must not be
  30. // NULL.
  31. void* array_ptr(const array_t* array);
  32. // Returns a pointer to the |index|th element of |array|. |index| must be less
  33. // than the array's length. |array| must not be NULL.
  34. void* array_at(const array_t* array, size_t index);
  35. // Returns the number of elements stored in |array|. |array| must not be NULL.
  36. size_t array_length(const array_t* array);
  37. // Inserts an element to the end of |array| by value. For example, a caller
  38. // may simply call array_append_value(array, 5) instead of storing 5 into a
  39. // variable and then inserting by pointer. Although |value| is a uint32_t,
  40. // only the lowest |element_size| bytes will be stored. |array| must not be
  41. // NULL. Returns true if the element could be inserted into the array, false
  42. // on error.
  43. bool array_append_value(array_t* array, uint32_t value);
  44. // Inserts an element to the end of |array|. The value pointed to by |data| must
  45. // be at least |element_size| bytes long and will be copied into the array.
  46. // Neither |array| nor |data| may be NULL. Returns true if the element could be
  47. // inserted into the array, false on error.
  48. bool array_append_ptr(array_t* array, void* data);