Upstream version 1.3.40
[profile/ivi/swig.git] / Lib / typemaps / cmalloc.swg
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.swg
6  *
7  * This library file contains 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(size_t nbytes = sizeof(TYPE);
24    TYPE *calloc_NAME(size_t nobj=1, size_t size=sizeof(TYPE));
25    TYPE *realloc_NAME(TYPE *ptr, size_t nbytes);
26    void free_NAME(TYPE *ptr);
27
28 */
29
30 %define %malloc(TYPE,NAME...)
31 #if #NAME != ""
32 %rename(malloc_##NAME) ::malloc(size_t nbytes);
33 #else
34 %rename(malloc_##TYPE) ::malloc(size_t nbytes);
35 #endif
36
37 #if #TYPE != "void"
38 %typemap(default) size_t nbytes "$1 = (size_t) sizeof(TYPE);"
39 #endif
40 TYPE *malloc(size_t nbytes);
41 %typemap(default) size_t nbytes;
42 %enddef
43
44 %define %calloc(TYPE,NAME...)
45 #if #NAME != ""
46 %rename(calloc_##NAME) ::calloc(size_t nobj, size_t sz);
47 #else
48 %rename(calloc_##TYPE) ::calloc(size_t nobj, size_t sz);
49 #endif
50 #if #TYPE != "void"
51 %typemap(default) size_t sz "$1 = (size_t) sizeof(TYPE);"
52 #else
53 %typemap(default) size_t sz "$1 = 1;"
54 #endif
55 %typemap(default) size_t nobj "$1 = 1;"
56 TYPE *calloc(size_t nobj, size_t sz);
57 %typemap(default) size_t sz;
58 %typemap(default) size_t nobj;
59 %enddef
60
61 %define %realloc(TYPE,NAME...)
62 %insert("header") {
63 #if #NAME != ""
64 TYPE *realloc_##NAME(TYPE *ptr, size_t nitems)
65 #else
66 TYPE *realloc_##TYPE(TYPE *ptr, size_t nitems)
67 #endif
68 {
69 #if #TYPE != "void"
70 return (TYPE *) realloc(ptr, nitems*sizeof(TYPE));
71 #else
72 return (TYPE *) realloc(ptr, nitems);
73 #endif
74 }
75 }
76 #if #NAME != ""
77 TYPE *realloc_##NAME(TYPE *ptr, size_t nitems);
78 #else
79 TYPE *realloc_##TYPE(TYPE *ptr, size_t 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
89 void free(TYPE *ptr);
90 %enddef
91
92 %define %sizeof(TYPE,NAME...)
93 #if #NAME != ""
94 %constant size_t sizeof_##NAME = sizeof(TYPE);
95 #else
96 %constant size_t 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