Merge branch 'linaro-android-3.10-lsk' of git://git.linaro.org/people/jstultz/android...
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / libsensors / general.c
1 /*
2     general.c - Part of libsensors, a Linux library for reading sensor data.
3     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Lesser General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18     MA 02110-1301 USA.
19 */
20
21 #include "error.h"
22 #include "general.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28
29 #define A_BUNCH 16
30
31 void sensors_malloc_array(void *list, int *num_el, int *max_el, int el_size)
32 {
33         void **my_list = (void **)list;
34
35         *my_list = malloc(el_size*A_BUNCH);
36         if (! *my_list)
37                 sensors_fatal_error(__func__, "Allocating new elements");
38         *max_el = A_BUNCH;
39         *num_el = 0;
40 }
41
42 void sensors_free_array(void *list, int *num_el, int *max_el)
43 {
44         void **my_list = (void **)list;
45
46         free(*my_list);
47         *my_list = NULL;
48         *num_el = 0;
49         *max_el = 0;
50 }
51
52 void sensors_add_array_el(const void *el, void *list, int *num_el,
53                           int *max_el, int el_size)
54 {
55         int new_max_el;
56         void **my_list = (void *)list;
57         if (*num_el + 1 > *max_el) {
58                 new_max_el = *max_el + A_BUNCH;
59                 *my_list = realloc(*my_list, new_max_el * el_size);
60                 if (! *my_list)
61                         sensors_fatal_error(__func__,
62                                             "Allocating new elements");
63                 *max_el = new_max_el;
64         }
65         memcpy(((char *) *my_list) + *num_el * el_size, el, el_size);
66         (*num_el) ++;
67 }
68
69 void sensors_add_array_els(const void *els, int nr_els, void *list,
70                            int *num_el, int *max_el, int el_size)
71 {
72         int new_max_el;
73         void **my_list = (void *)list;
74         if (*num_el + nr_els > *max_el) {
75                 new_max_el = (*max_el + nr_els + A_BUNCH);
76                 new_max_el -= new_max_el % A_BUNCH;
77                 *my_list = realloc(*my_list, new_max_el * el_size);
78                 if (! *my_list)
79                         sensors_fatal_error(__func__,
80                                             "Allocating new elements");
81                 *max_el = new_max_el;
82         }
83         memcpy(((char *)*my_list) + *num_el * el_size, els, el_size * nr_els);
84         *num_el += nr_els;
85 }