root/lm-sensors/trunk/lib/general.c @ 5674

Revision 5674, 2.4 KB (checked in by andy, 5 years ago)

Use func in sensors_fatal_error() to get function name.

  • 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., 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
31void 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
42void 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
52void 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
69void 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}
Note: See TracBrowser for help on using the browser.