summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/m_memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/m_memory.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/m_memory.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/m_memory.c b/apps/plugins/pdbox/PDa/src/m_memory.c
new file mode 100644
index 0000000000..085154299b
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/m_memory.c
@@ -0,0 +1,178 @@
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. */
4
5#include <stdlib.h>
6#include <string.h>
7#include "m_pd.h"
8#include "m_imp.h"
9
10/* #define LOUD */
11#ifdef LOUD
12#include <stdio.h>
13#endif
14
15/* #define DEBUGMEM */
16#ifdef DEBUGMEM
17static int totalmem = 0;
18#endif
19
20void *getbytes(size_t nbytes)
21{
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);
34}
35
36void *getzbytes(size_t nbytes) /* obsolete name */
37{
38 return (getbytes(nbytes));
39}
40
41void *copybytes(void *src, size_t nbytes)
42{
43 void *ret;
44 ret = getbytes(nbytes);
45 if (nbytes)
46 memcpy(ret, src, nbytes);
47 return (ret);
48}
49
50void *resizebytes(void *old, size_t oldsize, size_t newsize)
51{
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);
67}
68
69void freebytes(void *fatso, size_t nbytes)
70{
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);
80}
81
82#ifdef DEBUGMEM
83#include <stdio.h>
84
85void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
86{
87 fprintf(stderr, "total mem %d\n", totalmem);
88}
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. */
93
94#include <stdlib.h>
95#include <string.h>
96#include "m_pd.h"
97#include "m_imp.h"
98
99/* #define LOUD */
100#ifdef LOUD
101#include <stdio.h>
102#endif
103
104/* #define DEBUGMEM */
105#ifdef DEBUGMEM
106static int totalmem = 0;
107#endif
108
109void *getbytes(size_t nbytes)
110{
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);
123}
124
125void *getzbytes(size_t nbytes) /* obsolete name */
126{
127 return (getbytes(nbytes));
128}
129
130void *copybytes(void *src, size_t nbytes)
131{
132 void *ret;
133 ret = getbytes(nbytes);
134 if (nbytes)
135 memcpy(ret, src, nbytes);
136 return (ret);
137}
138
139void *resizebytes(void *old, size_t oldsize, size_t newsize)
140{
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);
156}
157
158void freebytes(void *fatso, size_t nbytes)
159{
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);
169}
170
171#ifdef DEBUGMEM
172#include <stdio.h>
173
174void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
175{
176 fprintf(stderr, "total mem %d\n", totalmem);
177}
178#endif