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