]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/array.c
2 Operservice 2 - array.c
3 Arrays are used all over in O2. This .c supplies some functions for handling
5 (C) Michael Meier 2000-2001 - released under GPL
6 -----------------------------------------------------------------------------
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 ------------------------------------------------------------------------------
26 #include "../core/error.h"
29 #define ARLIM1 100 /* If new memory is needed for an array, this many */
30 /* records will be allocated. */
33 #define ARLIM2 150 /* If more than this number of records are free in an */
34 /* array, memory is freed. */
37 void array_init(array
*a
, unsigned int memberlen
) {
40 a
->memberlen
=memberlen
;
46 void array_setlim1(array
*a
, unsigned int lim
) {
50 void array_setlim2(array
*a
, unsigned int lim
) {
54 int array_getfreeslot(array
*a
) {
55 if ((a
->cursi
+1)>=a
->maxsi
) {
56 /* Reallocation needed */
58 a
->content
=realloc(a
->content
,a
->maxsi
*a
->memberlen
);
59 if (a
->content
==NULL
) {
60 Error("array",ERR_FATAL
,"Couldn't allocate memory for growing array");
68 void array_delslot(array
*a
, int slotn
) {
69 if (slotn
>= a
->cursi
) { return; }
71 if (a
->cursi
!=slotn
) {
72 memcpy(a
->content
+ (slotn
* a
->memberlen
), a
->content
+ (a
->cursi
* a
->memberlen
),a
->memberlen
);
74 if ((a
->cursi
+ a
->arlim2
) < a
->maxsi
) {
75 /* Let's free some memory */
76 a
->maxsi
= a
->cursi
+ a
->arlim1
;
77 a
->content
=realloc(a
->content
,a
->maxsi
*a
->memberlen
);
78 if (a
->content
==NULL
) {
79 Error("array",ERR_FATAL
,"Couldn't allocate memory for shrinking array?!?");
85 void array_free(array
*a
) {