1/* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4 *
5 * cmalloc.i
6 *
7 * SWIG library file containing macros that can be used to create objects using
8 * the C malloc function.
9 * ----------------------------------------------------------------------------- */
10
11%{
12#include <stdlib.h>
13%}
14
15/* %malloc(TYPE [, NAME = TYPE])
16   %calloc(TYPE [, NAME = TYPE])
17   %realloc(TYPE [, NAME = TYPE])
18   %free(TYPE [, NAME = TYPE])
19   %allocators(TYPE [,NAME = TYPE])
20
21   Creates functions for allocating/reallocating memory.
22
23   TYPE *malloc_NAME(int nbytes = sizeof(TYPE);
24   TYPE *calloc_NAME(int nobj=1, int size=sizeof(TYPE));
25   TYPE *realloc_NAME(TYPE *ptr, int nbytes);
26   void free_NAME(TYPE *ptr);
27
28*/
29
30%define %malloc(TYPE,NAME...)
31#if #NAME != ""
32%rename(malloc_##NAME) ::malloc(int nbytes);
33#else
34%rename(malloc_##TYPE) ::malloc(int nbytes);
35#endif
36
37#if #TYPE != "void"
38%typemap(default) int nbytes "$1 = (int) sizeof(TYPE);"
39#endif
40TYPE *malloc(int nbytes);
41%typemap(default) int nbytes;
42%enddef
43
44%define %calloc(TYPE,NAME...)
45#if #NAME != ""
46%rename(calloc_##NAME) ::calloc(int nobj, int sz);
47#else
48%rename(calloc_##TYPE) ::calloc(int nobj, int sz);
49#endif
50#if #TYPE != "void"
51%typemap(default) int sz "$1 = (int) sizeof(TYPE);"
52#else
53%typemap(default) int sz "$1 = 1;"
54#endif
55%typemap(default) int nobj "$1 = 1;"
56TYPE *calloc(int nobj, int sz);
57%typemap(default) int sz;
58%typemap(default) int nobj;
59%enddef
60
61%define %realloc(TYPE,NAME...)
62%insert("header") {
63#if #NAME != ""
64TYPE *realloc_##NAME(TYPE *ptr, int nitems)
65#else
66TYPE *realloc_##TYPE(TYPE *ptr, int nitems)
67#endif
68{
69#if #TYPE != "void"
70return (TYPE *) realloc(ptr, nitems*sizeof(TYPE));
71#else
72return (TYPE *) realloc(ptr, nitems);
73#endif
74}
75}
76#if #NAME != ""
77TYPE *realloc_##NAME(TYPE *ptr, int nitems);
78#else
79TYPE *realloc_##TYPE(TYPE *ptr, int nitems);
80#endif
81%enddef
82
83%define %free(TYPE,NAME...)
84#if #NAME != ""
85%rename(free_##NAME) ::free(TYPE *ptr);
86#else
87%rename(free_##TYPE) ::free(TYPE *ptr);
88#endif
89void free(TYPE *ptr);
90%enddef
91
92%define %sizeof(TYPE,NAME...)
93#if #NAME != ""
94%constant int sizeof_##NAME = sizeof(TYPE);
95#else
96%constant int sizeof_##TYPE = sizeof(TYPE);
97#endif
98%enddef
99
100%define %allocators(TYPE,NAME...)
101%malloc(TYPE,NAME)
102%calloc(TYPE,NAME)
103%realloc(TYPE,NAME)
104%free(TYPE,NAME)
105#if #TYPE != "void"
106%sizeof(TYPE,NAME)
107#endif
108%enddef
109
110
111
112
113
114