Home | History | Annotate | Download | only in include
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 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 returned
     28 // object must be freed with |array_free|. |element_size| must be greater than 0. Returns
     29 // 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 NULL.
     36 void *array_ptr(const array_t *array);
     37 
     38 // Returns a pointer to the |index|th element of |array|. |index| must be less than
     39 // the array's length. |array| must not be NULL.
     40 void *array_at(const array_t *array, size_t index);
     41 
     42 // Returns the number of elements stored in |array|. |array| must not be NULL.
     43 size_t array_length(const array_t *array);
     44 
     45 // Inserts an element to the end of |array| by value. For example, a caller
     46 // may simply call array_append_value(array, 5) instead of storing 5 into a
     47 // variable and then inserting by pointer. Although |value| is a uint32_t,
     48 // only the lowest |element_size| bytes will be stored. |array| must not be
     49 // NULL. Returns true if the element could be inserted into the array, false
     50 // on error.
     51 bool array_append_value(array_t *array, uint32_t value);
     52 
     53 // Inserts an element to the end of |array|. The value pointed to by |data| must
     54 // be at least |element_size| bytes long and will be copied into the array. Neither
     55 // |array| nor |data| may be NULL. Returns true if the element could be inserted into
     56 // the array, false on error.
     57 bool array_append_ptr(array_t *array, void *data);
     58