Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / m_memory.c
blob085154299b6e92b7889b3b1dd4924b27e0fc8b3a
1 /* Copyright (c) 1997-1999 Miller Puckette.
2 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
3 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
5 #include <stdlib.h>
6 #include <string.h>
7 #include "m_pd.h"
8 #include "m_imp.h"
10 /* #define LOUD */
11 #ifdef LOUD
12 #include <stdio.h>
13 #endif
15 /* #define DEBUGMEM */
16 #ifdef DEBUGMEM
17 static int totalmem = 0;
18 #endif
20 void *getbytes(size_t nbytes)
22 void *ret;
23 if (nbytes < 1) nbytes = 1;
24 ret = (void *)calloc(nbytes, 1);
25 #ifdef LOUD
26 fprintf(stderr, "new %x %d\n", (int)ret, nbytes);
27 #endif /* LOUD */
28 #ifdef DEBUGMEM
29 totalmem += nbytes;
30 #endif
31 if (!ret)
32 post("pd: getbytes() failed -- out of memory");
33 return (ret);
36 void *getzbytes(size_t nbytes) /* obsolete name */
38 return (getbytes(nbytes));
41 void *copybytes(void *src, size_t nbytes)
43 void *ret;
44 ret = getbytes(nbytes);
45 if (nbytes)
46 memcpy(ret, src, nbytes);
47 return (ret);
50 void *resizebytes(void *old, size_t oldsize, size_t newsize)
52 void *ret;
53 if (newsize < 1) newsize = 1;
54 if (oldsize < 1) oldsize = 1;
55 ret = (void *)realloc((char *)old, newsize);
56 if (newsize > oldsize && ret)
57 memset(((char *)ret) + oldsize, 0, newsize - oldsize);
58 #ifdef LOUD
59 fprintf(stderr, "resize %x %d --> %x %d\n", (int)old, oldsize, (int)ret, newsize);
60 #endif /* LOUD */
61 #ifdef DEBUGMEM
62 totalmem += (newsize - oldsize);
63 #endif
64 if (!ret)
65 post("pd: resizebytes() failed -- out of memory");
66 return (ret);
69 void freebytes(void *fatso, size_t nbytes)
71 if (nbytes == 0)
72 nbytes = 1;
73 #ifdef LOUD
74 fprintf(stderr, "free %x %d\n", (int)fatso, nbytes);
75 #endif /* LOUD */
76 #ifdef DEBUGMEM
77 totalmem -= nbytes;
78 #endif
79 free(fatso);
82 #ifdef DEBUGMEM
83 #include <stdio.h>
85 void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
87 fprintf(stderr, "total mem %d\n", totalmem);
89 #endif
90 /* Copyright (c) 1997-1999 Miller Puckette.
91 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
92 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
94 #include <stdlib.h>
95 #include <string.h>
96 #include "m_pd.h"
97 #include "m_imp.h"
99 /* #define LOUD */
100 #ifdef LOUD
101 #include <stdio.h>
102 #endif
104 /* #define DEBUGMEM */
105 #ifdef DEBUGMEM
106 static int totalmem = 0;
107 #endif
109 void *getbytes(size_t nbytes)
111 void *ret;
112 if (nbytes < 1) nbytes = 1;
113 ret = (void *)calloc(nbytes, 1);
114 #ifdef LOUD
115 fprintf(stderr, "new %x %d\n", (int)ret, nbytes);
116 #endif /* LOUD */
117 #ifdef DEBUGMEM
118 totalmem += nbytes;
119 #endif
120 if (!ret)
121 post("pd: getbytes() failed -- out of memory");
122 return (ret);
125 void *getzbytes(size_t nbytes) /* obsolete name */
127 return (getbytes(nbytes));
130 void *copybytes(void *src, size_t nbytes)
132 void *ret;
133 ret = getbytes(nbytes);
134 if (nbytes)
135 memcpy(ret, src, nbytes);
136 return (ret);
139 void *resizebytes(void *old, size_t oldsize, size_t newsize)
141 void *ret;
142 if (newsize < 1) newsize = 1;
143 if (oldsize < 1) oldsize = 1;
144 ret = (void *)realloc((char *)old, newsize);
145 if (newsize > oldsize && ret)
146 memset(((char *)ret) + oldsize, 0, newsize - oldsize);
147 #ifdef LOUD
148 fprintf(stderr, "resize %x %d --> %x %d\n", (int)old, oldsize, (int)ret, newsize);
149 #endif /* LOUD */
150 #ifdef DEBUGMEM
151 totalmem += (newsize - oldsize);
152 #endif
153 if (!ret)
154 post("pd: resizebytes() failed -- out of memory");
155 return (ret);
158 void freebytes(void *fatso, size_t nbytes)
160 if (nbytes == 0)
161 nbytes = 1;
162 #ifdef LOUD
163 fprintf(stderr, "free %x %d\n", (int)fatso, nbytes);
164 #endif /* LOUD */
165 #ifdef DEBUGMEM
166 totalmem -= nbytes;
167 #endif
168 free(fatso);
171 #ifdef DEBUGMEM
172 #include <stdio.h>
174 void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
176 fprintf(stderr, "total mem %d\n", totalmem);
178 #endif