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