moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / indi / fli / libfli-mem.c
blobf6bee1bcef1858f3f3ee43bc68b0bc48f1bd5050
1 /*
3 Copyright (c) 2000, 2002 Finger Lakes Instrumentation (FLI), L.L.C.
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
18 Neither the name of Finger Lakes Instrumentation (FLI), LLC
19 nor the names of its contributors may be used to endorse or
20 promote products derived from this software without specific
21 prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
36 ======================================================================
38 Finger Lakes Instrumentation, L.L.C. (FLI)
39 web: http://www.fli-cam.com
40 email: support@fli-cam.com
44 #include <stdlib.h>
45 #include <string.h>
47 #include "libfli-libfli.h"
48 #include "libfli-mem.h"
50 #define DEFAULT_NUM_POINTERS (1024)
52 static struct {
53 void **pointers;
54 int total;
55 int used;
56 } allocated = {NULL, 0, 0};
58 void *xmalloc(size_t size)
60 int i;
61 void *ptr;
63 if (allocated.used + 1 > allocated.total)
65 void **tmp;
66 int num;
68 num = (allocated.total == 0 ? DEFAULT_NUM_POINTERS : 2 * allocated.total);
70 if ((tmp = (void **)realloc(allocated.pointers,
71 num * sizeof(void **))) == NULL)
72 return NULL;
74 allocated.pointers = tmp;
75 memset(&allocated.pointers[allocated.total], 0, num * sizeof(void **));
76 allocated.total += num;
79 if ((ptr = malloc(size)) == NULL)
80 return NULL;
82 for (i = 0; i < allocated.total; i++)
84 if (allocated.pointers[i] == NULL)
85 break;
88 if (i == allocated.total)
90 /* This shouldn't happen */
91 debug(FLIDEBUG_WARN, "Internal memory allocation error");
92 free(ptr);
94 return NULL;
97 allocated.pointers[i] = ptr;
98 allocated.used++;
100 return ptr;
104 void *xcalloc(size_t nmemb, size_t size)
106 void *ptr;
108 if ((ptr = xmalloc(nmemb * size)) == NULL)
109 return NULL;
111 memset(ptr, 0, nmemb * size);
113 return ptr;
116 void xfree(void *ptr)
118 int i;
120 for (i = 0; i < allocated.total; i++)
122 if (allocated.pointers[i] == ptr)
124 free(ptr);
125 allocated.pointers[i] = NULL;
126 allocated.used--;
128 return;
132 debug(FLIDEBUG_WARN, "Attempting to free an invalid pointer");
134 return;
137 void *xrealloc(void *ptr, size_t size)
139 int i;
141 for (i = 0; i < allocated.total; i++)
143 if (allocated.pointers[i] == ptr)
145 void *tmp;
147 if ((tmp = realloc(ptr, size)) == NULL)
148 return NULL;
150 allocated.pointers[i] = tmp;
152 return tmp;
156 debug(FLIDEBUG_WARN, "Attempting to realloc an invalid pointer");
158 return NULL;
161 int xfree_all(void)
163 int i;
164 int freed = 0;
166 for (i = 0; i < allocated.total; i++)
168 if (allocated.pointers[i] != NULL)
170 free(allocated.pointers[i]);
171 allocated.pointers[i] = NULL;
172 allocated.used--;
173 freed++;
177 if (allocated.used != 0)
178 debug(FLIDEBUG_WARN, "Internal memory handling error");
180 if (allocated.pointers != NULL)
181 free(allocated.pointers);
183 allocated.pointers = NULL;
184 allocated.used = 0;
185 allocated.total = 0;
187 return freed;
190 char *xstrdup(const char *s)
192 void *tmp;
193 size_t len;
195 len = strlen(s) + 1;
197 if ((tmp = xmalloc(len)) == NULL)
198 return NULL;
200 memcpy(tmp, s, len);
202 return tmp;