root/lm-sensors/tags/V3-0-1/lib/general.c

Revision 4756, 2.4 kB (checked in by khali, 1 year ago)

Drop sensors_strip_of_spaces. There's no rationale for stripping
trailing spaces of bus names and nothing else.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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 program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "error.h"
21 #include "general.h"
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27
28 #define A_BUNCH 16
29
30 void sensors_malloc_array(void *list, int *num_el, int *max_el, int el_size)
31 {
32         void **my_list = (void **)list;
33
34         *my_list = malloc(el_size*A_BUNCH);
35         if (! *my_list)
36                 sensors_fatal_error("sensors_malloc_array",
37                                     "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("sensors_add_array_el",
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("sensors_add_array_els",
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 }
Note: See TracBrowser for help on using the browser.